blob: 198a68749e16e54f14c6a6d4edf0675b3f79589c [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>
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +010016#include <netinet/tcp.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020017
Olivier Houchard4e694042017-03-14 20:01:29 +010018#include <import/xxhash.h>
19
Willy Tarreau272adea2014-03-31 10:39:59 +020020#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020021#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020022#include <common/errors.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010023#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020024#include <common/time.h>
25
William Lallemand222baf22016-11-19 02:00:33 +010026#include <types/applet.h>
27#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020028#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010029#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020030#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010031#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020032
William Lallemand222baf22016-11-19 02:00:33 +010033#include <proto/applet.h>
34#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090035#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020036#include <proto/port_range.h>
37#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020038#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010039#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020040#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020041#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010042#include <proto/stream_interface.h>
43#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020044#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020045#include <proto/dns.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020046
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020047static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010048static int srv_apply_lastaddr(struct server *srv, int *err_code);
Willy Tarreaubaaee002006-06-26 02:48:02 +020049
Willy Tarreau21faa912012-10-10 08:27:36 +020050/* List head of all known server keywords */
51static struct srv_kw_list srv_keywords = {
52 .list = LIST_HEAD_INIT(srv_keywords.list)
53};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020054
Simon Hormana3608442013-11-01 16:46:15 +090055int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020056{
Willy Tarreau892337c2014-05-13 23:41:20 +020057 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020058 return s->down_time;
59
60 return now.tv_sec - s->last_change + s->down_time;
61}
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050063int srv_lastsession(const struct server *s)
64{
65 if (s->counters.last_sess)
66 return now.tv_sec - s->counters.last_sess;
67
68 return -1;
69}
70
Simon Horman4a741432013-02-23 15:35:38 +090071int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020072{
Simon Horman4a741432013-02-23 15:35:38 +090073 const struct server *s = check->server;
74
Willy Tarreauff5ae352013-12-11 20:36:34 +010075 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090076 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010077
Willy Tarreau892337c2014-05-13 23:41:20 +020078 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090079 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010080
Simon Horman4a741432013-02-23 15:35:38 +090081 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010082}
83
Olivier Houchard4e694042017-03-14 20:01:29 +010084void srv_set_dyncookie(struct server *s)
85{
86 struct proxy *p = s->proxy;
87 struct server *tmpserv;
88 char *tmpbuf;
89 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010090 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +010091 size_t buffer_len;
92 int addr_len;
93 int port;
94
95 if ((s->flags & SRV_F_COOKIESET) ||
96 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
97 s->proxy->dyncookie_key == NULL)
98 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010099 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100100
101 if (s->addr.ss_family != AF_INET &&
102 s->addr.ss_family != AF_INET6)
103 return;
104 /*
105 * Buffer to calculate the cookie value.
106 * The buffer contains the secret key + the server IP address
107 * + the TCP port.
108 */
109 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
110 /*
111 * The TCP port should use only 2 bytes, but is stored in
112 * an unsigned int in struct server, so let's use 4, to be
113 * on the safe side.
114 */
115 buffer_len = key_len + addr_len + 4;
116 tmpbuf = trash.str;
117 memcpy(tmpbuf, p->dyncookie_key, key_len);
118 memcpy(&(tmpbuf[key_len]),
119 s->addr.ss_family == AF_INET ?
120 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
121 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
122 addr_len);
123 /*
124 * Make sure it's the same across all the load balancers,
125 * no matter their endianness.
126 */
127 port = htonl(s->svc_port);
128 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
129 hash_value = XXH64(tmpbuf, buffer_len, 0);
130 memprintf(&s->cookie, "%016llx", hash_value);
131 if (!s->cookie)
132 return;
133 s->cklen = 16;
134 /*
135 * Check that we did not get a hash collision.
136 * Unlikely, but it can happen.
137 */
138 for (p = proxy; p != NULL; p = p->next)
139 for (tmpserv = proxy->srv; tmpserv != NULL;
140 tmpserv = tmpserv->next) {
141 if (tmpserv == s)
142 continue;
143 if (tmpserv->cookie &&
144 strcmp(tmpserv->cookie, s->cookie) == 0) {
145 Warning("We generated two equal cookies for two different servers.\n"
146 "Please change the secret key for '%s'.\n",
147 s->proxy->id);
148 }
149 }
150}
151
Willy Tarreau21faa912012-10-10 08:27:36 +0200152/*
153 * Registers the server keyword list <kwl> as a list of valid keywords for next
154 * parsing sessions.
155 */
156void srv_register_keywords(struct srv_kw_list *kwl)
157{
158 LIST_ADDQ(&srv_keywords.list, &kwl->list);
159}
160
161/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
162 * keyword is found with a NULL ->parse() function, then an attempt is made to
163 * find one with a valid ->parse() function. This way it is possible to declare
164 * platform-dependant, known keywords as NULL, then only declare them as valid
165 * if some options are met. Note that if the requested keyword contains an
166 * opening parenthesis, everything from this point is ignored.
167 */
168struct srv_kw *srv_find_kw(const char *kw)
169{
170 int index;
171 const char *kwend;
172 struct srv_kw_list *kwl;
173 struct srv_kw *ret = NULL;
174
175 kwend = strchr(kw, '(');
176 if (!kwend)
177 kwend = kw + strlen(kw);
178
179 list_for_each_entry(kwl, &srv_keywords.list, list) {
180 for (index = 0; kwl->kw[index].kw != NULL; index++) {
181 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
182 kwl->kw[index].kw[kwend-kw] == 0) {
183 if (kwl->kw[index].parse)
184 return &kwl->kw[index]; /* found it !*/
185 else
186 ret = &kwl->kw[index]; /* may be OK */
187 }
188 }
189 }
190 return ret;
191}
192
193/* Dumps all registered "server" keywords to the <out> string pointer. The
194 * unsupported keywords are only dumped if their supported form was not
195 * found.
196 */
197void srv_dump_kws(char **out)
198{
199 struct srv_kw_list *kwl;
200 int index;
201
202 *out = NULL;
203 list_for_each_entry(kwl, &srv_keywords.list, list) {
204 for (index = 0; kwl->kw[index].kw != NULL; index++) {
205 if (kwl->kw[index].parse ||
206 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
207 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
208 kwl->scope,
209 kwl->kw[index].kw,
210 kwl->kw[index].skip ? " <arg>" : "",
211 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
212 kwl->kw[index].parse ? "" : " (not supported)");
213 }
214 }
215 }
216}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100217
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100218/* Parse the "addr" server keyword */
219static int srv_parse_addr(char **args, int *cur_arg,
220 struct proxy *curproxy, struct server *newsrv, char **err)
221{
222 char *errmsg, *arg;
223 struct sockaddr_storage *sk;
224 int port1, port2;
225 struct protocol *proto;
226
227 errmsg = NULL;
228 arg = args[*cur_arg + 1];
229
230 if (!*arg) {
231 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
232 goto err;
233 }
234
235 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
236 if (!sk) {
237 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
238 goto err;
239 }
240
241 proto = protocol_by_family(sk->ss_family);
242 if (!proto || !proto->connect) {
243 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
244 args[*cur_arg], arg);
245 goto err;
246 }
247
248 if (port1 != port2) {
249 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
250 args[*cur_arg], arg);
251 goto err;
252 }
253
254 newsrv->check.addr = newsrv->agent.addr = *sk;
255 newsrv->flags |= SRV_F_CHECKADDR;
256 newsrv->flags |= SRV_F_AGENTADDR;
257
258 return 0;
259
260 err:
261 free(errmsg);
262 return ERR_ALERT | ERR_FATAL;
263}
264
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100265/* Parse the "backup" server keyword */
266static int srv_parse_backup(char **args, int *cur_arg,
267 struct proxy *curproxy, struct server *newsrv, char **err)
268{
269 newsrv->flags |= SRV_F_BACKUP;
270 return 0;
271}
272
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100273/* Parse the "check" server keyword */
274static int srv_parse_check(char **args, int *cur_arg,
275 struct proxy *curproxy, struct server *newsrv, char **err)
276{
277 newsrv->do_check = 1;
278 return 0;
279}
280
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100281/* Parse the "check-send-proxy" server keyword */
282static int srv_parse_check_send_proxy(char **args, int *cur_arg,
283 struct proxy *curproxy, struct server *newsrv, char **err)
284{
285 newsrv->check.send_proxy = 1;
286 return 0;
287}
288
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100289/* Parse the "cookie" server keyword */
290static int srv_parse_cookie(char **args, int *cur_arg,
291 struct proxy *curproxy, struct server *newsrv, char **err)
292{
293 char *arg;
294
295 arg = args[*cur_arg + 1];
296 if (!*arg) {
297 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
298 return ERR_ALERT | ERR_FATAL;
299 }
300
301 free(newsrv->cookie);
302 newsrv->cookie = strdup(arg);
303 newsrv->cklen = strlen(arg);
304 newsrv->flags |= SRV_F_COOKIESET;
305 return 0;
306}
307
Willy Tarreaudff55432012-10-10 17:51:05 +0200308/* parse the "id" server keyword */
309static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
310{
311 struct eb32_node *node;
312
313 if (!*args[*cur_arg + 1]) {
314 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
315 return ERR_ALERT | ERR_FATAL;
316 }
317
318 newsrv->puid = atol(args[*cur_arg + 1]);
319 newsrv->conf.id.key = newsrv->puid;
320
321 if (newsrv->puid <= 0) {
322 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
323 return ERR_ALERT | ERR_FATAL;
324 }
325
326 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
327 if (node) {
328 struct server *target = container_of(node, struct server, conf.id);
329 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
330 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
331 target->id);
332 return ERR_ALERT | ERR_FATAL;
333 }
334
335 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200336 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200337 return 0;
338}
339
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100340/* Parse the "namespace" server keyword */
341static int srv_parse_namespace(char **args, int *cur_arg,
342 struct proxy *curproxy, struct server *newsrv, char **err)
343{
344#ifdef CONFIG_HAP_NS
345 char *arg;
346
347 arg = args[*cur_arg + 1];
348 if (!*arg) {
349 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
350 return ERR_ALERT | ERR_FATAL;
351 }
352
353 if (!strcmp(arg, "*")) {
354 /* Use the namespace associated with the connection (if present). */
355 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
356 return 0;
357 }
358
359 /*
360 * As this parser may be called several times for the same 'default-server'
361 * object, or for a new 'server' instance deriving from a 'default-server'
362 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
363 */
364 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
365
366 newsrv->netns = netns_store_lookup(arg, strlen(arg));
367 if (!newsrv->netns)
368 newsrv->netns = netns_store_insert(arg);
369
370 if (!newsrv->netns) {
371 memprintf(err, "Cannot open namespace '%s'", arg);
372 return ERR_ALERT | ERR_FATAL;
373 }
374
375 return 0;
376#else
377 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
378 return ERR_ALERT | ERR_FATAL;
379#endif
380}
381
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100382/* Parse the "no-backup" server keyword */
383static int srv_parse_no_backup(char **args, int *cur_arg,
384 struct proxy *curproxy, struct server *newsrv, char **err)
385{
386 newsrv->flags &= ~SRV_F_BACKUP;
387 return 0;
388}
389
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100390/* Parse the "no-check" server keyword */
391static int srv_parse_no_check(char **args, int *cur_arg,
392 struct proxy *curproxy, struct server *newsrv, char **err)
393{
394 free_check(&newsrv->check);
395 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
396 newsrv->do_check = 0;
397 return 0;
398}
399
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100400/* Parse the "no-check-send-proxy" server keyword */
401static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
402 struct proxy *curproxy, struct server *newsrv, char **err)
403{
404 newsrv->check.send_proxy = 0;
405 return 0;
406}
407
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100408/* Disable server PROXY protocol flags. */
409static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
410{
411 srv->pp_opts &= ~flags;
412 return 0;
413}
414
415/* Parse the "no-send-proxy" server keyword */
416static int srv_parse_no_send_proxy(char **args, int *cur_arg,
417 struct proxy *curproxy, struct server *newsrv, char **err)
418{
419 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
420}
421
422/* Parse the "no-send-proxy-v2" server keyword */
423static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
424 struct proxy *curproxy, struct server *newsrv, char **err)
425{
426 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
427}
428
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100429/* Parse the "non-stick" server keyword */
430static int srv_parse_non_stick(char **args, int *cur_arg,
431 struct proxy *curproxy, struct server *newsrv, char **err)
432{
433 newsrv->flags |= SRV_F_NON_STICK;
434 return 0;
435}
436
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100437/* Enable server PROXY protocol flags. */
438static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
439{
440 srv->pp_opts |= flags;
441 return 0;
442}
443
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100444/* Parse the "observe" server keyword */
445static int srv_parse_observe(char **args, int *cur_arg,
446 struct proxy *curproxy, struct server *newsrv, char **err)
447{
448 char *arg;
449
450 arg = args[*cur_arg + 1];
451 if (!*arg) {
452 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
453 return ERR_ALERT | ERR_FATAL;
454 }
455
456 if (!strcmp(arg, "none")) {
457 newsrv->observe = HANA_OBS_NONE;
458 }
459 else if (!strcmp(arg, "layer4")) {
460 newsrv->observe = HANA_OBS_LAYER4;
461 }
462 else if (!strcmp(arg, "layer7")) {
463 if (curproxy->mode != PR_MODE_HTTP) {
464 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
465 return ERR_ALERT;
466 }
467 newsrv->observe = HANA_OBS_LAYER7;
468 }
469 else {
470 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
471 "but got '%s'\n", args[*cur_arg], arg);
472 return ERR_ALERT | ERR_FATAL;
473 }
474
475 return 0;
476}
477
Frédéric Lécaille16186232017-03-14 16:42:49 +0100478/* Parse the "redir" server keyword */
479static int srv_parse_redir(char **args, int *cur_arg,
480 struct proxy *curproxy, struct server *newsrv, char **err)
481{
482 char *arg;
483
484 arg = args[*cur_arg + 1];
485 if (!*arg) {
486 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
487 return ERR_ALERT | ERR_FATAL;
488 }
489
490 free(newsrv->rdr_pfx);
491 newsrv->rdr_pfx = strdup(arg);
492 newsrv->rdr_len = strlen(arg);
493
494 return 0;
495}
496
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100497/* Parse the "send-proxy" server keyword */
498static int srv_parse_send_proxy(char **args, int *cur_arg,
499 struct proxy *curproxy, struct server *newsrv, char **err)
500{
501 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
502}
503
504/* Parse the "send-proxy-v2" server keyword */
505static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
506 struct proxy *curproxy, struct server *newsrv, char **err)
507{
508 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
509}
510
Frédéric Lécailledba97072017-03-17 15:33:50 +0100511
512/* Parse the "source" server keyword */
513static int srv_parse_source(char **args, int *cur_arg,
514 struct proxy *curproxy, struct server *newsrv, char **err)
515{
516 char *errmsg;
517 int port_low, port_high;
518 struct sockaddr_storage *sk;
519 struct protocol *proto;
520
521 errmsg = NULL;
522
523 if (!*args[*cur_arg + 1]) {
524 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
525 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
526 goto err;
527 }
528
529 /* 'sk' is statically allocated (no need to be freed). */
530 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
531 if (!sk) {
532 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
533 goto err;
534 }
535
536 proto = protocol_by_family(sk->ss_family);
537 if (!proto || !proto->connect) {
538 Alert("'%s %s' : connect() not supported for this address family.\n",
539 args[*cur_arg], args[*cur_arg + 1]);
540 goto err;
541 }
542
543 newsrv->conn_src.opts |= CO_SRC_BIND;
544 newsrv->conn_src.source_addr = *sk;
545
546 if (port_low != port_high) {
547 int i;
548
549 if (!port_low || !port_high) {
550 Alert("'%s' does not support port offsets (found '%s').\n",
551 args[*cur_arg], args[*cur_arg + 1]);
552 goto err;
553 }
554
555 if (port_low <= 0 || port_low > 65535 ||
556 port_high <= 0 || port_high > 65535 ||
557 port_low > port_high) {
558 Alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
559 goto err;
560 }
561 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
562 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
563 newsrv->conn_src.sport_range->ports[i] = port_low + i;
564 }
565
566 *cur_arg += 2;
567 while (*(args[*cur_arg])) {
568 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
569#if defined(CONFIG_HAP_TRANSPARENT)
570 if (!*args[*cur_arg + 1]) {
571 Alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
572 "or 'hdr_ip(name,#)' as argument.\n");
573 goto err;
574 }
575 if (!strcmp(args[*cur_arg + 1], "client")) {
576 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
577 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
578 }
579 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
580 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
581 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
582 }
583 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
584 char *name, *end;
585
586 name = args[*cur_arg + 1] + 7;
587 while (isspace(*name))
588 name++;
589
590 end = name;
591 while (*end && !isspace(*end) && *end != ',' && *end != ')')
592 end++;
593
594 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
595 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
596 free(newsrv->conn_src.bind_hdr_name);
597 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
598 newsrv->conn_src.bind_hdr_len = end - name;
599 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
600 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
601 newsrv->conn_src.bind_hdr_occ = -1;
602
603 /* now look for an occurrence number */
604 while (isspace(*end))
605 end++;
606 if (*end == ',') {
607 end++;
608 name = end;
609 if (*end == '-')
610 end++;
611 while (isdigit((int)*end))
612 end++;
613 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
614 }
615
616 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
617 Alert("usesrc hdr_ip(name,num) does not support negative"
618 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
619 goto err;
620 }
621 }
622 else {
623 struct sockaddr_storage *sk;
624 int port1, port2;
625
626 /* 'sk' is statically allocated (no need to be freed). */
627 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
628 if (!sk) {
629 Alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
630 goto err;
631 }
632
633 proto = protocol_by_family(sk->ss_family);
634 if (!proto || !proto->connect) {
635 Alert("'%s %s' : connect() not supported for this address family.\n",
636 args[*cur_arg], args[*cur_arg + 1]);
637 goto err;
638 }
639
640 if (port1 != port2) {
641 Alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
642 args[*cur_arg], args[*cur_arg + 1]);
643 goto err;
644 }
645 newsrv->conn_src.tproxy_addr = *sk;
646 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
647 }
648 global.last_checks |= LSTCHK_NETADM;
649 *cur_arg += 2;
650 continue;
651#else /* no TPROXY support */
652 Alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
653 goto err;
654#endif /* defined(CONFIG_HAP_TRANSPARENT) */
655 } /* "usesrc" */
656
657 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
658#ifdef SO_BINDTODEVICE
659 if (!*args[*cur_arg + 1]) {
660 Alert("'%s' : missing interface name.\n", args[0]);
661 goto err;
662 }
663 free(newsrv->conn_src.iface_name);
664 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
665 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
666 global.last_checks |= LSTCHK_NETADM;
667#else
668 Alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
669 goto err;
670#endif
671 *cur_arg += 2;
672 continue;
673 }
674 /* this keyword in not an option of "source" */
675 break;
676 } /* while */
677
678 return 0;
679
680 err:
681 free(errmsg);
682 return ERR_ALERT | ERR_FATAL;
683}
684
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100685/* Parse the "stick" server keyword */
686static int srv_parse_stick(char **args, int *cur_arg,
687 struct proxy *curproxy, struct server *newsrv, char **err)
688{
689 newsrv->flags &= ~SRV_F_NON_STICK;
690 return 0;
691}
692
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100693/* Parse the "track" server keyword */
694static int srv_parse_track(char **args, int *cur_arg,
695 struct proxy *curproxy, struct server *newsrv, char **err)
696{
697 char *arg;
698
699 arg = args[*cur_arg + 1];
700 if (!*arg) {
701 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
702 return ERR_ALERT | ERR_FATAL;
703 }
704
705 free(newsrv->trackit);
706 newsrv->trackit = strdup(arg);
707
708 return 0;
709}
710
Frédéric Lécailledba97072017-03-17 15:33:50 +0100711
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200712/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200713 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200714 * shutdown.
715 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200716void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200717{
Willy Tarreau87b09662015-04-03 00:22:06 +0200718 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200719
Willy Tarreau87b09662015-04-03 00:22:06 +0200720 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
721 if (stream->srv_conn == srv)
722 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200723}
724
725/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200726 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200727 * the reason for the shutdown.
728 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200729void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200730{
731 struct server *srv;
732
733 for (srv = px->srv; srv != NULL; srv = srv->next)
734 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200735 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200736}
737
Willy Tarreaubda92272014-05-20 21:55:30 +0200738/* Appends some information to a message string related to a server going UP or
739 * DOWN. If both <forced> and <reason> are null and the server tracks another
740 * one, a "via" information will be provided to know where the status came from.
741 * If <reason> is non-null, the entire string will be appended after a comma and
742 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200743 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200744 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200745 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200746void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200747{
Willy Tarreaubda92272014-05-20 21:55:30 +0200748 if (reason)
749 chunk_appendf(msg, ", %s", reason);
750 else if (!forced && s->track)
751 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200752
753 if (xferred >= 0) {
754 if (s->state == SRV_ST_STOPPED)
755 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
756 " %d sessions active, %d requeued, %d remaining in queue",
757 s->proxy->srv_act, s->proxy->srv_bck,
758 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
759 s->cur_sess, xferred, s->nbpend);
760 else
761 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
762 " %d sessions requeued, %d total in queue",
763 s->proxy->srv_act, s->proxy->srv_bck,
764 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
765 xferred, s->nbpend);
766 }
767}
768
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200769/* Marks server <s> down, regardless of its checks' statuses, notifies by all
770 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200771 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200772 * recomputes the number of servers, but not the map. Maintenance servers are
773 * ignored. It reports <reason> if non-null as the reason for going down. Note
774 * that it makes use of the trash to build the log strings, so <reason> must
775 * not be placed there.
776 */
777void srv_set_stopped(struct server *s, const char *reason)
778{
779 struct server *srv;
780 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
781 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900782 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200783 int xferred;
784
785 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
786 return;
787
788 s->last_change = now.tv_sec;
789 s->state = SRV_ST_STOPPED;
790 if (s->proxy->lbprm.set_server_status_down)
791 s->proxy->lbprm.set_server_status_down(s);
792
793 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200794 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200795
Willy Tarreau87b09662015-04-03 00:22:06 +0200796 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200797 * a connection. Those which are redispatchable will be queued
798 * to another server or to the proxy itself.
799 */
800 xferred = pendconn_redistribute(s);
801
802 chunk_printf(&trash,
803 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
804 s->proxy->id, s->id);
805
806 srv_append_status(&trash, s, reason, xferred, 0);
807 Warning("%s.\n", trash.str);
808
809 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900810 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
811 send_log(s->proxy, log_level, "%s.\n", trash.str);
812 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200813
814 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
815 set_backend_down(s->proxy);
816
817 s->counters.down_trans++;
818
819 for (srv = s->trackers; srv; srv = srv->tracknext)
820 srv_set_stopped(srv, NULL);
821}
822
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200823/* Marks server <s> up regardless of its checks' statuses and provided it isn't
824 * in maintenance. Notifies by all available means, recounts the remaining
825 * servers on the proxy and tries to grab requests from the proxy. It
826 * automatically recomputes the number of servers, but not the map. Maintenance
827 * servers are ignored. It reports <reason> if non-null as the reason for going
828 * up. Note that it makes use of the trash to build the log strings, so <reason>
829 * must not be placed there.
830 */
831void srv_set_running(struct server *s, const char *reason)
832{
833 struct server *srv;
834 int xferred;
835
836 if (s->admin & SRV_ADMF_MAINT)
837 return;
838
839 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
840 return;
841
842 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
843 if (s->proxy->last_change < now.tv_sec) // ignore negative times
844 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
845 s->proxy->last_change = now.tv_sec;
846 }
847
848 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
849 s->down_time += now.tv_sec - s->last_change;
850
851 s->last_change = now.tv_sec;
852
853 s->state = SRV_ST_STARTING;
854 if (s->slowstart > 0)
855 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
856 else
857 s->state = SRV_ST_RUNNING;
858
859 server_recalc_eweight(s);
860
861 /* If the server is set with "on-marked-up shutdown-backup-sessions",
862 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200863 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200864 * on all backup servers.
865 */
866 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
867 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200868 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200869
870 /* check if we can handle some connections queued at the proxy. We
871 * will take as many as we can handle.
872 */
873 xferred = pendconn_grab_from_px(s);
874
875 chunk_printf(&trash,
876 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
877 s->proxy->id, s->id);
878
879 srv_append_status(&trash, s, reason, xferred, 0);
880 Warning("%s.\n", trash.str);
881 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900882 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200883
884 for (srv = s->trackers; srv; srv = srv->tracknext)
885 srv_set_running(srv, NULL);
886}
887
Willy Tarreau8eb77842014-05-21 13:54:57 +0200888/* Marks server <s> stopping regardless of its checks' statuses and provided it
889 * isn't in maintenance. Notifies by all available means, recounts the remaining
890 * servers on the proxy and tries to grab requests from the proxy. It
891 * automatically recomputes the number of servers, but not the map. Maintenance
892 * servers are ignored. It reports <reason> if non-null as the reason for going
893 * up. Note that it makes use of the trash to build the log strings, so <reason>
894 * must not be placed there.
895 */
896void srv_set_stopping(struct server *s, const char *reason)
897{
898 struct server *srv;
899 int xferred;
900
901 if (s->admin & SRV_ADMF_MAINT)
902 return;
903
904 if (s->state == SRV_ST_STOPPING)
905 return;
906
907 s->last_change = now.tv_sec;
908 s->state = SRV_ST_STOPPING;
909 if (s->proxy->lbprm.set_server_status_down)
910 s->proxy->lbprm.set_server_status_down(s);
911
Willy Tarreau87b09662015-04-03 00:22:06 +0200912 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200913 * a connection. Those which are redispatchable will be queued
914 * to another server or to the proxy itself.
915 */
916 xferred = pendconn_redistribute(s);
917
918 chunk_printf(&trash,
919 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
920 s->proxy->id, s->id);
921
922 srv_append_status(&trash, s, reason, xferred, 0);
923
924 Warning("%s.\n", trash.str);
925 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
926
927 if (!s->proxy->srv_bck && !s->proxy->srv_act)
928 set_backend_down(s->proxy);
929
930 for (srv = s->trackers; srv; srv = srv->tracknext)
931 srv_set_stopping(srv, NULL);
932}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200933
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200934/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
935 * enforce either maint mode or drain mode. It is not allowed to set more than
936 * one flag at once. The equivalent "inherited" flag is propagated to all
937 * tracking servers. Maintenance mode disables health checks (but not agent
938 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100939 * is done. If <cause> is non-null, it will be displayed at the end of the log
940 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200941 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100942void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200943{
944 struct check *check = &s->check;
945 struct server *srv;
946 int xferred;
947
948 if (!mode)
949 return;
950
951 /* stop going down as soon as we meet a server already in the same state */
952 if (s->admin & mode)
953 return;
954
955 s->admin |= mode;
956
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200957 /* stop going down if the equivalent flag was already present (forced or inherited) */
958 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
959 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
960 return;
961
962 /* Maintenance must also disable health checks */
963 if (mode & SRV_ADMF_MAINT) {
964 if (s->check.state & CHK_ST_ENABLED) {
965 s->check.state |= CHK_ST_PAUSED;
966 check->health = 0;
967 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200968
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200969 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200970 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +0100971 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
972 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
973 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200974
Willy Tarreaubda92272014-05-20 21:55:30 +0200975 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200976
Willy Tarreau6fb8dc12016-11-03 19:42:36 +0100977 if (!(global.mode & MODE_STARTING)) {
978 Warning("%s.\n", trash.str);
979 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
980 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200981 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200982 else { /* server was still running */
983 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
984 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
985
986 check->health = 0; /* failure */
987 s->last_change = now.tv_sec;
988 s->state = SRV_ST_STOPPED;
989 if (s->proxy->lbprm.set_server_status_down)
990 s->proxy->lbprm.set_server_status_down(s);
991
992 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200993 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200994
Willy Tarreau87b09662015-04-03 00:22:06 +0200995 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200996 * a connection. Those which are redispatchable will be queued
997 * to another server or to the proxy itself.
998 */
999 xferred = pendconn_redistribute(s);
1000
1001 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +01001002 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001003 s->flags & SRV_F_BACKUP ? "Backup " : "",
Willy Tarreau8b428482016-11-07 15:53:43 +01001004 s->proxy->id, s->id,
1005 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001006
1007 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
1008
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001009 if (!(global.mode & MODE_STARTING)) {
1010 Warning("%s.\n", trash.str);
1011 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
1012 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001013
1014 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1015 set_backend_down(s->proxy);
1016
1017 s->counters.down_trans++;
1018 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001019 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001020
1021 /* drain state is applied only if not yet in maint */
1022 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001023 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1024
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001025 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001026 if (s->proxy->lbprm.set_server_status_down)
1027 s->proxy->lbprm.set_server_status_down(s);
1028
Willy Tarreau87b09662015-04-03 00:22:06 +02001029 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001030 * a connection. Those which are redispatchable will be queued
1031 * to another server or to the proxy itself.
1032 */
1033 xferred = pendconn_redistribute(s);
1034
Willy Tarreau8b428482016-11-07 15:53:43 +01001035 chunk_printf(&trash, "%sServer %s/%s enters drain state%s%s%s",
1036 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
1037 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001038
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001039 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001040
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001041 if (!(global.mode & MODE_STARTING)) {
1042 Warning("%s.\n", trash.str);
1043 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1044 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
1045 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001046 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1047 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001048 }
1049
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001050 /* compute the inherited flag to propagate */
1051 if (mode & SRV_ADMF_MAINT)
1052 mode = SRV_ADMF_IMAINT;
1053 else if (mode & SRV_ADMF_DRAIN)
1054 mode = SRV_ADMF_IDRAIN;
1055
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001056 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +01001057 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001058}
1059
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001060/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1061 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1062 * than one flag at once. The equivalent "inherited" flag is propagated to all
1063 * tracking servers. Leaving maintenance mode re-enables health checks. When
1064 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001065 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001066void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001067{
1068 struct check *check = &s->check;
1069 struct server *srv;
1070 int xferred = -1;
1071
1072 if (!mode)
1073 return;
1074
1075 /* stop going down as soon as we see the flag is not there anymore */
1076 if (!(s->admin & mode))
1077 return;
1078
1079 s->admin &= ~mode;
1080
1081 if (s->admin & SRV_ADMF_MAINT) {
1082 /* remaining in maintenance mode, let's inform precisely about the
1083 * situation.
1084 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001085 if (mode & SRV_ADMF_FMAINT) {
1086 chunk_printf(&trash,
1087 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
1088 s->flags & SRV_F_BACKUP ? "Backup " : "",
1089 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001090
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001091 if (s->track) /* normally it's mandatory here */
1092 chunk_appendf(&trash, " via %s/%s",
1093 s->track->proxy->id, s->track->id);
1094 Warning("%s.\n", trash.str);
1095 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1096 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001097 if (mode & SRV_ADMF_RMAINT) {
1098 chunk_printf(&trash,
1099 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
1100 s->flags & SRV_F_BACKUP ? "Backup " : "",
1101 s->proxy->id, s->id, s->hostname);
1102
1103 if (s->track) /* normally it's mandatory here */
1104 chunk_appendf(&trash, " via %s/%s",
1105 s->track->proxy->id, s->track->id);
1106 Warning("%s.\n", trash.str);
1107 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1108 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001109 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001110 chunk_printf(&trash,
1111 "%sServer %s/%s remains in forced maintenance",
1112 s->flags & SRV_F_BACKUP ? "Backup " : "",
1113 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001114 Warning("%s.\n", trash.str);
1115 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1116 }
1117 /* don't report anything when leaving drain mode and remaining in maintenance */
1118 }
1119 else if (mode & SRV_ADMF_MAINT) {
1120 /* OK here we're leaving maintenance, we have many things to check,
1121 * because the server might possibly be coming back up depending on
1122 * its state. In practice, leaving maintenance means that we should
1123 * immediately turn to UP (more or less the slowstart) under the
1124 * following conditions :
1125 * - server is neither checked nor tracked
1126 * - server tracks another server which is not checked
1127 * - server tracks another server which is already up
1128 * Which sums up as something simpler :
1129 * "either the tracking server is up or the server's checks are disabled
1130 * or up". Otherwise we only re-enable health checks. There's a special
1131 * case associated to the stopping state which can be inherited. Note
1132 * that the server might still be in drain mode, which is naturally dealt
1133 * with by the lower level functions.
1134 */
1135
1136 if (s->check.state & CHK_ST_ENABLED) {
1137 s->check.state &= ~CHK_ST_PAUSED;
1138 check->health = check->rise; /* start OK but check immediately */
1139 }
1140
1141 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
1142 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
1143 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
1144 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1145 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1146 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1147 s->proxy->last_change = now.tv_sec;
1148 }
1149
1150 if (s->last_change < now.tv_sec) // ignore negative times
1151 s->down_time += now.tv_sec - s->last_change;
1152 s->last_change = now.tv_sec;
1153
1154 if (s->track && s->track->state == SRV_ST_STOPPING)
1155 s->state = SRV_ST_STOPPING;
1156 else {
1157 s->state = SRV_ST_STARTING;
1158 if (s->slowstart > 0)
1159 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
1160 else
1161 s->state = SRV_ST_RUNNING;
1162 }
1163
1164 server_recalc_eweight(s);
1165
1166 /* If the server is set with "on-marked-up shutdown-backup-sessions",
1167 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +02001168 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001169 * on all backup servers.
1170 */
1171 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
1172 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001173 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001174
1175 /* check if we can handle some connections queued at the proxy. We
1176 * will take as many as we can handle.
1177 */
1178 xferred = pendconn_grab_from_px(s);
1179 }
1180
1181 if (mode & SRV_ADMF_FMAINT) {
1182 chunk_printf(&trash,
1183 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
1184 s->flags & SRV_F_BACKUP ? "Backup " : "",
1185 s->proxy->id, s->id,
1186 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1187 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001188 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001189 else if (mode & SRV_ADMF_RMAINT) {
1190 chunk_printf(&trash,
1191 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
1192 s->flags & SRV_F_BACKUP ? "Backup " : "",
1193 s->proxy->id, s->id, s->hostname,
1194 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1195 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
1196 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001197 else {
1198 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001199 "%sServer %s/%s is %s/%s (leaving maintenance)",
1200 s->flags & SRV_F_BACKUP ? "Backup " : "",
1201 s->proxy->id, s->id,
1202 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1203 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
1204 srv_append_status(&trash, s, NULL, xferred, 0);
1205 }
1206 Warning("%s.\n", trash.str);
1207 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1208 }
1209 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
1210 /* remaining in drain mode after removing one of its flags */
1211
1212 if (mode & SRV_ADMF_FDRAIN) {
1213 chunk_printf(&trash,
1214 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001215 s->flags & SRV_F_BACKUP ? "Backup " : "",
1216 s->proxy->id, s->id);
1217
1218 if (s->track) /* normally it's mandatory here */
1219 chunk_appendf(&trash, " via %s/%s",
1220 s->track->proxy->id, s->track->id);
1221 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001222 else {
1223 chunk_printf(&trash,
1224 "%sServer %s/%s remains in forced drain mode",
1225 s->flags & SRV_F_BACKUP ? "Backup " : "",
1226 s->proxy->id, s->id);
1227 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001228 Warning("%s.\n", trash.str);
1229 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001230 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001231 else if (mode & SRV_ADMF_DRAIN) {
1232 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001233 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1234 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1235 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1236 s->proxy->last_change = now.tv_sec;
1237 }
1238
1239 if (s->last_change < now.tv_sec) // ignore negative times
1240 s->down_time += now.tv_sec - s->last_change;
1241 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001242 server_recalc_eweight(s);
1243
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001244 if (mode & SRV_ADMF_FDRAIN) {
1245 chunk_printf(&trash,
1246 "%sServer %s/%s is %s (leaving forced drain)",
1247 s->flags & SRV_F_BACKUP ? "Backup " : "",
1248 s->proxy->id, s->id,
1249 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
1250 }
1251 else {
1252 chunk_printf(&trash,
1253 "%sServer %s/%s is %s (leaving drain)",
1254 s->flags & SRV_F_BACKUP ? "Backup " : "",
1255 s->proxy->id, s->id,
1256 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
1257 if (s->track) /* normally it's mandatory here */
1258 chunk_appendf(&trash, " via %s/%s",
1259 s->track->proxy->id, s->track->id);
1260 }
1261 Warning("%s.\n", trash.str);
1262 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001263 }
1264
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001265 /* stop going down if the equivalent flag is still present (forced or inherited) */
1266 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
1267 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
1268 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001269
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001270 if (mode & SRV_ADMF_MAINT)
1271 mode = SRV_ADMF_IMAINT;
1272 else if (mode & SRV_ADMF_DRAIN)
1273 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001274
1275 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001276 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001277}
1278
Willy Tarreau757478e2016-11-03 19:22:19 +01001279/* principle: propagate maint and drain to tracking servers. This is useful
1280 * upon startup so that inherited states are correct.
1281 */
1282static void srv_propagate_admin_state(struct server *srv)
1283{
1284 struct server *srv2;
1285
1286 if (!srv->trackers)
1287 return;
1288
1289 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
1290 if (srv->admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001291 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001292
1293 if (srv->admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001294 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001295 }
1296}
1297
1298/* Compute and propagate the admin states for all servers in proxy <px>.
1299 * Only servers *not* tracking another one are considered, because other
1300 * ones will be handled when the server they track is visited.
1301 */
1302void srv_compute_all_admin_states(struct proxy *px)
1303{
1304 struct server *srv;
1305
1306 for (srv = px->srv; srv; srv = srv->next) {
1307 if (srv->track)
1308 continue;
1309 srv_propagate_admin_state(srv);
1310 }
1311}
1312
Willy Tarreaudff55432012-10-10 17:51:05 +02001313/* Note: must not be declared <const> as its list will be overwritten.
1314 * Please take care of keeping this list alphabetically sorted, doing so helps
1315 * all code contributors.
1316 * Optional keywords are also declared with a NULL ->parse() function so that
1317 * the config parser can report an appropriate error when a known keyword was
1318 * not enabled.
1319 */
1320static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001321 { "addr", srv_parse_addr, 1, 1 }, /* IP address to send health to or to probe from agent-check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001322 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001323 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001324 { "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 +01001325 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001326 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001327 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001328 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001329 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001330 { "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 +01001331 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1332 { "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 +01001333 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001334 { "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 +01001335 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001336 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1337 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1 }, /* Enforce use of PROXY V2 protocol */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001338 /*
1339 * Note: the following 'skip' field value is 0.
1340 * Here this does not mean that "source" setting does not need any argument.
1341 * This means that the number of argument is variable.
1342 */
1343 { "source", srv_parse_source, 0, 1 }, /* Set the source address to be used to connect to the server */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001344 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001345 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001346 { NULL, NULL, 0 },
1347}};
1348
1349__attribute__((constructor))
1350static void __listener_init(void)
1351{
1352 srv_register_keywords(&srv_kws);
1353}
1354
Willy Tarreau004e0452013-11-21 11:22:01 +01001355/* Recomputes the server's eweight based on its state, uweight, the current time,
1356 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1357 * state is automatically disabled if the time is elapsed.
1358 */
1359void server_recalc_eweight(struct server *sv)
1360{
1361 struct proxy *px = sv->proxy;
1362 unsigned w;
1363
1364 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1365 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +02001366 if (sv->state == SRV_ST_STARTING)
1367 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001368 }
1369
1370 /* We must take care of not pushing the server to full throttle during slow starts.
1371 * It must also start immediately, at least at the minimal step when leaving maintenance.
1372 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001373 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001374 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1375 else
1376 w = px->lbprm.wdiv;
1377
1378 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
1379
1380 /* now propagate the status change to any LB algorithms */
1381 if (px->lbprm.update_server_eweight)
1382 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +02001383 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +01001384 if (px->lbprm.set_server_status_up)
1385 px->lbprm.set_server_status_up(sv);
1386 }
1387 else {
1388 if (px->lbprm.set_server_status_down)
1389 px->lbprm.set_server_status_down(sv);
1390 }
1391}
1392
Willy Tarreaubaaee002006-06-26 02:48:02 +02001393/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001394 * Parses weight_str and configures sv accordingly.
1395 * Returns NULL on success, error message string otherwise.
1396 */
1397const char *server_parse_weight_change_request(struct server *sv,
1398 const char *weight_str)
1399{
1400 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001401 long int w;
1402 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001403
1404 px = sv->proxy;
1405
1406 /* if the weight is terminated with '%', it is set relative to
1407 * the initial weight, otherwise it is absolute.
1408 */
1409 if (!*weight_str)
1410 return "Require <weight> or <weight%>.\n";
1411
Simon Hormanb796afa2013-02-12 10:45:53 +09001412 w = strtol(weight_str, &end, 10);
1413 if (end == weight_str)
1414 return "Empty weight string empty or preceded by garbage";
1415 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001416 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001417 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001418 /* Avoid integer overflow */
1419 if (w > 25600)
1420 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001421 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001422 if (w > 256)
1423 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001424 }
1425 else if (w < 0 || w > 256)
1426 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001427 else if (end[0] != '\0')
1428 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001429
1430 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1431 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1432
1433 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001434 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001435
1436 return NULL;
1437}
1438
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001439/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001440 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1441 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001442 * Returns:
1443 * - error string on error
1444 * - NULL on success
1445 */
1446const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001447 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001448{
1449 unsigned char ip[INET6_ADDRSTRLEN];
1450
1451 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001452 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001453 return NULL;
1454 }
1455 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001456 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001457 return NULL;
1458 }
1459
1460 return "Could not understand IP address format.\n";
1461}
1462
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001463const char *server_parse_maxconn_change_request(struct server *sv,
1464 const char *maxconn_str)
1465{
1466 long int v;
1467 char *end;
1468
1469 if (!*maxconn_str)
1470 return "Require <maxconn>.\n";
1471
1472 v = strtol(maxconn_str, &end, 10);
1473 if (end == maxconn_str)
1474 return "maxconn string empty or preceded by garbage";
1475 else if (end[0] != '\0')
1476 return "Trailing garbage in maxconn string";
1477
1478 if (sv->maxconn == sv->minconn) { // static maxconn
1479 sv->maxconn = sv->minconn = v;
1480 } else { // dynamic maxconn
1481 sv->maxconn = v;
1482 }
1483
1484 if (may_dequeue_tasks(sv, sv->proxy))
1485 process_srv_queue(sv);
1486
1487 return NULL;
1488}
1489
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001490#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1491static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1492{
1493 int idx;
1494 struct sample_expr *expr;
1495 const char *args[] = {
1496 newsrv->sni_expr,
1497 NULL,
1498 };
1499
1500 idx = 0;
1501 proxy->conf.args.ctx = ARGC_SRV;
1502
1503 expr = sample_parse_expr((char **)args, &idx, px->conf.file, px->conf.line,
1504 err, &proxy->conf.args);
1505 if (!expr) {
1506 memprintf(err, "error detected while parsing sni expression : %s", *err);
1507 return ERR_ALERT | ERR_FATAL;
1508 }
1509
1510 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1511 memprintf(err, "error detected while parsing sni expression : "
1512 " fetch method '%s' extracts information from '%s', "
1513 "none of which is available here.\n",
1514 args[0], sample_src_names(expr->fetch->use));
1515 return ERR_ALERT | ERR_FATAL;
1516 }
1517
1518 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1519 release_sample_expr(newsrv->ssl_ctx.sni);
1520 newsrv->ssl_ctx.sni = expr;
1521
1522 return 0;
1523}
1524#endif
1525
1526static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1527{
1528 if (err && *err) {
1529 indent_msg(err, 2);
1530 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1531 }
1532 else
1533 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1534 file, linenum, args[0], args[1], args[cur_arg]);
1535}
1536
Willy Tarreau272adea2014-03-31 10:39:59 +02001537int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1538{
1539 struct server *newsrv = NULL;
1540 const char *err;
1541 char *errmsg = NULL;
1542 int err_code = 0;
1543 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001544 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001545
1546 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
1547 int cur_arg;
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001548 int do_agent = 0, defsrv = (*args[0] == 'd');
Willy Tarreau272adea2014-03-31 10:39:59 +02001549
1550 if (!defsrv && curproxy == defproxy) {
1551 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1552 err_code |= ERR_ALERT | ERR_FATAL;
1553 goto out;
1554 }
1555 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1556 err_code |= ERR_ALERT | ERR_FATAL;
1557
Frédéric Lécaille8065b6d2017-03-09 14:01:02 +01001558 if (!defsrv && !*args[2]) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001559 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1560 file, linenum, args[0]);
1561 err_code |= ERR_ALERT | ERR_FATAL;
1562 goto out;
1563 }
1564
1565 err = invalid_char(args[1]);
1566 if (err && !defsrv) {
1567 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
1568 file, linenum, *err, args[1]);
1569 err_code |= ERR_ALERT | ERR_FATAL;
1570 goto out;
1571 }
1572
1573 if (!defsrv) {
1574 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001575 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001576 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001577 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +02001578
Vincent Bernat02779b62016-04-03 13:48:43 +02001579 if ((newsrv = calloc(1, sizeof(*newsrv))) == NULL) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001580 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1581 err_code |= ERR_ALERT | ERR_ABORT;
1582 goto out;
1583 }
1584
1585 /* the servers are linked backwards first */
1586 newsrv->next = curproxy->srv;
1587 curproxy->srv = newsrv;
1588 newsrv->proxy = curproxy;
1589 newsrv->conf.file = strdup(file);
1590 newsrv->conf.line = linenum;
1591
1592 newsrv->obj_type = OBJ_TYPE_SERVER;
1593 LIST_INIT(&newsrv->actconns);
1594 LIST_INIT(&newsrv->pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02001595 LIST_INIT(&newsrv->priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02001596 LIST_INIT(&newsrv->idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02001597 LIST_INIT(&newsrv->safe_conns);
Willy Tarreau272adea2014-03-31 10:39:59 +02001598 do_agent = 0;
Willy Tarreauc93cd162014-05-13 15:54:22 +02001599 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +02001600 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +02001601 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +02001602 newsrv->last_change = now.tv_sec;
1603 newsrv->id = strdup(args[1]);
1604
1605 /* several ways to check the port component :
1606 * - IP => port=+0, relative (IPv4 only)
1607 * - IP: => port=+0, relative
1608 * - IP:N => port=N, absolute
1609 * - IP:+N => port=+N, relative
1610 * - IP:-N => port=-N, relative
1611 */
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001612 sk = str2sa_range(args[2], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001613 if (!sk) {
1614 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1615 err_code |= ERR_ALERT | ERR_FATAL;
1616 goto out;
1617 }
1618
1619 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001620 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001621 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1622 file, linenum, args[0], args[1]);
1623 err_code |= ERR_ALERT | ERR_FATAL;
1624 goto out;
1625 }
1626
1627 if (!port1 || !port2) {
1628 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001629 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001630 }
1631 else if (port1 != port2) {
1632 /* port range */
1633 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1634 file, linenum, args[0], args[1], args[2]);
1635 err_code |= ERR_ALERT | ERR_FATAL;
1636 goto out;
1637 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001638
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001639 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +02001640 newsrv->hostname = fqdn;
1641 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001642 goto skip_name_resolution;
1643
Willy Tarreau07101d52015-09-08 16:16:35 +02001644 fqdn = NULL;
Vincent Bernat02779b62016-04-03 13:48:43 +02001645 if ((curr_resolution = calloc(1, sizeof(*curr_resolution))) == NULL)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001646 goto skip_name_resolution;
1647
1648 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
1649 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
1650 goto skip_name_resolution;
1651 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
1652 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
1653 file, linenum, args[2]);
1654 err_code |= ERR_ALERT | ERR_FATAL;
1655 goto out;
1656 }
1657
1658 curr_resolution->requester = newsrv;
1659 curr_resolution->requester_cb = snr_resolution_cb;
1660 curr_resolution->requester_error_cb = snr_resolution_error_cb;
1661 curr_resolution->status = RSLV_STATUS_NONE;
1662 curr_resolution->step = RSLV_STEP_NONE;
1663 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +02001664 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001665 newsrv->resolution = curr_resolution;
1666
1667 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +02001668 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001669 newsrv->svc_port = port;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001670 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = xprt_get(XPRT_RAW);
Willy Tarreau272adea2014-03-31 10:39:59 +02001671
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001672 if (!newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001673 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
1674 file, linenum, newsrv->addr.ss_family, args[2]);
1675 err_code |= ERR_ALERT | ERR_FATAL;
1676 goto out;
1677 }
1678
Frédéric Lécailledba97072017-03-17 15:33:50 +01001679 /*
1680 * In this section we copy default-server connection source settings to
1681 * the new server object connection source
1682 * (newsrv->conn_src <- curproxy->defsrv.conn_src).
1683 */
1684 newsrv->conn_src.opts = curproxy->defsrv.conn_src.opts;
1685 newsrv->conn_src.source_addr = curproxy->defsrv.conn_src.source_addr;
1686 if (curproxy->defsrv.conn_src.sport_range != NULL) {
1687 int i, def_sport_range_sz;
1688 struct server *default_srv;
1689
1690 default_srv = &curproxy->defsrv;
1691 def_sport_range_sz = default_srv->conn_src.sport_range->size;
1692 if (def_sport_range_sz > 0) {
1693 newsrv->conn_src.sport_range = port_range_alloc_range(def_sport_range_sz);
1694 if (newsrv->conn_src.sport_range) {
1695 for (i = 0; i < def_sport_range_sz; i++)
1696 newsrv->conn_src.sport_range->ports[i] = default_srv->conn_src.sport_range->ports[i];
1697 }
1698 }
1699 }
1700 if (curproxy->defsrv.conn_src.bind_hdr_name != NULL) {
1701 newsrv->conn_src.bind_hdr_name = strdup(curproxy->defsrv.conn_src.bind_hdr_name);
1702 newsrv->conn_src.bind_hdr_len = strlen(curproxy->defsrv.conn_src.bind_hdr_name);
1703 }
1704 newsrv->conn_src.bind_hdr_occ = curproxy->defsrv.conn_src.bind_hdr_occ;
1705 newsrv->conn_src.tproxy_addr = curproxy->defsrv.conn_src.tproxy_addr;
1706 if (curproxy->defsrv.conn_src.iface_name != NULL)
1707 newsrv->conn_src.iface_name = strdup(curproxy->defsrv.conn_src.iface_name);
1708
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001709 newsrv->pp_opts = curproxy->defsrv.pp_opts;
Frédéric Lécaille16186232017-03-14 16:42:49 +01001710 if (curproxy->defsrv.rdr_pfx != NULL) {
1711 newsrv->rdr_pfx = strdup(curproxy->defsrv.rdr_pfx);
1712 newsrv->rdr_len = curproxy->defsrv.rdr_len;
1713 }
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001714 if (curproxy->defsrv.cookie != NULL) {
1715 newsrv->cookie = strdup(curproxy->defsrv.cookie);
1716 newsrv->cklen = curproxy->defsrv.cklen;
1717 }
Willy Tarreau141ad852016-12-22 18:38:00 +01001718 newsrv->use_ssl = curproxy->defsrv.use_ssl;
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001719 newsrv->check.addr = newsrv->agent.addr = curproxy->defsrv.check.addr;
Willy Tarreau272adea2014-03-31 10:39:59 +02001720 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
1721 newsrv->check.port = curproxy->defsrv.check.port;
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +01001722 /* Note: 'flags' field has potentially been already initialized. */
1723 newsrv->flags |= curproxy->defsrv.flags;
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001724 newsrv->do_check = curproxy->defsrv.do_check;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001725 if (newsrv->check.port)
1726 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001727 newsrv->check.inter = curproxy->defsrv.check.inter;
1728 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
1729 newsrv->check.downinter = curproxy->defsrv.check.downinter;
1730 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
1731 newsrv->agent.port = curproxy->defsrv.agent.port;
James Brown55f9ff12015-10-21 18:19:05 -07001732 if (curproxy->defsrv.agent.send_string != NULL)
1733 newsrv->agent.send_string = strdup(curproxy->defsrv.agent.send_string);
1734 newsrv->agent.send_string_len = curproxy->defsrv.agent.send_string_len;
Willy Tarreau272adea2014-03-31 10:39:59 +02001735 newsrv->agent.inter = curproxy->defsrv.agent.inter;
1736 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
1737 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
1738 newsrv->maxqueue = curproxy->defsrv.maxqueue;
1739 newsrv->minconn = curproxy->defsrv.minconn;
1740 newsrv->maxconn = curproxy->defsrv.maxconn;
1741 newsrv->slowstart = curproxy->defsrv.slowstart;
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001742 newsrv->observe = curproxy->defsrv.observe;
Willy Tarreau272adea2014-03-31 10:39:59 +02001743 newsrv->onerror = curproxy->defsrv.onerror;
1744 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
1745 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001746 if (curproxy->defsrv.trackit != NULL)
1747 newsrv->trackit = strdup(curproxy->defsrv.trackit);
Willy Tarreau272adea2014-03-31 10:39:59 +02001748 newsrv->consecutive_errors_limit
1749 = curproxy->defsrv.consecutive_errors_limit;
Willy Tarreau272adea2014-03-31 10:39:59 +02001750 newsrv->uweight = newsrv->iweight
1751 = curproxy->defsrv.iweight;
1752
1753 newsrv->check.status = HCHK_STATUS_INI;
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001754 newsrv->check.send_proxy = curproxy->defsrv.check.send_proxy;
Willy Tarreau272adea2014-03-31 10:39:59 +02001755 newsrv->check.rise = curproxy->defsrv.check.rise;
1756 newsrv->check.fall = curproxy->defsrv.check.fall;
1757 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
1758 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +09001759 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +02001760
1761 newsrv->agent.status = HCHK_STATUS_INI;
1762 newsrv->agent.rise = curproxy->defsrv.agent.rise;
1763 newsrv->agent.fall = curproxy->defsrv.agent.fall;
1764 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
1765 newsrv->agent.server = newsrv;
Thierry Fournierada34842016-02-17 21:25:09 +01001766 newsrv->dns_opts.family_prio = curproxy->defsrv.dns_opts.family_prio;
1767 if (newsrv->dns_opts.family_prio == AF_UNSPEC)
1768 newsrv->dns_opts.family_prio = AF_INET6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001769 memcpy(newsrv->dns_opts.pref_net,
1770 curproxy->defsrv.dns_opts.pref_net,
1771 sizeof(newsrv->dns_opts.pref_net));
1772 newsrv->dns_opts.pref_net_nb = curproxy->defsrv.dns_opts.pref_net_nb;
Baptiste Assmann25938272016-09-21 20:26:16 +02001773 newsrv->init_addr_methods = curproxy->defsrv.init_addr_methods;
1774 newsrv->init_addr = curproxy->defsrv.init_addr;
Frédéric Lécaille7c8cd582017-03-13 13:41:16 +01001775#if defined(USE_OPENSSL)
1776 /* SSL config. */
Frédéric Lécaille5e576432017-03-14 15:52:04 +01001777 if (curproxy->defsrv.ssl_ctx.ca_file != NULL)
1778 newsrv->ssl_ctx.ca_file = strdup(curproxy->defsrv.ssl_ctx.ca_file);
1779 if (curproxy->defsrv.ssl_ctx.crl_file != NULL)
1780 newsrv->ssl_ctx.crl_file = strdup(curproxy->defsrv.ssl_ctx.crl_file);
1781 if (curproxy->defsrv.ssl_ctx.client_crt != NULL)
1782 newsrv->ssl_ctx.client_crt = strdup(curproxy->defsrv.ssl_ctx.crl_file);
Frédéric Lécaille7c8cd582017-03-13 13:41:16 +01001783 newsrv->ssl_ctx.verify = curproxy->defsrv.ssl_ctx.verify;
Frédéric Lécaille273f3212017-03-13 15:52:01 +01001784 if (curproxy->defsrv.ssl_ctx.verify_host != NULL)
1785 newsrv->ssl_ctx.verify_host = strdup(curproxy->defsrv.ssl_ctx.verify_host);
Frédéric Lécaillebcaf1d72017-03-15 16:20:02 +01001786 if (curproxy->defsrv.ssl_ctx.ciphers != NULL)
1787 newsrv->ssl_ctx.ciphers = strdup(curproxy->defsrv.ssl_ctx.ciphers);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001788 if (curproxy->defsrv.sni_expr != NULL)
1789 newsrv->sni_expr = strdup(curproxy->defsrv.sni_expr);
Frédéric Lécaille7c8cd582017-03-13 13:41:16 +01001790#endif
Willy Tarreau272adea2014-03-31 10:39:59 +02001791
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01001792#ifdef TCP_USER_TIMEOUT
1793 newsrv->tcp_ut = curproxy->defsrv.tcp_ut;
1794#endif
1795
Willy Tarreau272adea2014-03-31 10:39:59 +02001796 cur_arg = 3;
1797 } else {
1798 newsrv = &curproxy->defsrv;
1799 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01001800 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001801 }
1802
1803 while (*args[cur_arg]) {
1804 if (!strcmp(args[cur_arg], "agent-check")) {
1805 global.maxsock++;
1806 do_agent = 1;
1807 cur_arg += 1;
1808 } else if (!strcmp(args[cur_arg], "agent-inter")) {
1809 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1810 if (err) {
1811 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1812 file, linenum, *err, newsrv->id);
1813 err_code |= ERR_ALERT | ERR_FATAL;
1814 goto out;
1815 }
1816 if (val <= 0) {
1817 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1818 file, linenum, val, args[cur_arg], newsrv->id);
1819 err_code |= ERR_ALERT | ERR_FATAL;
1820 goto out;
1821 }
1822 newsrv->agent.inter = val;
1823 cur_arg += 2;
1824 }
Misiekea849332017-01-09 09:39:51 +01001825 else if (!strcmp(args[cur_arg], "agent-addr")) {
1826 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
1827 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
1828 goto out;
1829 }
1830
1831 cur_arg += 2;
1832 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001833 else if (!strcmp(args[cur_arg], "agent-port")) {
1834 global.maxsock++;
1835 newsrv->agent.port = atol(args[cur_arg + 1]);
1836 cur_arg += 2;
1837 }
James Brown55f9ff12015-10-21 18:19:05 -07001838 else if (!strcmp(args[cur_arg], "agent-send")) {
1839 global.maxsock++;
1840 free(newsrv->agent.send_string);
1841 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1842 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1843 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1844 cur_arg += 2;
1845 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001846 else if (!strcmp(args[cur_arg], "init-addr")) {
1847 char *p, *end;
1848 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01001849 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02001850
1851 newsrv->init_addr_methods = 0;
1852 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
1853
1854 for (p = args[cur_arg + 1]; *p; p = end) {
1855 /* cut on next comma */
1856 for (end = p; *end && *end != ','; end++);
1857 if (*end)
1858 *(end++) = 0;
1859
Willy Tarreau4310d362016-11-02 15:05:56 +01001860 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02001861 if (!strcmp(p, "libc")) {
1862 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
1863 }
1864 else if (!strcmp(p, "last")) {
1865 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
1866 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01001867 else if (!strcmp(p, "none")) {
1868 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
1869 }
Willy Tarreau4310d362016-11-02 15:05:56 +01001870 else if (str2ip2(p, &sa, 0)) {
1871 if (is_addr(&newsrv->init_addr)) {
1872 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
1873 file, linenum, args[cur_arg], p);
1874 err_code |= ERR_ALERT | ERR_FATAL;
1875 goto out;
1876 }
1877 newsrv->init_addr = sa;
1878 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
1879 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001880 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01001881 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02001882 file, linenum, args[cur_arg], p);
1883 err_code |= ERR_ALERT | ERR_FATAL;
1884 goto out;
1885 }
1886 if (!done) {
1887 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
1888 file, linenum, args[cur_arg], p);
1889 err_code |= ERR_ALERT | ERR_FATAL;
1890 goto out;
1891 }
1892 }
1893 cur_arg += 2;
1894 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001895 else if (!strcmp(args[cur_arg], "resolvers")) {
1896 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1897 cur_arg += 2;
1898 }
1899 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1900 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001901 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001902 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001903 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001904 else {
1905 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1906 file, linenum, args[cur_arg]);
1907 err_code |= ERR_ALERT | ERR_FATAL;
1908 goto out;
1909 }
1910 cur_arg += 2;
1911 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001912 else if (!strcmp(args[cur_arg], "resolve-net")) {
1913 char *p, *e;
1914 unsigned char mask;
1915 struct dns_options *opt;
1916
1917 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1918 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1919 file, linenum, args[cur_arg]);
1920 err_code |= ERR_ALERT | ERR_FATAL;
1921 goto out;
1922 }
1923
1924 opt = &newsrv->dns_opts;
1925
1926 /* Split arguments by comma, and convert it from ipv4 or ipv6
1927 * string network in in_addr or in6_addr.
1928 */
1929 p = args[cur_arg + 1];
1930 e = p;
1931 while (*p != '\0') {
1932 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001933 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001934 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1935 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1936 err_code |= ERR_ALERT | ERR_FATAL;
1937 goto out;
1938 }
1939 /* look for end or comma. */
1940 while (*e != ',' && *e != '\0')
1941 e++;
1942 if (*e == ',') {
1943 *e = '\0';
1944 e++;
1945 }
1946 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1947 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1948 /* Try to convert input string from ipv4 or ipv6 network. */
1949 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1950 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1951 &mask)) {
1952 /* Try to convert input string from ipv6 network. */
1953 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1954 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1955 } else {
1956 /* All network conversions fail, retrun error. */
1957 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
1958 file, linenum, args[cur_arg], p);
1959 err_code |= ERR_ALERT | ERR_FATAL;
1960 goto out;
1961 }
1962 opt->pref_net_nb++;
1963 p = e;
1964 }
1965
1966 cur_arg += 2;
1967 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001968 else if (!strcmp(args[cur_arg], "rise")) {
1969 if (!*args[cur_arg + 1]) {
1970 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1971 file, linenum, args[cur_arg]);
1972 err_code |= ERR_ALERT | ERR_FATAL;
1973 goto out;
1974 }
1975
1976 newsrv->check.rise = atol(args[cur_arg + 1]);
1977 if (newsrv->check.rise <= 0) {
1978 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
1979 file, linenum, args[cur_arg]);
1980 err_code |= ERR_ALERT | ERR_FATAL;
1981 goto out;
1982 }
1983
1984 if (newsrv->check.health)
1985 newsrv->check.health = newsrv->check.rise;
1986 cur_arg += 2;
1987 }
1988 else if (!strcmp(args[cur_arg], "fall")) {
1989 newsrv->check.fall = atol(args[cur_arg + 1]);
1990
1991 if (!*args[cur_arg + 1]) {
1992 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
1993 file, linenum, args[cur_arg]);
1994 err_code |= ERR_ALERT | ERR_FATAL;
1995 goto out;
1996 }
1997
1998 if (newsrv->check.fall <= 0) {
1999 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2000 file, linenum, args[cur_arg]);
2001 err_code |= ERR_ALERT | ERR_FATAL;
2002 goto out;
2003 }
2004
2005 cur_arg += 2;
2006 }
2007 else if (!strcmp(args[cur_arg], "inter")) {
2008 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2009 if (err) {
2010 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2011 file, linenum, *err, newsrv->id);
2012 err_code |= ERR_ALERT | ERR_FATAL;
2013 goto out;
2014 }
2015 if (val <= 0) {
2016 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2017 file, linenum, val, args[cur_arg], newsrv->id);
2018 err_code |= ERR_ALERT | ERR_FATAL;
2019 goto out;
2020 }
2021 newsrv->check.inter = val;
2022 cur_arg += 2;
2023 }
2024 else if (!strcmp(args[cur_arg], "fastinter")) {
2025 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2026 if (err) {
2027 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2028 file, linenum, *err, newsrv->id);
2029 err_code |= ERR_ALERT | ERR_FATAL;
2030 goto out;
2031 }
2032 if (val <= 0) {
2033 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2034 file, linenum, val, args[cur_arg], newsrv->id);
2035 err_code |= ERR_ALERT | ERR_FATAL;
2036 goto out;
2037 }
2038 newsrv->check.fastinter = val;
2039 cur_arg += 2;
2040 }
2041 else if (!strcmp(args[cur_arg], "downinter")) {
2042 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2043 if (err) {
2044 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2045 file, linenum, *err, newsrv->id);
2046 err_code |= ERR_ALERT | ERR_FATAL;
2047 goto out;
2048 }
2049 if (val <= 0) {
2050 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2051 file, linenum, val, args[cur_arg], newsrv->id);
2052 err_code |= ERR_ALERT | ERR_FATAL;
2053 goto out;
2054 }
2055 newsrv->check.downinter = val;
2056 cur_arg += 2;
2057 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002058 else if (!strcmp(args[cur_arg], "port")) {
2059 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002060 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002061 cur_arg += 2;
2062 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002063 else if (!strcmp(args[cur_arg], "weight")) {
2064 int w;
2065 w = atol(args[cur_arg + 1]);
2066 if (w < 0 || w > SRV_UWGHT_MAX) {
2067 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2068 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2069 err_code |= ERR_ALERT | ERR_FATAL;
2070 goto out;
2071 }
2072 newsrv->uweight = newsrv->iweight = w;
2073 cur_arg += 2;
2074 }
2075 else if (!strcmp(args[cur_arg], "minconn")) {
2076 newsrv->minconn = atol(args[cur_arg + 1]);
2077 cur_arg += 2;
2078 }
2079 else if (!strcmp(args[cur_arg], "maxconn")) {
2080 newsrv->maxconn = atol(args[cur_arg + 1]);
2081 cur_arg += 2;
2082 }
2083 else if (!strcmp(args[cur_arg], "maxqueue")) {
2084 newsrv->maxqueue = atol(args[cur_arg + 1]);
2085 cur_arg += 2;
2086 }
2087 else if (!strcmp(args[cur_arg], "slowstart")) {
2088 /* slowstart is stored in seconds */
2089 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2090 if (err) {
2091 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2092 file, linenum, *err, newsrv->id);
2093 err_code |= ERR_ALERT | ERR_FATAL;
2094 goto out;
2095 }
2096 newsrv->slowstart = (val + 999) / 1000;
2097 cur_arg += 2;
2098 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002099 else if (!defsrv && !strcmp(args[cur_arg], "disabled")) {
Baptiste Assmann9f5ada32015-08-08 15:49:13 +02002100 newsrv->admin |= SRV_ADMF_CMAINT;
Baptiste Assmann54a47302015-09-18 10:30:03 +02002101 newsrv->admin |= SRV_ADMF_FMAINT;
Willy Tarreau892337c2014-05-13 23:41:20 +02002102 newsrv->state = SRV_ST_STOPPED;
Willy Tarreau272adea2014-03-31 10:39:59 +02002103 newsrv->check.state |= CHK_ST_PAUSED;
2104 newsrv->check.health = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002105 cur_arg += 1;
2106 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002107 else if (!strcmp(args[cur_arg], "on-error")) {
2108 if (!strcmp(args[cur_arg + 1], "fastinter"))
2109 newsrv->onerror = HANA_ONERR_FASTINTER;
2110 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2111 newsrv->onerror = HANA_ONERR_FAILCHK;
2112 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2113 newsrv->onerror = HANA_ONERR_SUDDTH;
2114 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2115 newsrv->onerror = HANA_ONERR_MARKDWN;
2116 else {
2117 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2118 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2119 file, linenum, args[cur_arg], args[cur_arg + 1]);
2120 err_code |= ERR_ALERT | ERR_FATAL;
2121 goto out;
2122 }
2123
2124 cur_arg += 2;
2125 }
2126 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2127 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2128 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2129 else {
2130 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2131 file, linenum, args[cur_arg], args[cur_arg + 1]);
2132 err_code |= ERR_ALERT | ERR_FATAL;
2133 goto out;
2134 }
2135
2136 cur_arg += 2;
2137 }
2138 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2139 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2140 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2141 else {
2142 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2143 file, linenum, args[cur_arg], args[cur_arg + 1]);
2144 err_code |= ERR_ALERT | ERR_FATAL;
2145 goto out;
2146 }
2147
2148 cur_arg += 2;
2149 }
2150 else if (!strcmp(args[cur_arg], "error-limit")) {
2151 if (!*args[cur_arg + 1]) {
2152 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2153 file, linenum, args[cur_arg]);
2154 err_code |= ERR_ALERT | ERR_FATAL;
2155 goto out;
2156 }
2157
2158 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2159
2160 if (newsrv->consecutive_errors_limit <= 0) {
2161 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2162 file, linenum, args[cur_arg]);
2163 err_code |= ERR_ALERT | ERR_FATAL;
2164 goto out;
2165 }
2166 cur_arg += 2;
2167 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002168 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
2169 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2170 file, linenum, "usesrc", "source");
2171 err_code |= ERR_ALERT | ERR_FATAL;
2172 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002173 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002174 else {
2175 static int srv_dumped;
2176 struct srv_kw *kw;
2177 char *err;
2178
2179 kw = srv_find_kw(args[cur_arg]);
2180 if (kw) {
2181 char *err = NULL;
2182 int code;
2183
2184 if (!kw->parse) {
2185 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2186 file, linenum, args[0], args[1], args[cur_arg]);
2187 cur_arg += 1 + kw->skip ;
2188 err_code |= ERR_ALERT | ERR_FATAL;
2189 goto out;
2190 }
2191
2192 if (defsrv && !kw->default_ok) {
2193 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2194 file, linenum, args[0], args[1], args[cur_arg]);
2195 cur_arg += 1 + kw->skip ;
2196 err_code |= ERR_ALERT;
2197 continue;
2198 }
2199
2200 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2201 err_code |= code;
2202
2203 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002204 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002205 if (code & ERR_FATAL) {
2206 free(err);
2207 cur_arg += 1 + kw->skip;
2208 goto out;
2209 }
2210 }
2211 free(err);
2212 cur_arg += 1 + kw->skip;
2213 continue;
2214 }
2215
2216 err = NULL;
2217 if (!srv_dumped) {
2218 srv_dump_kws(&err);
2219 indent_msg(&err, 4);
2220 srv_dumped = 1;
2221 }
2222
2223 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2224 file, linenum, args[0], args[1], args[cur_arg],
2225 err ? " Registered keywords :" : "", err ? err : "");
2226 free(err);
2227
2228 err_code |= ERR_ALERT | ERR_FATAL;
2229 goto out;
2230 }
2231 }
2232
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01002233 /* This check is done only for 'server' instances. */
2234 if (!defsrv && newsrv->do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002235 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02002236
2237 if (newsrv->trackit) {
2238 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
2239 file, linenum);
2240 err_code |= ERR_ALERT | ERR_FATAL;
2241 goto out;
2242 }
2243
Willy Tarreau272adea2014-03-31 10:39:59 +02002244 /*
2245 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02002246 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02002247 */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002248 if ((srv_check_healthcheck_port(&newsrv->check) == 0) &&
Simon Horman41f58762015-01-30 11:22:56 +09002249 (is_inet_addr(&newsrv->check.addr) ||
2250 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01002251 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002252 struct list *l;
2253
2254 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
2255 if (!r) {
2256 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
2257 file, linenum, newsrv->id);
2258 err_code |= ERR_ALERT | ERR_FATAL;
2259 goto out;
2260 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01002261 /* search the first action (connect / send / expect) in the list */
2262 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01002263 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01002264 if (r->action != TCPCHK_ACT_COMMENT)
2265 break;
2266 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002267 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
2268 Alert("parsing [%s:%d] : server %s has neither service port nor check port nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
2269 file, linenum, newsrv->id);
2270 err_code |= ERR_ALERT | ERR_FATAL;
2271 goto out;
2272 }
2273 else {
2274 /* scan the tcp-check ruleset to ensure a port has been configured */
2275 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01002276 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002277 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
2278 Alert("parsing [%s:%d] : server %s has neither service port nor check port, and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
2279 file, linenum, newsrv->id);
2280 err_code |= ERR_ALERT | ERR_FATAL;
2281 goto out;
2282 }
2283 }
2284 }
2285 }
2286
2287 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09002288 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002289 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002290 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2291 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002292 goto out;
2293 }
2294
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002295 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01002296 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002297
Willy Tarreau272adea2014-03-31 10:39:59 +02002298 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01002299 global.maxsock++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002300 }
2301
2302 if (do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002303 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02002304
2305 if (!newsrv->agent.port) {
2306 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
2307 file, linenum, newsrv->id);
2308 err_code |= ERR_ALERT | ERR_FATAL;
2309 goto out;
2310 }
2311
2312 if (!newsrv->agent.inter)
2313 newsrv->agent.inter = newsrv->check.inter;
2314
Simon Hormanb1900d52015-01-30 11:22:54 +09002315 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02002316 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002317 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2318 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002319 goto out;
2320 }
2321
2322 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2323 }
2324
2325 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02002326 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02002327 curproxy->srv_bck++;
2328 else
2329 curproxy->srv_act++;
2330
Willy Tarreauc5150da2014-05-13 19:27:31 +02002331 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02002332 }
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002333#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2334 if (!defsrv && newsrv->sni_expr) {
2335 int code;
2336 char *err;
2337
2338 err = NULL;
2339
2340 code = server_parse_sni_expr(newsrv, curproxy, &err);
2341 err_code |= code;
2342 if (code) {
2343 display_parser_err(file, linenum, args, cur_arg, &err);
2344 free(err);
2345 if (code & ERR_FATAL)
2346 goto out;
2347 }
2348 }
2349#endif
Willy Tarreau272adea2014-03-31 10:39:59 +02002350 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002351 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002352 return 0;
2353
2354 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002355 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002356 free(errmsg);
2357 return err_code;
2358}
2359
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002360/* Returns a pointer to the first server matching either id <id>.
2361 * NULL is returned if no match is found.
2362 * the lookup is performed in the backend <bk>
2363 */
2364struct server *server_find_by_id(struct proxy *bk, int id)
2365{
2366 struct eb32_node *eb32;
2367 struct server *curserver;
2368
2369 if (!bk || (id ==0))
2370 return NULL;
2371
2372 /* <bk> has no backend capabilities, so it can't have a server */
2373 if (!(bk->cap & PR_CAP_BE))
2374 return NULL;
2375
2376 curserver = NULL;
2377
2378 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2379 if (eb32)
2380 curserver = container_of(eb32, struct server, conf.id);
2381
2382 return curserver;
2383}
2384
2385/* Returns a pointer to the first server matching either name <name>, or id
2386 * if <name> starts with a '#'. NULL is returned if no match is found.
2387 * the lookup is performed in the backend <bk>
2388 */
2389struct server *server_find_by_name(struct proxy *bk, const char *name)
2390{
2391 struct server *curserver;
2392
2393 if (!bk || !name)
2394 return NULL;
2395
2396 /* <bk> has no backend capabilities, so it can't have a server */
2397 if (!(bk->cap & PR_CAP_BE))
2398 return NULL;
2399
2400 curserver = NULL;
2401 if (*name == '#') {
2402 curserver = server_find_by_id(bk, atoi(name + 1));
2403 if (curserver)
2404 return curserver;
2405 }
2406 else {
2407 curserver = bk->srv;
2408
2409 while (curserver && (strcmp(curserver->id, name) != 0))
2410 curserver = curserver->next;
2411
2412 if (curserver)
2413 return curserver;
2414 }
2415
2416 return NULL;
2417}
2418
2419struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2420{
2421 struct server *byname;
2422 struct server *byid;
2423
2424 if (!name && !id)
2425 return NULL;
2426
2427 if (diff)
2428 *diff = 0;
2429
2430 byname = byid = NULL;
2431
2432 if (name) {
2433 byname = server_find_by_name(bk, name);
2434 if (byname && (!id || byname->puid == id))
2435 return byname;
2436 }
2437
2438 /* remaining possibilities :
2439 * - name not set
2440 * - name set but not found
2441 * - name found but ID doesn't match
2442 */
2443 if (id) {
2444 byid = server_find_by_id(bk, id);
2445 if (byid) {
2446 if (byname) {
2447 /* use id only if forced by configuration */
2448 if (byid->flags & SRV_F_FORCED_ID) {
2449 if (diff)
2450 *diff |= 2;
2451 return byid;
2452 }
2453 else {
2454 if (diff)
2455 *diff |= 1;
2456 return byname;
2457 }
2458 }
2459
2460 /* remaining possibilities:
2461 * - name not set
2462 * - name set but not found
2463 */
2464 if (name && diff)
2465 *diff |= 2;
2466 return byid;
2467 }
2468
2469 /* id bot found */
2470 if (byname) {
2471 if (diff)
2472 *diff |= 1;
2473 return byname;
2474 }
2475 }
2476
2477 return NULL;
2478}
2479
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002480/* Update a server state using the parameters available in the params list */
2481static void srv_update_state(struct server *srv, int version, char **params)
2482{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002483 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002484 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002485
2486 /* fields since version 1
2487 * and common to all other upcoming versions
2488 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002489 enum srv_state srv_op_state;
2490 enum srv_admin srv_admin_state;
2491 unsigned srv_uweight, srv_iweight;
2492 unsigned long srv_last_time_change;
2493 short srv_check_status;
2494 enum chk_result srv_check_result;
2495 int srv_check_health;
2496 int srv_check_state, srv_agent_state;
2497 int bk_f_forced_id;
2498 int srv_f_forced_id;
2499
Willy Tarreau31138fa2015-09-29 18:38:47 +02002500 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002501 switch (version) {
2502 case 1:
2503 /*
2504 * now we can proceed with server's state update:
2505 * srv_addr: params[0]
2506 * srv_op_state: params[1]
2507 * srv_admin_state: params[2]
2508 * srv_uweight: params[3]
2509 * srv_iweight: params[4]
2510 * srv_last_time_change: params[5]
2511 * srv_check_status: params[6]
2512 * srv_check_result: params[7]
2513 * srv_check_health: params[8]
2514 * srv_check_state: params[9]
2515 * srv_agent_state: params[10]
2516 * bk_f_forced_id: params[11]
2517 * srv_f_forced_id: params[12]
2518 */
2519
2520 /* validating srv_op_state */
2521 p = NULL;
2522 errno = 0;
2523 srv_op_state = strtol(params[1], &p, 10);
2524 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2525 (srv_op_state != SRV_ST_STOPPED &&
2526 srv_op_state != SRV_ST_STARTING &&
2527 srv_op_state != SRV_ST_RUNNING &&
2528 srv_op_state != SRV_ST_STOPPING)) {
2529 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2530 }
2531
2532 /* validating srv_admin_state */
2533 p = NULL;
2534 errno = 0;
2535 srv_admin_state = strtol(params[2], &p, 10);
Willy Tarreau757478e2016-11-03 19:22:19 +01002536
2537 /* inherited statuses will be recomputed later */
2538 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT;
2539
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002540 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2541 (srv_admin_state != 0 &&
2542 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002543 srv_admin_state != SRV_ADMF_CMAINT &&
2544 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002545 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002546 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002547 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2548 }
2549
2550 /* validating srv_uweight */
2551 p = NULL;
2552 errno = 0;
2553 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002554 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002555 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2556
2557 /* validating srv_iweight */
2558 p = NULL;
2559 errno = 0;
2560 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002561 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002562 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2563
2564 /* validating srv_last_time_change */
2565 p = NULL;
2566 errno = 0;
2567 srv_last_time_change = strtol(params[5], &p, 10);
2568 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2569 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2570
2571 /* validating srv_check_status */
2572 p = NULL;
2573 errno = 0;
2574 srv_check_status = strtol(params[6], &p, 10);
2575 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2576 (srv_check_status >= HCHK_STATUS_SIZE))
2577 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2578
2579 /* validating srv_check_result */
2580 p = NULL;
2581 errno = 0;
2582 srv_check_result = strtol(params[7], &p, 10);
2583 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2584 (srv_check_result != CHK_RES_UNKNOWN &&
2585 srv_check_result != CHK_RES_NEUTRAL &&
2586 srv_check_result != CHK_RES_FAILED &&
2587 srv_check_result != CHK_RES_PASSED &&
2588 srv_check_result != CHK_RES_CONDPASS)) {
2589 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2590 }
2591
2592 /* validating srv_check_health */
2593 p = NULL;
2594 errno = 0;
2595 srv_check_health = strtol(params[8], &p, 10);
2596 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2597 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2598
2599 /* validating srv_check_state */
2600 p = NULL;
2601 errno = 0;
2602 srv_check_state = strtol(params[9], &p, 10);
2603 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2604 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2605 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2606
2607 /* validating srv_agent_state */
2608 p = NULL;
2609 errno = 0;
2610 srv_agent_state = strtol(params[10], &p, 10);
2611 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2612 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2613 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2614
2615 /* validating bk_f_forced_id */
2616 p = NULL;
2617 errno = 0;
2618 bk_f_forced_id = strtol(params[11], &p, 10);
2619 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2620 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2621
2622 /* validating srv_f_forced_id */
2623 p = NULL;
2624 errno = 0;
2625 srv_f_forced_id = strtol(params[12], &p, 10);
2626 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2627 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2628
2629
2630 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002631 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002632 goto out;
2633
2634 /* recover operational state and apply it to this server
2635 * and all servers tracking this one */
2636 switch (srv_op_state) {
2637 case SRV_ST_STOPPED:
2638 srv->check.health = 0;
2639 srv_set_stopped(srv, "changed from server-state after a reload");
2640 break;
2641 case SRV_ST_STARTING:
2642 srv->state = srv_op_state;
2643 break;
2644 case SRV_ST_STOPPING:
2645 srv->check.health = srv->check.rise + srv->check.fall - 1;
2646 srv_set_stopping(srv, "changed from server-state after a reload");
2647 break;
2648 case SRV_ST_RUNNING:
2649 srv->check.health = srv->check.rise + srv->check.fall - 1;
2650 srv_set_running(srv, "");
2651 break;
2652 }
2653
2654 /* When applying server state, the following rules apply:
2655 * - in case of a configuration change, we apply the setting from the new
2656 * configuration, regardless of old running state
2657 * - if no configuration change, we apply old running state only if old running
2658 * state is different from new configuration state
2659 */
2660 /* configuration has changed */
2661 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2662 if (srv->admin & SRV_ADMF_CMAINT)
2663 srv_adm_set_maint(srv);
2664 else
2665 srv_adm_set_ready(srv);
2666 }
2667 /* configuration is the same, let's compate old running state and new conf state */
2668 else {
2669 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2670 srv_adm_set_maint(srv);
2671 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2672 srv_adm_set_ready(srv);
2673 }
2674 /* apply drain mode if server is currently enabled */
2675 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2676 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002677 * (srv->iweight is the weight set up in configuration).
2678 * There are two possible reasons for FDRAIN to have been present :
2679 * - previous config weight was zero
2680 * - "set server b/s drain" was sent to the CLI
2681 *
2682 * In the first case, we simply want to drop this drain state
2683 * if the new weight is not zero anymore, meaning the administrator
2684 * has intentionally turned the weight back to a positive value to
2685 * enable the server again after an operation. In the second case,
2686 * the drain state was forced on the CLI regardless of the config's
2687 * weight so we don't want a change to the config weight to lose this
2688 * status. What this means is :
2689 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2690 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002691 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002692 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002693 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002694 }
2695
2696 srv->last_change = date.tv_sec - srv_last_time_change;
2697 srv->check.status = srv_check_status;
2698 srv->check.result = srv_check_result;
2699 srv->check.health = srv_check_health;
2700
2701 /* Only case we want to apply is removing ENABLED flag which could have been
2702 * done by the "disable health" command over the stats socket
2703 */
2704 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2705 (srv_check_state & CHK_ST_CONFIGURED) &&
2706 !(srv_check_state & CHK_ST_ENABLED))
2707 srv->check.state &= ~CHK_ST_ENABLED;
2708
2709 /* Only case we want to apply is removing ENABLED flag which could have been
2710 * done by the "disable agent" command over the stats socket
2711 */
2712 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2713 (srv_agent_state & CHK_ST_CONFIGURED) &&
2714 !(srv_agent_state & CHK_ST_ENABLED))
2715 srv->agent.state &= ~CHK_ST_ENABLED;
2716
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002717 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2718 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002719 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002720 * It means that a configuration file change has precedence over a unix socket change
2721 * for server's weight
2722 *
2723 * by default, HAProxy applies the following weight when parsing the configuration
2724 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002725 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002726 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002727 srv->uweight = srv_uweight;
2728 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002729 server_recalc_eweight(srv);
2730
Willy Tarreaue5a60682016-11-09 14:54:53 +01002731 /* load server IP address */
2732 srv->lastaddr = strdup(params[0]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002733 break;
2734 default:
2735 chunk_appendf(msg, ", version '%d' not supported", version);
2736 }
2737
2738 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002739 if (msg->len) {
2740 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002741 Warning("server-state application failed for server '%s/%s'%s",
2742 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002743 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002744}
2745
2746/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2747 * For each proxy, it does the following:
2748 * - opens its server state file (either one or local one)
2749 * - read whole file, line by line
2750 * - analyse each line to check if it matches our current backend:
2751 * - backend name matches
2752 * - backend id matches if id is forced and name doesn't match
2753 * - if the server pointed by the line is found, then state is applied
2754 *
2755 * If the running backend uuid or id differs from the state file, then HAProxy reports
2756 * a warning.
2757 */
2758void apply_server_state(void)
2759{
2760 char *cur, *end;
2761 char mybuf[SRV_STATE_LINE_MAXLEN];
2762 int mybuflen;
2763 char *params[SRV_STATE_FILE_MAX_FIELDS];
2764 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2765 int arg, srv_arg, version, diff;
2766 FILE *f;
2767 char *filepath;
2768 char globalfilepath[MAXPATHLEN + 1];
2769 char localfilepath[MAXPATHLEN + 1];
2770 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2771 extern struct proxy *proxy;
2772 struct proxy *curproxy, *bk;
2773 struct server *srv;
2774
2775 globalfilepathlen = 0;
2776 /* create the globalfilepath variable */
2777 if (global.server_state_file) {
2778 /* absolute path or no base directory provided */
2779 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2780 len = strlen(global.server_state_file);
2781 if (len > MAXPATHLEN) {
2782 globalfilepathlen = 0;
2783 goto globalfileerror;
2784 }
2785 memcpy(globalfilepath, global.server_state_file, len);
2786 globalfilepath[len] = '\0';
2787 globalfilepathlen = len;
2788 }
2789 else if (global.server_state_base) {
2790 len = strlen(global.server_state_base);
2791 globalfilepathlen += len;
2792
2793 if (globalfilepathlen > MAXPATHLEN) {
2794 globalfilepathlen = 0;
2795 goto globalfileerror;
2796 }
2797 strncpy(globalfilepath, global.server_state_base, len);
2798 globalfilepath[globalfilepathlen] = 0;
2799
2800 /* append a slash if needed */
2801 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2802 if (globalfilepathlen + 1 > MAXPATHLEN) {
2803 globalfilepathlen = 0;
2804 goto globalfileerror;
2805 }
2806 globalfilepath[globalfilepathlen++] = '/';
2807 }
2808
2809 len = strlen(global.server_state_file);
2810 if (globalfilepathlen + len > MAXPATHLEN) {
2811 globalfilepathlen = 0;
2812 goto globalfileerror;
2813 }
2814 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2815 globalfilepathlen += len;
2816 globalfilepath[globalfilepathlen++] = 0;
2817 }
2818 }
2819 globalfileerror:
2820 if (globalfilepathlen == 0)
2821 globalfilepath[0] = '\0';
2822
2823 /* read servers state from local file */
2824 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2825 /* servers are only in backends */
2826 if (!(curproxy->cap & PR_CAP_BE))
2827 continue;
2828 fileopenerr = 0;
2829 filepath = NULL;
2830
2831 /* search server state file path and name */
2832 switch (curproxy->load_server_state_from_file) {
2833 /* read servers state from global file */
2834 case PR_SRV_STATE_FILE_GLOBAL:
2835 /* there was an error while generating global server state file path */
2836 if (globalfilepathlen == 0)
2837 continue;
2838 filepath = globalfilepath;
2839 fileopenerr = 1;
2840 break;
2841 /* this backend has its own file */
2842 case PR_SRV_STATE_FILE_LOCAL:
2843 localfilepathlen = 0;
2844 localfilepath[0] = '\0';
2845 len = 0;
2846 /* create the localfilepath variable */
2847 /* absolute path or no base directory provided */
2848 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2849 len = strlen(curproxy->server_state_file_name);
2850 if (len > MAXPATHLEN) {
2851 localfilepathlen = 0;
2852 goto localfileerror;
2853 }
2854 memcpy(localfilepath, curproxy->server_state_file_name, len);
2855 localfilepath[len] = '\0';
2856 localfilepathlen = len;
2857 }
2858 else if (global.server_state_base) {
2859 len = strlen(global.server_state_base);
2860 localfilepathlen += len;
2861
2862 if (localfilepathlen > MAXPATHLEN) {
2863 localfilepathlen = 0;
2864 goto localfileerror;
2865 }
2866 strncpy(localfilepath, global.server_state_base, len);
2867 localfilepath[localfilepathlen] = 0;
2868
2869 /* append a slash if needed */
2870 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2871 if (localfilepathlen + 1 > MAXPATHLEN) {
2872 localfilepathlen = 0;
2873 goto localfileerror;
2874 }
2875 localfilepath[localfilepathlen++] = '/';
2876 }
2877
2878 len = strlen(curproxy->server_state_file_name);
2879 if (localfilepathlen + len > MAXPATHLEN) {
2880 localfilepathlen = 0;
2881 goto localfileerror;
2882 }
2883 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2884 localfilepathlen += len;
2885 localfilepath[localfilepathlen++] = 0;
2886 }
2887 filepath = localfilepath;
2888 localfileerror:
2889 if (localfilepathlen == 0)
2890 localfilepath[0] = '\0';
2891
2892 break;
2893 case PR_SRV_STATE_FILE_NONE:
2894 default:
2895 continue;
2896 }
2897
2898 /* preload global state file */
2899 errno = 0;
2900 f = fopen(filepath, "r");
2901 if (errno && fileopenerr)
2902 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2903 if (!f)
2904 continue;
2905
2906 mybuf[0] = '\0';
2907 mybuflen = 0;
2908 version = 0;
2909
2910 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002911 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2912 Warning("Can't read first line of the server state file '%s'\n", filepath);
2913 goto fileclose;
2914 }
2915
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002916 cur = mybuf;
2917 version = atoi(cur);
2918 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2919 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002920 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002921
2922 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2923 int bk_f_forced_id = 0;
2924 int check_id = 0;
2925 int check_name = 0;
2926
2927 mybuflen = strlen(mybuf);
2928 cur = mybuf;
2929 end = cur + mybuflen;
2930
2931 bk = NULL;
2932 srv = NULL;
2933
2934 /* we need at least one character */
2935 if (mybuflen == 0)
2936 continue;
2937
2938 /* ignore blank characters at the beginning of the line */
2939 while (isspace(*cur))
2940 ++cur;
2941
2942 if (cur == end)
2943 continue;
2944
2945 /* ignore comment line */
2946 if (*cur == '#')
2947 continue;
2948
2949 /* we're now ready to move the line into *srv_params[] */
2950 params[0] = cur;
2951 arg = 1;
2952 srv_arg = 0;
2953 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2954 if (isspace(*cur)) {
2955 *cur = '\0';
2956 ++cur;
2957 while (isspace(*cur))
2958 ++cur;
2959 switch (version) {
2960 case 1:
2961 /*
2962 * srv_addr: params[4] => srv_params[0]
2963 * srv_op_state: params[5] => srv_params[1]
2964 * srv_admin_state: params[6] => srv_params[2]
2965 * srv_uweight: params[7] => srv_params[3]
2966 * srv_iweight: params[8] => srv_params[4]
2967 * srv_last_time_change: params[9] => srv_params[5]
2968 * srv_check_status: params[10] => srv_params[6]
2969 * srv_check_result: params[11] => srv_params[7]
2970 * srv_check_health: params[12] => srv_params[8]
2971 * srv_check_state: params[13] => srv_params[9]
2972 * srv_agent_state: params[14] => srv_params[10]
2973 * bk_f_forced_id: params[15] => srv_params[11]
2974 * srv_f_forced_id: params[16] => srv_params[12]
2975 */
2976 if (arg >= 4) {
2977 srv_params[srv_arg] = cur;
2978 ++srv_arg;
2979 }
2980 break;
2981 }
2982
2983 params[arg] = cur;
2984 ++arg;
2985 }
2986 else {
2987 ++cur;
2988 }
2989 }
2990
2991 /* if line is incomplete line, then ignore it.
2992 * otherwise, update useful flags */
2993 switch (version) {
2994 case 1:
2995 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
2996 continue;
2997 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
2998 check_id = (atoi(params[0]) == curproxy->uuid);
2999 check_name = (strcmp(curproxy->id, params[1]) == 0);
3000 break;
3001 }
3002
3003 diff = 0;
3004 bk = curproxy;
3005
3006 /* if backend can't be found, let's continue */
3007 if (!check_id && !check_name)
3008 continue;
3009 else if (!check_id && check_name) {
3010 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3011 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3012 }
3013 else if (check_id && !check_name) {
3014 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3015 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3016 /* if name doesn't match, we still want to update curproxy if the backend id
3017 * was forced in previous the previous configuration */
3018 if (!bk_f_forced_id)
3019 continue;
3020 }
3021
3022 /* look for the server by its id: param[2] */
3023 /* else look for the server by its name: param[3] */
3024 diff = 0;
3025 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3026
3027 if (!srv) {
3028 /* if no server found, then warning and continue with next line */
3029 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3030 params[3], params[2], params[0], params[1]);
3031 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3032 params[3], params[2], params[0], params[1]);
3033 continue;
3034 }
3035 else if (diff & PR_FBM_MISMATCH_ID) {
3036 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);
3037 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);
3038 }
3039 else if (diff & PR_FBM_MISMATCH_NAME) {
3040 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);
3041 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);
3042 }
3043
3044 /* now we can proceed with server's state update */
3045 srv_update_state(srv, version, srv_params);
3046 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003047fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003048 fclose(f);
3049 }
3050}
3051
Simon Horman7d09b9a2013-02-12 10:45:51 +09003052/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003053 * update a server's current IP address.
3054 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3055 * ip is in network format.
3056 * updater is a string which contains an information about the requester of the update.
3057 * updater is used if not NULL.
3058 *
3059 * A log line and a stderr warning message is generated based on server's backend options.
3060 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003061int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003062{
3063 /* generates a log line and a warning on stderr */
3064 if (1) {
3065 /* book enough space for both IPv4 and IPv6 */
3066 char oldip[INET6_ADDRSTRLEN];
3067 char newip[INET6_ADDRSTRLEN];
3068
3069 memset(oldip, '\0', INET6_ADDRSTRLEN);
3070 memset(newip, '\0', INET6_ADDRSTRLEN);
3071
3072 /* copy old IP address in a string */
3073 switch (s->addr.ss_family) {
3074 case AF_INET:
3075 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3076 break;
3077 case AF_INET6:
3078 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3079 break;
3080 };
3081
3082 /* copy new IP address in a string */
3083 switch (ip_sin_family) {
3084 case AF_INET:
3085 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3086 break;
3087 case AF_INET6:
3088 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3089 break;
3090 };
3091
3092 /* save log line into a buffer */
3093 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3094 s->proxy->id, s->id, oldip, newip, updater);
3095
3096 /* write the buffer on stderr */
3097 Warning("%s.\n", trash.str);
3098
3099 /* send a log */
3100 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3101 }
3102
3103 /* save the new IP family */
3104 s->addr.ss_family = ip_sin_family;
3105 /* save the new IP address */
3106 switch (ip_sin_family) {
3107 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003108 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003109 break;
3110 case AF_INET6:
3111 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3112 break;
3113 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003114 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003115
3116 return 0;
3117}
3118
3119/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003120 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3121 *
3122 * Caller can pass its name through <updater> to get it integrated in the response message
3123 * returned by the function.
3124 *
3125 * The function first does the following, in that order:
3126 * - validates the new addr and/or port
3127 * - checks if an update is required (new IP or port is different than current ones)
3128 * - checks the update is allowed:
3129 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3130 * - allow all changes if no CHECKS are configured
3131 * - if CHECK is configured:
3132 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3133 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3134 * conditions are met
3135 */
3136const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3137{
3138 struct sockaddr_storage sa;
3139 int ret, port_change_required;
3140 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003141 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003142 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003143 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003144
3145 msg = get_trash_chunk();
3146 chunk_reset(msg);
3147
3148 if (addr) {
3149 memset(&sa, 0, sizeof(struct sockaddr_storage));
3150 if (str2ip2(addr, &sa, 0) == NULL) {
3151 chunk_printf(msg, "Invalid addr '%s'", addr);
3152 goto out;
3153 }
3154
3155 /* changes are allowed on AF_INET* families only */
3156 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3157 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3158 goto out;
3159 }
3160
3161 /* collecting data currently setup */
3162 memset(current_addr, '\0', sizeof(current_addr));
3163 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3164 /* changes are allowed on AF_INET* families only */
3165 if ((ret != AF_INET) && (ret != AF_INET6)) {
3166 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3167 goto out;
3168 }
3169
3170 /* applying ADDR changes if required and allowed
3171 * ipcmp returns 0 when both ADDR are the same
3172 */
3173 if (ipcmp(&s->addr, &sa) == 0) {
3174 chunk_appendf(msg, "no need to change the addr");
3175 goto port;
3176 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003177 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003178 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003179
3180 /* we also need to update check's ADDR only if it uses the server's one */
3181 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003182 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003183 }
3184
3185 /* we also need to update agent ADDR only if it use the server's one */
3186 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003187 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003188 }
3189
3190 /* update report for caller */
3191 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3192 }
3193
3194 port:
3195 if (port) {
3196 char sign = '\0';
3197 char *endptr;
3198
3199 if (addr)
3200 chunk_appendf(msg, ", ");
3201
3202 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003203 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003204
3205 /* check if PORT change is required */
3206 port_change_required = 0;
3207
3208 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003209 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003210 new_port = strtol(port, &endptr, 10);
3211 if ((errno != 0) || (port == endptr)) {
3212 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3213 goto out;
3214 }
3215
3216 /* check if caller triggers a port mapped or offset */
3217 if (sign == '-' || (sign == '+')) {
3218 /* check if server currently uses port map */
3219 if (!(s->flags & SRV_F_MAPPORTS)) {
3220 /* switch from fixed port to port map mandatorily triggers
3221 * a port change */
3222 port_change_required = 1;
3223 /* check is configured
3224 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3225 * prevent PORT change if check doesn't have it's dedicated port while switching
3226 * to port mapping */
3227 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3228 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.");
3229 goto out;
3230 }
3231 }
3232 /* we're already using port maps */
3233 else {
3234 port_change_required = current_port != new_port;
3235 }
3236 }
3237 /* fixed port */
3238 else {
3239 port_change_required = current_port != new_port;
3240 }
3241
3242 /* applying PORT changes if required and update response message */
3243 if (port_change_required) {
3244 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003245 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003246 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003247
3248 /* prepare message */
3249 chunk_appendf(msg, "port changed from '");
3250 if (s->flags & SRV_F_MAPPORTS)
3251 chunk_appendf(msg, "+");
3252 chunk_appendf(msg, "%d' to '", current_port);
3253
3254 if (sign == '-') {
3255 s->flags |= SRV_F_MAPPORTS;
3256 chunk_appendf(msg, "%c", sign);
3257 /* just use for result output */
3258 new_port = -new_port;
3259 }
3260 else if (sign == '+') {
3261 s->flags |= SRV_F_MAPPORTS;
3262 chunk_appendf(msg, "%c", sign);
3263 }
3264 else {
3265 s->flags &= ~SRV_F_MAPPORTS;
3266 }
3267
3268 chunk_appendf(msg, "%d'", new_port);
3269
3270 /* we also need to update health checks port only if it uses server's realport */
3271 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3272 s->check.port = new_port;
3273 }
3274 }
3275 else {
3276 chunk_appendf(msg, "no need to change the port");
3277 }
3278 }
3279
3280out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003281 if (changed)
3282 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003283 if (updater)
3284 chunk_appendf(msg, " by '%s'", updater);
3285 chunk_appendf(msg, "\n");
3286 return msg->str;
3287}
3288
3289
3290/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003291 * update server status based on result of name resolution
3292 * returns:
3293 * 0 if server status is updated
3294 * 1 if server status has not changed
3295 */
3296int snr_update_srv_status(struct server *s)
3297{
3298 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003299 struct dns_resolvers *resolvers;
3300
3301 resolvers = resolution->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003302
3303 switch (resolution->status) {
3304 case RSLV_STATUS_NONE:
3305 /* status when HAProxy has just (re)started */
3306 trigger_resolution(s);
3307 break;
3308
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003309 case RSLV_STATUS_VALID:
3310 /*
3311 * resume health checks
3312 * server will be turned back on if health check is safe
3313 */
3314 if (!(s->admin & SRV_ADMF_RMAINT))
3315 return 1;
3316 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3317 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3318 s->proxy->id, s->id);
3319
3320 Warning("%s.\n", trash.str);
3321 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3322 return 0;
3323
3324 case RSLV_STATUS_NX:
3325 /* stop server if resolution is NX for a long enough period */
3326 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
3327 if (s->admin & SRV_ADMF_RMAINT)
3328 return 1;
3329 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3330 return 0;
3331 }
3332 break;
3333
3334 case RSLV_STATUS_TIMEOUT:
3335 /* stop server if resolution is TIMEOUT for a long enough period */
3336 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
3337 if (s->admin & SRV_ADMF_RMAINT)
3338 return 1;
3339 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3340 return 0;
3341 }
3342 break;
3343
3344 case RSLV_STATUS_REFUSED:
3345 /* stop server if resolution is REFUSED for a long enough period */
3346 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
3347 if (s->admin & SRV_ADMF_RMAINT)
3348 return 1;
3349 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3350 return 0;
3351 }
3352 break;
3353
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003354 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003355 /* stop server if resolution is in unmatched error for a long enough period */
3356 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
3357 if (s->admin & SRV_ADMF_RMAINT)
3358 return 1;
3359 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3360 return 0;
3361 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003362 break;
3363 }
3364
3365 return 1;
3366}
3367
3368/*
3369 * Server Name Resolution valid response callback
3370 * It expects:
3371 * - <nameserver>: the name server which answered the valid response
3372 * - <response>: buffer containing a valid DNS response
3373 * - <response_len>: size of <response>
3374 * It performs the following actions:
3375 * - ignore response if current ip found and server family not met
3376 * - update with first new ip found if family is met and current IP is not found
3377 * returns:
3378 * 0 on error
3379 * 1 when no error or safe ignore
3380 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003381int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003382{
3383 struct server *s;
3384 void *serverip, *firstip;
3385 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003386 int ret;
3387 struct chunk *chk = get_trash_chunk();
3388
3389 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003390 firstip = NULL; /* pointer to the first valid response found */
3391 /* it will be used as the new IP if a change is required */
3392 firstip_sin_family = AF_UNSPEC;
3393 serverip = NULL; /* current server IP address */
3394
3395 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003396 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003397
3398 /* initializing server IP pointer */
3399 server_sin_family = s->addr.ss_family;
3400 switch (server_sin_family) {
3401 case AF_INET:
3402 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3403 break;
3404
3405 case AF_INET6:
3406 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3407 break;
3408
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003409 case AF_UNSPEC:
3410 break;
3411
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003412 default:
3413 goto invalid;
3414 }
3415
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003416 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01003417 serverip, server_sin_family, &firstip,
3418 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003419
3420 switch (ret) {
3421 case DNS_UPD_NO:
3422 if (resolution->status != RSLV_STATUS_VALID) {
3423 resolution->status = RSLV_STATUS_VALID;
3424 resolution->last_status_change = now_ms;
3425 }
3426 goto stop_resolution;
3427
3428 case DNS_UPD_SRVIP_NOT_FOUND:
3429 goto save_ip;
3430
3431 case DNS_UPD_CNAME:
3432 if (resolution->status != RSLV_STATUS_VALID) {
3433 resolution->status = RSLV_STATUS_VALID;
3434 resolution->last_status_change = now_ms;
3435 }
3436 goto invalid;
3437
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003438 case DNS_UPD_NO_IP_FOUND:
3439 if (resolution->status != RSLV_STATUS_OTHER) {
3440 resolution->status = RSLV_STATUS_OTHER;
3441 resolution->last_status_change = now_ms;
3442 }
3443 goto stop_resolution;
3444
Baptiste Assmannfad03182015-10-28 02:03:32 +01003445 case DNS_UPD_NAME_ERROR:
3446 /* if this is not the last expected response, we ignore it */
3447 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
3448 return 0;
3449 /* update resolution status to OTHER error type */
3450 if (resolution->status != RSLV_STATUS_OTHER) {
3451 resolution->status = RSLV_STATUS_OTHER;
3452 resolution->last_status_change = now_ms;
3453 }
3454 goto stop_resolution;
3455
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003456 default:
3457 goto invalid;
3458
3459 }
3460
3461 save_ip:
3462 nameserver->counters.update += 1;
3463 if (resolution->status != RSLV_STATUS_VALID) {
3464 resolution->status = RSLV_STATUS_VALID;
3465 resolution->last_status_change = now_ms;
3466 }
3467
3468 /* save the first ip we found */
3469 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3470 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3471
3472 stop_resolution:
3473 /* update last resolution date and time */
3474 resolution->last_resolution = now_ms;
3475 /* reset current status flag */
3476 resolution->step = RSLV_STEP_NONE;
3477 /* reset values */
3478 dns_reset_resolution(resolution);
3479
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003480 dns_update_resolvers_timeout(nameserver->resolvers);
3481
3482 snr_update_srv_status(s);
3483 return 0;
3484
3485 invalid:
3486 nameserver->counters.invalid += 1;
3487 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3488 goto stop_resolution;
3489
3490 snr_update_srv_status(s);
3491 return 0;
3492}
3493
3494/*
3495 * Server Name Resolution error management callback
3496 * returns:
3497 * 0 on error
3498 * 1 when no error or safe ignore
3499 */
3500int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
3501{
3502 struct server *s;
3503 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003504 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003505
3506 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003507 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003508 resolvers = resolution->resolvers;
3509
3510 /* can be ignored if this is not the last response */
3511 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3512 return 1;
3513 }
3514
3515 switch (error_code) {
3516 case DNS_RESP_INVALID:
3517 case DNS_RESP_WRONG_NAME:
3518 if (resolution->status != RSLV_STATUS_INVALID) {
3519 resolution->status = RSLV_STATUS_INVALID;
3520 resolution->last_status_change = now_ms;
3521 }
3522 break;
3523
3524 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02003525 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003526 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02003527 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02003528 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01003529 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
3530 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02003531
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003532 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003533 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003534 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003535 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02003536 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003537 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02003538 }
3539 else if (res_preferred_afinet) {
3540 /* fallback from A to AAAA */
3541 resolution->query_type = DNS_RTYPE_AAAA;
3542 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003543 else {
3544 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01003545 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003546 resolution->query_type = DNS_RTYPE_A;
3547 } else {
3548 resolution->query_type = DNS_RTYPE_AAAA;
3549 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003550 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003551
3552 dns_send_query(resolution);
3553
3554 /*
3555 * move the resolution to the last element of the FIFO queue
3556 * and update timeout wakeup based on the new first entry
3557 */
3558 if (dns_check_resolution_queue(resolvers) > 1) {
3559 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02003560 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003561 /* ex first resolution goes to the end of the queue */
3562 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
3563 }
3564 dns_update_resolvers_timeout(resolvers);
3565 goto leave;
3566 }
3567 else {
3568 if (resolution->status != RSLV_STATUS_OTHER) {
3569 resolution->status = RSLV_STATUS_OTHER;
3570 resolution->last_status_change = now_ms;
3571 }
3572 }
3573 break;
3574
3575 case DNS_RESP_NX_DOMAIN:
3576 if (resolution->status != RSLV_STATUS_NX) {
3577 resolution->status = RSLV_STATUS_NX;
3578 resolution->last_status_change = now_ms;
3579 }
3580 break;
3581
3582 case DNS_RESP_REFUSED:
3583 if (resolution->status != RSLV_STATUS_REFUSED) {
3584 resolution->status = RSLV_STATUS_REFUSED;
3585 resolution->last_status_change = now_ms;
3586 }
3587 break;
3588
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003589 case DNS_RESP_TIMEOUT:
3590 if (resolution->status != RSLV_STATUS_TIMEOUT) {
3591 resolution->status = RSLV_STATUS_TIMEOUT;
3592 resolution->last_status_change = now_ms;
3593 }
3594 break;
3595 }
3596
3597 /* update last resolution date and time */
3598 resolution->last_resolution = now_ms;
3599 /* reset current status flag */
3600 resolution->step = RSLV_STEP_NONE;
3601 /* reset values */
3602 dns_reset_resolution(resolution);
3603
3604 LIST_DEL(&resolution->list);
3605 dns_update_resolvers_timeout(resolvers);
3606
3607 leave:
3608 snr_update_srv_status(s);
3609 return 1;
3610}
3611
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003612/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3613 * resolver. This is suited for initial address configuration. Returns 0 on
3614 * success otherwise a non-zero error code. In case of error, *err_code, if
3615 * not NULL, is filled up.
3616 */
3617int srv_set_addr_via_libc(struct server *srv, int *err_code)
3618{
3619 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003620 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003621 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003622 return 1;
3623 }
3624 return 0;
3625}
3626
3627/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3628 * from the state file. This is suited for initial address configuration.
3629 * Returns 0 on success otherwise a non-zero error code. In case of error,
3630 * *err_code, if not NULL, is filled up.
3631 */
3632static int srv_apply_lastaddr(struct server *srv, int *err_code)
3633{
3634 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3635 if (err_code)
3636 *err_code |= ERR_WARN;
3637 return 1;
3638 }
3639 return 0;
3640}
3641
Willy Tarreau25e51522016-11-04 15:10:17 +01003642/* returns 0 if no error, otherwise a combination of ERR_* flags */
3643static int srv_iterate_initaddr(struct server *srv)
3644{
3645 int return_code = 0;
3646 int err_code;
3647 unsigned int methods;
3648
3649 methods = srv->init_addr_methods;
3650 if (!methods) { // default to "last,libc"
3651 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3652 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3653 }
3654
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003655 /* "-dr" : always append "none" so that server addresses resolution
3656 * failures are silently ignored, this is convenient to validate some
3657 * configs out of their environment.
3658 */
3659 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3660 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3661
Willy Tarreau25e51522016-11-04 15:10:17 +01003662 while (methods) {
3663 err_code = 0;
3664 switch (srv_get_next_initaddr(&methods)) {
3665 case SRV_IADDR_LAST:
3666 if (!srv->lastaddr)
3667 continue;
3668 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003669 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003670 return_code |= err_code;
3671 break;
3672
3673 case SRV_IADDR_LIBC:
3674 if (!srv->hostname)
3675 continue;
3676 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003677 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003678 return_code |= err_code;
3679 break;
3680
Willy Tarreau37ebe122016-11-04 15:17:58 +01003681 case SRV_IADDR_NONE:
3682 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003683 if (return_code) {
3684 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3685 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3686 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003687 return return_code;
3688
Willy Tarreau4310d362016-11-02 15:05:56 +01003689 case SRV_IADDR_IP:
3690 ipcpy(&srv->init_addr, &srv->addr);
3691 if (return_code) {
3692 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3693 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3694 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003695 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003696
Willy Tarreau25e51522016-11-04 15:10:17 +01003697 default: /* unhandled method */
3698 break;
3699 }
3700 }
3701
3702 if (!return_code) {
3703 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3704 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3705 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003706 else {
3707 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3708 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3709 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003710
3711 return_code |= ERR_ALERT | ERR_FATAL;
3712 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003713out:
3714 srv_set_dyncookie(srv);
3715 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003716}
3717
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003718/*
3719 * This function parses all backends and all servers within each backend
3720 * and performs servers' addr resolution based on information provided by:
3721 * - configuration file
3722 * - server-state file (states provided by an 'old' haproxy process)
3723 *
3724 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3725 */
3726int srv_init_addr(void)
3727{
3728 struct proxy *curproxy;
3729 int return_code = 0;
3730
3731 curproxy = proxy;
3732 while (curproxy) {
3733 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003734
3735 /* servers are in backend only */
3736 if (!(curproxy->cap & PR_CAP_BE))
3737 goto srv_init_addr_next;
3738
Willy Tarreau25e51522016-11-04 15:10:17 +01003739 for (srv = curproxy->srv; srv; srv = srv->next)
3740 if (srv->hostname)
3741 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003742
3743 srv_init_addr_next:
3744 curproxy = curproxy->next;
3745 }
3746
3747 return return_code;
3748}
3749
Willy Tarreau21b069d2016-11-23 17:15:08 +01003750/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3751 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003752 * 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 +01003753 * used for CLI commands requiring a server name.
3754 * Important: the <arg> is modified to remove the '/'.
3755 */
3756struct server *cli_find_server(struct appctx *appctx, char *arg)
3757{
3758 struct proxy *px;
3759 struct server *sv;
3760 char *line;
3761
3762 /* split "backend/server" and make <line> point to server */
3763 for (line = arg; *line; line++)
3764 if (*line == '/') {
3765 *line++ = '\0';
3766 break;
3767 }
3768
3769 if (!*line || !*arg) {
3770 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003771 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003772 return NULL;
3773 }
3774
3775 if (!get_backend_server(arg, line, &px, &sv)) {
3776 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003777 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003778 return NULL;
3779 }
3780
3781 if (px->state == PR_STSTOPPED) {
3782 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003783 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003784 return NULL;
3785 }
3786
3787 return sv;
3788}
3789
William Lallemand222baf22016-11-19 02:00:33 +01003790
3791static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
3792{
3793 struct server *sv;
3794 const char *warning;
3795
3796 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3797 return 1;
3798
3799 sv = cli_find_server(appctx, args[2]);
3800 if (!sv)
3801 return 1;
3802
3803 if (strcmp(args[3], "weight") == 0) {
3804 warning = server_parse_weight_change_request(sv, args[4]);
3805 if (warning) {
3806 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003807 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003808 }
3809 }
3810 else if (strcmp(args[3], "state") == 0) {
3811 if (strcmp(args[4], "ready") == 0)
3812 srv_adm_set_ready(sv);
3813 else if (strcmp(args[4], "drain") == 0)
3814 srv_adm_set_drain(sv);
3815 else if (strcmp(args[4], "maint") == 0)
3816 srv_adm_set_maint(sv);
3817 else {
3818 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003819 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003820 }
3821 }
3822 else if (strcmp(args[3], "health") == 0) {
3823 if (sv->track) {
3824 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003825 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003826 }
3827 else if (strcmp(args[4], "up") == 0) {
3828 sv->check.health = sv->check.rise + sv->check.fall - 1;
3829 srv_set_running(sv, "changed from CLI");
3830 }
3831 else if (strcmp(args[4], "stopping") == 0) {
3832 sv->check.health = sv->check.rise + sv->check.fall - 1;
3833 srv_set_stopping(sv, "changed from CLI");
3834 }
3835 else if (strcmp(args[4], "down") == 0) {
3836 sv->check.health = 0;
3837 srv_set_stopped(sv, "changed from CLI");
3838 }
3839 else {
3840 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003841 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003842 }
3843 }
3844 else if (strcmp(args[3], "agent") == 0) {
3845 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3846 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003847 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003848 }
3849 else if (strcmp(args[4], "up") == 0) {
3850 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
3851 srv_set_running(sv, "changed from CLI");
3852 }
3853 else if (strcmp(args[4], "down") == 0) {
3854 sv->agent.health = 0;
3855 srv_set_stopped(sv, "changed from CLI");
3856 }
3857 else {
3858 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003859 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003860 }
3861 }
Misiek2da082d2017-01-09 09:40:42 +01003862 else if (strcmp(args[3], "agent-addr") == 0) {
3863 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3864 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3865 appctx->st0 = CLI_ST_PRINT;
3866 } else {
3867 if (str2ip(args[4], &sv->agent.addr) == NULL) {
3868 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
3869 appctx->st0 = CLI_ST_PRINT;
3870 }
3871 }
3872 }
3873 else if (strcmp(args[3], "agent-send") == 0) {
3874 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3875 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3876 appctx->st0 = CLI_ST_PRINT;
3877 } else {
3878 char *nss = strdup(args[4]);
3879 if (!nss) {
3880 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
3881 appctx->st0 = CLI_ST_PRINT;
3882 } else {
3883 free(sv->agent.send_string);
3884 sv->agent.send_string = nss;
3885 sv->agent.send_string_len = strlen(args[4]);
3886 }
3887 }
3888 }
William Lallemand222baf22016-11-19 02:00:33 +01003889 else if (strcmp(args[3], "check-port") == 0) {
3890 int i = 0;
3891 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
3892 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003893 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003894 }
3895 if ((i < 0) || (i > 65535)) {
3896 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003897 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003898 }
3899 /* prevent the update of port to 0 if MAPPORTS are in use */
3900 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
3901 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003902 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003903 return 1;
3904 }
3905 sv->check.port = i;
3906 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003907 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003908 }
3909 else if (strcmp(args[3], "addr") == 0) {
3910 char *addr = NULL;
3911 char *port = NULL;
3912 if (strlen(args[4]) == 0) {
3913 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003914 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003915 return 1;
3916 }
3917 else {
3918 addr = args[4];
3919 }
3920 if (strcmp(args[5], "port") == 0) {
3921 port = args[6];
3922 }
3923 warning = update_server_addr_port(sv, addr, port, "stats socket command");
3924 if (warning) {
3925 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003926 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003927 }
3928 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
3929 }
3930 else {
3931 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight', 'addr' and 'check-port'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003932 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003933 }
3934 return 1;
3935}
3936
William Lallemand6b160942016-11-22 12:34:35 +01003937static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
3938{
3939 struct stream_interface *si = appctx->owner;
3940 struct proxy *px;
3941 struct server *sv;
3942 char *line;
3943
3944
3945 /* split "backend/server" and make <line> point to server */
3946 for (line = args[2]; *line; line++)
3947 if (*line == '/') {
3948 *line++ = '\0';
3949 break;
3950 }
3951
3952 if (!*line) {
3953 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003954 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003955 return 1;
3956 }
3957
3958 if (!get_backend_server(args[2], line, &px, &sv)) {
3959 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003960 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003961 return 1;
3962 }
3963
3964 /* return server's effective weight at the moment */
3965 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003966 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01003967 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01003968 return 0;
3969 }
William Lallemand6b160942016-11-22 12:34:35 +01003970 return 1;
3971}
3972
3973static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
3974{
3975 struct server *sv;
3976 const char *warning;
3977
3978 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3979 return 1;
3980
3981 sv = cli_find_server(appctx, args[2]);
3982 if (!sv)
3983 return 1;
3984
3985 warning = server_parse_weight_change_request(sv, args[3]);
3986 if (warning) {
3987 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003988 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003989 }
3990 return 1;
3991}
3992
Willy Tarreaub8026272016-11-23 11:26:56 +01003993/* parse a "set maxconn server" command. It always returns 1. */
3994static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
3995{
3996 struct server *sv;
3997 const char *warning;
3998
3999 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4000 return 1;
4001
4002 sv = cli_find_server(appctx, args[3]);
4003 if (!sv)
4004 return 1;
4005
4006 warning = server_parse_maxconn_change_request(sv, args[4]);
4007 if (warning) {
4008 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004009 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004010 }
4011 return 1;
4012}
William Lallemand6b160942016-11-22 12:34:35 +01004013
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004014/* parse a "disable agent" command. It always returns 1. */
4015static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4016{
4017 struct server *sv;
4018
4019 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4020 return 1;
4021
4022 sv = cli_find_server(appctx, args[2]);
4023 if (!sv)
4024 return 1;
4025
4026 sv->agent.state &= ~CHK_ST_ENABLED;
4027 return 1;
4028}
4029
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004030/* parse a "disable health" command. It always returns 1. */
4031static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4032{
4033 struct server *sv;
4034
4035 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4036 return 1;
4037
4038 sv = cli_find_server(appctx, args[2]);
4039 if (!sv)
4040 return 1;
4041
4042 sv->check.state &= ~CHK_ST_ENABLED;
4043 return 1;
4044}
4045
Willy Tarreauffb4d582016-11-24 12:47:00 +01004046/* parse a "disable server" command. It always returns 1. */
4047static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4048{
4049 struct server *sv;
4050
4051 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4052 return 1;
4053
4054 sv = cli_find_server(appctx, args[2]);
4055 if (!sv)
4056 return 1;
4057
4058 srv_adm_set_maint(sv);
4059 return 1;
4060}
4061
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004062/* parse a "enable agent" command. It always returns 1. */
4063static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4064{
4065 struct server *sv;
4066
4067 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4068 return 1;
4069
4070 sv = cli_find_server(appctx, args[2]);
4071 if (!sv)
4072 return 1;
4073
4074 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
4075 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004076 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004077 return 1;
4078 }
4079
4080 sv->agent.state |= CHK_ST_ENABLED;
4081 return 1;
4082}
4083
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004084/* parse a "enable health" command. It always returns 1. */
4085static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4086{
4087 struct server *sv;
4088
4089 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4090 return 1;
4091
4092 sv = cli_find_server(appctx, args[2]);
4093 if (!sv)
4094 return 1;
4095
4096 sv->check.state |= CHK_ST_ENABLED;
4097 return 1;
4098}
4099
Willy Tarreauffb4d582016-11-24 12:47:00 +01004100/* parse a "enable server" command. It always returns 1. */
4101static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4102{
4103 struct server *sv;
4104
4105 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4106 return 1;
4107
4108 sv = cli_find_server(appctx, args[2]);
4109 if (!sv)
4110 return 1;
4111
4112 srv_adm_set_ready(sv);
4113 return 1;
4114}
4115
William Lallemand222baf22016-11-19 02:00:33 +01004116/* register cli keywords */
4117static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004118 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004119 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004120 { { "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 +01004121 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004122 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004123 { { "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 +01004124 { { "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 +01004125 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004126 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4127 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4128
William Lallemand222baf22016-11-19 02:00:33 +01004129 {{},}
4130}};
4131
4132__attribute__((constructor))
4133static void __server_init(void)
4134{
4135 cli_register_kw(&cli_kws);
4136}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004137
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004138/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004139 * Local variables:
4140 * c-indent-level: 8
4141 * c-basic-offset: 8
4142 * End:
4143 */