blob: 4c70ac4ad46d84c944da610c3e6382791cf8b544 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
Olivier Houchard4e694042017-03-14 20:01:29 +010017#include <import/xxhash.h>
18
Willy Tarreau272adea2014-03-31 10:39:59 +020019#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020020#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020021#include <common/errors.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010022#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020023#include <common/time.h>
24
William Lallemand222baf22016-11-19 02:00:33 +010025#include <types/applet.h>
26#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020027#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010028#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020029#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010030#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020031
William Lallemand222baf22016-11-19 02:00:33 +010032#include <proto/applet.h>
33#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090034#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020035#include <proto/port_range.h>
36#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020037#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010038#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020039#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020040#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010041#include <proto/stream_interface.h>
42#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020043#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020044#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010045#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020046
Emeric Brun64cc49c2017-10-03 14:46:45 +020047struct list updated_servers = LIST_HEAD_INIT(updated_servers);
48
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020049static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010050static int srv_apply_lastaddr(struct server *srv, int *err_code);
Frédéric Lécailleb418c122017-04-26 11:24:02 +020051static int srv_set_fqdn(struct server *srv, const char *fqdn);
Willy Tarreaubaaee002006-06-26 02:48:02 +020052
Willy Tarreau21faa912012-10-10 08:27:36 +020053/* List head of all known server keywords */
54static struct srv_kw_list srv_keywords = {
55 .list = LIST_HEAD_INIT(srv_keywords.list)
56};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020057
Simon Hormana3608442013-11-01 16:46:15 +090058int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020059{
Emeric Brun52a91d32017-08-31 14:41:55 +020060 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020061 return s->down_time;
62
63 return now.tv_sec - s->last_change + s->down_time;
64}
Willy Tarreaubaaee002006-06-26 02:48:02 +020065
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050066int srv_lastsession(const struct server *s)
67{
68 if (s->counters.last_sess)
69 return now.tv_sec - s->counters.last_sess;
70
71 return -1;
72}
73
Simon Horman4a741432013-02-23 15:35:38 +090074int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020075{
Simon Horman4a741432013-02-23 15:35:38 +090076 const struct server *s = check->server;
77
Willy Tarreauff5ae352013-12-11 20:36:34 +010078 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090079 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010080
Emeric Brun52a91d32017-08-31 14:41:55 +020081 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090082 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010083
Simon Horman4a741432013-02-23 15:35:38 +090084 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010085}
86
Olivier Houchard4e694042017-03-14 20:01:29 +010087void srv_set_dyncookie(struct server *s)
88{
89 struct proxy *p = s->proxy;
90 struct server *tmpserv;
91 char *tmpbuf;
92 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010093 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +010094 size_t buffer_len;
95 int addr_len;
96 int port;
97
98 if ((s->flags & SRV_F_COOKIESET) ||
99 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
100 s->proxy->dyncookie_key == NULL)
101 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100102 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100103
104 if (s->addr.ss_family != AF_INET &&
105 s->addr.ss_family != AF_INET6)
106 return;
107 /*
108 * Buffer to calculate the cookie value.
109 * The buffer contains the secret key + the server IP address
110 * + the TCP port.
111 */
112 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
113 /*
114 * The TCP port should use only 2 bytes, but is stored in
115 * an unsigned int in struct server, so let's use 4, to be
116 * on the safe side.
117 */
118 buffer_len = key_len + addr_len + 4;
119 tmpbuf = trash.str;
120 memcpy(tmpbuf, p->dyncookie_key, key_len);
121 memcpy(&(tmpbuf[key_len]),
122 s->addr.ss_family == AF_INET ?
123 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
124 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
125 addr_len);
126 /*
127 * Make sure it's the same across all the load balancers,
128 * no matter their endianness.
129 */
130 port = htonl(s->svc_port);
131 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
132 hash_value = XXH64(tmpbuf, buffer_len, 0);
133 memprintf(&s->cookie, "%016llx", hash_value);
134 if (!s->cookie)
135 return;
136 s->cklen = 16;
137 /*
138 * Check that we did not get a hash collision.
139 * Unlikely, but it can happen.
140 */
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200141 for (tmpserv = p->srv; tmpserv != NULL;
142 tmpserv = tmpserv->next) {
143 if (tmpserv == s)
144 continue;
145 if (tmpserv->cookie &&
146 strcmp(tmpserv->cookie, s->cookie) == 0) {
147 Warning("We generated two equal cookies for two different servers.\n"
148 "Please change the secret key for '%s'.\n",
149 s->proxy->id);
Olivier Houchard4e694042017-03-14 20:01:29 +0100150 }
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200151 }
Olivier Houchard4e694042017-03-14 20:01:29 +0100152}
153
Willy Tarreau21faa912012-10-10 08:27:36 +0200154/*
155 * Registers the server keyword list <kwl> as a list of valid keywords for next
156 * parsing sessions.
157 */
158void srv_register_keywords(struct srv_kw_list *kwl)
159{
160 LIST_ADDQ(&srv_keywords.list, &kwl->list);
161}
162
163/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
164 * keyword is found with a NULL ->parse() function, then an attempt is made to
165 * find one with a valid ->parse() function. This way it is possible to declare
166 * platform-dependant, known keywords as NULL, then only declare them as valid
167 * if some options are met. Note that if the requested keyword contains an
168 * opening parenthesis, everything from this point is ignored.
169 */
170struct srv_kw *srv_find_kw(const char *kw)
171{
172 int index;
173 const char *kwend;
174 struct srv_kw_list *kwl;
175 struct srv_kw *ret = NULL;
176
177 kwend = strchr(kw, '(');
178 if (!kwend)
179 kwend = kw + strlen(kw);
180
181 list_for_each_entry(kwl, &srv_keywords.list, list) {
182 for (index = 0; kwl->kw[index].kw != NULL; index++) {
183 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
184 kwl->kw[index].kw[kwend-kw] == 0) {
185 if (kwl->kw[index].parse)
186 return &kwl->kw[index]; /* found it !*/
187 else
188 ret = &kwl->kw[index]; /* may be OK */
189 }
190 }
191 }
192 return ret;
193}
194
195/* Dumps all registered "server" keywords to the <out> string pointer. The
196 * unsupported keywords are only dumped if their supported form was not
197 * found.
198 */
199void srv_dump_kws(char **out)
200{
201 struct srv_kw_list *kwl;
202 int index;
203
204 *out = NULL;
205 list_for_each_entry(kwl, &srv_keywords.list, list) {
206 for (index = 0; kwl->kw[index].kw != NULL; index++) {
207 if (kwl->kw[index].parse ||
208 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
209 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
210 kwl->scope,
211 kwl->kw[index].kw,
212 kwl->kw[index].skip ? " <arg>" : "",
213 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
214 kwl->kw[index].parse ? "" : " (not supported)");
215 }
216 }
217 }
218}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100219
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100220/* Parse the "addr" server keyword */
221static int srv_parse_addr(char **args, int *cur_arg,
222 struct proxy *curproxy, struct server *newsrv, char **err)
223{
224 char *errmsg, *arg;
225 struct sockaddr_storage *sk;
226 int port1, port2;
227 struct protocol *proto;
228
229 errmsg = NULL;
230 arg = args[*cur_arg + 1];
231
232 if (!*arg) {
233 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
234 goto err;
235 }
236
237 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
238 if (!sk) {
239 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
240 goto err;
241 }
242
243 proto = protocol_by_family(sk->ss_family);
244 if (!proto || !proto->connect) {
245 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
246 args[*cur_arg], arg);
247 goto err;
248 }
249
250 if (port1 != port2) {
251 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
252 args[*cur_arg], arg);
253 goto err;
254 }
255
256 newsrv->check.addr = newsrv->agent.addr = *sk;
257 newsrv->flags |= SRV_F_CHECKADDR;
258 newsrv->flags |= SRV_F_AGENTADDR;
259
260 return 0;
261
262 err:
263 free(errmsg);
264 return ERR_ALERT | ERR_FATAL;
265}
266
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100267/* Parse the "agent-check" server keyword */
268static int srv_parse_agent_check(char **args, int *cur_arg,
269 struct proxy *curproxy, struct server *newsrv, char **err)
270{
271 newsrv->do_agent = 1;
272 return 0;
273}
274
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100275/* Parse the "backup" server keyword */
276static int srv_parse_backup(char **args, int *cur_arg,
277 struct proxy *curproxy, struct server *newsrv, char **err)
278{
279 newsrv->flags |= SRV_F_BACKUP;
280 return 0;
281}
282
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100283/* Parse the "check" server keyword */
284static int srv_parse_check(char **args, int *cur_arg,
285 struct proxy *curproxy, struct server *newsrv, char **err)
286{
287 newsrv->do_check = 1;
288 return 0;
289}
290
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100291/* Parse the "check-send-proxy" server keyword */
292static int srv_parse_check_send_proxy(char **args, int *cur_arg,
293 struct proxy *curproxy, struct server *newsrv, char **err)
294{
295 newsrv->check.send_proxy = 1;
296 return 0;
297}
298
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100299/* Parse the "cookie" server keyword */
300static int srv_parse_cookie(char **args, int *cur_arg,
301 struct proxy *curproxy, struct server *newsrv, char **err)
302{
303 char *arg;
304
305 arg = args[*cur_arg + 1];
306 if (!*arg) {
307 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
308 return ERR_ALERT | ERR_FATAL;
309 }
310
311 free(newsrv->cookie);
312 newsrv->cookie = strdup(arg);
313 newsrv->cklen = strlen(arg);
314 newsrv->flags |= SRV_F_COOKIESET;
315 return 0;
316}
317
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100318/* Parse the "disabled" server keyword */
319static int srv_parse_disabled(char **args, int *cur_arg,
320 struct proxy *curproxy, struct server *newsrv, char **err)
321{
Emeric Brun52a91d32017-08-31 14:41:55 +0200322 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
323 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100324 newsrv->check.state |= CHK_ST_PAUSED;
325 newsrv->check.health = 0;
326 return 0;
327}
328
329/* Parse the "enabled" server keyword */
330static int srv_parse_enabled(char **args, int *cur_arg,
331 struct proxy *curproxy, struct server *newsrv, char **err)
332{
Emeric Brun52a91d32017-08-31 14:41:55 +0200333 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
334 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100335 newsrv->check.state &= ~CHK_ST_PAUSED;
336 newsrv->check.health = newsrv->check.rise;
337 return 0;
338}
339
Willy Tarreaudff55432012-10-10 17:51:05 +0200340/* parse the "id" server keyword */
341static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
342{
343 struct eb32_node *node;
344
345 if (!*args[*cur_arg + 1]) {
346 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
347 return ERR_ALERT | ERR_FATAL;
348 }
349
350 newsrv->puid = atol(args[*cur_arg + 1]);
351 newsrv->conf.id.key = newsrv->puid;
352
353 if (newsrv->puid <= 0) {
354 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
355 return ERR_ALERT | ERR_FATAL;
356 }
357
358 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
359 if (node) {
360 struct server *target = container_of(node, struct server, conf.id);
361 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
362 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
363 target->id);
364 return ERR_ALERT | ERR_FATAL;
365 }
366
367 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200368 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200369 return 0;
370}
371
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100372/* Parse the "namespace" server keyword */
373static int srv_parse_namespace(char **args, int *cur_arg,
374 struct proxy *curproxy, struct server *newsrv, char **err)
375{
376#ifdef CONFIG_HAP_NS
377 char *arg;
378
379 arg = args[*cur_arg + 1];
380 if (!*arg) {
381 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
382 return ERR_ALERT | ERR_FATAL;
383 }
384
385 if (!strcmp(arg, "*")) {
386 /* Use the namespace associated with the connection (if present). */
387 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
388 return 0;
389 }
390
391 /*
392 * As this parser may be called several times for the same 'default-server'
393 * object, or for a new 'server' instance deriving from a 'default-server'
394 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
395 */
396 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
397
398 newsrv->netns = netns_store_lookup(arg, strlen(arg));
399 if (!newsrv->netns)
400 newsrv->netns = netns_store_insert(arg);
401
402 if (!newsrv->netns) {
403 memprintf(err, "Cannot open namespace '%s'", arg);
404 return ERR_ALERT | ERR_FATAL;
405 }
406
407 return 0;
408#else
409 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
410 return ERR_ALERT | ERR_FATAL;
411#endif
412}
413
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100414/* Parse the "no-agent-check" server keyword */
415static int srv_parse_no_agent_check(char **args, int *cur_arg,
416 struct proxy *curproxy, struct server *newsrv, char **err)
417{
418 free_check(&newsrv->agent);
419 newsrv->agent.inter = 0;
420 newsrv->agent.port = 0;
421 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
422 newsrv->do_agent = 0;
423 return 0;
424}
425
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100426/* Parse the "no-backup" server keyword */
427static int srv_parse_no_backup(char **args, int *cur_arg,
428 struct proxy *curproxy, struct server *newsrv, char **err)
429{
430 newsrv->flags &= ~SRV_F_BACKUP;
431 return 0;
432}
433
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100434/* Parse the "no-check" server keyword */
435static int srv_parse_no_check(char **args, int *cur_arg,
436 struct proxy *curproxy, struct server *newsrv, char **err)
437{
438 free_check(&newsrv->check);
439 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
440 newsrv->do_check = 0;
441 return 0;
442}
443
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100444/* Parse the "no-check-send-proxy" server keyword */
445static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
446 struct proxy *curproxy, struct server *newsrv, char **err)
447{
448 newsrv->check.send_proxy = 0;
449 return 0;
450}
451
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100452/* Disable server PROXY protocol flags. */
453static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
454{
455 srv->pp_opts &= ~flags;
456 return 0;
457}
458
459/* Parse the "no-send-proxy" server keyword */
460static int srv_parse_no_send_proxy(char **args, int *cur_arg,
461 struct proxy *curproxy, struct server *newsrv, char **err)
462{
463 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
464}
465
466/* Parse the "no-send-proxy-v2" server keyword */
467static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
468 struct proxy *curproxy, struct server *newsrv, char **err)
469{
470 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
471}
472
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100473/* Parse the "non-stick" server keyword */
474static int srv_parse_non_stick(char **args, int *cur_arg,
475 struct proxy *curproxy, struct server *newsrv, char **err)
476{
477 newsrv->flags |= SRV_F_NON_STICK;
478 return 0;
479}
480
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100481/* Enable server PROXY protocol flags. */
482static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
483{
484 srv->pp_opts |= flags;
485 return 0;
486}
487
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100488/* Parse the "observe" server keyword */
489static int srv_parse_observe(char **args, int *cur_arg,
490 struct proxy *curproxy, struct server *newsrv, char **err)
491{
492 char *arg;
493
494 arg = args[*cur_arg + 1];
495 if (!*arg) {
496 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
497 return ERR_ALERT | ERR_FATAL;
498 }
499
500 if (!strcmp(arg, "none")) {
501 newsrv->observe = HANA_OBS_NONE;
502 }
503 else if (!strcmp(arg, "layer4")) {
504 newsrv->observe = HANA_OBS_LAYER4;
505 }
506 else if (!strcmp(arg, "layer7")) {
507 if (curproxy->mode != PR_MODE_HTTP) {
508 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
509 return ERR_ALERT;
510 }
511 newsrv->observe = HANA_OBS_LAYER7;
512 }
513 else {
514 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
515 "but got '%s'\n", args[*cur_arg], arg);
516 return ERR_ALERT | ERR_FATAL;
517 }
518
519 return 0;
520}
521
Frédéric Lécaille16186232017-03-14 16:42:49 +0100522/* Parse the "redir" server keyword */
523static int srv_parse_redir(char **args, int *cur_arg,
524 struct proxy *curproxy, struct server *newsrv, char **err)
525{
526 char *arg;
527
528 arg = args[*cur_arg + 1];
529 if (!*arg) {
530 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
531 return ERR_ALERT | ERR_FATAL;
532 }
533
534 free(newsrv->rdr_pfx);
535 newsrv->rdr_pfx = strdup(arg);
536 newsrv->rdr_len = strlen(arg);
537
538 return 0;
539}
540
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100541/* Parse the "send-proxy" server keyword */
542static int srv_parse_send_proxy(char **args, int *cur_arg,
543 struct proxy *curproxy, struct server *newsrv, char **err)
544{
545 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
546}
547
548/* Parse the "send-proxy-v2" server keyword */
549static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
550 struct proxy *curproxy, struct server *newsrv, char **err)
551{
552 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
553}
554
Frédéric Lécailledba97072017-03-17 15:33:50 +0100555
556/* Parse the "source" server keyword */
557static int srv_parse_source(char **args, int *cur_arg,
558 struct proxy *curproxy, struct server *newsrv, char **err)
559{
560 char *errmsg;
561 int port_low, port_high;
562 struct sockaddr_storage *sk;
563 struct protocol *proto;
564
565 errmsg = NULL;
566
567 if (!*args[*cur_arg + 1]) {
568 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
569 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
570 goto err;
571 }
572
573 /* 'sk' is statically allocated (no need to be freed). */
574 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
575 if (!sk) {
576 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
577 goto err;
578 }
579
580 proto = protocol_by_family(sk->ss_family);
581 if (!proto || !proto->connect) {
582 Alert("'%s %s' : connect() not supported for this address family.\n",
583 args[*cur_arg], args[*cur_arg + 1]);
584 goto err;
585 }
586
587 newsrv->conn_src.opts |= CO_SRC_BIND;
588 newsrv->conn_src.source_addr = *sk;
589
590 if (port_low != port_high) {
591 int i;
592
593 if (!port_low || !port_high) {
594 Alert("'%s' does not support port offsets (found '%s').\n",
595 args[*cur_arg], args[*cur_arg + 1]);
596 goto err;
597 }
598
599 if (port_low <= 0 || port_low > 65535 ||
600 port_high <= 0 || port_high > 65535 ||
601 port_low > port_high) {
602 Alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
603 goto err;
604 }
605 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
606 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
607 newsrv->conn_src.sport_range->ports[i] = port_low + i;
608 }
609
610 *cur_arg += 2;
611 while (*(args[*cur_arg])) {
612 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
613#if defined(CONFIG_HAP_TRANSPARENT)
614 if (!*args[*cur_arg + 1]) {
615 Alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
616 "or 'hdr_ip(name,#)' as argument.\n");
617 goto err;
618 }
619 if (!strcmp(args[*cur_arg + 1], "client")) {
620 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
621 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
622 }
623 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
624 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
625 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
626 }
627 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
628 char *name, *end;
629
630 name = args[*cur_arg + 1] + 7;
631 while (isspace(*name))
632 name++;
633
634 end = name;
635 while (*end && !isspace(*end) && *end != ',' && *end != ')')
636 end++;
637
638 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
639 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
640 free(newsrv->conn_src.bind_hdr_name);
641 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
642 newsrv->conn_src.bind_hdr_len = end - name;
643 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
644 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
645 newsrv->conn_src.bind_hdr_occ = -1;
646
647 /* now look for an occurrence number */
648 while (isspace(*end))
649 end++;
650 if (*end == ',') {
651 end++;
652 name = end;
653 if (*end == '-')
654 end++;
655 while (isdigit((int)*end))
656 end++;
657 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
658 }
659
660 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
661 Alert("usesrc hdr_ip(name,num) does not support negative"
662 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
663 goto err;
664 }
665 }
666 else {
667 struct sockaddr_storage *sk;
668 int port1, port2;
669
670 /* 'sk' is statically allocated (no need to be freed). */
671 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
672 if (!sk) {
673 Alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
674 goto err;
675 }
676
677 proto = protocol_by_family(sk->ss_family);
678 if (!proto || !proto->connect) {
679 Alert("'%s %s' : connect() not supported for this address family.\n",
680 args[*cur_arg], args[*cur_arg + 1]);
681 goto err;
682 }
683
684 if (port1 != port2) {
685 Alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
686 args[*cur_arg], args[*cur_arg + 1]);
687 goto err;
688 }
689 newsrv->conn_src.tproxy_addr = *sk;
690 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
691 }
692 global.last_checks |= LSTCHK_NETADM;
693 *cur_arg += 2;
694 continue;
695#else /* no TPROXY support */
696 Alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
697 goto err;
698#endif /* defined(CONFIG_HAP_TRANSPARENT) */
699 } /* "usesrc" */
700
701 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
702#ifdef SO_BINDTODEVICE
703 if (!*args[*cur_arg + 1]) {
704 Alert("'%s' : missing interface name.\n", args[0]);
705 goto err;
706 }
707 free(newsrv->conn_src.iface_name);
708 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
709 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
710 global.last_checks |= LSTCHK_NETADM;
711#else
712 Alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
713 goto err;
714#endif
715 *cur_arg += 2;
716 continue;
717 }
718 /* this keyword in not an option of "source" */
719 break;
720 } /* while */
721
722 return 0;
723
724 err:
725 free(errmsg);
726 return ERR_ALERT | ERR_FATAL;
727}
728
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100729/* Parse the "stick" server keyword */
730static int srv_parse_stick(char **args, int *cur_arg,
731 struct proxy *curproxy, struct server *newsrv, char **err)
732{
733 newsrv->flags &= ~SRV_F_NON_STICK;
734 return 0;
735}
736
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100737/* Parse the "track" server keyword */
738static int srv_parse_track(char **args, int *cur_arg,
739 struct proxy *curproxy, struct server *newsrv, char **err)
740{
741 char *arg;
742
743 arg = args[*cur_arg + 1];
744 if (!*arg) {
745 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
746 return ERR_ALERT | ERR_FATAL;
747 }
748
749 free(newsrv->trackit);
750 newsrv->trackit = strdup(arg);
751
752 return 0;
753}
754
Frédéric Lécailledba97072017-03-17 15:33:50 +0100755
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200756/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200757 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200758 * shutdown.
759 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200760void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200761{
Willy Tarreau87b09662015-04-03 00:22:06 +0200762 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200763
Willy Tarreau87b09662015-04-03 00:22:06 +0200764 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
765 if (stream->srv_conn == srv)
766 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200767}
768
769/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200770 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200771 * the reason for the shutdown.
772 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200773void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200774{
775 struct server *srv;
776
777 for (srv = px->srv; srv != NULL; srv = srv->next)
778 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200779 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200780}
781
Willy Tarreaubda92272014-05-20 21:55:30 +0200782/* Appends some information to a message string related to a server going UP or
783 * DOWN. If both <forced> and <reason> are null and the server tracks another
784 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200785 * If <check> is non-null, an entire string describing the check result will be
786 * appended after a comma and a space (eg: to report some information from the
787 * check that changed the state). In the other case, the string will be built
788 * using the check results stored into the struct server if present.
789 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200790 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200791 */
Emeric Brun5a133512017-10-19 14:42:30 +0200792void srv_append_status(struct chunk *msg, struct server *s, struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200793{
Emeric Brun5a133512017-10-19 14:42:30 +0200794 short status = s->op_st_chg.status;
795 short code = s->op_st_chg.code;
796 long duration = s->op_st_chg.duration;
797 char *desc = s->op_st_chg.reason;
798
799 if (check) {
800 status = check->status;
801 code = check->code;
802 duration = check->duration;
803 desc = check->desc;
804 }
805
806 if (status != -1) {
807 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
808
809 if (status >= HCHK_STATUS_L57DATA)
810 chunk_appendf(msg, ", code: %d", code);
811
812 if (desc && *desc) {
813 struct chunk src;
814
815 chunk_appendf(msg, ", info: \"");
816
817 chunk_initlen(&src, desc, 0, strlen(desc));
818 chunk_asciiencode(msg, &src, '"');
819
820 chunk_appendf(msg, "\"");
821 }
822
823 if (duration >= 0)
824 chunk_appendf(msg, ", check duration: %ldms", duration);
825 }
826 else if (desc && *desc) {
827 chunk_appendf(msg, ", %s", desc);
828 }
829 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200830 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200831 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200832
833 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200834 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200835 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
836 " %d sessions active, %d requeued, %d remaining in queue",
837 s->proxy->srv_act, s->proxy->srv_bck,
838 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
839 s->cur_sess, xferred, s->nbpend);
840 else
841 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
842 " %d sessions requeued, %d total in queue",
843 s->proxy->srv_act, s->proxy->srv_bck,
844 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
845 xferred, s->nbpend);
846 }
847}
848
Emeric Brun5a133512017-10-19 14:42:30 +0200849/* Marks server <s> down, regardless of its checks' statuses. The server is
850 * registered in a list to postpone the counting of the remaining servers on
851 * the proxy and transfers queued streams whenever possible to other servers at
852 * a sync point. Maintenance servers are ignored. It stores the <reason> if
853 * non-null as the reason for going down or the available data from the check
854 * struct to recompute this reason later.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200855 */
Emeric Brun5a133512017-10-19 14:42:30 +0200856void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200857{
858 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200859
Emeric Brun64cc49c2017-10-03 14:46:45 +0200860 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200861 return;
862
Emeric Brun52a91d32017-08-31 14:41:55 +0200863 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200864 *s->op_st_chg.reason = 0;
865 s->op_st_chg.status = -1;
866 if (reason) {
867 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
868 }
869 else if (check) {
870 if (check->desc)
871 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
872 s->op_st_chg.code = check->code;
873 s->op_st_chg.status = check->status;
874 s->op_st_chg.duration = check->duration;
875 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200876
Emeric Brun64cc49c2017-10-03 14:46:45 +0200877 /* Register changes to be applied asynchronously */
878 if (LIST_ISEMPTY(&s->update_status))
879 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200880
881 for (srv = s->trackers; srv; srv = srv->tracknext)
Emeric Brun5a133512017-10-19 14:42:30 +0200882 srv_set_stopped(srv, NULL, NULL);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200883}
884
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200885/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200886 * in maintenance. The server is registered in a list to postpone the counting
887 * of the remaining servers on the proxy and tries to grab requests from the
888 * proxy at a sync point. Maintenance servers are ignored. It stores the
889 * <reason> if non-null as the reason for going down or the available data
890 * from the check struct to recompute this reason later.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200891 */
Emeric Brun5a133512017-10-19 14:42:30 +0200892void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200893{
894 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200895
Emeric Brun64cc49c2017-10-03 14:46:45 +0200896 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200897 return;
898
Emeric Brun52a91d32017-08-31 14:41:55 +0200899 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200900 return;
901
Emeric Brun52a91d32017-08-31 14:41:55 +0200902 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200903 *s->op_st_chg.reason = 0;
904 s->op_st_chg.status = -1;
905 if (reason) {
906 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
907 }
908 else if (check) {
909 if (check->desc)
910 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
911 s->op_st_chg.code = check->code;
912 s->op_st_chg.status = check->status;
913 s->op_st_chg.duration = check->duration;
914 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200915
Emeric Brun64cc49c2017-10-03 14:46:45 +0200916 if (s->slowstart <= 0)
917 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200918
Emeric Brun64cc49c2017-10-03 14:46:45 +0200919 /* Register changes to be applied asynchronously */
920 if (LIST_ISEMPTY(&s->update_status))
921 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200922
923 for (srv = s->trackers; srv; srv = srv->tracknext)
Emeric Brun5a133512017-10-19 14:42:30 +0200924 srv_set_running(srv, NULL, NULL);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200925}
926
Willy Tarreau8eb77842014-05-21 13:54:57 +0200927/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +0200928 * isn't in maintenance. The server is registered in a list to postpone the
929 * counting of the remaining servers on the proxy and tries to grab requests
930 * from the proxy. Maintenance servers are ignored. It stores the
931 * <reason> if non-null as the reason for going down or the available data
932 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +0200933 * up. Note that it makes use of the trash to build the log strings, so <reason>
934 * must not be placed there.
935 */
Emeric Brun5a133512017-10-19 14:42:30 +0200936void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200937{
938 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200939
Emeric Brun64cc49c2017-10-03 14:46:45 +0200940 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200941 return;
942
Emeric Brun52a91d32017-08-31 14:41:55 +0200943 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200944 return;
945
Emeric Brun52a91d32017-08-31 14:41:55 +0200946 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +0200947 *s->op_st_chg.reason = 0;
948 s->op_st_chg.status = -1;
949 if (reason) {
950 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
951 }
952 else if (check) {
953 if (check->desc)
954 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
955 s->op_st_chg.code = check->code;
956 s->op_st_chg.status = check->status;
957 s->op_st_chg.duration = check->duration;
958 }
Willy Tarreau8eb77842014-05-21 13:54:57 +0200959
Emeric Brun64cc49c2017-10-03 14:46:45 +0200960 /* Register changes to be applied asynchronously */
961 if (LIST_ISEMPTY(&s->update_status))
962 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreau8eb77842014-05-21 13:54:57 +0200963
964 for (srv = s->trackers; srv; srv = srv->tracknext)
Emeric Brun5a133512017-10-19 14:42:30 +0200965 srv_set_stopping(srv, NULL, NULL);
Willy Tarreau8eb77842014-05-21 13:54:57 +0200966}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200967
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200968/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
969 * enforce either maint mode or drain mode. It is not allowed to set more than
970 * one flag at once. The equivalent "inherited" flag is propagated to all
971 * tracking servers. Maintenance mode disables health checks (but not agent
972 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100973 * is done. If <cause> is non-null, it will be displayed at the end of the log
974 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200975 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100976void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200977{
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200978 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200979
980 if (!mode)
981 return;
982
983 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +0200984 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200985 return;
986
Emeric Brun52a91d32017-08-31 14:41:55 +0200987 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +0200988 if (cause)
989 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
990
991 /* Register changes to be applied asynchronously */
992 if (LIST_ISEMPTY(&s->update_status))
993 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200994
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200995 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +0200996 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
997 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200998 return;
999
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001000 /* compute the inherited flag to propagate */
1001 if (mode & SRV_ADMF_MAINT)
1002 mode = SRV_ADMF_IMAINT;
1003 else if (mode & SRV_ADMF_DRAIN)
1004 mode = SRV_ADMF_IDRAIN;
1005
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001006 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +01001007 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001008}
1009
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001010/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1011 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1012 * than one flag at once. The equivalent "inherited" flag is propagated to all
1013 * tracking servers. Leaving maintenance mode re-enables health checks. When
1014 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001015 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001016void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001017{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001018 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001019
1020 if (!mode)
1021 return;
1022
1023 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001024 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001025 return;
1026
Emeric Brun52a91d32017-08-31 14:41:55 +02001027 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001028
Emeric Brun64cc49c2017-10-03 14:46:45 +02001029 /* Register changes to be applied asynchronously */
1030 if (LIST_ISEMPTY(&s->update_status))
1031 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001032
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001033 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001034 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1035 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001036 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001037
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001038 if (mode & SRV_ADMF_MAINT)
1039 mode = SRV_ADMF_IMAINT;
1040 else if (mode & SRV_ADMF_DRAIN)
1041 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001042
1043 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001044 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001045}
1046
Willy Tarreau757478e2016-11-03 19:22:19 +01001047/* principle: propagate maint and drain to tracking servers. This is useful
1048 * upon startup so that inherited states are correct.
1049 */
1050static void srv_propagate_admin_state(struct server *srv)
1051{
1052 struct server *srv2;
1053
1054 if (!srv->trackers)
1055 return;
1056
1057 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001058 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001059 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001060
Emeric Brun52a91d32017-08-31 14:41:55 +02001061 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001062 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001063 }
1064}
1065
1066/* Compute and propagate the admin states for all servers in proxy <px>.
1067 * Only servers *not* tracking another one are considered, because other
1068 * ones will be handled when the server they track is visited.
1069 */
1070void srv_compute_all_admin_states(struct proxy *px)
1071{
1072 struct server *srv;
1073
1074 for (srv = px->srv; srv; srv = srv->next) {
1075 if (srv->track)
1076 continue;
1077 srv_propagate_admin_state(srv);
1078 }
1079}
1080
Willy Tarreaudff55432012-10-10 17:51:05 +02001081/* Note: must not be declared <const> as its list will be overwritten.
1082 * Please take care of keeping this list alphabetically sorted, doing so helps
1083 * all code contributors.
1084 * Optional keywords are also declared with a NULL ->parse() function so that
1085 * the config parser can report an appropriate error when a known keyword was
1086 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001087 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001088 */
1089static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001090 { "addr", srv_parse_addr, 1, 1 }, /* IP address to send health to or to probe from agent-check */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001091 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001092 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001093 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001094 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1 }, /* enable PROXY protocol for health checks */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001095 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001096 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1097 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001098 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001099 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001100 { "no-agent-check", srv_parse_no_agent_check, 0, 1 }, /* Do not enable any auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001101 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001102 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001103 { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1 }, /* disable PROXY protol for health checks */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001104 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1105 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001106 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001107 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Frédéric Lécaille16186232017-03-14 16:42:49 +01001108 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001109 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1110 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1 }, /* Enforce use of PROXY V2 protocol */
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001111 { "source", srv_parse_source, -1, 1 }, /* Set the source address to be used to connect to the server */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001112 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001113 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001114 { NULL, NULL, 0 },
1115}};
1116
1117__attribute__((constructor))
1118static void __listener_init(void)
1119{
1120 srv_register_keywords(&srv_kws);
1121}
1122
Willy Tarreau004e0452013-11-21 11:22:01 +01001123/* Recomputes the server's eweight based on its state, uweight, the current time,
1124 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1125 * state is automatically disabled if the time is elapsed.
1126 */
1127void server_recalc_eweight(struct server *sv)
1128{
1129 struct proxy *px = sv->proxy;
1130 unsigned w;
1131
1132 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1133 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001134 if (sv->next_state == SRV_ST_STARTING)
1135 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001136 }
1137
1138 /* We must take care of not pushing the server to full throttle during slow starts.
1139 * It must also start immediately, at least at the minimal step when leaving maintenance.
1140 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001141 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001142 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1143 else
1144 w = px->lbprm.wdiv;
1145
Emeric Brun52a91d32017-08-31 14:41:55 +02001146 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001147
Emeric Brun64cc49c2017-10-03 14:46:45 +02001148 /* Register changes to be applied asynchronously */
1149 if (LIST_ISEMPTY(&sv->update_status))
1150 LIST_ADDQ(&updated_servers, &sv->update_status);
Willy Tarreau004e0452013-11-21 11:22:01 +01001151}
1152
Willy Tarreaubaaee002006-06-26 02:48:02 +02001153/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001154 * Parses weight_str and configures sv accordingly.
1155 * Returns NULL on success, error message string otherwise.
1156 */
1157const char *server_parse_weight_change_request(struct server *sv,
1158 const char *weight_str)
1159{
1160 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001161 long int w;
1162 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001163
1164 px = sv->proxy;
1165
1166 /* if the weight is terminated with '%', it is set relative to
1167 * the initial weight, otherwise it is absolute.
1168 */
1169 if (!*weight_str)
1170 return "Require <weight> or <weight%>.\n";
1171
Simon Hormanb796afa2013-02-12 10:45:53 +09001172 w = strtol(weight_str, &end, 10);
1173 if (end == weight_str)
1174 return "Empty weight string empty or preceded by garbage";
1175 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001176 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001177 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001178 /* Avoid integer overflow */
1179 if (w > 25600)
1180 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001181 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001182 if (w > 256)
1183 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001184 }
1185 else if (w < 0 || w > 256)
1186 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001187 else if (end[0] != '\0')
1188 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001189
1190 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1191 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1192
1193 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001194 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001195
1196 return NULL;
1197}
1198
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001199/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001200 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1201 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001202 * Returns:
1203 * - error string on error
1204 * - NULL on success
1205 */
1206const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001207 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001208{
1209 unsigned char ip[INET6_ADDRSTRLEN];
1210
1211 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001212 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001213 return NULL;
1214 }
1215 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001216 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001217 return NULL;
1218 }
1219
1220 return "Could not understand IP address format.\n";
1221}
1222
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001223const char *server_parse_maxconn_change_request(struct server *sv,
1224 const char *maxconn_str)
1225{
1226 long int v;
1227 char *end;
1228
1229 if (!*maxconn_str)
1230 return "Require <maxconn>.\n";
1231
1232 v = strtol(maxconn_str, &end, 10);
1233 if (end == maxconn_str)
1234 return "maxconn string empty or preceded by garbage";
1235 else if (end[0] != '\0')
1236 return "Trailing garbage in maxconn string";
1237
1238 if (sv->maxconn == sv->minconn) { // static maxconn
1239 sv->maxconn = sv->minconn = v;
1240 } else { // dynamic maxconn
1241 sv->maxconn = v;
1242 }
1243
1244 if (may_dequeue_tasks(sv, sv->proxy))
1245 process_srv_queue(sv);
1246
1247 return NULL;
1248}
1249
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001250#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001251static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1252 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001253{
1254 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001255 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001256 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001257 NULL,
1258 };
1259
1260 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001261 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001262
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001263 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1264}
1265
1266static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1267{
1268 struct sample_expr *expr;
1269
1270 expr = srv_sni_sample_parse_expr(newsrv, px, px->conf.file, px->conf.line, err);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001271 if (!expr) {
1272 memprintf(err, "error detected while parsing sni expression : %s", *err);
1273 return ERR_ALERT | ERR_FATAL;
1274 }
1275
1276 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1277 memprintf(err, "error detected while parsing sni expression : "
1278 " fetch method '%s' extracts information from '%s', "
1279 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001280 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001281 return ERR_ALERT | ERR_FATAL;
1282 }
1283
1284 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1285 release_sample_expr(newsrv->ssl_ctx.sni);
1286 newsrv->ssl_ctx.sni = expr;
1287
1288 return 0;
1289}
1290#endif
1291
1292static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1293{
1294 if (err && *err) {
1295 indent_msg(err, 2);
1296 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1297 }
1298 else
1299 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1300 file, linenum, args[0], args[1], args[cur_arg]);
1301}
1302
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001303static void srv_conn_src_sport_range_cpy(struct server *srv,
1304 struct server *src)
1305{
1306 int range_sz;
1307
1308 range_sz = src->conn_src.sport_range->size;
1309 if (range_sz > 0) {
1310 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1311 if (srv->conn_src.sport_range != NULL) {
1312 int i;
1313
1314 for (i = 0; i < range_sz; i++) {
1315 srv->conn_src.sport_range->ports[i] =
1316 src->conn_src.sport_range->ports[i];
1317 }
1318 }
1319 }
1320}
1321
1322/*
1323 * Copy <src> server connection source settings to <srv> server everything needed.
1324 */
1325static void srv_conn_src_cpy(struct server *srv, struct server *src)
1326{
1327 srv->conn_src.opts = src->conn_src.opts;
1328 srv->conn_src.source_addr = src->conn_src.source_addr;
1329
1330 /* Source port range copy. */
1331 if (src->conn_src.sport_range != NULL)
1332 srv_conn_src_sport_range_cpy(srv, src);
1333
1334#ifdef CONFIG_HAP_TRANSPARENT
1335 if (src->conn_src.bind_hdr_name != NULL) {
1336 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1337 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1338 }
1339 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1340 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1341#endif
1342 if (src->conn_src.iface_name != NULL)
1343 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1344}
1345
1346/*
1347 * Copy <src> server SSL settings to <srv> server allocating
1348 * everything needed.
1349 */
1350#if defined(USE_OPENSSL)
1351static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1352{
1353 if (src->ssl_ctx.ca_file != NULL)
1354 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1355 if (src->ssl_ctx.crl_file != NULL)
1356 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1357 if (src->ssl_ctx.client_crt != NULL)
1358 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1359
1360 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1361
1362 if (src->ssl_ctx.verify_host != NULL)
1363 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1364 if (src->ssl_ctx.ciphers != NULL)
1365 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1366 if (src->sni_expr != NULL)
1367 srv->sni_expr = strdup(src->sni_expr);
1368}
1369#endif
1370
1371/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001372 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001373 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001374 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001375 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001376static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001377{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001378 char *hostname_dn;
1379 int hostname_len, hostname_dn_len;
1380
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001381 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001382 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001383
Christopher Faulet67957bd2017-09-27 11:00:59 +02001384 hostname_len = strlen(hostname);
1385 hostname_dn = trash.str;
1386 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1387 hostname_dn, trash.size);
1388 if (hostname_dn_len == -1)
1389 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001390
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001391
Christopher Faulet67957bd2017-09-27 11:00:59 +02001392 free(srv->hostname);
1393 free(srv->hostname_dn);
1394 srv->hostname = strdup(hostname);
1395 srv->hostname_dn = strdup(hostname_dn);
1396 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001397 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001398 goto err;
1399
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001400 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001401
1402 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001403 free(srv->hostname); srv->hostname = NULL;
1404 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001405 return -1;
1406}
1407
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001408/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001409 * Copy <src> server settings to <srv> server allocating
1410 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001411 * This function is not supposed to be called at any time, but only
1412 * during server settings parsing or during server allocations from
1413 * a server template, and just after having calloc()'ed a new server.
1414 * So, <src> may only be a default server (when parsing server settings)
1415 * or a server template (during server allocations from a server template).
1416 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1417 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001418 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001419static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001420{
1421 /* Connection source settings copy */
1422 srv_conn_src_cpy(srv, src);
1423
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001424 if (srv_tmpl) {
1425 srv->addr = src->addr;
1426 srv->svc_port = src->svc_port;
1427 }
1428
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001429 srv->pp_opts = src->pp_opts;
1430 if (src->rdr_pfx != NULL) {
1431 srv->rdr_pfx = strdup(src->rdr_pfx);
1432 srv->rdr_len = src->rdr_len;
1433 }
1434 if (src->cookie != NULL) {
1435 srv->cookie = strdup(src->cookie);
1436 srv->cklen = src->cklen;
1437 }
1438 srv->use_ssl = src->use_ssl;
1439 srv->check.addr = srv->agent.addr = src->check.addr;
1440 srv->check.use_ssl = src->check.use_ssl;
1441 srv->check.port = src->check.port;
1442 /* Note: 'flags' field has potentially been already initialized. */
1443 srv->flags |= src->flags;
1444 srv->do_check = src->do_check;
1445 srv->do_agent = src->do_agent;
1446 if (srv->check.port)
1447 srv->flags |= SRV_F_CHECKPORT;
1448 srv->check.inter = src->check.inter;
1449 srv->check.fastinter = src->check.fastinter;
1450 srv->check.downinter = src->check.downinter;
1451 srv->agent.use_ssl = src->agent.use_ssl;
1452 srv->agent.port = src->agent.port;
1453 if (src->agent.send_string != NULL)
1454 srv->agent.send_string = strdup(src->agent.send_string);
1455 srv->agent.send_string_len = src->agent.send_string_len;
1456 srv->agent.inter = src->agent.inter;
1457 srv->agent.fastinter = src->agent.fastinter;
1458 srv->agent.downinter = src->agent.downinter;
1459 srv->maxqueue = src->maxqueue;
1460 srv->minconn = src->minconn;
1461 srv->maxconn = src->maxconn;
1462 srv->slowstart = src->slowstart;
1463 srv->observe = src->observe;
1464 srv->onerror = src->onerror;
1465 srv->onmarkeddown = src->onmarkeddown;
1466 srv->onmarkedup = src->onmarkedup;
1467 if (src->trackit != NULL)
1468 srv->trackit = strdup(src->trackit);
1469 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1470 srv->uweight = srv->iweight = src->iweight;
1471
1472 srv->check.send_proxy = src->check.send_proxy;
1473 /* health: up, but will fall down at first failure */
1474 srv->check.rise = srv->check.health = src->check.rise;
1475 srv->check.fall = src->check.fall;
1476
1477 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001478 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1479 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1480 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001481 srv->check.state |= CHK_ST_PAUSED;
1482 srv->check.health = 0;
1483 }
1484
1485 /* health: up but will fall down at first failure */
1486 srv->agent.rise = srv->agent.health = src->agent.rise;
1487 srv->agent.fall = src->agent.fall;
1488
1489 if (src->resolvers_id != NULL)
1490 srv->resolvers_id = strdup(src->resolvers_id);
1491 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1492 if (srv->dns_opts.family_prio == AF_UNSPEC)
1493 srv->dns_opts.family_prio = AF_INET6;
1494 memcpy(srv->dns_opts.pref_net,
1495 src->dns_opts.pref_net,
1496 sizeof srv->dns_opts.pref_net);
1497 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1498
1499 srv->init_addr_methods = src->init_addr_methods;
1500 srv->init_addr = src->init_addr;
1501#if defined(USE_OPENSSL)
1502 srv_ssl_settings_cpy(srv, src);
1503#endif
1504#ifdef TCP_USER_TIMEOUT
1505 srv->tcp_ut = src->tcp_ut;
1506#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001507 if (srv_tmpl)
1508 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001509}
1510
1511static struct server *new_server(struct proxy *proxy)
1512{
1513 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001514 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001515
1516 srv = calloc(1, sizeof *srv);
1517 if (!srv)
1518 return NULL;
1519
1520 srv->obj_type = OBJ_TYPE_SERVER;
1521 srv->proxy = proxy;
1522 LIST_INIT(&srv->actconns);
1523 LIST_INIT(&srv->pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02001524
1525 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1526 goto free_srv;
1527 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1528 goto free_priv_conns;
1529 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1530 goto free_idle_conns;
1531
1532 for (i = 0; i < global.nbthread; i++) {
1533 LIST_INIT(&srv->priv_conns[i]);
1534 LIST_INIT(&srv->idle_conns[i]);
1535 LIST_INIT(&srv->safe_conns[i]);
1536 }
1537
Emeric Brun64cc49c2017-10-03 14:46:45 +02001538 LIST_INIT(&srv->update_status);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001539
Emeric Brun52a91d32017-08-31 14:41:55 +02001540 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001541 srv->last_change = now.tv_sec;
1542
1543 srv->check.status = HCHK_STATUS_INI;
1544 srv->check.server = srv;
1545 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1546
1547 srv->agent.status = HCHK_STATUS_INI;
1548 srv->agent.server = srv;
1549 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1550
1551 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001552
1553 free_idle_conns:
1554 free(srv->idle_conns);
1555 free_priv_conns:
1556 free(srv->priv_conns);
1557 free_srv:
1558 free(srv);
1559 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001560}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001561
1562/*
1563 * Validate <srv> server health-check settings.
1564 * Returns 0 if everything is OK, -1 if not.
1565 */
1566static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1567{
1568 struct tcpcheck_rule *r = NULL;
1569 struct list *l;
1570
1571 /*
1572 * We need at least a service port, a check port or the first tcp-check rule must
1573 * be a 'connect' one when checking an IPv4/IPv6 server.
1574 */
1575 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1576 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1577 return 0;
1578
1579 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1580 if (!r) {
1581 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1582 "Check has been disabled.\n",
1583 file, linenum, srv->id);
1584 return -1;
1585 }
1586
1587 /* search the first action (connect / send / expect) in the list */
1588 l = &srv->proxy->tcpcheck_rules;
1589 list_for_each_entry(r, l, list) {
1590 if (r->action != TCPCHK_ACT_COMMENT)
1591 break;
1592 }
1593
1594 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1595 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1596 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1597 file, linenum, srv->id);
1598 return -1;
1599 }
1600
1601 /* scan the tcp-check ruleset to ensure a port has been configured */
1602 l = &srv->proxy->tcpcheck_rules;
1603 list_for_each_entry(r, l, list) {
1604 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1605 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1606 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1607 file, linenum, srv->id);
1608 return -1;
1609 }
1610 }
1611
1612 return 0;
1613}
1614
1615/*
1616 * Initialize <srv> health-check structure.
1617 * Returns the error string in case of memory allocation failure, NULL if not.
1618 */
1619static const char *do_health_check_init(struct server *srv, int check_type, int state)
1620{
1621 const char *ret;
1622
1623 if (!srv->do_check)
1624 return NULL;
1625
1626 ret = init_check(&srv->check, check_type);
1627 if (ret)
1628 return ret;
1629
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001630 srv->check.state |= state;
1631 global.maxsock++;
1632
1633 return NULL;
1634}
1635
1636static int server_health_check_init(const char *file, int linenum,
1637 struct server *srv, struct proxy *curproxy)
1638{
1639 const char *ret;
1640
1641 if (!srv->do_check)
1642 return 0;
1643
1644 if (srv->trackit) {
1645 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1646 file, linenum);
1647 return ERR_ALERT | ERR_FATAL;
1648 }
1649
1650 if (server_healthcheck_validate(file, linenum, srv) < 0)
1651 return ERR_ALERT | ERR_ABORT;
1652
1653 /* note: check type will be set during the config review phase */
1654 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1655 if (ret) {
1656 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1657 return ERR_ALERT | ERR_ABORT;
1658 }
1659
1660 return 0;
1661}
1662
1663/*
1664 * Initialize <srv> agent check structure.
1665 * Returns the error string in case of memory allocation failure, NULL if not.
1666 */
1667static const char *do_server_agent_check_init(struct server *srv, int state)
1668{
1669 const char *ret;
1670
1671 if (!srv->do_agent)
1672 return NULL;
1673
1674 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1675 if (ret)
1676 return ret;
1677
1678 if (!srv->agent.inter)
1679 srv->agent.inter = srv->check.inter;
1680
1681 srv->agent.state |= state;
1682 global.maxsock++;
1683
1684 return NULL;
1685}
1686
1687static int server_agent_check_init(const char *file, int linenum,
1688 struct server *srv, struct proxy *curproxy)
1689{
1690 const char *ret;
1691
1692 if (!srv->do_agent)
1693 return 0;
1694
1695 if (!srv->agent.port) {
1696 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1697 file, linenum, srv->id);
1698 return ERR_ALERT | ERR_FATAL;
1699 }
1700
1701 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1702 if (ret) {
1703 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1704 return ERR_ALERT | ERR_ABORT;
1705 }
1706
1707 return 0;
1708}
1709
1710#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1711static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1712 struct server *srv, struct proxy *proxy)
1713{
1714 int ret;
1715 char *err = NULL;
1716
1717 if (!srv->sni_expr)
1718 return 0;
1719
1720 ret = server_parse_sni_expr(srv, proxy, &err);
1721 if (!ret)
1722 return 0;
1723
1724 display_parser_err(file, linenum, args, cur_arg, &err);
1725 free(err);
1726
1727 return ret;
1728}
1729#endif
1730
1731/*
1732 * Server initializations finalization.
1733 * Initialize health check, agent check and SNI expression if enabled.
1734 * Must not be called for a default server instance.
1735 */
1736static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1737 struct server *srv, struct proxy *px)
1738{
1739 int ret;
1740
1741 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1742 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1743 return ret;
1744 }
1745
1746#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1747 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1748 return ret;
1749#endif
1750
1751 if (srv->flags & SRV_F_BACKUP)
1752 px->srv_bck++;
1753 else
1754 px->srv_act++;
1755 srv_lb_commit_status(srv);
1756
1757 return 0;
1758}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001759
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001760/*
1761 * Parse as much as possible such a range string argument: low[-high]
1762 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1763 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1764 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1765 * Returns 0 if succeeded, -1 if not.
1766 */
1767static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1768{
1769 char *nb_high_arg;
1770
1771 *nb_high = 0;
1772 chunk_printf(&trash, "%s", arg);
1773 *nb_low = atoi(trash.str);
1774
1775 if ((nb_high_arg = strchr(trash.str, '-'))) {
1776 *nb_high_arg++ = '\0';
1777 *nb_high = atoi(nb_high_arg);
1778 }
1779 else {
1780 *nb_high += *nb_low;
1781 *nb_low = 1;
1782 }
1783
1784 if (*nb_low < 0 || *nb_high < *nb_low)
1785 return -1;
1786
1787 return 0;
1788}
1789
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001790static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1791{
1792 chunk_printf(&trash, "%s%d", prefix, nb);
1793 free(srv->id);
1794 srv->id = strdup(trash.str);
1795}
1796
1797/*
1798 * Initialize as much as possible servers from <srv> server template.
1799 * Note that a server template is a special server with
1800 * a few different parameters than a server which has
1801 * been parsed mostly the same way as a server.
1802 * Returns the number of servers succesfully allocated,
1803 * 'srv' template included.
1804 */
1805static int server_template_init(struct server *srv, struct proxy *px)
1806{
1807 int i;
1808 struct server *newsrv;
1809
1810 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1811 int check_init_state;
1812 int agent_init_state;
1813
1814 newsrv = new_server(px);
1815 if (!newsrv)
1816 goto err;
1817
1818 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001819 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001820#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1821 if (newsrv->sni_expr) {
1822 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1823 if (!newsrv->ssl_ctx.sni)
1824 goto err;
1825 }
1826#endif
1827 /* Set this new server ID. */
1828 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1829
1830 /* Initial checks states. */
1831 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1832 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1833
1834 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1835 do_server_agent_check_init(newsrv, agent_init_state))
1836 goto err;
1837
1838 /* Linked backwards first. This will be restablished after parsing. */
1839 newsrv->next = px->srv;
1840 px->srv = newsrv;
1841 }
1842 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1843
1844 return i - srv->tmpl_info.nb_low;
1845
1846 err:
1847 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1848 if (newsrv) {
1849#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1850 release_sample_expr(newsrv->ssl_ctx.sni);
1851#endif
1852 free_check(&newsrv->agent);
1853 free_check(&newsrv->check);
1854 }
1855 free(newsrv);
1856 return i - srv->tmpl_info.nb_low;
1857}
1858
Willy Tarreau272adea2014-03-31 10:39:59 +02001859int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1860{
1861 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001862 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001863 char *errmsg = NULL;
1864 int err_code = 0;
1865 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001866 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001867
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001868 if (!strcmp(args[0], "server") ||
1869 !strcmp(args[0], "default-server") ||
1870 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001871 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001872 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001873 int srv = !defsrv && !strcmp(args[0], "server");
1874 int srv_tmpl = !defsrv && !srv;
1875 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001876
1877 if (!defsrv && curproxy == defproxy) {
1878 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1879 err_code |= ERR_ALERT | ERR_FATAL;
1880 goto out;
1881 }
1882 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1883 err_code |= ERR_ALERT | ERR_FATAL;
1884
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001885 /* There is no mandatory first arguments for default server. */
1886 if (srv) {
1887 if (!*args[2]) {
1888 /* 'server' line number of argument check. */
1889 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1890 file, linenum, args[0]);
1891 err_code |= ERR_ALERT | ERR_FATAL;
1892 goto out;
1893 }
1894
1895 err = invalid_char(args[1]);
1896 }
1897 else if (srv_tmpl) {
1898 if (!*args[3]) {
1899 /* 'server-template' line number of argument check. */
1900 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
1901 file, linenum, args[0]);
1902 err_code |= ERR_ALERT | ERR_FATAL;
1903 goto out;
1904 }
1905
1906 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001907 }
1908
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001909 if (err) {
1910 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
1911 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001912 err_code |= ERR_ALERT | ERR_FATAL;
1913 goto out;
1914 }
1915
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001916 cur_arg = 2;
1917 if (srv_tmpl) {
1918 /* Parse server-template <nb | range> arg. */
1919 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
1920 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
1921 file, linenum, args[0], args[cur_arg]);
1922 err_code |= ERR_ALERT | ERR_FATAL;
1923 goto out;
1924 }
1925 cur_arg++;
1926 }
1927
Willy Tarreau272adea2014-03-31 10:39:59 +02001928 if (!defsrv) {
1929 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001930 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001931 struct protocol *proto;
1932
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001933 newsrv = new_server(curproxy);
1934 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001935 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1936 err_code |= ERR_ALERT | ERR_ABORT;
1937 goto out;
1938 }
1939
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001940 if (srv_tmpl) {
1941 newsrv->tmpl_info.nb_low = tmpl_range_low;
1942 newsrv->tmpl_info.nb_high = tmpl_range_high;
1943 }
1944
Willy Tarreau272adea2014-03-31 10:39:59 +02001945 /* the servers are linked backwards first */
1946 newsrv->next = curproxy->srv;
1947 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02001948 newsrv->conf.file = strdup(file);
1949 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001950 /* Note: for a server template, its id is its prefix.
1951 * This is a temporary id which will be used for server allocations to come
1952 * after parsing.
1953 */
1954 if (srv)
1955 newsrv->id = strdup(args[1]);
1956 else
1957 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001958
1959 /* several ways to check the port component :
1960 * - IP => port=+0, relative (IPv4 only)
1961 * - IP: => port=+0, relative
1962 * - IP:N => port=N, absolute
1963 * - IP:+N => port=+N, relative
1964 * - IP:-N => port=-N, relative
1965 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001966 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001967 if (!sk) {
1968 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1969 err_code |= ERR_ALERT | ERR_FATAL;
1970 goto out;
1971 }
1972
1973 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001974 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001975 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1976 file, linenum, args[0], args[1]);
1977 err_code |= ERR_ALERT | ERR_FATAL;
1978 goto out;
1979 }
1980
1981 if (!port1 || !port2) {
1982 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001983 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001984 }
1985 else if (port1 != port2) {
1986 /* port range */
1987 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1988 file, linenum, args[0], args[1], args[2]);
1989 err_code |= ERR_ALERT | ERR_FATAL;
1990 goto out;
1991 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001992
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001993 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02001994 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001995 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02001996 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001997 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
1998 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
1999 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002000 err_code |= ERR_ALERT | ERR_FATAL;
2001 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002002 }
2003 }
2004 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
2005 Alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
2006 file, linenum, newsrv->id);
2007 err_code |= ERR_ALERT | ERR_FATAL;
2008 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002009 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002010 }
2011
Willy Tarreau272adea2014-03-31 10:39:59 +02002012 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002013 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002014
Olivier Houchard8da5f982017-08-04 18:35:36 +02002015 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002016 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002017 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002018 err_code |= ERR_ALERT | ERR_FATAL;
2019 goto out;
2020 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002021
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002022 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002023 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002024 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002025 } else {
2026 newsrv = &curproxy->defsrv;
2027 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002028 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002029 }
2030
2031 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002032 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002033 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2034 if (err) {
2035 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2036 file, linenum, *err, newsrv->id);
2037 err_code |= ERR_ALERT | ERR_FATAL;
2038 goto out;
2039 }
2040 if (val <= 0) {
2041 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2042 file, linenum, val, args[cur_arg], newsrv->id);
2043 err_code |= ERR_ALERT | ERR_FATAL;
2044 goto out;
2045 }
2046 newsrv->agent.inter = val;
2047 cur_arg += 2;
2048 }
Misiekea849332017-01-09 09:39:51 +01002049 else if (!strcmp(args[cur_arg], "agent-addr")) {
2050 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2051 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2052 goto out;
2053 }
2054
2055 cur_arg += 2;
2056 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002057 else if (!strcmp(args[cur_arg], "agent-port")) {
2058 global.maxsock++;
2059 newsrv->agent.port = atol(args[cur_arg + 1]);
2060 cur_arg += 2;
2061 }
James Brown55f9ff12015-10-21 18:19:05 -07002062 else if (!strcmp(args[cur_arg], "agent-send")) {
2063 global.maxsock++;
2064 free(newsrv->agent.send_string);
2065 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2066 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2067 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2068 cur_arg += 2;
2069 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002070 else if (!strcmp(args[cur_arg], "init-addr")) {
2071 char *p, *end;
2072 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002073 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002074
2075 newsrv->init_addr_methods = 0;
2076 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2077
2078 for (p = args[cur_arg + 1]; *p; p = end) {
2079 /* cut on next comma */
2080 for (end = p; *end && *end != ','; end++);
2081 if (*end)
2082 *(end++) = 0;
2083
Willy Tarreau4310d362016-11-02 15:05:56 +01002084 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002085 if (!strcmp(p, "libc")) {
2086 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2087 }
2088 else if (!strcmp(p, "last")) {
2089 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2090 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002091 else if (!strcmp(p, "none")) {
2092 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2093 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002094 else if (str2ip2(p, &sa, 0)) {
2095 if (is_addr(&newsrv->init_addr)) {
2096 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2097 file, linenum, args[cur_arg], p);
2098 err_code |= ERR_ALERT | ERR_FATAL;
2099 goto out;
2100 }
2101 newsrv->init_addr = sa;
2102 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2103 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002104 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002105 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002106 file, linenum, args[cur_arg], p);
2107 err_code |= ERR_ALERT | ERR_FATAL;
2108 goto out;
2109 }
2110 if (!done) {
2111 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2112 file, linenum, args[cur_arg], p);
2113 err_code |= ERR_ALERT | ERR_FATAL;
2114 goto out;
2115 }
2116 }
2117 cur_arg += 2;
2118 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002119 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002120 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002121 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2122 cur_arg += 2;
2123 }
2124 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2125 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002126 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002127 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002128 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002129 else {
2130 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2131 file, linenum, args[cur_arg]);
2132 err_code |= ERR_ALERT | ERR_FATAL;
2133 goto out;
2134 }
2135 cur_arg += 2;
2136 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002137 else if (!strcmp(args[cur_arg], "resolve-net")) {
2138 char *p, *e;
2139 unsigned char mask;
2140 struct dns_options *opt;
2141
2142 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
2143 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2144 file, linenum, args[cur_arg]);
2145 err_code |= ERR_ALERT | ERR_FATAL;
2146 goto out;
2147 }
2148
2149 opt = &newsrv->dns_opts;
2150
2151 /* Split arguments by comma, and convert it from ipv4 or ipv6
2152 * string network in in_addr or in6_addr.
2153 */
2154 p = args[cur_arg + 1];
2155 e = p;
2156 while (*p != '\0') {
2157 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002158 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002159 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2160 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2161 err_code |= ERR_ALERT | ERR_FATAL;
2162 goto out;
2163 }
2164 /* look for end or comma. */
2165 while (*e != ',' && *e != '\0')
2166 e++;
2167 if (*e == ',') {
2168 *e = '\0';
2169 e++;
2170 }
2171 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2172 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2173 /* Try to convert input string from ipv4 or ipv6 network. */
2174 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2175 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2176 &mask)) {
2177 /* Try to convert input string from ipv6 network. */
2178 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2179 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2180 } else {
2181 /* All network conversions fail, retrun error. */
2182 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2183 file, linenum, args[cur_arg], p);
2184 err_code |= ERR_ALERT | ERR_FATAL;
2185 goto out;
2186 }
2187 opt->pref_net_nb++;
2188 p = e;
2189 }
2190
2191 cur_arg += 2;
2192 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002193 else if (!strcmp(args[cur_arg], "rise")) {
2194 if (!*args[cur_arg + 1]) {
2195 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2196 file, linenum, args[cur_arg]);
2197 err_code |= ERR_ALERT | ERR_FATAL;
2198 goto out;
2199 }
2200
2201 newsrv->check.rise = atol(args[cur_arg + 1]);
2202 if (newsrv->check.rise <= 0) {
2203 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2204 file, linenum, args[cur_arg]);
2205 err_code |= ERR_ALERT | ERR_FATAL;
2206 goto out;
2207 }
2208
2209 if (newsrv->check.health)
2210 newsrv->check.health = newsrv->check.rise;
2211 cur_arg += 2;
2212 }
2213 else if (!strcmp(args[cur_arg], "fall")) {
2214 newsrv->check.fall = atol(args[cur_arg + 1]);
2215
2216 if (!*args[cur_arg + 1]) {
2217 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2218 file, linenum, args[cur_arg]);
2219 err_code |= ERR_ALERT | ERR_FATAL;
2220 goto out;
2221 }
2222
2223 if (newsrv->check.fall <= 0) {
2224 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2225 file, linenum, args[cur_arg]);
2226 err_code |= ERR_ALERT | ERR_FATAL;
2227 goto out;
2228 }
2229
2230 cur_arg += 2;
2231 }
2232 else if (!strcmp(args[cur_arg], "inter")) {
2233 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2234 if (err) {
2235 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2236 file, linenum, *err, newsrv->id);
2237 err_code |= ERR_ALERT | ERR_FATAL;
2238 goto out;
2239 }
2240 if (val <= 0) {
2241 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2242 file, linenum, val, args[cur_arg], newsrv->id);
2243 err_code |= ERR_ALERT | ERR_FATAL;
2244 goto out;
2245 }
2246 newsrv->check.inter = val;
2247 cur_arg += 2;
2248 }
2249 else if (!strcmp(args[cur_arg], "fastinter")) {
2250 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2251 if (err) {
2252 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2253 file, linenum, *err, newsrv->id);
2254 err_code |= ERR_ALERT | ERR_FATAL;
2255 goto out;
2256 }
2257 if (val <= 0) {
2258 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2259 file, linenum, val, args[cur_arg], newsrv->id);
2260 err_code |= ERR_ALERT | ERR_FATAL;
2261 goto out;
2262 }
2263 newsrv->check.fastinter = val;
2264 cur_arg += 2;
2265 }
2266 else if (!strcmp(args[cur_arg], "downinter")) {
2267 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2268 if (err) {
2269 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2270 file, linenum, *err, newsrv->id);
2271 err_code |= ERR_ALERT | ERR_FATAL;
2272 goto out;
2273 }
2274 if (val <= 0) {
2275 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2276 file, linenum, val, args[cur_arg], newsrv->id);
2277 err_code |= ERR_ALERT | ERR_FATAL;
2278 goto out;
2279 }
2280 newsrv->check.downinter = val;
2281 cur_arg += 2;
2282 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002283 else if (!strcmp(args[cur_arg], "port")) {
2284 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002285 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002286 cur_arg += 2;
2287 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002288 else if (!strcmp(args[cur_arg], "weight")) {
2289 int w;
2290 w = atol(args[cur_arg + 1]);
2291 if (w < 0 || w > SRV_UWGHT_MAX) {
2292 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2293 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2294 err_code |= ERR_ALERT | ERR_FATAL;
2295 goto out;
2296 }
2297 newsrv->uweight = newsrv->iweight = w;
2298 cur_arg += 2;
2299 }
2300 else if (!strcmp(args[cur_arg], "minconn")) {
2301 newsrv->minconn = atol(args[cur_arg + 1]);
2302 cur_arg += 2;
2303 }
2304 else if (!strcmp(args[cur_arg], "maxconn")) {
2305 newsrv->maxconn = atol(args[cur_arg + 1]);
2306 cur_arg += 2;
2307 }
2308 else if (!strcmp(args[cur_arg], "maxqueue")) {
2309 newsrv->maxqueue = atol(args[cur_arg + 1]);
2310 cur_arg += 2;
2311 }
2312 else if (!strcmp(args[cur_arg], "slowstart")) {
2313 /* slowstart is stored in seconds */
2314 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2315 if (err) {
2316 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2317 file, linenum, *err, newsrv->id);
2318 err_code |= ERR_ALERT | ERR_FATAL;
2319 goto out;
2320 }
2321 newsrv->slowstart = (val + 999) / 1000;
2322 cur_arg += 2;
2323 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002324 else if (!strcmp(args[cur_arg], "on-error")) {
2325 if (!strcmp(args[cur_arg + 1], "fastinter"))
2326 newsrv->onerror = HANA_ONERR_FASTINTER;
2327 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2328 newsrv->onerror = HANA_ONERR_FAILCHK;
2329 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2330 newsrv->onerror = HANA_ONERR_SUDDTH;
2331 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2332 newsrv->onerror = HANA_ONERR_MARKDWN;
2333 else {
2334 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2335 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2336 file, linenum, args[cur_arg], args[cur_arg + 1]);
2337 err_code |= ERR_ALERT | ERR_FATAL;
2338 goto out;
2339 }
2340
2341 cur_arg += 2;
2342 }
2343 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2344 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2345 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2346 else {
2347 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2348 file, linenum, args[cur_arg], args[cur_arg + 1]);
2349 err_code |= ERR_ALERT | ERR_FATAL;
2350 goto out;
2351 }
2352
2353 cur_arg += 2;
2354 }
2355 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2356 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2357 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2358 else {
2359 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2360 file, linenum, args[cur_arg], args[cur_arg + 1]);
2361 err_code |= ERR_ALERT | ERR_FATAL;
2362 goto out;
2363 }
2364
2365 cur_arg += 2;
2366 }
2367 else if (!strcmp(args[cur_arg], "error-limit")) {
2368 if (!*args[cur_arg + 1]) {
2369 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2370 file, linenum, args[cur_arg]);
2371 err_code |= ERR_ALERT | ERR_FATAL;
2372 goto out;
2373 }
2374
2375 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2376
2377 if (newsrv->consecutive_errors_limit <= 0) {
2378 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2379 file, linenum, args[cur_arg]);
2380 err_code |= ERR_ALERT | ERR_FATAL;
2381 goto out;
2382 }
2383 cur_arg += 2;
2384 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002385 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002386 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2387 file, linenum, "usesrc", "source");
2388 err_code |= ERR_ALERT | ERR_FATAL;
2389 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002390 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002391 else {
2392 static int srv_dumped;
2393 struct srv_kw *kw;
2394 char *err;
2395
2396 kw = srv_find_kw(args[cur_arg]);
2397 if (kw) {
2398 char *err = NULL;
2399 int code;
2400
2401 if (!kw->parse) {
2402 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2403 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002404 if (kw->skip != -1)
2405 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002406 err_code |= ERR_ALERT | ERR_FATAL;
2407 goto out;
2408 }
2409
2410 if (defsrv && !kw->default_ok) {
2411 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2412 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002413 if (kw->skip != -1)
2414 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002415 err_code |= ERR_ALERT;
2416 continue;
2417 }
2418
2419 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2420 err_code |= code;
2421
2422 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002423 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002424 if (code & ERR_FATAL) {
2425 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002426 if (kw->skip != -1)
2427 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002428 goto out;
2429 }
2430 }
2431 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002432 if (kw->skip != -1)
2433 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002434 continue;
2435 }
2436
2437 err = NULL;
2438 if (!srv_dumped) {
2439 srv_dump_kws(&err);
2440 indent_msg(&err, 4);
2441 srv_dumped = 1;
2442 }
2443
2444 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2445 file, linenum, args[0], args[1], args[cur_arg],
2446 err ? " Registered keywords :" : "", err ? err : "");
2447 free(err);
2448
2449 err_code |= ERR_ALERT | ERR_FATAL;
2450 goto out;
2451 }
2452 }
2453
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002454 if (!defsrv)
2455 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2456 if (err_code & ERR_FATAL)
2457 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002458 if (srv_tmpl)
2459 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002460 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002461 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002462 return 0;
2463
2464 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002465 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002466 free(errmsg);
2467 return err_code;
2468}
2469
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002470/* Returns a pointer to the first server matching either id <id>.
2471 * NULL is returned if no match is found.
2472 * the lookup is performed in the backend <bk>
2473 */
2474struct server *server_find_by_id(struct proxy *bk, int id)
2475{
2476 struct eb32_node *eb32;
2477 struct server *curserver;
2478
2479 if (!bk || (id ==0))
2480 return NULL;
2481
2482 /* <bk> has no backend capabilities, so it can't have a server */
2483 if (!(bk->cap & PR_CAP_BE))
2484 return NULL;
2485
2486 curserver = NULL;
2487
2488 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2489 if (eb32)
2490 curserver = container_of(eb32, struct server, conf.id);
2491
2492 return curserver;
2493}
2494
2495/* Returns a pointer to the first server matching either name <name>, or id
2496 * if <name> starts with a '#'. NULL is returned if no match is found.
2497 * the lookup is performed in the backend <bk>
2498 */
2499struct server *server_find_by_name(struct proxy *bk, const char *name)
2500{
2501 struct server *curserver;
2502
2503 if (!bk || !name)
2504 return NULL;
2505
2506 /* <bk> has no backend capabilities, so it can't have a server */
2507 if (!(bk->cap & PR_CAP_BE))
2508 return NULL;
2509
2510 curserver = NULL;
2511 if (*name == '#') {
2512 curserver = server_find_by_id(bk, atoi(name + 1));
2513 if (curserver)
2514 return curserver;
2515 }
2516 else {
2517 curserver = bk->srv;
2518
2519 while (curserver && (strcmp(curserver->id, name) != 0))
2520 curserver = curserver->next;
2521
2522 if (curserver)
2523 return curserver;
2524 }
2525
2526 return NULL;
2527}
2528
2529struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2530{
2531 struct server *byname;
2532 struct server *byid;
2533
2534 if (!name && !id)
2535 return NULL;
2536
2537 if (diff)
2538 *diff = 0;
2539
2540 byname = byid = NULL;
2541
2542 if (name) {
2543 byname = server_find_by_name(bk, name);
2544 if (byname && (!id || byname->puid == id))
2545 return byname;
2546 }
2547
2548 /* remaining possibilities :
2549 * - name not set
2550 * - name set but not found
2551 * - name found but ID doesn't match
2552 */
2553 if (id) {
2554 byid = server_find_by_id(bk, id);
2555 if (byid) {
2556 if (byname) {
2557 /* use id only if forced by configuration */
2558 if (byid->flags & SRV_F_FORCED_ID) {
2559 if (diff)
2560 *diff |= 2;
2561 return byid;
2562 }
2563 else {
2564 if (diff)
2565 *diff |= 1;
2566 return byname;
2567 }
2568 }
2569
2570 /* remaining possibilities:
2571 * - name not set
2572 * - name set but not found
2573 */
2574 if (name && diff)
2575 *diff |= 2;
2576 return byid;
2577 }
2578
2579 /* id bot found */
2580 if (byname) {
2581 if (diff)
2582 *diff |= 1;
2583 return byname;
2584 }
2585 }
2586
2587 return NULL;
2588}
2589
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002590/* Update a server state using the parameters available in the params list */
2591static void srv_update_state(struct server *srv, int version, char **params)
2592{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002593 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002594 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002595
2596 /* fields since version 1
2597 * and common to all other upcoming versions
2598 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002599 enum srv_state srv_op_state;
2600 enum srv_admin srv_admin_state;
2601 unsigned srv_uweight, srv_iweight;
2602 unsigned long srv_last_time_change;
2603 short srv_check_status;
2604 enum chk_result srv_check_result;
2605 int srv_check_health;
2606 int srv_check_state, srv_agent_state;
2607 int bk_f_forced_id;
2608 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002609 int fqdn_set_by_cli;
2610 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002611 const char *port_str;
2612 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002613
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002614 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002615 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002616 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002617 switch (version) {
2618 case 1:
2619 /*
2620 * now we can proceed with server's state update:
2621 * srv_addr: params[0]
2622 * srv_op_state: params[1]
2623 * srv_admin_state: params[2]
2624 * srv_uweight: params[3]
2625 * srv_iweight: params[4]
2626 * srv_last_time_change: params[5]
2627 * srv_check_status: params[6]
2628 * srv_check_result: params[7]
2629 * srv_check_health: params[8]
2630 * srv_check_state: params[9]
2631 * srv_agent_state: params[10]
2632 * bk_f_forced_id: params[11]
2633 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002634 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002635 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002636 */
2637
2638 /* validating srv_op_state */
2639 p = NULL;
2640 errno = 0;
2641 srv_op_state = strtol(params[1], &p, 10);
2642 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2643 (srv_op_state != SRV_ST_STOPPED &&
2644 srv_op_state != SRV_ST_STARTING &&
2645 srv_op_state != SRV_ST_RUNNING &&
2646 srv_op_state != SRV_ST_STOPPING)) {
2647 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2648 }
2649
2650 /* validating srv_admin_state */
2651 p = NULL;
2652 errno = 0;
2653 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002654 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002655
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002656 /* inherited statuses will be recomputed later.
2657 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2658 */
2659 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002660
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002661 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2662 (srv_admin_state != 0 &&
2663 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002664 srv_admin_state != SRV_ADMF_CMAINT &&
2665 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002666 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002667 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002668 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2669 }
2670
2671 /* validating srv_uweight */
2672 p = NULL;
2673 errno = 0;
2674 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002675 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002676 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2677
2678 /* validating srv_iweight */
2679 p = NULL;
2680 errno = 0;
2681 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002682 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002683 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2684
2685 /* validating srv_last_time_change */
2686 p = NULL;
2687 errno = 0;
2688 srv_last_time_change = strtol(params[5], &p, 10);
2689 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2690 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2691
2692 /* validating srv_check_status */
2693 p = NULL;
2694 errno = 0;
2695 srv_check_status = strtol(params[6], &p, 10);
2696 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2697 (srv_check_status >= HCHK_STATUS_SIZE))
2698 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2699
2700 /* validating srv_check_result */
2701 p = NULL;
2702 errno = 0;
2703 srv_check_result = strtol(params[7], &p, 10);
2704 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2705 (srv_check_result != CHK_RES_UNKNOWN &&
2706 srv_check_result != CHK_RES_NEUTRAL &&
2707 srv_check_result != CHK_RES_FAILED &&
2708 srv_check_result != CHK_RES_PASSED &&
2709 srv_check_result != CHK_RES_CONDPASS)) {
2710 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2711 }
2712
2713 /* validating srv_check_health */
2714 p = NULL;
2715 errno = 0;
2716 srv_check_health = strtol(params[8], &p, 10);
2717 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2718 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2719
2720 /* validating srv_check_state */
2721 p = NULL;
2722 errno = 0;
2723 srv_check_state = strtol(params[9], &p, 10);
2724 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2725 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2726 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2727
2728 /* validating srv_agent_state */
2729 p = NULL;
2730 errno = 0;
2731 srv_agent_state = strtol(params[10], &p, 10);
2732 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2733 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2734 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2735
2736 /* validating bk_f_forced_id */
2737 p = NULL;
2738 errno = 0;
2739 bk_f_forced_id = strtol(params[11], &p, 10);
2740 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2741 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2742
2743 /* validating srv_f_forced_id */
2744 p = NULL;
2745 errno = 0;
2746 srv_f_forced_id = strtol(params[12], &p, 10);
2747 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2748 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2749
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002750 /* validating srv_fqdn */
2751 fqdn = params[13];
2752 if (fqdn && *fqdn == '-')
2753 fqdn = NULL;
2754 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2755 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2756 fqdn = NULL;
2757 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002758
Frédéric Lécaille31694712017-08-01 08:47:19 +02002759 port_str = params[14];
2760 if (port_str) {
2761 port = strl2uic(port_str, strlen(port_str));
2762 if (port > USHRT_MAX) {
2763 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2764 port_str = NULL;
2765 }
2766 }
2767
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002768 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002769 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002770 goto out;
2771
2772 /* recover operational state and apply it to this server
2773 * and all servers tracking this one */
2774 switch (srv_op_state) {
2775 case SRV_ST_STOPPED:
2776 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002777 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002778 break;
2779 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002780 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002781 break;
2782 case SRV_ST_STOPPING:
2783 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002784 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002785 break;
2786 case SRV_ST_RUNNING:
2787 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002788 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002789 break;
2790 }
2791
2792 /* When applying server state, the following rules apply:
2793 * - in case of a configuration change, we apply the setting from the new
2794 * configuration, regardless of old running state
2795 * - if no configuration change, we apply old running state only if old running
2796 * state is different from new configuration state
2797 */
2798 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002799 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2800 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002801 srv_adm_set_maint(srv);
2802 else
2803 srv_adm_set_ready(srv);
2804 }
2805 /* configuration is the same, let's compate old running state and new conf state */
2806 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002807 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002808 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002809 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002810 srv_adm_set_ready(srv);
2811 }
2812 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002813 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002814 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002815 * (srv->iweight is the weight set up in configuration).
2816 * There are two possible reasons for FDRAIN to have been present :
2817 * - previous config weight was zero
2818 * - "set server b/s drain" was sent to the CLI
2819 *
2820 * In the first case, we simply want to drop this drain state
2821 * if the new weight is not zero anymore, meaning the administrator
2822 * has intentionally turned the weight back to a positive value to
2823 * enable the server again after an operation. In the second case,
2824 * the drain state was forced on the CLI regardless of the config's
2825 * weight so we don't want a change to the config weight to lose this
2826 * status. What this means is :
2827 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2828 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002829 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002830 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002831 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002832 }
2833
2834 srv->last_change = date.tv_sec - srv_last_time_change;
2835 srv->check.status = srv_check_status;
2836 srv->check.result = srv_check_result;
2837 srv->check.health = srv_check_health;
2838
2839 /* Only case we want to apply is removing ENABLED flag which could have been
2840 * done by the "disable health" command over the stats socket
2841 */
2842 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2843 (srv_check_state & CHK_ST_CONFIGURED) &&
2844 !(srv_check_state & CHK_ST_ENABLED))
2845 srv->check.state &= ~CHK_ST_ENABLED;
2846
2847 /* Only case we want to apply is removing ENABLED flag which could have been
2848 * done by the "disable agent" command over the stats socket
2849 */
2850 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2851 (srv_agent_state & CHK_ST_CONFIGURED) &&
2852 !(srv_agent_state & CHK_ST_ENABLED))
2853 srv->agent.state &= ~CHK_ST_ENABLED;
2854
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002855 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2856 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002857 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002858 * It means that a configuration file change has precedence over a unix socket change
2859 * for server's weight
2860 *
2861 * by default, HAProxy applies the following weight when parsing the configuration
2862 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002863 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002864 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002865 srv->uweight = srv_uweight;
2866 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002867 server_recalc_eweight(srv);
2868
Willy Tarreaue5a60682016-11-09 14:54:53 +01002869 /* load server IP address */
2870 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002871
2872 if (fqdn && srv->hostname) {
2873 if (!strcmp(srv->hostname, fqdn)) {
2874 /* Here we reset the 'set from stats socket FQDN' flag
2875 * to support such transitions:
2876 * Let's say initial FQDN value is foo1 (in configuration file).
2877 * - FQDN changed from stats socket, from foo1 to foo2 value,
2878 * - FQDN changed again from file configuration (with the same previous value
2879 set from stats socket, from foo1 to foo2 value),
2880 * - reload for any other reason than a FQDN modification,
2881 * the configuration file FQDN matches the fqdn server state file value.
2882 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
2883 * any futher FQDN modification.
2884 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002885 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002886 }
2887 else {
2888 /* If the FDQN has been changed from stats socket,
2889 * apply fqdn state file value (which is the value set
2890 * from stats socket).
2891 */
2892 if (fqdn_set_by_cli) {
2893 srv_set_fqdn(srv, fqdn);
Emeric Brun52a91d32017-08-31 14:41:55 +02002894 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002895 }
2896 }
2897 }
2898
Frédéric Lécaille31694712017-08-01 08:47:19 +02002899 if (port_str)
2900 srv->svc_port = port;
2901
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002902 break;
2903 default:
2904 chunk_appendf(msg, ", version '%d' not supported", version);
2905 }
2906
2907 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002908 if (msg->len) {
2909 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002910 Warning("server-state application failed for server '%s/%s'%s",
2911 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002912 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002913}
2914
2915/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2916 * For each proxy, it does the following:
2917 * - opens its server state file (either one or local one)
2918 * - read whole file, line by line
2919 * - analyse each line to check if it matches our current backend:
2920 * - backend name matches
2921 * - backend id matches if id is forced and name doesn't match
2922 * - if the server pointed by the line is found, then state is applied
2923 *
2924 * If the running backend uuid or id differs from the state file, then HAProxy reports
2925 * a warning.
2926 */
2927void apply_server_state(void)
2928{
2929 char *cur, *end;
2930 char mybuf[SRV_STATE_LINE_MAXLEN];
2931 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002932 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
2933 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002934 int arg, srv_arg, version, diff;
2935 FILE *f;
2936 char *filepath;
2937 char globalfilepath[MAXPATHLEN + 1];
2938 char localfilepath[MAXPATHLEN + 1];
2939 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2940 extern struct proxy *proxy;
2941 struct proxy *curproxy, *bk;
2942 struct server *srv;
2943
2944 globalfilepathlen = 0;
2945 /* create the globalfilepath variable */
2946 if (global.server_state_file) {
2947 /* absolute path or no base directory provided */
2948 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2949 len = strlen(global.server_state_file);
2950 if (len > MAXPATHLEN) {
2951 globalfilepathlen = 0;
2952 goto globalfileerror;
2953 }
2954 memcpy(globalfilepath, global.server_state_file, len);
2955 globalfilepath[len] = '\0';
2956 globalfilepathlen = len;
2957 }
2958 else if (global.server_state_base) {
2959 len = strlen(global.server_state_base);
2960 globalfilepathlen += len;
2961
2962 if (globalfilepathlen > MAXPATHLEN) {
2963 globalfilepathlen = 0;
2964 goto globalfileerror;
2965 }
2966 strncpy(globalfilepath, global.server_state_base, len);
2967 globalfilepath[globalfilepathlen] = 0;
2968
2969 /* append a slash if needed */
2970 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2971 if (globalfilepathlen + 1 > MAXPATHLEN) {
2972 globalfilepathlen = 0;
2973 goto globalfileerror;
2974 }
2975 globalfilepath[globalfilepathlen++] = '/';
2976 }
2977
2978 len = strlen(global.server_state_file);
2979 if (globalfilepathlen + len > MAXPATHLEN) {
2980 globalfilepathlen = 0;
2981 goto globalfileerror;
2982 }
2983 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2984 globalfilepathlen += len;
2985 globalfilepath[globalfilepathlen++] = 0;
2986 }
2987 }
2988 globalfileerror:
2989 if (globalfilepathlen == 0)
2990 globalfilepath[0] = '\0';
2991
2992 /* read servers state from local file */
2993 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2994 /* servers are only in backends */
2995 if (!(curproxy->cap & PR_CAP_BE))
2996 continue;
2997 fileopenerr = 0;
2998 filepath = NULL;
2999
3000 /* search server state file path and name */
3001 switch (curproxy->load_server_state_from_file) {
3002 /* read servers state from global file */
3003 case PR_SRV_STATE_FILE_GLOBAL:
3004 /* there was an error while generating global server state file path */
3005 if (globalfilepathlen == 0)
3006 continue;
3007 filepath = globalfilepath;
3008 fileopenerr = 1;
3009 break;
3010 /* this backend has its own file */
3011 case PR_SRV_STATE_FILE_LOCAL:
3012 localfilepathlen = 0;
3013 localfilepath[0] = '\0';
3014 len = 0;
3015 /* create the localfilepath variable */
3016 /* absolute path or no base directory provided */
3017 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3018 len = strlen(curproxy->server_state_file_name);
3019 if (len > MAXPATHLEN) {
3020 localfilepathlen = 0;
3021 goto localfileerror;
3022 }
3023 memcpy(localfilepath, curproxy->server_state_file_name, len);
3024 localfilepath[len] = '\0';
3025 localfilepathlen = len;
3026 }
3027 else if (global.server_state_base) {
3028 len = strlen(global.server_state_base);
3029 localfilepathlen += len;
3030
3031 if (localfilepathlen > MAXPATHLEN) {
3032 localfilepathlen = 0;
3033 goto localfileerror;
3034 }
3035 strncpy(localfilepath, global.server_state_base, len);
3036 localfilepath[localfilepathlen] = 0;
3037
3038 /* append a slash if needed */
3039 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3040 if (localfilepathlen + 1 > MAXPATHLEN) {
3041 localfilepathlen = 0;
3042 goto localfileerror;
3043 }
3044 localfilepath[localfilepathlen++] = '/';
3045 }
3046
3047 len = strlen(curproxy->server_state_file_name);
3048 if (localfilepathlen + len > MAXPATHLEN) {
3049 localfilepathlen = 0;
3050 goto localfileerror;
3051 }
3052 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3053 localfilepathlen += len;
3054 localfilepath[localfilepathlen++] = 0;
3055 }
3056 filepath = localfilepath;
3057 localfileerror:
3058 if (localfilepathlen == 0)
3059 localfilepath[0] = '\0';
3060
3061 break;
3062 case PR_SRV_STATE_FILE_NONE:
3063 default:
3064 continue;
3065 }
3066
3067 /* preload global state file */
3068 errno = 0;
3069 f = fopen(filepath, "r");
3070 if (errno && fileopenerr)
3071 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3072 if (!f)
3073 continue;
3074
3075 mybuf[0] = '\0';
3076 mybuflen = 0;
3077 version = 0;
3078
3079 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003080 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3081 Warning("Can't read first line of the server state file '%s'\n", filepath);
3082 goto fileclose;
3083 }
3084
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003085 cur = mybuf;
3086 version = atoi(cur);
3087 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3088 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003089 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003090
3091 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3092 int bk_f_forced_id = 0;
3093 int check_id = 0;
3094 int check_name = 0;
3095
3096 mybuflen = strlen(mybuf);
3097 cur = mybuf;
3098 end = cur + mybuflen;
3099
3100 bk = NULL;
3101 srv = NULL;
3102
3103 /* we need at least one character */
3104 if (mybuflen == 0)
3105 continue;
3106
3107 /* ignore blank characters at the beginning of the line */
3108 while (isspace(*cur))
3109 ++cur;
3110
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003111 /* Ignore empty or commented lines */
3112 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003113 continue;
3114
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003115 /* truncated lines */
3116 if (mybuf[mybuflen - 1] != '\n') {
3117 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003118 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003119 }
3120
3121 /* Removes trailing '\n' */
3122 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003123
3124 /* we're now ready to move the line into *srv_params[] */
3125 params[0] = cur;
3126 arg = 1;
3127 srv_arg = 0;
3128 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3129 if (isspace(*cur)) {
3130 *cur = '\0';
3131 ++cur;
3132 while (isspace(*cur))
3133 ++cur;
3134 switch (version) {
3135 case 1:
3136 /*
3137 * srv_addr: params[4] => srv_params[0]
3138 * srv_op_state: params[5] => srv_params[1]
3139 * srv_admin_state: params[6] => srv_params[2]
3140 * srv_uweight: params[7] => srv_params[3]
3141 * srv_iweight: params[8] => srv_params[4]
3142 * srv_last_time_change: params[9] => srv_params[5]
3143 * srv_check_status: params[10] => srv_params[6]
3144 * srv_check_result: params[11] => srv_params[7]
3145 * srv_check_health: params[12] => srv_params[8]
3146 * srv_check_state: params[13] => srv_params[9]
3147 * srv_agent_state: params[14] => srv_params[10]
3148 * bk_f_forced_id: params[15] => srv_params[11]
3149 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003150 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003151 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003152 */
3153 if (arg >= 4) {
3154 srv_params[srv_arg] = cur;
3155 ++srv_arg;
3156 }
3157 break;
3158 }
3159
3160 params[arg] = cur;
3161 ++arg;
3162 }
3163 else {
3164 ++cur;
3165 }
3166 }
3167
3168 /* if line is incomplete line, then ignore it.
3169 * otherwise, update useful flags */
3170 switch (version) {
3171 case 1:
3172 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3173 continue;
3174 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3175 check_id = (atoi(params[0]) == curproxy->uuid);
3176 check_name = (strcmp(curproxy->id, params[1]) == 0);
3177 break;
3178 }
3179
3180 diff = 0;
3181 bk = curproxy;
3182
3183 /* if backend can't be found, let's continue */
3184 if (!check_id && !check_name)
3185 continue;
3186 else if (!check_id && check_name) {
3187 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3188 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3189 }
3190 else if (check_id && !check_name) {
3191 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3192 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3193 /* if name doesn't match, we still want to update curproxy if the backend id
3194 * was forced in previous the previous configuration */
3195 if (!bk_f_forced_id)
3196 continue;
3197 }
3198
3199 /* look for the server by its id: param[2] */
3200 /* else look for the server by its name: param[3] */
3201 diff = 0;
3202 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3203
3204 if (!srv) {
3205 /* if no server found, then warning and continue with next line */
3206 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3207 params[3], params[2], params[0], params[1]);
3208 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3209 params[3], params[2], params[0], params[1]);
3210 continue;
3211 }
3212 else if (diff & PR_FBM_MISMATCH_ID) {
3213 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);
3214 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 +02003215 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003216 }
3217 else if (diff & PR_FBM_MISMATCH_NAME) {
3218 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);
3219 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 +02003220 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003221 }
3222
3223 /* now we can proceed with server's state update */
3224 srv_update_state(srv, version, srv_params);
3225 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003226fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003227 fclose(f);
3228 }
3229}
3230
Simon Horman7d09b9a2013-02-12 10:45:51 +09003231/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003232 * update a server's current IP address.
3233 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3234 * ip is in network format.
3235 * updater is a string which contains an information about the requester of the update.
3236 * updater is used if not NULL.
3237 *
3238 * A log line and a stderr warning message is generated based on server's backend options.
3239 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003240int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003241{
3242 /* generates a log line and a warning on stderr */
3243 if (1) {
3244 /* book enough space for both IPv4 and IPv6 */
3245 char oldip[INET6_ADDRSTRLEN];
3246 char newip[INET6_ADDRSTRLEN];
3247
3248 memset(oldip, '\0', INET6_ADDRSTRLEN);
3249 memset(newip, '\0', INET6_ADDRSTRLEN);
3250
3251 /* copy old IP address in a string */
3252 switch (s->addr.ss_family) {
3253 case AF_INET:
3254 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3255 break;
3256 case AF_INET6:
3257 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3258 break;
3259 };
3260
3261 /* copy new IP address in a string */
3262 switch (ip_sin_family) {
3263 case AF_INET:
3264 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3265 break;
3266 case AF_INET6:
3267 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3268 break;
3269 };
3270
3271 /* save log line into a buffer */
3272 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3273 s->proxy->id, s->id, oldip, newip, updater);
3274
3275 /* write the buffer on stderr */
3276 Warning("%s.\n", trash.str);
3277
3278 /* send a log */
3279 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3280 }
3281
3282 /* save the new IP family */
3283 s->addr.ss_family = ip_sin_family;
3284 /* save the new IP address */
3285 switch (ip_sin_family) {
3286 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003287 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003288 break;
3289 case AF_INET6:
3290 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3291 break;
3292 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003293 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003294
3295 return 0;
3296}
3297
3298/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003299 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3300 *
3301 * Caller can pass its name through <updater> to get it integrated in the response message
3302 * returned by the function.
3303 *
3304 * The function first does the following, in that order:
3305 * - validates the new addr and/or port
3306 * - checks if an update is required (new IP or port is different than current ones)
3307 * - checks the update is allowed:
3308 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3309 * - allow all changes if no CHECKS are configured
3310 * - if CHECK is configured:
3311 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3312 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3313 * conditions are met
3314 */
3315const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3316{
3317 struct sockaddr_storage sa;
3318 int ret, port_change_required;
3319 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003320 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003321 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003322 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003323
3324 msg = get_trash_chunk();
3325 chunk_reset(msg);
3326
3327 if (addr) {
3328 memset(&sa, 0, sizeof(struct sockaddr_storage));
3329 if (str2ip2(addr, &sa, 0) == NULL) {
3330 chunk_printf(msg, "Invalid addr '%s'", addr);
3331 goto out;
3332 }
3333
3334 /* changes are allowed on AF_INET* families only */
3335 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3336 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3337 goto out;
3338 }
3339
3340 /* collecting data currently setup */
3341 memset(current_addr, '\0', sizeof(current_addr));
3342 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3343 /* changes are allowed on AF_INET* families only */
3344 if ((ret != AF_INET) && (ret != AF_INET6)) {
3345 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3346 goto out;
3347 }
3348
3349 /* applying ADDR changes if required and allowed
3350 * ipcmp returns 0 when both ADDR are the same
3351 */
3352 if (ipcmp(&s->addr, &sa) == 0) {
3353 chunk_appendf(msg, "no need to change the addr");
3354 goto port;
3355 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003356 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003357 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003358
3359 /* we also need to update check's ADDR only if it uses the server's one */
3360 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003361 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003362 }
3363
3364 /* we also need to update agent ADDR only if it use the server's one */
3365 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003366 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003367 }
3368
3369 /* update report for caller */
3370 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3371 }
3372
3373 port:
3374 if (port) {
3375 char sign = '\0';
3376 char *endptr;
3377
3378 if (addr)
3379 chunk_appendf(msg, ", ");
3380
3381 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003382 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003383
3384 /* check if PORT change is required */
3385 port_change_required = 0;
3386
3387 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003388 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003389 new_port = strtol(port, &endptr, 10);
3390 if ((errno != 0) || (port == endptr)) {
3391 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3392 goto out;
3393 }
3394
3395 /* check if caller triggers a port mapped or offset */
3396 if (sign == '-' || (sign == '+')) {
3397 /* check if server currently uses port map */
3398 if (!(s->flags & SRV_F_MAPPORTS)) {
3399 /* switch from fixed port to port map mandatorily triggers
3400 * a port change */
3401 port_change_required = 1;
3402 /* check is configured
3403 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3404 * prevent PORT change if check doesn't have it's dedicated port while switching
3405 * to port mapping */
3406 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3407 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.");
3408 goto out;
3409 }
3410 }
3411 /* we're already using port maps */
3412 else {
3413 port_change_required = current_port != new_port;
3414 }
3415 }
3416 /* fixed port */
3417 else {
3418 port_change_required = current_port != new_port;
3419 }
3420
3421 /* applying PORT changes if required and update response message */
3422 if (port_change_required) {
3423 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003424 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003425 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003426
3427 /* prepare message */
3428 chunk_appendf(msg, "port changed from '");
3429 if (s->flags & SRV_F_MAPPORTS)
3430 chunk_appendf(msg, "+");
3431 chunk_appendf(msg, "%d' to '", current_port);
3432
3433 if (sign == '-') {
3434 s->flags |= SRV_F_MAPPORTS;
3435 chunk_appendf(msg, "%c", sign);
3436 /* just use for result output */
3437 new_port = -new_port;
3438 }
3439 else if (sign == '+') {
3440 s->flags |= SRV_F_MAPPORTS;
3441 chunk_appendf(msg, "%c", sign);
3442 }
3443 else {
3444 s->flags &= ~SRV_F_MAPPORTS;
3445 }
3446
3447 chunk_appendf(msg, "%d'", new_port);
3448
3449 /* we also need to update health checks port only if it uses server's realport */
3450 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3451 s->check.port = new_port;
3452 }
3453 }
3454 else {
3455 chunk_appendf(msg, "no need to change the port");
3456 }
3457 }
3458
3459out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003460 if (changed)
3461 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003462 if (updater)
3463 chunk_appendf(msg, " by '%s'", updater);
3464 chunk_appendf(msg, "\n");
3465 return msg->str;
3466}
3467
3468
3469/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003470 * update server status based on result of name resolution
3471 * returns:
3472 * 0 if server status is updated
3473 * 1 if server status has not changed
3474 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003475int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003476{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003477 struct dns_resolvers *resolvers = s->resolvers;
3478 struct dns_resolution *resolution = s->dns_requester->resolution;
3479 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003480
3481 switch (resolution->status) {
3482 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003483 /* status when HAProxy has just (re)started.
3484 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003485 break;
3486
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003487 case RSLV_STATUS_VALID:
3488 /*
3489 * resume health checks
3490 * server will be turned back on if health check is safe
3491 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003492 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003493 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003494 return 1;
3495 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3496 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003497 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003498 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003499
Emeric Brun52a91d32017-08-31 14:41:55 +02003500 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003501 return 1;
3502 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3503 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3504 s->proxy->id, s->id);
3505
3506 Warning("%s.\n", trash.str);
3507 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3508 return 0;
3509
3510 case RSLV_STATUS_NX:
3511 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003512 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3513 if (!tick_is_expired(exp, now_ms))
3514 break;
3515
3516 if (s->next_admin & SRV_ADMF_RMAINT)
3517 return 1;
3518 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3519 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003520
3521 case RSLV_STATUS_TIMEOUT:
3522 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003523 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3524 if (!tick_is_expired(exp, now_ms))
3525 break;
3526
3527 if (s->next_admin & SRV_ADMF_RMAINT)
3528 return 1;
3529 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3530 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003531
3532 case RSLV_STATUS_REFUSED:
3533 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003534 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3535 if (!tick_is_expired(exp, now_ms))
3536 break;
3537
3538 if (s->next_admin & SRV_ADMF_RMAINT)
3539 return 1;
3540 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3541 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003542
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003543 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003544 /* stop server if resolution failed for a long enough period */
3545 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3546 if (!tick_is_expired(exp, now_ms))
3547 break;
3548
3549 if (s->next_admin & SRV_ADMF_RMAINT)
3550 return 1;
3551 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3552 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003553 }
3554
3555 return 1;
3556}
3557
3558/*
3559 * Server Name Resolution valid response callback
3560 * It expects:
3561 * - <nameserver>: the name server which answered the valid response
3562 * - <response>: buffer containing a valid DNS response
3563 * - <response_len>: size of <response>
3564 * It performs the following actions:
3565 * - ignore response if current ip found and server family not met
3566 * - update with first new ip found if family is met and current IP is not found
3567 * returns:
3568 * 0 on error
3569 * 1 when no error or safe ignore
3570 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003571int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003572{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003573 struct server *s = NULL;
3574 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003575 void *serverip, *firstip;
3576 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003577 int ret;
3578 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003579 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003580
Christopher Faulet67957bd2017-09-27 11:00:59 +02003581 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003582 if (!s)
3583 return 1;
3584
Christopher Faulet67957bd2017-09-27 11:00:59 +02003585 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003586
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003587 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003588 firstip = NULL; /* pointer to the first valid response found */
3589 /* it will be used as the new IP if a change is required */
3590 firstip_sin_family = AF_UNSPEC;
3591 serverip = NULL; /* current server IP address */
3592
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003593 /* initializing server IP pointer */
3594 server_sin_family = s->addr.ss_family;
3595 switch (server_sin_family) {
3596 case AF_INET:
3597 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3598 break;
3599
3600 case AF_INET6:
3601 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3602 break;
3603
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003604 case AF_UNSPEC:
3605 break;
3606
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003607 default:
3608 goto invalid;
3609 }
3610
Baptiste Assmann729c9012017-05-22 15:13:10 +02003611 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003612 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003613 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003614
3615 switch (ret) {
3616 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003617 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003618
3619 case DNS_UPD_SRVIP_NOT_FOUND:
3620 goto save_ip;
3621
3622 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003623 goto invalid;
3624
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003625 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003626 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003627 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003628
Baptiste Assmannfad03182015-10-28 02:03:32 +01003629 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003630 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003631 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003632 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003633
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003634 default:
3635 goto invalid;
3636
3637 }
3638
3639 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003640 if (nameserver) {
3641 nameserver->counters.update++;
3642 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003643 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003644 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003645 else
3646 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003647 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3648
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003649 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003650 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003651 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003652
3653 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003654 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003655 nameserver->counters.invalid++;
3656 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003657 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003658 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003659 return 0;
3660}
3661
3662/*
3663 * Server Name Resolution error management callback
3664 * returns:
3665 * 0 on error
3666 * 1 when no error or safe ignore
3667 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003668int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003669{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003670 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003671
Christopher Faulet67957bd2017-09-27 11:00:59 +02003672 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003673 if (!s)
3674 return 1;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003675 snr_update_srv_status(s, 0);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003676 return 1;
3677}
3678
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003679/*
3680 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003681 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003682 * It returns a pointer to the first server found or NULL if <ip> is not yet
3683 * assigned.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003684 */
3685struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3686{
3687 struct server *tmpsrv;
3688 struct proxy *be;
3689
3690 if (!srv)
3691 return NULL;
3692
3693 be = srv->proxy;
3694 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
3695 /* We want to compare the IP in the record with the IP of the servers in the
3696 * same backend, only if:
3697 * * DNS resolution is enabled on the server
3698 * * the hostname used for the resolution by our server is the same than the
3699 * one used for the server found in the backend
3700 * * the server found in the backend is not our current server
3701 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003702 if ((tmpsrv->hostname_dn == NULL) ||
3703 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3704 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003705 (srv->puid == tmpsrv->puid))
3706 continue;
3707
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003708 /* If the server has been taken down, don't consider it */
Emeric Brun52a91d32017-08-31 14:41:55 +02003709 if (tmpsrv->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003710 continue;
3711
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003712 /* At this point, we have 2 different servers using the same DNS hostname
3713 * for their respective resolution.
3714 */
3715 if (*ip_family == tmpsrv->addr.ss_family &&
3716 ((tmpsrv->addr.ss_family == AF_INET &&
3717 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3718 (tmpsrv->addr.ss_family == AF_INET6 &&
3719 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
3720 return tmpsrv;
3721 }
3722 }
3723
3724 return NULL;
3725}
3726
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003727/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3728 * resolver. This is suited for initial address configuration. Returns 0 on
3729 * success otherwise a non-zero error code. In case of error, *err_code, if
3730 * not NULL, is filled up.
3731 */
3732int srv_set_addr_via_libc(struct server *srv, int *err_code)
3733{
3734 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003735 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003736 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003737 return 1;
3738 }
3739 return 0;
3740}
3741
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003742/* Set the server's FDQN (->hostname) from <hostname>.
3743 * Returns -1 if failed, 0 if not.
3744 */
3745int srv_set_fqdn(struct server *srv, const char *hostname)
3746{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003747 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003748 char *hostname_dn;
3749 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003750
3751 /* run time DNS resolution was not active for this server
3752 * and we can't enable it at run time for now.
3753 */
3754 if (!srv->dns_requester)
3755 return -1;
3756
3757 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003758 hostname_len = strlen(hostname);
3759 hostname_dn = trash.str;
3760 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3761 hostname_dn, trash.size);
3762 if (hostname_dn_len == -1)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003763 return -1;
3764
Christopher Faulet67957bd2017-09-27 11:00:59 +02003765 resolution = srv->dns_requester->resolution;
3766 if (resolution &&
3767 resolution->hostname_dn &&
3768 !strcmp(resolution->hostname_dn, hostname_dn))
3769 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003770
Christopher Faulet67957bd2017-09-27 11:00:59 +02003771 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003772
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003773 free(srv->hostname);
3774 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003775 srv->hostname = strdup(hostname);
3776 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003777 srv->hostname_dn_len = hostname_dn_len;
3778 if (!srv->hostname || !srv->hostname_dn)
3779 return -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003780
Christopher Faulet67957bd2017-09-27 11:00:59 +02003781 if (dns_link_resolution(srv, OBJ_TYPE_SERVER) == -1)
3782 return -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003783 return 0;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003784}
3785
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003786/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3787 * from the state file. This is suited for initial address configuration.
3788 * Returns 0 on success otherwise a non-zero error code. In case of error,
3789 * *err_code, if not NULL, is filled up.
3790 */
3791static int srv_apply_lastaddr(struct server *srv, int *err_code)
3792{
3793 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3794 if (err_code)
3795 *err_code |= ERR_WARN;
3796 return 1;
3797 }
3798 return 0;
3799}
3800
Willy Tarreau25e51522016-11-04 15:10:17 +01003801/* returns 0 if no error, otherwise a combination of ERR_* flags */
3802static int srv_iterate_initaddr(struct server *srv)
3803{
3804 int return_code = 0;
3805 int err_code;
3806 unsigned int methods;
3807
3808 methods = srv->init_addr_methods;
3809 if (!methods) { // default to "last,libc"
3810 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3811 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3812 }
3813
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003814 /* "-dr" : always append "none" so that server addresses resolution
3815 * failures are silently ignored, this is convenient to validate some
3816 * configs out of their environment.
3817 */
3818 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3819 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3820
Willy Tarreau25e51522016-11-04 15:10:17 +01003821 while (methods) {
3822 err_code = 0;
3823 switch (srv_get_next_initaddr(&methods)) {
3824 case SRV_IADDR_LAST:
3825 if (!srv->lastaddr)
3826 continue;
3827 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003828 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003829 return_code |= err_code;
3830 break;
3831
3832 case SRV_IADDR_LIBC:
3833 if (!srv->hostname)
3834 continue;
3835 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003836 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003837 return_code |= err_code;
3838 break;
3839
Willy Tarreau37ebe122016-11-04 15:17:58 +01003840 case SRV_IADDR_NONE:
3841 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003842 if (return_code) {
3843 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3844 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3845 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003846 return return_code;
3847
Willy Tarreau4310d362016-11-02 15:05:56 +01003848 case SRV_IADDR_IP:
3849 ipcpy(&srv->init_addr, &srv->addr);
3850 if (return_code) {
3851 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3852 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3853 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003854 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003855
Willy Tarreau25e51522016-11-04 15:10:17 +01003856 default: /* unhandled method */
3857 break;
3858 }
3859 }
3860
3861 if (!return_code) {
3862 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3863 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3864 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003865 else {
3866 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3867 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3868 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003869
3870 return_code |= ERR_ALERT | ERR_FATAL;
3871 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003872out:
3873 srv_set_dyncookie(srv);
3874 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003875}
3876
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003877/*
3878 * This function parses all backends and all servers within each backend
3879 * and performs servers' addr resolution based on information provided by:
3880 * - configuration file
3881 * - server-state file (states provided by an 'old' haproxy process)
3882 *
3883 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3884 */
3885int srv_init_addr(void)
3886{
3887 struct proxy *curproxy;
3888 int return_code = 0;
3889
3890 curproxy = proxy;
3891 while (curproxy) {
3892 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003893
3894 /* servers are in backend only */
3895 if (!(curproxy->cap & PR_CAP_BE))
3896 goto srv_init_addr_next;
3897
Willy Tarreau25e51522016-11-04 15:10:17 +01003898 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003899 if (srv->hostname)
3900 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003901
3902 srv_init_addr_next:
3903 curproxy = curproxy->next;
3904 }
3905
3906 return return_code;
3907}
3908
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003909const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater)
3910{
3911
3912 struct chunk *msg;
3913
3914 msg = get_trash_chunk();
3915 chunk_reset(msg);
3916
Olivier Houchard8da5f982017-08-04 18:35:36 +02003917 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003918 chunk_appendf(msg, "no need to change the FDQN");
3919 goto out;
3920 }
3921
3922 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
3923 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
3924 goto out;
3925 }
3926
3927 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
3928 server->proxy->id, server->id, server->hostname, fqdn);
3929
3930 if (srv_set_fqdn(server, fqdn) < 0) {
3931 chunk_reset(msg);
3932 chunk_appendf(msg, "could not update %s/%s FQDN",
3933 server->proxy->id, server->id);
3934 goto out;
3935 }
3936
3937 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02003938 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003939
3940 out:
3941 if (updater)
3942 chunk_appendf(msg, " by '%s'", updater);
3943 chunk_appendf(msg, "\n");
3944
3945 return msg->str;
3946}
3947
3948
Willy Tarreau21b069d2016-11-23 17:15:08 +01003949/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3950 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003951 * 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 +01003952 * used for CLI commands requiring a server name.
3953 * Important: the <arg> is modified to remove the '/'.
3954 */
3955struct server *cli_find_server(struct appctx *appctx, char *arg)
3956{
3957 struct proxy *px;
3958 struct server *sv;
3959 char *line;
3960
3961 /* split "backend/server" and make <line> point to server */
3962 for (line = arg; *line; line++)
3963 if (*line == '/') {
3964 *line++ = '\0';
3965 break;
3966 }
3967
3968 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003969 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003970 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003971 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003972 return NULL;
3973 }
3974
3975 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003976 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003977 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003978 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003979 return NULL;
3980 }
3981
3982 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003983 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003984 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003985 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003986 return NULL;
3987 }
3988
3989 return sv;
3990}
3991
William Lallemand222baf22016-11-19 02:00:33 +01003992
3993static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
3994{
3995 struct server *sv;
3996 const char *warning;
3997
3998 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3999 return 1;
4000
4001 sv = cli_find_server(appctx, args[2]);
4002 if (!sv)
4003 return 1;
4004
4005 if (strcmp(args[3], "weight") == 0) {
4006 warning = server_parse_weight_change_request(sv, args[4]);
4007 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004008 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004009 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004010 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004011 }
4012 }
4013 else if (strcmp(args[3], "state") == 0) {
4014 if (strcmp(args[4], "ready") == 0)
4015 srv_adm_set_ready(sv);
4016 else if (strcmp(args[4], "drain") == 0)
4017 srv_adm_set_drain(sv);
4018 else if (strcmp(args[4], "maint") == 0)
4019 srv_adm_set_maint(sv);
4020 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004021 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004022 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004023 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004024 }
4025 }
4026 else if (strcmp(args[3], "health") == 0) {
4027 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004028 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004029 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004030 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004031 }
4032 else if (strcmp(args[4], "up") == 0) {
4033 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004034 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004035 }
4036 else if (strcmp(args[4], "stopping") == 0) {
4037 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004038 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004039 }
4040 else if (strcmp(args[4], "down") == 0) {
4041 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004042 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004043 }
4044 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004045 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004046 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004047 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004048 }
4049 }
4050 else if (strcmp(args[3], "agent") == 0) {
4051 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004052 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004053 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004054 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004055 }
4056 else if (strcmp(args[4], "up") == 0) {
4057 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004058 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004059 }
4060 else if (strcmp(args[4], "down") == 0) {
4061 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004062 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004063 }
4064 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004065 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004066 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004067 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004068 }
4069 }
Misiek2da082d2017-01-09 09:40:42 +01004070 else if (strcmp(args[3], "agent-addr") == 0) {
4071 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004072 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004073 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4074 appctx->st0 = CLI_ST_PRINT;
4075 } else {
4076 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004077 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004078 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4079 appctx->st0 = CLI_ST_PRINT;
4080 }
4081 }
4082 }
4083 else if (strcmp(args[3], "agent-send") == 0) {
4084 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004085 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004086 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4087 appctx->st0 = CLI_ST_PRINT;
4088 } else {
4089 char *nss = strdup(args[4]);
4090 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004091 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004092 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4093 appctx->st0 = CLI_ST_PRINT;
4094 } else {
4095 free(sv->agent.send_string);
4096 sv->agent.send_string = nss;
4097 sv->agent.send_string_len = strlen(args[4]);
4098 }
4099 }
4100 }
William Lallemand222baf22016-11-19 02:00:33 +01004101 else if (strcmp(args[3], "check-port") == 0) {
4102 int i = 0;
4103 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004104 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004105 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004106 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004107 }
4108 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004109 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004110 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004111 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004112 }
4113 /* prevent the update of port to 0 if MAPPORTS are in use */
4114 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004115 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004116 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004117 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004118 return 1;
4119 }
4120 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004121 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004122 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004123 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004124 }
4125 else if (strcmp(args[3], "addr") == 0) {
4126 char *addr = NULL;
4127 char *port = NULL;
4128 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004129 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004130 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004131 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004132 return 1;
4133 }
4134 else {
4135 addr = args[4];
4136 }
4137 if (strcmp(args[5], "port") == 0) {
4138 port = args[6];
4139 }
4140 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4141 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004142 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004143 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004144 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004145 }
4146 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4147 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004148 else if (strcmp(args[3], "fqdn") == 0) {
4149 if (!*args[4]) {
4150 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4151 appctx->st0 = CLI_ST_PRINT;
4152 return 1;
4153 }
4154 warning = update_server_fqdn(sv, args[4], "stats socket command");
4155 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004156 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004157 appctx->ctx.cli.msg = warning;
4158 appctx->st0 = CLI_ST_PRINT;
4159 }
4160 }
William Lallemand222baf22016-11-19 02:00:33 +01004161 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004162 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004163 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 +01004164 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004165 }
4166 return 1;
4167}
4168
William Lallemand6b160942016-11-22 12:34:35 +01004169static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4170{
4171 struct stream_interface *si = appctx->owner;
4172 struct proxy *px;
4173 struct server *sv;
4174 char *line;
4175
4176
4177 /* split "backend/server" and make <line> point to server */
4178 for (line = args[2]; *line; line++)
4179 if (*line == '/') {
4180 *line++ = '\0';
4181 break;
4182 }
4183
4184 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004185 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004186 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004187 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004188 return 1;
4189 }
4190
4191 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004192 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004193 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004194 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004195 return 1;
4196 }
4197
4198 /* return server's effective weight at the moment */
4199 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004200 if (ci_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004201 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004202 return 0;
4203 }
William Lallemand6b160942016-11-22 12:34:35 +01004204 return 1;
4205}
4206
4207static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4208{
4209 struct server *sv;
4210 const char *warning;
4211
4212 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4213 return 1;
4214
4215 sv = cli_find_server(appctx, args[2]);
4216 if (!sv)
4217 return 1;
4218
4219 warning = server_parse_weight_change_request(sv, args[3]);
4220 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004221 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004222 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004223 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004224 }
4225 return 1;
4226}
4227
Willy Tarreaub8026272016-11-23 11:26:56 +01004228/* parse a "set maxconn server" command. It always returns 1. */
4229static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4230{
4231 struct server *sv;
4232 const char *warning;
4233
4234 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4235 return 1;
4236
4237 sv = cli_find_server(appctx, args[3]);
4238 if (!sv)
4239 return 1;
4240
4241 warning = server_parse_maxconn_change_request(sv, args[4]);
4242 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004243 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004244 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004245 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004246 }
4247 return 1;
4248}
William Lallemand6b160942016-11-22 12:34:35 +01004249
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004250/* parse a "disable agent" command. It always returns 1. */
4251static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4252{
4253 struct server *sv;
4254
4255 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4256 return 1;
4257
4258 sv = cli_find_server(appctx, args[2]);
4259 if (!sv)
4260 return 1;
4261
4262 sv->agent.state &= ~CHK_ST_ENABLED;
4263 return 1;
4264}
4265
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004266/* parse a "disable health" command. It always returns 1. */
4267static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4268{
4269 struct server *sv;
4270
4271 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4272 return 1;
4273
4274 sv = cli_find_server(appctx, args[2]);
4275 if (!sv)
4276 return 1;
4277
4278 sv->check.state &= ~CHK_ST_ENABLED;
4279 return 1;
4280}
4281
Willy Tarreauffb4d582016-11-24 12:47:00 +01004282/* parse a "disable server" command. It always returns 1. */
4283static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4284{
4285 struct server *sv;
4286
4287 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4288 return 1;
4289
4290 sv = cli_find_server(appctx, args[2]);
4291 if (!sv)
4292 return 1;
4293
4294 srv_adm_set_maint(sv);
4295 return 1;
4296}
4297
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004298/* parse a "enable agent" command. It always returns 1. */
4299static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4300{
4301 struct server *sv;
4302
4303 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4304 return 1;
4305
4306 sv = cli_find_server(appctx, args[2]);
4307 if (!sv)
4308 return 1;
4309
4310 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004311 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004312 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004313 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004314 return 1;
4315 }
4316
4317 sv->agent.state |= CHK_ST_ENABLED;
4318 return 1;
4319}
4320
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004321/* parse a "enable health" command. It always returns 1. */
4322static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4323{
4324 struct server *sv;
4325
4326 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4327 return 1;
4328
4329 sv = cli_find_server(appctx, args[2]);
4330 if (!sv)
4331 return 1;
4332
4333 sv->check.state |= CHK_ST_ENABLED;
4334 return 1;
4335}
4336
Willy Tarreauffb4d582016-11-24 12:47:00 +01004337/* parse a "enable server" command. It always returns 1. */
4338static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4339{
4340 struct server *sv;
4341
4342 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4343 return 1;
4344
4345 sv = cli_find_server(appctx, args[2]);
4346 if (!sv)
4347 return 1;
4348
4349 srv_adm_set_ready(sv);
4350 return 1;
4351}
4352
William Lallemand222baf22016-11-19 02:00:33 +01004353/* register cli keywords */
4354static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004355 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004356 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004357 { { "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 +01004358 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004359 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004360 { { "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 +01004361 { { "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 +01004362 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004363 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4364 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4365
William Lallemand222baf22016-11-19 02:00:33 +01004366 {{},}
4367}};
4368
4369__attribute__((constructor))
4370static void __server_init(void)
4371{
4372 cli_register_kw(&cli_kws);
4373}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004374
Emeric Brun64cc49c2017-10-03 14:46:45 +02004375/*
4376 * This function applies server's status changes, it is
4377 * is designed to be called asynchronously.
4378 *
4379 */
4380void srv_update_status(struct server *s)
4381{
4382 struct check *check = &s->check;
4383 int xferred;
4384 struct proxy *px = s->proxy;
4385 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4386 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4387 int log_level;
4388 struct chunk *tmptrash = NULL;
4389
4390
4391 /* If currently main is not set we try to apply pending state changes */
4392 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4393 int next_admin;
4394
4395 /* Backup next admin */
4396 next_admin = s->next_admin;
4397
4398 /* restore current admin state */
4399 s->next_admin = s->cur_admin;
4400
4401 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4402 s->last_change = now.tv_sec;
4403 if (s->proxy->lbprm.set_server_status_down)
4404 s->proxy->lbprm.set_server_status_down(s);
4405
4406 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4407 srv_shutdown_streams(s, SF_ERR_DOWN);
4408
4409 /* we might have streams queued on this server and waiting for
4410 * a connection. Those which are redispatchable will be queued
4411 * to another server or to the proxy itself.
4412 */
4413 xferred = pendconn_redistribute(s);
4414
4415 tmptrash = alloc_trash_chunk();
4416 if (tmptrash) {
4417 chunk_printf(tmptrash,
4418 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4419 s->proxy->id, s->id);
4420
Emeric Brun5a133512017-10-19 14:42:30 +02004421 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004422 Warning("%s.\n", tmptrash->str);
4423
4424 /* we don't send an alert if the server was previously paused */
4425 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
4426 send_log(s->proxy, log_level, "%s.\n", tmptrash->str);
4427 send_email_alert(s, log_level, "%s", tmptrash->str);
4428 free_trash_chunk(tmptrash);
4429 tmptrash = NULL;
4430 }
4431 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4432 set_backend_down(s->proxy);
4433
4434 s->counters.down_trans++;
4435 }
4436 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4437 s->last_change = now.tv_sec;
4438 if (s->proxy->lbprm.set_server_status_down)
4439 s->proxy->lbprm.set_server_status_down(s);
4440
4441 /* we might have streams queued on this server and waiting for
4442 * a connection. Those which are redispatchable will be queued
4443 * to another server or to the proxy itself.
4444 */
4445 xferred = pendconn_redistribute(s);
4446
4447 tmptrash = alloc_trash_chunk();
4448 if (tmptrash) {
4449 chunk_printf(tmptrash,
4450 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4451 s->proxy->id, s->id);
4452
Emeric Brun5a133512017-10-19 14:42:30 +02004453 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004454
4455 Warning("%s.\n", tmptrash->str);
4456 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4457 free_trash_chunk(tmptrash);
4458 tmptrash = NULL;
4459 }
4460
4461 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4462 set_backend_down(s->proxy);
4463 }
4464 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4465 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4466 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4467 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4468 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4469 s->proxy->last_change = now.tv_sec;
4470 }
4471
4472 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4473 s->down_time += now.tv_sec - s->last_change;
4474
4475 s->last_change = now.tv_sec;
4476 if (s->next_state == SRV_ST_STARTING)
4477 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4478
4479 server_recalc_eweight(s);
4480 /* now propagate the status change to any LB algorithms */
4481 if (px->lbprm.update_server_eweight)
4482 px->lbprm.update_server_eweight(s);
4483 else if (srv_willbe_usable(s)) {
4484 if (px->lbprm.set_server_status_up)
4485 px->lbprm.set_server_status_up(s);
4486 }
4487 else {
4488 if (px->lbprm.set_server_status_down)
4489 px->lbprm.set_server_status_down(s);
4490 }
4491
4492 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4493 * and it's not a backup server and its effective weight is > 0,
4494 * then it can accept new connections, so we shut down all streams
4495 * on all backup servers.
4496 */
4497 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4498 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4499 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4500
4501 /* check if we can handle some connections queued at the proxy. We
4502 * will take as many as we can handle.
4503 */
4504 xferred = pendconn_grab_from_px(s);
4505
4506 tmptrash = alloc_trash_chunk();
4507 if (tmptrash) {
4508 chunk_printf(tmptrash,
4509 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4510 s->proxy->id, s->id);
4511
Emeric Brun5a133512017-10-19 14:42:30 +02004512 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004513 Warning("%s.\n", tmptrash->str);
4514 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4515 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4516 free_trash_chunk(tmptrash);
4517 tmptrash = NULL;
4518 }
4519
4520 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4521 set_backend_down(s->proxy);
4522 }
4523 else if (s->cur_eweight != s->next_eweight) {
4524 /* now propagate the status change to any LB algorithms */
4525 if (px->lbprm.update_server_eweight)
4526 px->lbprm.update_server_eweight(s);
4527 else if (srv_willbe_usable(s)) {
4528 if (px->lbprm.set_server_status_up)
4529 px->lbprm.set_server_status_up(s);
4530 }
4531 else {
4532 if (px->lbprm.set_server_status_down)
4533 px->lbprm.set_server_status_down(s);
4534 }
4535
4536 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4537 set_backend_down(s->proxy);
4538 }
4539
4540 s->next_admin = next_admin;
4541 }
4542
Emeric Brun5a133512017-10-19 14:42:30 +02004543 /* reset operational state change */
4544 *s->op_st_chg.reason = 0;
4545 s->op_st_chg.status = s->op_st_chg.code = -1;
4546 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004547
4548 /* Now we try to apply pending admin changes */
4549
4550 /* Maintenance must also disable health checks */
4551 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4552 if (s->check.state & CHK_ST_ENABLED) {
4553 s->check.state |= CHK_ST_PAUSED;
4554 check->health = 0;
4555 }
4556
4557 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004558 tmptrash = alloc_trash_chunk();
4559 if (tmptrash) {
4560 chunk_printf(tmptrash,
4561 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4562 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4563 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004564
Olivier Houchard796a2b32017-10-24 17:42:47 +02004565 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004566
Olivier Houchard796a2b32017-10-24 17:42:47 +02004567 if (!(global.mode & MODE_STARTING)) {
4568 Warning("%s.\n", tmptrash->str);
4569 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4570 }
4571 free_trash_chunk(tmptrash);
4572 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004573 }
4574 }
4575 else { /* server was still running */
4576 check->health = 0; /* failure */
4577 s->last_change = now.tv_sec;
4578 if (s->proxy->lbprm.set_server_status_down)
4579 s->proxy->lbprm.set_server_status_down(s);
4580
4581 s->next_state = SRV_ST_STOPPED;
4582 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4583 srv_shutdown_streams(s, SF_ERR_DOWN);
4584
4585 /* we might have streams queued on this server and waiting for
4586 * a connection. Those which are redispatchable will be queued
4587 * to another server or to the proxy itself.
4588 */
4589 xferred = pendconn_redistribute(s);
4590
4591 tmptrash = alloc_trash_chunk();
4592 if (tmptrash) {
4593 chunk_printf(tmptrash,
4594 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4595 s->flags & SRV_F_BACKUP ? "Backup " : "",
4596 s->proxy->id, s->id,
4597 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4598
4599 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4600
4601 if (!(global.mode & MODE_STARTING)) {
4602 Warning("%s.\n", tmptrash->str);
4603 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", tmptrash->str);
4604 }
4605 free_trash_chunk(tmptrash);
4606 tmptrash = NULL;
4607 }
4608 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4609 set_backend_down(s->proxy);
4610
4611 s->counters.down_trans++;
4612 }
4613 }
4614 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4615 /* OK here we're leaving maintenance, we have many things to check,
4616 * because the server might possibly be coming back up depending on
4617 * its state. In practice, leaving maintenance means that we should
4618 * immediately turn to UP (more or less the slowstart) under the
4619 * following conditions :
4620 * - server is neither checked nor tracked
4621 * - server tracks another server which is not checked
4622 * - server tracks another server which is already up
4623 * Which sums up as something simpler :
4624 * "either the tracking server is up or the server's checks are disabled
4625 * or up". Otherwise we only re-enable health checks. There's a special
4626 * case associated to the stopping state which can be inherited. Note
4627 * that the server might still be in drain mode, which is naturally dealt
4628 * with by the lower level functions.
4629 */
4630
4631 if (s->check.state & CHK_ST_ENABLED) {
4632 s->check.state &= ~CHK_ST_PAUSED;
4633 check->health = check->rise; /* start OK but check immediately */
4634 }
4635
4636 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4637 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4638 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4639 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4640 s->next_state = SRV_ST_STOPPING;
4641 }
4642 else {
4643 s->next_state = SRV_ST_STARTING;
4644 if (s->slowstart > 0)
4645 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4646 else
4647 s->next_state = SRV_ST_RUNNING;
4648 }
4649
4650 }
4651
4652 tmptrash = alloc_trash_chunk();
4653 if (tmptrash) {
4654 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4655 chunk_printf(tmptrash,
4656 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4657 s->flags & SRV_F_BACKUP ? "Backup " : "",
4658 s->proxy->id, s->id,
4659 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4660 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4661 }
4662 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4663 chunk_printf(tmptrash,
4664 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4665 s->flags & SRV_F_BACKUP ? "Backup " : "",
4666 s->proxy->id, s->id, s->hostname,
4667 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4668 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4669 }
4670 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4671 chunk_printf(tmptrash,
4672 "%sServer %s/%s is %s/%s (leaving maintenance)",
4673 s->flags & SRV_F_BACKUP ? "Backup " : "",
4674 s->proxy->id, s->id,
4675 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4676 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4677 }
4678 Warning("%s.\n", tmptrash->str);
4679 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4680 free_trash_chunk(tmptrash);
4681 tmptrash = NULL;
4682 }
4683
4684 server_recalc_eweight(s);
4685 /* now propagate the status change to any LB algorithms */
4686 if (px->lbprm.update_server_eweight)
4687 px->lbprm.update_server_eweight(s);
4688 else if (srv_willbe_usable(s)) {
4689 if (px->lbprm.set_server_status_up)
4690 px->lbprm.set_server_status_up(s);
4691 }
4692 else {
4693 if (px->lbprm.set_server_status_down)
4694 px->lbprm.set_server_status_down(s);
4695 }
4696
4697 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4698 set_backend_down(s->proxy);
4699
4700 }
4701 else if (s->next_admin & SRV_ADMF_MAINT) {
4702 /* remaining in maintenance mode, let's inform precisely about the
4703 * situation.
4704 */
4705 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4706 tmptrash = alloc_trash_chunk();
4707 if (tmptrash) {
4708 chunk_printf(tmptrash,
4709 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4710 s->flags & SRV_F_BACKUP ? "Backup " : "",
4711 s->proxy->id, s->id);
4712
4713 if (s->track) /* normally it's mandatory here */
4714 chunk_appendf(tmptrash, " via %s/%s",
4715 s->track->proxy->id, s->track->id);
4716 Warning("%s.\n", tmptrash->str);
4717 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4718 free_trash_chunk(tmptrash);
4719 tmptrash = NULL;
4720 }
4721 }
4722 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4723 tmptrash = alloc_trash_chunk();
4724 if (tmptrash) {
4725 chunk_printf(tmptrash,
4726 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4727 s->flags & SRV_F_BACKUP ? "Backup " : "",
4728 s->proxy->id, s->id, s->hostname);
4729
4730 if (s->track) /* normally it's mandatory here */
4731 chunk_appendf(tmptrash, " via %s/%s",
4732 s->track->proxy->id, s->track->id);
4733 Warning("%s.\n", tmptrash->str);
4734 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4735 free_trash_chunk(tmptrash);
4736 tmptrash = NULL;
4737 }
4738 }
4739 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4740 tmptrash = alloc_trash_chunk();
4741 if (tmptrash) {
4742 chunk_printf(tmptrash,
4743 "%sServer %s/%s remains in forced maintenance",
4744 s->flags & SRV_F_BACKUP ? "Backup " : "",
4745 s->proxy->id, s->id);
4746 Warning("%s.\n", tmptrash->str);
4747 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4748 free_trash_chunk(tmptrash);
4749 tmptrash = NULL;
4750 }
4751 }
4752 /* don't report anything when leaving drain mode and remaining in maintenance */
4753
4754 s->cur_admin = s->next_admin;
4755 }
4756
4757 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4758 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4759 /* drain state is applied only if not yet in maint */
4760
4761 s->last_change = now.tv_sec;
4762 if (px->lbprm.set_server_status_down)
4763 px->lbprm.set_server_status_down(s);
4764
4765 /* we might have streams queued on this server and waiting for
4766 * a connection. Those which are redispatchable will be queued
4767 * to another server or to the proxy itself.
4768 */
4769 xferred = pendconn_redistribute(s);
4770
4771 tmptrash = alloc_trash_chunk();
4772 if (tmptrash) {
4773 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4774 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4775 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4776
4777 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4778
4779 if (!(global.mode & MODE_STARTING)) {
4780 Warning("%s.\n", tmptrash->str);
4781 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4782 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4783 }
4784 free_trash_chunk(tmptrash);
4785 tmptrash = NULL;
4786 }
4787
4788 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4789 set_backend_down(s->proxy);
4790 }
4791 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4792 /* OK completely leaving drain mode */
4793 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4794 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4795 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4796 s->proxy->last_change = now.tv_sec;
4797 }
4798
4799 if (s->last_change < now.tv_sec) // ignore negative times
4800 s->down_time += now.tv_sec - s->last_change;
4801 s->last_change = now.tv_sec;
4802 server_recalc_eweight(s);
4803
4804 tmptrash = alloc_trash_chunk();
4805 if (tmptrash) {
4806 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4807 chunk_printf(tmptrash,
4808 "%sServer %s/%s is %s (leaving forced drain)",
4809 s->flags & SRV_F_BACKUP ? "Backup " : "",
4810 s->proxy->id, s->id,
4811 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4812 }
4813 else {
4814 chunk_printf(tmptrash,
4815 "%sServer %s/%s is %s (leaving drain)",
4816 s->flags & SRV_F_BACKUP ? "Backup " : "",
4817 s->proxy->id, s->id,
4818 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4819 if (s->track) /* normally it's mandatory here */
4820 chunk_appendf(tmptrash, " via %s/%s",
4821 s->track->proxy->id, s->track->id);
4822 }
4823
4824 Warning("%s.\n", tmptrash->str);
4825 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4826 free_trash_chunk(tmptrash);
4827 tmptrash = NULL;
4828 }
4829
4830 /* now propagate the status change to any LB algorithms */
4831 if (px->lbprm.update_server_eweight)
4832 px->lbprm.update_server_eweight(s);
4833 else if (srv_willbe_usable(s)) {
4834 if (px->lbprm.set_server_status_up)
4835 px->lbprm.set_server_status_up(s);
4836 }
4837 else {
4838 if (px->lbprm.set_server_status_down)
4839 px->lbprm.set_server_status_down(s);
4840 }
4841 }
4842 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
4843 /* remaining in drain mode after removing one of its flags */
4844
4845 tmptrash = alloc_trash_chunk();
4846 if (tmptrash) {
4847 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4848 chunk_printf(tmptrash,
4849 "%sServer %s/%s is leaving forced drain but remains in drain mode",
4850 s->flags & SRV_F_BACKUP ? "Backup " : "",
4851 s->proxy->id, s->id);
4852
4853 if (s->track) /* normally it's mandatory here */
4854 chunk_appendf(tmptrash, " via %s/%s",
4855 s->track->proxy->id, s->track->id);
4856 }
4857 else {
4858 chunk_printf(tmptrash,
4859 "%sServer %s/%s remains in forced drain mode",
4860 s->flags & SRV_F_BACKUP ? "Backup " : "",
4861 s->proxy->id, s->id);
4862 }
4863 Warning("%s.\n", tmptrash->str);
4864 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4865 free_trash_chunk(tmptrash);
4866 tmptrash = NULL;
4867 }
4868
4869 /* commit new admin status */
4870
4871 s->cur_admin = s->next_admin;
4872 }
4873 }
4874
4875 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02004876 *s->adm_st_chg_cause = 0;
4877}
4878/*
4879 * This function loops on servers registered for asynchronous
4880 * status changes
4881 */
4882void servers_update_status(void) {
4883 struct server *s, *stmp;
4884
4885 list_for_each_entry_safe(s, stmp, &updated_servers, update_status) {
4886 srv_update_status(s);
4887 LIST_DEL(&s->update_status);
4888 LIST_INIT(&s->update_status);
4889 }
4890}
4891
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004892/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004893 * Local variables:
4894 * c-indent-level: 8
4895 * c-basic-offset: 8
4896 * End:
4897 */