blob: 0795e25696585238495cc012392aec78fa4835c3 [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écaille6e0843c2017-03-21 16:39:15 +0100265/* Parse the "agent-check" server keyword */
266static int srv_parse_agent_check(char **args, int *cur_arg,
267 struct proxy *curproxy, struct server *newsrv, char **err)
268{
269 newsrv->do_agent = 1;
270 return 0;
271}
272
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100273/* Parse the "backup" server keyword */
274static int srv_parse_backup(char **args, int *cur_arg,
275 struct proxy *curproxy, struct server *newsrv, char **err)
276{
277 newsrv->flags |= SRV_F_BACKUP;
278 return 0;
279}
280
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100281/* Parse the "check" server keyword */
282static int srv_parse_check(char **args, int *cur_arg,
283 struct proxy *curproxy, struct server *newsrv, char **err)
284{
285 newsrv->do_check = 1;
286 return 0;
287}
288
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100289/* Parse the "check-send-proxy" server keyword */
290static int srv_parse_check_send_proxy(char **args, int *cur_arg,
291 struct proxy *curproxy, struct server *newsrv, char **err)
292{
293 newsrv->check.send_proxy = 1;
294 return 0;
295}
296
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100297/* Parse the "cookie" server keyword */
298static int srv_parse_cookie(char **args, int *cur_arg,
299 struct proxy *curproxy, struct server *newsrv, char **err)
300{
301 char *arg;
302
303 arg = args[*cur_arg + 1];
304 if (!*arg) {
305 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
306 return ERR_ALERT | ERR_FATAL;
307 }
308
309 free(newsrv->cookie);
310 newsrv->cookie = strdup(arg);
311 newsrv->cklen = strlen(arg);
312 newsrv->flags |= SRV_F_COOKIESET;
313 return 0;
314}
315
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100316/* Parse the "disabled" server keyword */
317static int srv_parse_disabled(char **args, int *cur_arg,
318 struct proxy *curproxy, struct server *newsrv, char **err)
319{
320 newsrv->admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
321 newsrv->state = SRV_ST_STOPPED;
322 newsrv->check.state |= CHK_ST_PAUSED;
323 newsrv->check.health = 0;
324 return 0;
325}
326
327/* Parse the "enabled" server keyword */
328static int srv_parse_enabled(char **args, int *cur_arg,
329 struct proxy *curproxy, struct server *newsrv, char **err)
330{
331 newsrv->admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
332 newsrv->state = SRV_ST_RUNNING;
333 newsrv->check.state &= ~CHK_ST_PAUSED;
334 newsrv->check.health = newsrv->check.rise;
335 return 0;
336}
337
Willy Tarreaudff55432012-10-10 17:51:05 +0200338/* parse the "id" server keyword */
339static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
340{
341 struct eb32_node *node;
342
343 if (!*args[*cur_arg + 1]) {
344 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
345 return ERR_ALERT | ERR_FATAL;
346 }
347
348 newsrv->puid = atol(args[*cur_arg + 1]);
349 newsrv->conf.id.key = newsrv->puid;
350
351 if (newsrv->puid <= 0) {
352 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
353 return ERR_ALERT | ERR_FATAL;
354 }
355
356 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
357 if (node) {
358 struct server *target = container_of(node, struct server, conf.id);
359 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
360 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
361 target->id);
362 return ERR_ALERT | ERR_FATAL;
363 }
364
365 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200366 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200367 return 0;
368}
369
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100370/* Parse the "namespace" server keyword */
371static int srv_parse_namespace(char **args, int *cur_arg,
372 struct proxy *curproxy, struct server *newsrv, char **err)
373{
374#ifdef CONFIG_HAP_NS
375 char *arg;
376
377 arg = args[*cur_arg + 1];
378 if (!*arg) {
379 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
380 return ERR_ALERT | ERR_FATAL;
381 }
382
383 if (!strcmp(arg, "*")) {
384 /* Use the namespace associated with the connection (if present). */
385 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
386 return 0;
387 }
388
389 /*
390 * As this parser may be called several times for the same 'default-server'
391 * object, or for a new 'server' instance deriving from a 'default-server'
392 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
393 */
394 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
395
396 newsrv->netns = netns_store_lookup(arg, strlen(arg));
397 if (!newsrv->netns)
398 newsrv->netns = netns_store_insert(arg);
399
400 if (!newsrv->netns) {
401 memprintf(err, "Cannot open namespace '%s'", arg);
402 return ERR_ALERT | ERR_FATAL;
403 }
404
405 return 0;
406#else
407 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
408 return ERR_ALERT | ERR_FATAL;
409#endif
410}
411
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100412/* Parse the "no-agent-check" server keyword */
413static int srv_parse_no_agent_check(char **args, int *cur_arg,
414 struct proxy *curproxy, struct server *newsrv, char **err)
415{
416 free_check(&newsrv->agent);
417 newsrv->agent.inter = 0;
418 newsrv->agent.port = 0;
419 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
420 newsrv->do_agent = 0;
421 return 0;
422}
423
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100424/* Parse the "no-backup" server keyword */
425static int srv_parse_no_backup(char **args, int *cur_arg,
426 struct proxy *curproxy, struct server *newsrv, char **err)
427{
428 newsrv->flags &= ~SRV_F_BACKUP;
429 return 0;
430}
431
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100432/* Parse the "no-check" server keyword */
433static int srv_parse_no_check(char **args, int *cur_arg,
434 struct proxy *curproxy, struct server *newsrv, char **err)
435{
436 free_check(&newsrv->check);
437 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
438 newsrv->do_check = 0;
439 return 0;
440}
441
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100442/* Parse the "no-check-send-proxy" server keyword */
443static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
444 struct proxy *curproxy, struct server *newsrv, char **err)
445{
446 newsrv->check.send_proxy = 0;
447 return 0;
448}
449
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100450/* Disable server PROXY protocol flags. */
451static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
452{
453 srv->pp_opts &= ~flags;
454 return 0;
455}
456
457/* Parse the "no-send-proxy" server keyword */
458static int srv_parse_no_send_proxy(char **args, int *cur_arg,
459 struct proxy *curproxy, struct server *newsrv, char **err)
460{
461 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
462}
463
464/* Parse the "no-send-proxy-v2" server keyword */
465static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
466 struct proxy *curproxy, struct server *newsrv, char **err)
467{
468 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
469}
470
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100471/* Parse the "non-stick" server keyword */
472static int srv_parse_non_stick(char **args, int *cur_arg,
473 struct proxy *curproxy, struct server *newsrv, char **err)
474{
475 newsrv->flags |= SRV_F_NON_STICK;
476 return 0;
477}
478
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100479/* Enable server PROXY protocol flags. */
480static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
481{
482 srv->pp_opts |= flags;
483 return 0;
484}
485
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100486/* Parse the "observe" server keyword */
487static int srv_parse_observe(char **args, int *cur_arg,
488 struct proxy *curproxy, struct server *newsrv, char **err)
489{
490 char *arg;
491
492 arg = args[*cur_arg + 1];
493 if (!*arg) {
494 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
495 return ERR_ALERT | ERR_FATAL;
496 }
497
498 if (!strcmp(arg, "none")) {
499 newsrv->observe = HANA_OBS_NONE;
500 }
501 else if (!strcmp(arg, "layer4")) {
502 newsrv->observe = HANA_OBS_LAYER4;
503 }
504 else if (!strcmp(arg, "layer7")) {
505 if (curproxy->mode != PR_MODE_HTTP) {
506 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
507 return ERR_ALERT;
508 }
509 newsrv->observe = HANA_OBS_LAYER7;
510 }
511 else {
512 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
513 "but got '%s'\n", args[*cur_arg], arg);
514 return ERR_ALERT | ERR_FATAL;
515 }
516
517 return 0;
518}
519
Frédéric Lécaille16186232017-03-14 16:42:49 +0100520/* Parse the "redir" server keyword */
521static int srv_parse_redir(char **args, int *cur_arg,
522 struct proxy *curproxy, struct server *newsrv, char **err)
523{
524 char *arg;
525
526 arg = args[*cur_arg + 1];
527 if (!*arg) {
528 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
529 return ERR_ALERT | ERR_FATAL;
530 }
531
532 free(newsrv->rdr_pfx);
533 newsrv->rdr_pfx = strdup(arg);
534 newsrv->rdr_len = strlen(arg);
535
536 return 0;
537}
538
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100539/* Parse the "send-proxy" server keyword */
540static int srv_parse_send_proxy(char **args, int *cur_arg,
541 struct proxy *curproxy, struct server *newsrv, char **err)
542{
543 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
544}
545
546/* Parse the "send-proxy-v2" server keyword */
547static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
548 struct proxy *curproxy, struct server *newsrv, char **err)
549{
550 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
551}
552
Frédéric Lécailledba97072017-03-17 15:33:50 +0100553
554/* Parse the "source" server keyword */
555static int srv_parse_source(char **args, int *cur_arg,
556 struct proxy *curproxy, struct server *newsrv, char **err)
557{
558 char *errmsg;
559 int port_low, port_high;
560 struct sockaddr_storage *sk;
561 struct protocol *proto;
562
563 errmsg = NULL;
564
565 if (!*args[*cur_arg + 1]) {
566 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
567 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
568 goto err;
569 }
570
571 /* 'sk' is statically allocated (no need to be freed). */
572 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
573 if (!sk) {
574 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
575 goto err;
576 }
577
578 proto = protocol_by_family(sk->ss_family);
579 if (!proto || !proto->connect) {
580 Alert("'%s %s' : connect() not supported for this address family.\n",
581 args[*cur_arg], args[*cur_arg + 1]);
582 goto err;
583 }
584
585 newsrv->conn_src.opts |= CO_SRC_BIND;
586 newsrv->conn_src.source_addr = *sk;
587
588 if (port_low != port_high) {
589 int i;
590
591 if (!port_low || !port_high) {
592 Alert("'%s' does not support port offsets (found '%s').\n",
593 args[*cur_arg], args[*cur_arg + 1]);
594 goto err;
595 }
596
597 if (port_low <= 0 || port_low > 65535 ||
598 port_high <= 0 || port_high > 65535 ||
599 port_low > port_high) {
600 Alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
601 goto err;
602 }
603 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
604 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
605 newsrv->conn_src.sport_range->ports[i] = port_low + i;
606 }
607
608 *cur_arg += 2;
609 while (*(args[*cur_arg])) {
610 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
611#if defined(CONFIG_HAP_TRANSPARENT)
612 if (!*args[*cur_arg + 1]) {
613 Alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
614 "or 'hdr_ip(name,#)' as argument.\n");
615 goto err;
616 }
617 if (!strcmp(args[*cur_arg + 1], "client")) {
618 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
619 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
620 }
621 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
622 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
623 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
624 }
625 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
626 char *name, *end;
627
628 name = args[*cur_arg + 1] + 7;
629 while (isspace(*name))
630 name++;
631
632 end = name;
633 while (*end && !isspace(*end) && *end != ',' && *end != ')')
634 end++;
635
636 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
637 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
638 free(newsrv->conn_src.bind_hdr_name);
639 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
640 newsrv->conn_src.bind_hdr_len = end - name;
641 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
642 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
643 newsrv->conn_src.bind_hdr_occ = -1;
644
645 /* now look for an occurrence number */
646 while (isspace(*end))
647 end++;
648 if (*end == ',') {
649 end++;
650 name = end;
651 if (*end == '-')
652 end++;
653 while (isdigit((int)*end))
654 end++;
655 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
656 }
657
658 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
659 Alert("usesrc hdr_ip(name,num) does not support negative"
660 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
661 goto err;
662 }
663 }
664 else {
665 struct sockaddr_storage *sk;
666 int port1, port2;
667
668 /* 'sk' is statically allocated (no need to be freed). */
669 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
670 if (!sk) {
671 Alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
672 goto err;
673 }
674
675 proto = protocol_by_family(sk->ss_family);
676 if (!proto || !proto->connect) {
677 Alert("'%s %s' : connect() not supported for this address family.\n",
678 args[*cur_arg], args[*cur_arg + 1]);
679 goto err;
680 }
681
682 if (port1 != port2) {
683 Alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
684 args[*cur_arg], args[*cur_arg + 1]);
685 goto err;
686 }
687 newsrv->conn_src.tproxy_addr = *sk;
688 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
689 }
690 global.last_checks |= LSTCHK_NETADM;
691 *cur_arg += 2;
692 continue;
693#else /* no TPROXY support */
694 Alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
695 goto err;
696#endif /* defined(CONFIG_HAP_TRANSPARENT) */
697 } /* "usesrc" */
698
699 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
700#ifdef SO_BINDTODEVICE
701 if (!*args[*cur_arg + 1]) {
702 Alert("'%s' : missing interface name.\n", args[0]);
703 goto err;
704 }
705 free(newsrv->conn_src.iface_name);
706 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
707 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
708 global.last_checks |= LSTCHK_NETADM;
709#else
710 Alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
711 goto err;
712#endif
713 *cur_arg += 2;
714 continue;
715 }
716 /* this keyword in not an option of "source" */
717 break;
718 } /* while */
719
720 return 0;
721
722 err:
723 free(errmsg);
724 return ERR_ALERT | ERR_FATAL;
725}
726
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100727/* Parse the "stick" server keyword */
728static int srv_parse_stick(char **args, int *cur_arg,
729 struct proxy *curproxy, struct server *newsrv, char **err)
730{
731 newsrv->flags &= ~SRV_F_NON_STICK;
732 return 0;
733}
734
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100735/* Parse the "track" server keyword */
736static int srv_parse_track(char **args, int *cur_arg,
737 struct proxy *curproxy, struct server *newsrv, char **err)
738{
739 char *arg;
740
741 arg = args[*cur_arg + 1];
742 if (!*arg) {
743 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
744 return ERR_ALERT | ERR_FATAL;
745 }
746
747 free(newsrv->trackit);
748 newsrv->trackit = strdup(arg);
749
750 return 0;
751}
752
Frédéric Lécailledba97072017-03-17 15:33:50 +0100753
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200754/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200755 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200756 * shutdown.
757 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200758void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200759{
Willy Tarreau87b09662015-04-03 00:22:06 +0200760 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200761
Willy Tarreau87b09662015-04-03 00:22:06 +0200762 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
763 if (stream->srv_conn == srv)
764 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200765}
766
767/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200768 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200769 * the reason for the shutdown.
770 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200771void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200772{
773 struct server *srv;
774
775 for (srv = px->srv; srv != NULL; srv = srv->next)
776 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200777 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200778}
779
Willy Tarreaubda92272014-05-20 21:55:30 +0200780/* Appends some information to a message string related to a server going UP or
781 * DOWN. If both <forced> and <reason> are null and the server tracks another
782 * one, a "via" information will be provided to know where the status came from.
783 * If <reason> is non-null, the entire string will be appended after a comma and
784 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200785 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200786 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200787 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200788void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200789{
Willy Tarreaubda92272014-05-20 21:55:30 +0200790 if (reason)
791 chunk_appendf(msg, ", %s", reason);
792 else if (!forced && s->track)
793 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200794
795 if (xferred >= 0) {
796 if (s->state == SRV_ST_STOPPED)
797 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
798 " %d sessions active, %d requeued, %d remaining in queue",
799 s->proxy->srv_act, s->proxy->srv_bck,
800 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
801 s->cur_sess, xferred, s->nbpend);
802 else
803 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
804 " %d sessions requeued, %d total in queue",
805 s->proxy->srv_act, s->proxy->srv_bck,
806 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
807 xferred, s->nbpend);
808 }
809}
810
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200811/* Marks server <s> down, regardless of its checks' statuses, notifies by all
812 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200813 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200814 * recomputes the number of servers, but not the map. Maintenance servers are
815 * ignored. It reports <reason> if non-null as the reason for going down. Note
816 * that it makes use of the trash to build the log strings, so <reason> must
817 * not be placed there.
818 */
819void srv_set_stopped(struct server *s, const char *reason)
820{
821 struct server *srv;
822 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
823 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900824 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200825 int xferred;
826
827 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
828 return;
829
830 s->last_change = now.tv_sec;
831 s->state = SRV_ST_STOPPED;
832 if (s->proxy->lbprm.set_server_status_down)
833 s->proxy->lbprm.set_server_status_down(s);
834
835 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200836 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200837
Willy Tarreau87b09662015-04-03 00:22:06 +0200838 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200839 * a connection. Those which are redispatchable will be queued
840 * to another server or to the proxy itself.
841 */
842 xferred = pendconn_redistribute(s);
843
844 chunk_printf(&trash,
845 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
846 s->proxy->id, s->id);
847
848 srv_append_status(&trash, s, reason, xferred, 0);
849 Warning("%s.\n", trash.str);
850
851 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900852 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
853 send_log(s->proxy, log_level, "%s.\n", trash.str);
854 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200855
856 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
857 set_backend_down(s->proxy);
858
859 s->counters.down_trans++;
860
861 for (srv = s->trackers; srv; srv = srv->tracknext)
862 srv_set_stopped(srv, NULL);
863}
864
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200865/* Marks server <s> up regardless of its checks' statuses and provided it isn't
866 * in maintenance. Notifies by all available means, recounts the remaining
867 * servers on the proxy and tries to grab requests from the proxy. It
868 * automatically recomputes the number of servers, but not the map. Maintenance
869 * servers are ignored. It reports <reason> if non-null as the reason for going
870 * up. Note that it makes use of the trash to build the log strings, so <reason>
871 * must not be placed there.
872 */
873void srv_set_running(struct server *s, const char *reason)
874{
875 struct server *srv;
876 int xferred;
877
878 if (s->admin & SRV_ADMF_MAINT)
879 return;
880
881 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
882 return;
883
884 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
885 if (s->proxy->last_change < now.tv_sec) // ignore negative times
886 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
887 s->proxy->last_change = now.tv_sec;
888 }
889
890 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
891 s->down_time += now.tv_sec - s->last_change;
892
893 s->last_change = now.tv_sec;
894
895 s->state = SRV_ST_STARTING;
896 if (s->slowstart > 0)
897 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
898 else
899 s->state = SRV_ST_RUNNING;
900
901 server_recalc_eweight(s);
902
903 /* If the server is set with "on-marked-up shutdown-backup-sessions",
904 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200905 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200906 * on all backup servers.
907 */
908 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
909 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200910 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200911
912 /* check if we can handle some connections queued at the proxy. We
913 * will take as many as we can handle.
914 */
915 xferred = pendconn_grab_from_px(s);
916
917 chunk_printf(&trash,
918 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
919 s->proxy->id, s->id);
920
921 srv_append_status(&trash, s, reason, xferred, 0);
922 Warning("%s.\n", trash.str);
923 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900924 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200925
926 for (srv = s->trackers; srv; srv = srv->tracknext)
927 srv_set_running(srv, NULL);
928}
929
Willy Tarreau8eb77842014-05-21 13:54:57 +0200930/* Marks server <s> stopping regardless of its checks' statuses and provided it
931 * isn't in maintenance. Notifies by all available means, recounts the remaining
932 * servers on the proxy and tries to grab requests from the proxy. It
933 * automatically recomputes the number of servers, but not the map. Maintenance
934 * servers are ignored. It reports <reason> if non-null as the reason for going
935 * up. Note that it makes use of the trash to build the log strings, so <reason>
936 * must not be placed there.
937 */
938void srv_set_stopping(struct server *s, const char *reason)
939{
940 struct server *srv;
941 int xferred;
942
943 if (s->admin & SRV_ADMF_MAINT)
944 return;
945
946 if (s->state == SRV_ST_STOPPING)
947 return;
948
949 s->last_change = now.tv_sec;
950 s->state = SRV_ST_STOPPING;
951 if (s->proxy->lbprm.set_server_status_down)
952 s->proxy->lbprm.set_server_status_down(s);
953
Willy Tarreau87b09662015-04-03 00:22:06 +0200954 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200955 * a connection. Those which are redispatchable will be queued
956 * to another server or to the proxy itself.
957 */
958 xferred = pendconn_redistribute(s);
959
960 chunk_printf(&trash,
961 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
962 s->proxy->id, s->id);
963
964 srv_append_status(&trash, s, reason, xferred, 0);
965
966 Warning("%s.\n", trash.str);
967 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
968
969 if (!s->proxy->srv_bck && !s->proxy->srv_act)
970 set_backend_down(s->proxy);
971
972 for (srv = s->trackers; srv; srv = srv->tracknext)
973 srv_set_stopping(srv, NULL);
974}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200975
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200976/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
977 * enforce either maint mode or drain mode. It is not allowed to set more than
978 * one flag at once. The equivalent "inherited" flag is propagated to all
979 * tracking servers. Maintenance mode disables health checks (but not agent
980 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100981 * is done. If <cause> is non-null, it will be displayed at the end of the log
982 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200983 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100984void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200985{
986 struct check *check = &s->check;
987 struct server *srv;
988 int xferred;
989
990 if (!mode)
991 return;
992
993 /* stop going down as soon as we meet a server already in the same state */
994 if (s->admin & mode)
995 return;
996
997 s->admin |= mode;
998
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200999 /* stop going down if the equivalent flag was already present (forced or inherited) */
1000 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
1001 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
1002 return;
1003
1004 /* Maintenance must also disable health checks */
1005 if (mode & SRV_ADMF_MAINT) {
1006 if (s->check.state & CHK_ST_ENABLED) {
1007 s->check.state |= CHK_ST_PAUSED;
1008 check->health = 0;
1009 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001010
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001011 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001012 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +01001013 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
1014 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
1015 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001016
Willy Tarreaubda92272014-05-20 21:55:30 +02001017 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001018
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001019 if (!(global.mode & MODE_STARTING)) {
1020 Warning("%s.\n", trash.str);
1021 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1022 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001023 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001024 else { /* server was still running */
1025 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
1026 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1027
1028 check->health = 0; /* failure */
1029 s->last_change = now.tv_sec;
1030 s->state = SRV_ST_STOPPED;
1031 if (s->proxy->lbprm.set_server_status_down)
1032 s->proxy->lbprm.set_server_status_down(s);
1033
1034 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001035 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001036
Willy Tarreau87b09662015-04-03 00:22:06 +02001037 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001038 * a connection. Those which are redispatchable will be queued
1039 * to another server or to the proxy itself.
1040 */
1041 xferred = pendconn_redistribute(s);
1042
1043 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +01001044 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001045 s->flags & SRV_F_BACKUP ? "Backup " : "",
Willy Tarreau8b428482016-11-07 15:53:43 +01001046 s->proxy->id, s->id,
1047 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001048
1049 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
1050
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001051 if (!(global.mode & MODE_STARTING)) {
1052 Warning("%s.\n", trash.str);
1053 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
1054 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001055
1056 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1057 set_backend_down(s->proxy);
1058
1059 s->counters.down_trans++;
1060 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001061 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001062
1063 /* drain state is applied only if not yet in maint */
1064 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001065 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1066
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001067 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001068 if (s->proxy->lbprm.set_server_status_down)
1069 s->proxy->lbprm.set_server_status_down(s);
1070
Willy Tarreau87b09662015-04-03 00:22:06 +02001071 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001072 * a connection. Those which are redispatchable will be queued
1073 * to another server or to the proxy itself.
1074 */
1075 xferred = pendconn_redistribute(s);
1076
Willy Tarreau8b428482016-11-07 15:53:43 +01001077 chunk_printf(&trash, "%sServer %s/%s enters drain state%s%s%s",
1078 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
1079 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001080
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001081 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001082
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001083 if (!(global.mode & MODE_STARTING)) {
1084 Warning("%s.\n", trash.str);
1085 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1086 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
1087 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001088 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1089 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001090 }
1091
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001092 /* compute the inherited flag to propagate */
1093 if (mode & SRV_ADMF_MAINT)
1094 mode = SRV_ADMF_IMAINT;
1095 else if (mode & SRV_ADMF_DRAIN)
1096 mode = SRV_ADMF_IDRAIN;
1097
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001098 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +01001099 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001100}
1101
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001102/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1103 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1104 * than one flag at once. The equivalent "inherited" flag is propagated to all
1105 * tracking servers. Leaving maintenance mode re-enables health checks. When
1106 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001107 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001108void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001109{
1110 struct check *check = &s->check;
1111 struct server *srv;
1112 int xferred = -1;
1113
1114 if (!mode)
1115 return;
1116
1117 /* stop going down as soon as we see the flag is not there anymore */
1118 if (!(s->admin & mode))
1119 return;
1120
1121 s->admin &= ~mode;
1122
1123 if (s->admin & SRV_ADMF_MAINT) {
1124 /* remaining in maintenance mode, let's inform precisely about the
1125 * situation.
1126 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001127 if (mode & SRV_ADMF_FMAINT) {
1128 chunk_printf(&trash,
1129 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
1130 s->flags & SRV_F_BACKUP ? "Backup " : "",
1131 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001132
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001133 if (s->track) /* normally it's mandatory here */
1134 chunk_appendf(&trash, " via %s/%s",
1135 s->track->proxy->id, s->track->id);
1136 Warning("%s.\n", trash.str);
1137 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1138 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001139 if (mode & SRV_ADMF_RMAINT) {
1140 chunk_printf(&trash,
1141 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
1142 s->flags & SRV_F_BACKUP ? "Backup " : "",
1143 s->proxy->id, s->id, s->hostname);
1144
1145 if (s->track) /* normally it's mandatory here */
1146 chunk_appendf(&trash, " via %s/%s",
1147 s->track->proxy->id, s->track->id);
1148 Warning("%s.\n", trash.str);
1149 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1150 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001151 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001152 chunk_printf(&trash,
1153 "%sServer %s/%s remains in forced maintenance",
1154 s->flags & SRV_F_BACKUP ? "Backup " : "",
1155 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001156 Warning("%s.\n", trash.str);
1157 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1158 }
1159 /* don't report anything when leaving drain mode and remaining in maintenance */
1160 }
1161 else if (mode & SRV_ADMF_MAINT) {
1162 /* OK here we're leaving maintenance, we have many things to check,
1163 * because the server might possibly be coming back up depending on
1164 * its state. In practice, leaving maintenance means that we should
1165 * immediately turn to UP (more or less the slowstart) under the
1166 * following conditions :
1167 * - server is neither checked nor tracked
1168 * - server tracks another server which is not checked
1169 * - server tracks another server which is already up
1170 * Which sums up as something simpler :
1171 * "either the tracking server is up or the server's checks are disabled
1172 * or up". Otherwise we only re-enable health checks. There's a special
1173 * case associated to the stopping state which can be inherited. Note
1174 * that the server might still be in drain mode, which is naturally dealt
1175 * with by the lower level functions.
1176 */
1177
1178 if (s->check.state & CHK_ST_ENABLED) {
1179 s->check.state &= ~CHK_ST_PAUSED;
1180 check->health = check->rise; /* start OK but check immediately */
1181 }
1182
1183 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
1184 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
1185 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
1186 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1187 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1188 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1189 s->proxy->last_change = now.tv_sec;
1190 }
1191
1192 if (s->last_change < now.tv_sec) // ignore negative times
1193 s->down_time += now.tv_sec - s->last_change;
1194 s->last_change = now.tv_sec;
1195
1196 if (s->track && s->track->state == SRV_ST_STOPPING)
1197 s->state = SRV_ST_STOPPING;
1198 else {
1199 s->state = SRV_ST_STARTING;
1200 if (s->slowstart > 0)
1201 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
1202 else
1203 s->state = SRV_ST_RUNNING;
1204 }
1205
1206 server_recalc_eweight(s);
1207
1208 /* If the server is set with "on-marked-up shutdown-backup-sessions",
1209 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +02001210 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001211 * on all backup servers.
1212 */
1213 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
1214 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001215 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001216
1217 /* check if we can handle some connections queued at the proxy. We
1218 * will take as many as we can handle.
1219 */
1220 xferred = pendconn_grab_from_px(s);
1221 }
1222
1223 if (mode & SRV_ADMF_FMAINT) {
1224 chunk_printf(&trash,
1225 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
1226 s->flags & SRV_F_BACKUP ? "Backup " : "",
1227 s->proxy->id, s->id,
1228 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1229 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001230 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001231 else if (mode & SRV_ADMF_RMAINT) {
1232 chunk_printf(&trash,
1233 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
1234 s->flags & SRV_F_BACKUP ? "Backup " : "",
1235 s->proxy->id, s->id, s->hostname,
1236 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1237 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
1238 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001239 else {
1240 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001241 "%sServer %s/%s is %s/%s (leaving maintenance)",
1242 s->flags & SRV_F_BACKUP ? "Backup " : "",
1243 s->proxy->id, s->id,
1244 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1245 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
1246 srv_append_status(&trash, s, NULL, xferred, 0);
1247 }
1248 Warning("%s.\n", trash.str);
1249 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1250 }
1251 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
1252 /* remaining in drain mode after removing one of its flags */
1253
1254 if (mode & SRV_ADMF_FDRAIN) {
1255 chunk_printf(&trash,
1256 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001257 s->flags & SRV_F_BACKUP ? "Backup " : "",
1258 s->proxy->id, s->id);
1259
1260 if (s->track) /* normally it's mandatory here */
1261 chunk_appendf(&trash, " via %s/%s",
1262 s->track->proxy->id, s->track->id);
1263 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001264 else {
1265 chunk_printf(&trash,
1266 "%sServer %s/%s remains in forced drain mode",
1267 s->flags & SRV_F_BACKUP ? "Backup " : "",
1268 s->proxy->id, s->id);
1269 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001270 Warning("%s.\n", trash.str);
1271 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001272 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001273 else if (mode & SRV_ADMF_DRAIN) {
1274 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001275 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1276 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1277 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1278 s->proxy->last_change = now.tv_sec;
1279 }
1280
1281 if (s->last_change < now.tv_sec) // ignore negative times
1282 s->down_time += now.tv_sec - s->last_change;
1283 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001284 server_recalc_eweight(s);
1285
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001286 if (mode & SRV_ADMF_FDRAIN) {
1287 chunk_printf(&trash,
1288 "%sServer %s/%s is %s (leaving forced drain)",
1289 s->flags & SRV_F_BACKUP ? "Backup " : "",
1290 s->proxy->id, s->id,
1291 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
1292 }
1293 else {
1294 chunk_printf(&trash,
1295 "%sServer %s/%s is %s (leaving drain)",
1296 s->flags & SRV_F_BACKUP ? "Backup " : "",
1297 s->proxy->id, s->id,
1298 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
1299 if (s->track) /* normally it's mandatory here */
1300 chunk_appendf(&trash, " via %s/%s",
1301 s->track->proxy->id, s->track->id);
1302 }
1303 Warning("%s.\n", trash.str);
1304 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001305 }
1306
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001307 /* stop going down if the equivalent flag is still present (forced or inherited) */
1308 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
1309 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
1310 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001311
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001312 if (mode & SRV_ADMF_MAINT)
1313 mode = SRV_ADMF_IMAINT;
1314 else if (mode & SRV_ADMF_DRAIN)
1315 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001316
1317 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001318 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001319}
1320
Willy Tarreau757478e2016-11-03 19:22:19 +01001321/* principle: propagate maint and drain to tracking servers. This is useful
1322 * upon startup so that inherited states are correct.
1323 */
1324static void srv_propagate_admin_state(struct server *srv)
1325{
1326 struct server *srv2;
1327
1328 if (!srv->trackers)
1329 return;
1330
1331 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
1332 if (srv->admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001333 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001334
1335 if (srv->admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001336 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001337 }
1338}
1339
1340/* Compute and propagate the admin states for all servers in proxy <px>.
1341 * Only servers *not* tracking another one are considered, because other
1342 * ones will be handled when the server they track is visited.
1343 */
1344void srv_compute_all_admin_states(struct proxy *px)
1345{
1346 struct server *srv;
1347
1348 for (srv = px->srv; srv; srv = srv->next) {
1349 if (srv->track)
1350 continue;
1351 srv_propagate_admin_state(srv);
1352 }
1353}
1354
Willy Tarreaudff55432012-10-10 17:51:05 +02001355/* Note: must not be declared <const> as its list will be overwritten.
1356 * Please take care of keeping this list alphabetically sorted, doing so helps
1357 * all code contributors.
1358 * Optional keywords are also declared with a NULL ->parse() function so that
1359 * the config parser can report an appropriate error when a known keyword was
1360 * not enabled.
1361 */
1362static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001363 { "addr", srv_parse_addr, 1, 1 }, /* IP address to send health to or to probe from agent-check */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001364 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001365 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001366 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001367 { "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 +01001368 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001369 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1370 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001371 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001372 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001373 { "no-agent-check", srv_parse_no_agent_check, 0, 1 }, /* Do not enable any auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001374 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001375 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001376 { "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 +01001377 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1378 { "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 +01001379 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001380 { "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 +01001381 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001382 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1383 { "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 +01001384 /*
1385 * Note: the following 'skip' field value is 0.
1386 * Here this does not mean that "source" setting does not need any argument.
1387 * This means that the number of argument is variable.
1388 */
1389 { "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 +01001390 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001391 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001392 { NULL, NULL, 0 },
1393}};
1394
1395__attribute__((constructor))
1396static void __listener_init(void)
1397{
1398 srv_register_keywords(&srv_kws);
1399}
1400
Willy Tarreau004e0452013-11-21 11:22:01 +01001401/* Recomputes the server's eweight based on its state, uweight, the current time,
1402 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1403 * state is automatically disabled if the time is elapsed.
1404 */
1405void server_recalc_eweight(struct server *sv)
1406{
1407 struct proxy *px = sv->proxy;
1408 unsigned w;
1409
1410 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1411 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +02001412 if (sv->state == SRV_ST_STARTING)
1413 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001414 }
1415
1416 /* We must take care of not pushing the server to full throttle during slow starts.
1417 * It must also start immediately, at least at the minimal step when leaving maintenance.
1418 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001419 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001420 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1421 else
1422 w = px->lbprm.wdiv;
1423
1424 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
1425
1426 /* now propagate the status change to any LB algorithms */
1427 if (px->lbprm.update_server_eweight)
1428 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +02001429 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +01001430 if (px->lbprm.set_server_status_up)
1431 px->lbprm.set_server_status_up(sv);
1432 }
1433 else {
1434 if (px->lbprm.set_server_status_down)
1435 px->lbprm.set_server_status_down(sv);
1436 }
1437}
1438
Willy Tarreaubaaee002006-06-26 02:48:02 +02001439/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001440 * Parses weight_str and configures sv accordingly.
1441 * Returns NULL on success, error message string otherwise.
1442 */
1443const char *server_parse_weight_change_request(struct server *sv,
1444 const char *weight_str)
1445{
1446 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001447 long int w;
1448 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001449
1450 px = sv->proxy;
1451
1452 /* if the weight is terminated with '%', it is set relative to
1453 * the initial weight, otherwise it is absolute.
1454 */
1455 if (!*weight_str)
1456 return "Require <weight> or <weight%>.\n";
1457
Simon Hormanb796afa2013-02-12 10:45:53 +09001458 w = strtol(weight_str, &end, 10);
1459 if (end == weight_str)
1460 return "Empty weight string empty or preceded by garbage";
1461 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001462 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001463 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001464 /* Avoid integer overflow */
1465 if (w > 25600)
1466 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001467 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001468 if (w > 256)
1469 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001470 }
1471 else if (w < 0 || w > 256)
1472 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001473 else if (end[0] != '\0')
1474 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001475
1476 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1477 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1478
1479 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001480 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001481
1482 return NULL;
1483}
1484
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001485/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001486 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1487 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001488 * Returns:
1489 * - error string on error
1490 * - NULL on success
1491 */
1492const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001493 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001494{
1495 unsigned char ip[INET6_ADDRSTRLEN];
1496
1497 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001498 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001499 return NULL;
1500 }
1501 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001502 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001503 return NULL;
1504 }
1505
1506 return "Could not understand IP address format.\n";
1507}
1508
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001509const char *server_parse_maxconn_change_request(struct server *sv,
1510 const char *maxconn_str)
1511{
1512 long int v;
1513 char *end;
1514
1515 if (!*maxconn_str)
1516 return "Require <maxconn>.\n";
1517
1518 v = strtol(maxconn_str, &end, 10);
1519 if (end == maxconn_str)
1520 return "maxconn string empty or preceded by garbage";
1521 else if (end[0] != '\0')
1522 return "Trailing garbage in maxconn string";
1523
1524 if (sv->maxconn == sv->minconn) { // static maxconn
1525 sv->maxconn = sv->minconn = v;
1526 } else { // dynamic maxconn
1527 sv->maxconn = v;
1528 }
1529
1530 if (may_dequeue_tasks(sv, sv->proxy))
1531 process_srv_queue(sv);
1532
1533 return NULL;
1534}
1535
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001536#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1537static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1538{
1539 int idx;
1540 struct sample_expr *expr;
1541 const char *args[] = {
1542 newsrv->sni_expr,
1543 NULL,
1544 };
1545
1546 idx = 0;
1547 proxy->conf.args.ctx = ARGC_SRV;
1548
1549 expr = sample_parse_expr((char **)args, &idx, px->conf.file, px->conf.line,
1550 err, &proxy->conf.args);
1551 if (!expr) {
1552 memprintf(err, "error detected while parsing sni expression : %s", *err);
1553 return ERR_ALERT | ERR_FATAL;
1554 }
1555
1556 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1557 memprintf(err, "error detected while parsing sni expression : "
1558 " fetch method '%s' extracts information from '%s', "
1559 "none of which is available here.\n",
1560 args[0], sample_src_names(expr->fetch->use));
1561 return ERR_ALERT | ERR_FATAL;
1562 }
1563
1564 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1565 release_sample_expr(newsrv->ssl_ctx.sni);
1566 newsrv->ssl_ctx.sni = expr;
1567
1568 return 0;
1569}
1570#endif
1571
1572static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1573{
1574 if (err && *err) {
1575 indent_msg(err, 2);
1576 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1577 }
1578 else
1579 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1580 file, linenum, args[0], args[1], args[cur_arg]);
1581}
1582
Willy Tarreau272adea2014-03-31 10:39:59 +02001583int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1584{
1585 struct server *newsrv = NULL;
1586 const char *err;
1587 char *errmsg = NULL;
1588 int err_code = 0;
1589 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001590 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001591
1592 if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */
1593 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001594 int defsrv = (*args[0] == 'd');
Willy Tarreau272adea2014-03-31 10:39:59 +02001595
1596 if (!defsrv && curproxy == defproxy) {
1597 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1598 err_code |= ERR_ALERT | ERR_FATAL;
1599 goto out;
1600 }
1601 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1602 err_code |= ERR_ALERT | ERR_FATAL;
1603
Frédéric Lécaille8065b6d2017-03-09 14:01:02 +01001604 if (!defsrv && !*args[2]) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001605 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1606 file, linenum, args[0]);
1607 err_code |= ERR_ALERT | ERR_FATAL;
1608 goto out;
1609 }
1610
1611 err = invalid_char(args[1]);
1612 if (err && !defsrv) {
1613 Alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n",
1614 file, linenum, *err, args[1]);
1615 err_code |= ERR_ALERT | ERR_FATAL;
1616 goto out;
1617 }
1618
1619 if (!defsrv) {
1620 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001621 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001622 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001623 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +02001624
Vincent Bernat02779b62016-04-03 13:48:43 +02001625 if ((newsrv = calloc(1, sizeof(*newsrv))) == NULL) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001626 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1627 err_code |= ERR_ALERT | ERR_ABORT;
1628 goto out;
1629 }
1630
1631 /* the servers are linked backwards first */
1632 newsrv->next = curproxy->srv;
1633 curproxy->srv = newsrv;
1634 newsrv->proxy = curproxy;
1635 newsrv->conf.file = strdup(file);
1636 newsrv->conf.line = linenum;
1637
1638 newsrv->obj_type = OBJ_TYPE_SERVER;
1639 LIST_INIT(&newsrv->actconns);
1640 LIST_INIT(&newsrv->pendconns);
Willy Tarreau600802a2015-08-04 17:19:06 +02001641 LIST_INIT(&newsrv->priv_conns);
Willy Tarreau173a1c62015-08-05 10:31:57 +02001642 LIST_INIT(&newsrv->idle_conns);
Willy Tarreau7017cb02015-08-05 16:35:23 +02001643 LIST_INIT(&newsrv->safe_conns);
Willy Tarreauc93cd162014-05-13 15:54:22 +02001644 newsrv->flags = 0;
Willy Tarreau20125212014-05-13 19:44:56 +02001645 newsrv->admin = 0;
Willy Tarreau892337c2014-05-13 23:41:20 +02001646 newsrv->state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau272adea2014-03-31 10:39:59 +02001647 newsrv->last_change = now.tv_sec;
1648 newsrv->id = strdup(args[1]);
1649
1650 /* several ways to check the port component :
1651 * - IP => port=+0, relative (IPv4 only)
1652 * - IP: => port=+0, relative
1653 * - IP:N => port=N, absolute
1654 * - IP:+N => port=+N, relative
1655 * - IP:-N => port=-N, relative
1656 */
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001657 sk = str2sa_range(args[2], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001658 if (!sk) {
1659 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1660 err_code |= ERR_ALERT | ERR_FATAL;
1661 goto out;
1662 }
1663
1664 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001665 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001666 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1667 file, linenum, args[0], args[1]);
1668 err_code |= ERR_ALERT | ERR_FATAL;
1669 goto out;
1670 }
1671
1672 if (!port1 || !port2) {
1673 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001674 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001675 }
1676 else if (port1 != port2) {
1677 /* port range */
1678 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1679 file, linenum, args[0], args[1], args[2]);
1680 err_code |= ERR_ALERT | ERR_FATAL;
1681 goto out;
1682 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001683
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001684 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +02001685 newsrv->hostname = fqdn;
1686 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001687 goto skip_name_resolution;
1688
Willy Tarreau07101d52015-09-08 16:16:35 +02001689 fqdn = NULL;
Vincent Bernat02779b62016-04-03 13:48:43 +02001690 if ((curr_resolution = calloc(1, sizeof(*curr_resolution))) == NULL)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001691 goto skip_name_resolution;
1692
1693 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
1694 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
1695 goto skip_name_resolution;
1696 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
1697 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
1698 file, linenum, args[2]);
1699 err_code |= ERR_ALERT | ERR_FATAL;
1700 goto out;
1701 }
1702
1703 curr_resolution->requester = newsrv;
1704 curr_resolution->requester_cb = snr_resolution_cb;
1705 curr_resolution->requester_error_cb = snr_resolution_error_cb;
1706 curr_resolution->status = RSLV_STATUS_NONE;
1707 curr_resolution->step = RSLV_STEP_NONE;
1708 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +02001709 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001710 newsrv->resolution = curr_resolution;
1711
1712 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +02001713 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001714 newsrv->svc_port = port;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01001715 newsrv->xprt = newsrv->check.xprt = newsrv->agent.xprt = xprt_get(XPRT_RAW);
Willy Tarreau272adea2014-03-31 10:39:59 +02001716
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001717 if (!newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001718 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
1719 file, linenum, newsrv->addr.ss_family, args[2]);
1720 err_code |= ERR_ALERT | ERR_FATAL;
1721 goto out;
1722 }
1723
Frédéric Lécailledba97072017-03-17 15:33:50 +01001724 /*
1725 * In this section we copy default-server connection source settings to
1726 * the new server object connection source
1727 * (newsrv->conn_src <- curproxy->defsrv.conn_src).
1728 */
1729 newsrv->conn_src.opts = curproxy->defsrv.conn_src.opts;
1730 newsrv->conn_src.source_addr = curproxy->defsrv.conn_src.source_addr;
1731 if (curproxy->defsrv.conn_src.sport_range != NULL) {
1732 int i, def_sport_range_sz;
1733 struct server *default_srv;
1734
1735 default_srv = &curproxy->defsrv;
1736 def_sport_range_sz = default_srv->conn_src.sport_range->size;
1737 if (def_sport_range_sz > 0) {
1738 newsrv->conn_src.sport_range = port_range_alloc_range(def_sport_range_sz);
1739 if (newsrv->conn_src.sport_range) {
1740 for (i = 0; i < def_sport_range_sz; i++)
1741 newsrv->conn_src.sport_range->ports[i] = default_srv->conn_src.sport_range->ports[i];
1742 }
1743 }
1744 }
1745 if (curproxy->defsrv.conn_src.bind_hdr_name != NULL) {
1746 newsrv->conn_src.bind_hdr_name = strdup(curproxy->defsrv.conn_src.bind_hdr_name);
1747 newsrv->conn_src.bind_hdr_len = strlen(curproxy->defsrv.conn_src.bind_hdr_name);
1748 }
1749 newsrv->conn_src.bind_hdr_occ = curproxy->defsrv.conn_src.bind_hdr_occ;
1750 newsrv->conn_src.tproxy_addr = curproxy->defsrv.conn_src.tproxy_addr;
1751 if (curproxy->defsrv.conn_src.iface_name != NULL)
1752 newsrv->conn_src.iface_name = strdup(curproxy->defsrv.conn_src.iface_name);
1753
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001754 newsrv->pp_opts = curproxy->defsrv.pp_opts;
Frédéric Lécaille16186232017-03-14 16:42:49 +01001755 if (curproxy->defsrv.rdr_pfx != NULL) {
1756 newsrv->rdr_pfx = strdup(curproxy->defsrv.rdr_pfx);
1757 newsrv->rdr_len = curproxy->defsrv.rdr_len;
1758 }
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001759 if (curproxy->defsrv.cookie != NULL) {
1760 newsrv->cookie = strdup(curproxy->defsrv.cookie);
1761 newsrv->cklen = curproxy->defsrv.cklen;
1762 }
Willy Tarreau141ad852016-12-22 18:38:00 +01001763 newsrv->use_ssl = curproxy->defsrv.use_ssl;
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001764 newsrv->check.addr = newsrv->agent.addr = curproxy->defsrv.check.addr;
Willy Tarreau272adea2014-03-31 10:39:59 +02001765 newsrv->check.use_ssl = curproxy->defsrv.check.use_ssl;
1766 newsrv->check.port = curproxy->defsrv.check.port;
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +01001767 /* Note: 'flags' field has potentially been already initialized. */
1768 newsrv->flags |= curproxy->defsrv.flags;
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001769 newsrv->do_check = curproxy->defsrv.do_check;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001770 newsrv->do_agent = curproxy->defsrv.do_agent;
Baptiste Assmann6b453f12016-08-11 23:12:18 +02001771 if (newsrv->check.port)
1772 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02001773 newsrv->check.inter = curproxy->defsrv.check.inter;
1774 newsrv->check.fastinter = curproxy->defsrv.check.fastinter;
1775 newsrv->check.downinter = curproxy->defsrv.check.downinter;
1776 newsrv->agent.use_ssl = curproxy->defsrv.agent.use_ssl;
1777 newsrv->agent.port = curproxy->defsrv.agent.port;
James Brown55f9ff12015-10-21 18:19:05 -07001778 if (curproxy->defsrv.agent.send_string != NULL)
1779 newsrv->agent.send_string = strdup(curproxy->defsrv.agent.send_string);
1780 newsrv->agent.send_string_len = curproxy->defsrv.agent.send_string_len;
Willy Tarreau272adea2014-03-31 10:39:59 +02001781 newsrv->agent.inter = curproxy->defsrv.agent.inter;
1782 newsrv->agent.fastinter = curproxy->defsrv.agent.fastinter;
1783 newsrv->agent.downinter = curproxy->defsrv.agent.downinter;
1784 newsrv->maxqueue = curproxy->defsrv.maxqueue;
1785 newsrv->minconn = curproxy->defsrv.minconn;
1786 newsrv->maxconn = curproxy->defsrv.maxconn;
1787 newsrv->slowstart = curproxy->defsrv.slowstart;
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001788 newsrv->observe = curproxy->defsrv.observe;
Willy Tarreau272adea2014-03-31 10:39:59 +02001789 newsrv->onerror = curproxy->defsrv.onerror;
1790 newsrv->onmarkeddown = curproxy->defsrv.onmarkeddown;
1791 newsrv->onmarkedup = curproxy->defsrv.onmarkedup;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001792 if (curproxy->defsrv.trackit != NULL)
1793 newsrv->trackit = strdup(curproxy->defsrv.trackit);
Willy Tarreau272adea2014-03-31 10:39:59 +02001794 newsrv->consecutive_errors_limit
1795 = curproxy->defsrv.consecutive_errors_limit;
Willy Tarreau272adea2014-03-31 10:39:59 +02001796 newsrv->uweight = newsrv->iweight
1797 = curproxy->defsrv.iweight;
1798
1799 newsrv->check.status = HCHK_STATUS_INI;
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001800 newsrv->check.send_proxy = curproxy->defsrv.check.send_proxy;
Willy Tarreau272adea2014-03-31 10:39:59 +02001801 newsrv->check.rise = curproxy->defsrv.check.rise;
1802 newsrv->check.fall = curproxy->defsrv.check.fall;
1803 newsrv->check.health = newsrv->check.rise; /* up, but will fall down at first failure */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001804 /* Here we check if 'disabled' is the default server state */
1805 if (curproxy->defsrv.admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1806 newsrv->admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1807 newsrv->state = SRV_ST_STOPPED;
1808 newsrv->check.state |= CHK_ST_PAUSED;
1809 newsrv->check.health = 0;
1810 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001811 newsrv->check.server = newsrv;
Simon Hormane16c1b32015-01-30 11:22:57 +09001812 newsrv->check.tcpcheck_rules = &curproxy->tcpcheck_rules;
Willy Tarreau272adea2014-03-31 10:39:59 +02001813
1814 newsrv->agent.status = HCHK_STATUS_INI;
1815 newsrv->agent.rise = curproxy->defsrv.agent.rise;
1816 newsrv->agent.fall = curproxy->defsrv.agent.fall;
1817 newsrv->agent.health = newsrv->agent.rise; /* up, but will fall down at first failure */
1818 newsrv->agent.server = newsrv;
Thierry Fournierada34842016-02-17 21:25:09 +01001819 newsrv->dns_opts.family_prio = curproxy->defsrv.dns_opts.family_prio;
1820 if (newsrv->dns_opts.family_prio == AF_UNSPEC)
1821 newsrv->dns_opts.family_prio = AF_INET6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001822 memcpy(newsrv->dns_opts.pref_net,
1823 curproxy->defsrv.dns_opts.pref_net,
1824 sizeof(newsrv->dns_opts.pref_net));
1825 newsrv->dns_opts.pref_net_nb = curproxy->defsrv.dns_opts.pref_net_nb;
Baptiste Assmann25938272016-09-21 20:26:16 +02001826 newsrv->init_addr_methods = curproxy->defsrv.init_addr_methods;
1827 newsrv->init_addr = curproxy->defsrv.init_addr;
Frédéric Lécaille7c8cd582017-03-13 13:41:16 +01001828#if defined(USE_OPENSSL)
1829 /* SSL config. */
Frédéric Lécaille5e576432017-03-14 15:52:04 +01001830 if (curproxy->defsrv.ssl_ctx.ca_file != NULL)
1831 newsrv->ssl_ctx.ca_file = strdup(curproxy->defsrv.ssl_ctx.ca_file);
1832 if (curproxy->defsrv.ssl_ctx.crl_file != NULL)
1833 newsrv->ssl_ctx.crl_file = strdup(curproxy->defsrv.ssl_ctx.crl_file);
1834 if (curproxy->defsrv.ssl_ctx.client_crt != NULL)
Frédéric Lécailleacd48272017-03-29 14:58:09 +02001835 newsrv->ssl_ctx.client_crt = strdup(curproxy->defsrv.ssl_ctx.client_crt);
Frédéric Lécaille7c8cd582017-03-13 13:41:16 +01001836 newsrv->ssl_ctx.verify = curproxy->defsrv.ssl_ctx.verify;
Frédéric Lécaille273f3212017-03-13 15:52:01 +01001837 if (curproxy->defsrv.ssl_ctx.verify_host != NULL)
1838 newsrv->ssl_ctx.verify_host = strdup(curproxy->defsrv.ssl_ctx.verify_host);
Frédéric Lécaillebcaf1d72017-03-15 16:20:02 +01001839 if (curproxy->defsrv.ssl_ctx.ciphers != NULL)
1840 newsrv->ssl_ctx.ciphers = strdup(curproxy->defsrv.ssl_ctx.ciphers);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001841 if (curproxy->defsrv.sni_expr != NULL)
1842 newsrv->sni_expr = strdup(curproxy->defsrv.sni_expr);
Frédéric Lécaille7c8cd582017-03-13 13:41:16 +01001843#endif
Willy Tarreau272adea2014-03-31 10:39:59 +02001844
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01001845#ifdef TCP_USER_TIMEOUT
1846 newsrv->tcp_ut = curproxy->defsrv.tcp_ut;
1847#endif
1848
Willy Tarreau272adea2014-03-31 10:39:59 +02001849 cur_arg = 3;
1850 } else {
1851 newsrv = &curproxy->defsrv;
1852 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01001853 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02001854 }
1855
1856 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001857 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001858 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
1859 if (err) {
1860 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
1861 file, linenum, *err, newsrv->id);
1862 err_code |= ERR_ALERT | ERR_FATAL;
1863 goto out;
1864 }
1865 if (val <= 0) {
1866 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
1867 file, linenum, val, args[cur_arg], newsrv->id);
1868 err_code |= ERR_ALERT | ERR_FATAL;
1869 goto out;
1870 }
1871 newsrv->agent.inter = val;
1872 cur_arg += 2;
1873 }
Misiekea849332017-01-09 09:39:51 +01001874 else if (!strcmp(args[cur_arg], "agent-addr")) {
1875 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
1876 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
1877 goto out;
1878 }
1879
1880 cur_arg += 2;
1881 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001882 else if (!strcmp(args[cur_arg], "agent-port")) {
1883 global.maxsock++;
1884 newsrv->agent.port = atol(args[cur_arg + 1]);
1885 cur_arg += 2;
1886 }
James Brown55f9ff12015-10-21 18:19:05 -07001887 else if (!strcmp(args[cur_arg], "agent-send")) {
1888 global.maxsock++;
1889 free(newsrv->agent.send_string);
1890 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
1891 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
1892 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
1893 cur_arg += 2;
1894 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001895 else if (!strcmp(args[cur_arg], "init-addr")) {
1896 char *p, *end;
1897 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01001898 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02001899
1900 newsrv->init_addr_methods = 0;
1901 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
1902
1903 for (p = args[cur_arg + 1]; *p; p = end) {
1904 /* cut on next comma */
1905 for (end = p; *end && *end != ','; end++);
1906 if (*end)
1907 *(end++) = 0;
1908
Willy Tarreau4310d362016-11-02 15:05:56 +01001909 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02001910 if (!strcmp(p, "libc")) {
1911 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
1912 }
1913 else if (!strcmp(p, "last")) {
1914 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
1915 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01001916 else if (!strcmp(p, "none")) {
1917 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
1918 }
Willy Tarreau4310d362016-11-02 15:05:56 +01001919 else if (str2ip2(p, &sa, 0)) {
1920 if (is_addr(&newsrv->init_addr)) {
1921 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
1922 file, linenum, args[cur_arg], p);
1923 err_code |= ERR_ALERT | ERR_FATAL;
1924 goto out;
1925 }
1926 newsrv->init_addr = sa;
1927 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
1928 }
Baptiste Assmann25938272016-09-21 20:26:16 +02001929 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01001930 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02001931 file, linenum, args[cur_arg], p);
1932 err_code |= ERR_ALERT | ERR_FATAL;
1933 goto out;
1934 }
1935 if (!done) {
1936 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
1937 file, linenum, args[cur_arg], p);
1938 err_code |= ERR_ALERT | ERR_FATAL;
1939 goto out;
1940 }
1941 }
1942 cur_arg += 2;
1943 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001944 else if (!strcmp(args[cur_arg], "resolvers")) {
1945 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
1946 cur_arg += 2;
1947 }
1948 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
1949 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01001950 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001951 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01001952 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001953 else {
1954 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
1955 file, linenum, args[cur_arg]);
1956 err_code |= ERR_ALERT | ERR_FATAL;
1957 goto out;
1958 }
1959 cur_arg += 2;
1960 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001961 else if (!strcmp(args[cur_arg], "resolve-net")) {
1962 char *p, *e;
1963 unsigned char mask;
1964 struct dns_options *opt;
1965
1966 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
1967 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
1968 file, linenum, args[cur_arg]);
1969 err_code |= ERR_ALERT | ERR_FATAL;
1970 goto out;
1971 }
1972
1973 opt = &newsrv->dns_opts;
1974
1975 /* Split arguments by comma, and convert it from ipv4 or ipv6
1976 * string network in in_addr or in6_addr.
1977 */
1978 p = args[cur_arg + 1];
1979 e = p;
1980 while (*p != '\0') {
1981 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01001982 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001983 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
1984 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
1985 err_code |= ERR_ALERT | ERR_FATAL;
1986 goto out;
1987 }
1988 /* look for end or comma. */
1989 while (*e != ',' && *e != '\0')
1990 e++;
1991 if (*e == ',') {
1992 *e = '\0';
1993 e++;
1994 }
1995 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1996 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1997 /* Try to convert input string from ipv4 or ipv6 network. */
1998 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1999 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2000 &mask)) {
2001 /* Try to convert input string from ipv6 network. */
2002 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2003 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2004 } else {
2005 /* All network conversions fail, retrun error. */
2006 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2007 file, linenum, args[cur_arg], p);
2008 err_code |= ERR_ALERT | ERR_FATAL;
2009 goto out;
2010 }
2011 opt->pref_net_nb++;
2012 p = e;
2013 }
2014
2015 cur_arg += 2;
2016 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002017 else if (!strcmp(args[cur_arg], "rise")) {
2018 if (!*args[cur_arg + 1]) {
2019 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2020 file, linenum, args[cur_arg]);
2021 err_code |= ERR_ALERT | ERR_FATAL;
2022 goto out;
2023 }
2024
2025 newsrv->check.rise = atol(args[cur_arg + 1]);
2026 if (newsrv->check.rise <= 0) {
2027 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2028 file, linenum, args[cur_arg]);
2029 err_code |= ERR_ALERT | ERR_FATAL;
2030 goto out;
2031 }
2032
2033 if (newsrv->check.health)
2034 newsrv->check.health = newsrv->check.rise;
2035 cur_arg += 2;
2036 }
2037 else if (!strcmp(args[cur_arg], "fall")) {
2038 newsrv->check.fall = atol(args[cur_arg + 1]);
2039
2040 if (!*args[cur_arg + 1]) {
2041 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2042 file, linenum, args[cur_arg]);
2043 err_code |= ERR_ALERT | ERR_FATAL;
2044 goto out;
2045 }
2046
2047 if (newsrv->check.fall <= 0) {
2048 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2049 file, linenum, args[cur_arg]);
2050 err_code |= ERR_ALERT | ERR_FATAL;
2051 goto out;
2052 }
2053
2054 cur_arg += 2;
2055 }
2056 else if (!strcmp(args[cur_arg], "inter")) {
2057 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2058 if (err) {
2059 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2060 file, linenum, *err, newsrv->id);
2061 err_code |= ERR_ALERT | ERR_FATAL;
2062 goto out;
2063 }
2064 if (val <= 0) {
2065 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2066 file, linenum, val, args[cur_arg], newsrv->id);
2067 err_code |= ERR_ALERT | ERR_FATAL;
2068 goto out;
2069 }
2070 newsrv->check.inter = val;
2071 cur_arg += 2;
2072 }
2073 else if (!strcmp(args[cur_arg], "fastinter")) {
2074 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2075 if (err) {
2076 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2077 file, linenum, *err, newsrv->id);
2078 err_code |= ERR_ALERT | ERR_FATAL;
2079 goto out;
2080 }
2081 if (val <= 0) {
2082 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2083 file, linenum, val, args[cur_arg], newsrv->id);
2084 err_code |= ERR_ALERT | ERR_FATAL;
2085 goto out;
2086 }
2087 newsrv->check.fastinter = val;
2088 cur_arg += 2;
2089 }
2090 else if (!strcmp(args[cur_arg], "downinter")) {
2091 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2092 if (err) {
2093 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2094 file, linenum, *err, newsrv->id);
2095 err_code |= ERR_ALERT | ERR_FATAL;
2096 goto out;
2097 }
2098 if (val <= 0) {
2099 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2100 file, linenum, val, args[cur_arg], newsrv->id);
2101 err_code |= ERR_ALERT | ERR_FATAL;
2102 goto out;
2103 }
2104 newsrv->check.downinter = val;
2105 cur_arg += 2;
2106 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002107 else if (!strcmp(args[cur_arg], "port")) {
2108 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002109 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002110 cur_arg += 2;
2111 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002112 else if (!strcmp(args[cur_arg], "weight")) {
2113 int w;
2114 w = atol(args[cur_arg + 1]);
2115 if (w < 0 || w > SRV_UWGHT_MAX) {
2116 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2117 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2118 err_code |= ERR_ALERT | ERR_FATAL;
2119 goto out;
2120 }
2121 newsrv->uweight = newsrv->iweight = w;
2122 cur_arg += 2;
2123 }
2124 else if (!strcmp(args[cur_arg], "minconn")) {
2125 newsrv->minconn = atol(args[cur_arg + 1]);
2126 cur_arg += 2;
2127 }
2128 else if (!strcmp(args[cur_arg], "maxconn")) {
2129 newsrv->maxconn = atol(args[cur_arg + 1]);
2130 cur_arg += 2;
2131 }
2132 else if (!strcmp(args[cur_arg], "maxqueue")) {
2133 newsrv->maxqueue = atol(args[cur_arg + 1]);
2134 cur_arg += 2;
2135 }
2136 else if (!strcmp(args[cur_arg], "slowstart")) {
2137 /* slowstart is stored in seconds */
2138 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2139 if (err) {
2140 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2141 file, linenum, *err, newsrv->id);
2142 err_code |= ERR_ALERT | ERR_FATAL;
2143 goto out;
2144 }
2145 newsrv->slowstart = (val + 999) / 1000;
2146 cur_arg += 2;
2147 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002148 else if (!strcmp(args[cur_arg], "on-error")) {
2149 if (!strcmp(args[cur_arg + 1], "fastinter"))
2150 newsrv->onerror = HANA_ONERR_FASTINTER;
2151 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2152 newsrv->onerror = HANA_ONERR_FAILCHK;
2153 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2154 newsrv->onerror = HANA_ONERR_SUDDTH;
2155 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2156 newsrv->onerror = HANA_ONERR_MARKDWN;
2157 else {
2158 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2159 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2160 file, linenum, args[cur_arg], args[cur_arg + 1]);
2161 err_code |= ERR_ALERT | ERR_FATAL;
2162 goto out;
2163 }
2164
2165 cur_arg += 2;
2166 }
2167 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2168 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2169 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2170 else {
2171 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2172 file, linenum, args[cur_arg], args[cur_arg + 1]);
2173 err_code |= ERR_ALERT | ERR_FATAL;
2174 goto out;
2175 }
2176
2177 cur_arg += 2;
2178 }
2179 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2180 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2181 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2182 else {
2183 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2184 file, linenum, args[cur_arg], args[cur_arg + 1]);
2185 err_code |= ERR_ALERT | ERR_FATAL;
2186 goto out;
2187 }
2188
2189 cur_arg += 2;
2190 }
2191 else if (!strcmp(args[cur_arg], "error-limit")) {
2192 if (!*args[cur_arg + 1]) {
2193 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2194 file, linenum, args[cur_arg]);
2195 err_code |= ERR_ALERT | ERR_FATAL;
2196 goto out;
2197 }
2198
2199 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2200
2201 if (newsrv->consecutive_errors_limit <= 0) {
2202 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2203 file, linenum, args[cur_arg]);
2204 err_code |= ERR_ALERT | ERR_FATAL;
2205 goto out;
2206 }
2207 cur_arg += 2;
2208 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002209 else if (!defsrv && !strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
2210 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2211 file, linenum, "usesrc", "source");
2212 err_code |= ERR_ALERT | ERR_FATAL;
2213 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002214 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002215 else {
2216 static int srv_dumped;
2217 struct srv_kw *kw;
2218 char *err;
2219
2220 kw = srv_find_kw(args[cur_arg]);
2221 if (kw) {
2222 char *err = NULL;
2223 int code;
2224
2225 if (!kw->parse) {
2226 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2227 file, linenum, args[0], args[1], args[cur_arg]);
2228 cur_arg += 1 + kw->skip ;
2229 err_code |= ERR_ALERT | ERR_FATAL;
2230 goto out;
2231 }
2232
2233 if (defsrv && !kw->default_ok) {
2234 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2235 file, linenum, args[0], args[1], args[cur_arg]);
2236 cur_arg += 1 + kw->skip ;
2237 err_code |= ERR_ALERT;
2238 continue;
2239 }
2240
2241 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2242 err_code |= code;
2243
2244 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002245 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002246 if (code & ERR_FATAL) {
2247 free(err);
2248 cur_arg += 1 + kw->skip;
2249 goto out;
2250 }
2251 }
2252 free(err);
2253 cur_arg += 1 + kw->skip;
2254 continue;
2255 }
2256
2257 err = NULL;
2258 if (!srv_dumped) {
2259 srv_dump_kws(&err);
2260 indent_msg(&err, 4);
2261 srv_dumped = 1;
2262 }
2263
2264 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2265 file, linenum, args[0], args[1], args[cur_arg],
2266 err ? " Registered keywords :" : "", err ? err : "");
2267 free(err);
2268
2269 err_code |= ERR_ALERT | ERR_FATAL;
2270 goto out;
2271 }
2272 }
2273
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01002274 /* This check is done only for 'server' instances. */
2275 if (!defsrv && newsrv->do_check) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002276 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02002277
2278 if (newsrv->trackit) {
2279 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
2280 file, linenum);
2281 err_code |= ERR_ALERT | ERR_FATAL;
2282 goto out;
2283 }
2284
Willy Tarreau272adea2014-03-31 10:39:59 +02002285 /*
2286 * We need at least a service port, a check port or the first tcp-check rule must
Willy Tarreau5cf0b522014-05-09 23:59:19 +02002287 * be a 'connect' one when checking an IPv4/IPv6 server.
Willy Tarreau272adea2014-03-31 10:39:59 +02002288 */
Baptiste Assmann95db2bc2016-06-13 14:15:41 +02002289 if ((srv_check_healthcheck_port(&newsrv->check) == 0) &&
Simon Horman41f58762015-01-30 11:22:56 +09002290 (is_inet_addr(&newsrv->check.addr) ||
2291 (!is_addr(&newsrv->check.addr) && is_inet_addr(&newsrv->addr)))) {
Willy Tarreau1a786d72016-03-08 15:20:25 +01002292 struct tcpcheck_rule *r = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002293 struct list *l;
2294
2295 r = (struct tcpcheck_rule *)newsrv->proxy->tcpcheck_rules.n;
2296 if (!r) {
2297 Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
2298 file, linenum, newsrv->id);
2299 err_code |= ERR_ALERT | ERR_FATAL;
2300 goto out;
2301 }
Baptiste Assmannbaf97942015-12-04 06:49:31 +01002302 /* search the first action (connect / send / expect) in the list */
2303 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01002304 list_for_each_entry(r, l, list) {
Baptiste Assmannbaf97942015-12-04 06:49:31 +01002305 if (r->action != TCPCHK_ACT_COMMENT)
2306 break;
2307 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002308 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
2309 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",
2310 file, linenum, newsrv->id);
2311 err_code |= ERR_ALERT | ERR_FATAL;
2312 goto out;
2313 }
2314 else {
2315 /* scan the tcp-check ruleset to ensure a port has been configured */
2316 l = &newsrv->proxy->tcpcheck_rules;
Willy Tarreau1a786d72016-03-08 15:20:25 +01002317 list_for_each_entry(r, l, list) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002318 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
2319 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",
2320 file, linenum, newsrv->id);
2321 err_code |= ERR_ALERT | ERR_FATAL;
2322 goto out;
2323 }
2324 }
2325 }
2326 }
2327
2328 /* note: check type will be set during the config review phase */
Simon Hormanb1900d52015-01-30 11:22:54 +09002329 ret = init_check(&newsrv->check, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002330 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002331 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2332 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002333 goto out;
2334 }
2335
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002336 if (newsrv->resolution)
Thierry Fournierada34842016-02-17 21:25:09 +01002337 newsrv->resolution->opts = &newsrv->dns_opts;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002338
Willy Tarreau272adea2014-03-31 10:39:59 +02002339 newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01002340 global.maxsock++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002341 }
2342
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002343 if (!defsrv && newsrv->do_agent) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002344 const char *ret;
Willy Tarreau272adea2014-03-31 10:39:59 +02002345
2346 if (!newsrv->agent.port) {
2347 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
2348 file, linenum, newsrv->id);
2349 err_code |= ERR_ALERT | ERR_FATAL;
2350 goto out;
2351 }
2352
2353 if (!newsrv->agent.inter)
2354 newsrv->agent.inter = newsrv->check.inter;
2355
Simon Hormanb1900d52015-01-30 11:22:54 +09002356 ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
Willy Tarreau272adea2014-03-31 10:39:59 +02002357 if (ret) {
Simon Hormanb1900d52015-01-30 11:22:54 +09002358 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2359 err_code |= ERR_ALERT | ERR_ABORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002360 goto out;
2361 }
2362
2363 newsrv->agent.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002364 global.maxsock++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002365 }
2366
2367 if (!defsrv) {
Willy Tarreauc93cd162014-05-13 15:54:22 +02002368 if (newsrv->flags & SRV_F_BACKUP)
Willy Tarreau272adea2014-03-31 10:39:59 +02002369 curproxy->srv_bck++;
2370 else
2371 curproxy->srv_act++;
2372
Willy Tarreauc5150da2014-05-13 19:27:31 +02002373 srv_lb_commit_status(newsrv);
Willy Tarreau272adea2014-03-31 10:39:59 +02002374 }
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002375#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2376 if (!defsrv && newsrv->sni_expr) {
2377 int code;
2378 char *err;
2379
2380 err = NULL;
2381
2382 code = server_parse_sni_expr(newsrv, curproxy, &err);
2383 err_code |= code;
2384 if (code) {
2385 display_parser_err(file, linenum, args, cur_arg, &err);
2386 free(err);
2387 if (code & ERR_FATAL)
2388 goto out;
2389 }
2390 }
2391#endif
Willy Tarreau272adea2014-03-31 10:39:59 +02002392 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002393 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002394 return 0;
2395
2396 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002397 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002398 free(errmsg);
2399 return err_code;
2400}
2401
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002402/* Returns a pointer to the first server matching either id <id>.
2403 * NULL is returned if no match is found.
2404 * the lookup is performed in the backend <bk>
2405 */
2406struct server *server_find_by_id(struct proxy *bk, int id)
2407{
2408 struct eb32_node *eb32;
2409 struct server *curserver;
2410
2411 if (!bk || (id ==0))
2412 return NULL;
2413
2414 /* <bk> has no backend capabilities, so it can't have a server */
2415 if (!(bk->cap & PR_CAP_BE))
2416 return NULL;
2417
2418 curserver = NULL;
2419
2420 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2421 if (eb32)
2422 curserver = container_of(eb32, struct server, conf.id);
2423
2424 return curserver;
2425}
2426
2427/* Returns a pointer to the first server matching either name <name>, or id
2428 * if <name> starts with a '#'. NULL is returned if no match is found.
2429 * the lookup is performed in the backend <bk>
2430 */
2431struct server *server_find_by_name(struct proxy *bk, const char *name)
2432{
2433 struct server *curserver;
2434
2435 if (!bk || !name)
2436 return NULL;
2437
2438 /* <bk> has no backend capabilities, so it can't have a server */
2439 if (!(bk->cap & PR_CAP_BE))
2440 return NULL;
2441
2442 curserver = NULL;
2443 if (*name == '#') {
2444 curserver = server_find_by_id(bk, atoi(name + 1));
2445 if (curserver)
2446 return curserver;
2447 }
2448 else {
2449 curserver = bk->srv;
2450
2451 while (curserver && (strcmp(curserver->id, name) != 0))
2452 curserver = curserver->next;
2453
2454 if (curserver)
2455 return curserver;
2456 }
2457
2458 return NULL;
2459}
2460
2461struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2462{
2463 struct server *byname;
2464 struct server *byid;
2465
2466 if (!name && !id)
2467 return NULL;
2468
2469 if (diff)
2470 *diff = 0;
2471
2472 byname = byid = NULL;
2473
2474 if (name) {
2475 byname = server_find_by_name(bk, name);
2476 if (byname && (!id || byname->puid == id))
2477 return byname;
2478 }
2479
2480 /* remaining possibilities :
2481 * - name not set
2482 * - name set but not found
2483 * - name found but ID doesn't match
2484 */
2485 if (id) {
2486 byid = server_find_by_id(bk, id);
2487 if (byid) {
2488 if (byname) {
2489 /* use id only if forced by configuration */
2490 if (byid->flags & SRV_F_FORCED_ID) {
2491 if (diff)
2492 *diff |= 2;
2493 return byid;
2494 }
2495 else {
2496 if (diff)
2497 *diff |= 1;
2498 return byname;
2499 }
2500 }
2501
2502 /* remaining possibilities:
2503 * - name not set
2504 * - name set but not found
2505 */
2506 if (name && diff)
2507 *diff |= 2;
2508 return byid;
2509 }
2510
2511 /* id bot found */
2512 if (byname) {
2513 if (diff)
2514 *diff |= 1;
2515 return byname;
2516 }
2517 }
2518
2519 return NULL;
2520}
2521
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002522/* Update a server state using the parameters available in the params list */
2523static void srv_update_state(struct server *srv, int version, char **params)
2524{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002525 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002526 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002527
2528 /* fields since version 1
2529 * and common to all other upcoming versions
2530 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002531 enum srv_state srv_op_state;
2532 enum srv_admin srv_admin_state;
2533 unsigned srv_uweight, srv_iweight;
2534 unsigned long srv_last_time_change;
2535 short srv_check_status;
2536 enum chk_result srv_check_result;
2537 int srv_check_health;
2538 int srv_check_state, srv_agent_state;
2539 int bk_f_forced_id;
2540 int srv_f_forced_id;
2541
Willy Tarreau31138fa2015-09-29 18:38:47 +02002542 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002543 switch (version) {
2544 case 1:
2545 /*
2546 * now we can proceed with server's state update:
2547 * srv_addr: params[0]
2548 * srv_op_state: params[1]
2549 * srv_admin_state: params[2]
2550 * srv_uweight: params[3]
2551 * srv_iweight: params[4]
2552 * srv_last_time_change: params[5]
2553 * srv_check_status: params[6]
2554 * srv_check_result: params[7]
2555 * srv_check_health: params[8]
2556 * srv_check_state: params[9]
2557 * srv_agent_state: params[10]
2558 * bk_f_forced_id: params[11]
2559 * srv_f_forced_id: params[12]
2560 */
2561
2562 /* validating srv_op_state */
2563 p = NULL;
2564 errno = 0;
2565 srv_op_state = strtol(params[1], &p, 10);
2566 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2567 (srv_op_state != SRV_ST_STOPPED &&
2568 srv_op_state != SRV_ST_STARTING &&
2569 srv_op_state != SRV_ST_RUNNING &&
2570 srv_op_state != SRV_ST_STOPPING)) {
2571 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2572 }
2573
2574 /* validating srv_admin_state */
2575 p = NULL;
2576 errno = 0;
2577 srv_admin_state = strtol(params[2], &p, 10);
Willy Tarreau757478e2016-11-03 19:22:19 +01002578
2579 /* inherited statuses will be recomputed later */
2580 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT;
2581
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002582 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2583 (srv_admin_state != 0 &&
2584 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002585 srv_admin_state != SRV_ADMF_CMAINT &&
2586 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002587 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002588 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002589 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2590 }
2591
2592 /* validating srv_uweight */
2593 p = NULL;
2594 errno = 0;
2595 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002596 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002597 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2598
2599 /* validating srv_iweight */
2600 p = NULL;
2601 errno = 0;
2602 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002603 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002604 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2605
2606 /* validating srv_last_time_change */
2607 p = NULL;
2608 errno = 0;
2609 srv_last_time_change = strtol(params[5], &p, 10);
2610 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2611 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2612
2613 /* validating srv_check_status */
2614 p = NULL;
2615 errno = 0;
2616 srv_check_status = strtol(params[6], &p, 10);
2617 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2618 (srv_check_status >= HCHK_STATUS_SIZE))
2619 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2620
2621 /* validating srv_check_result */
2622 p = NULL;
2623 errno = 0;
2624 srv_check_result = strtol(params[7], &p, 10);
2625 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2626 (srv_check_result != CHK_RES_UNKNOWN &&
2627 srv_check_result != CHK_RES_NEUTRAL &&
2628 srv_check_result != CHK_RES_FAILED &&
2629 srv_check_result != CHK_RES_PASSED &&
2630 srv_check_result != CHK_RES_CONDPASS)) {
2631 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2632 }
2633
2634 /* validating srv_check_health */
2635 p = NULL;
2636 errno = 0;
2637 srv_check_health = strtol(params[8], &p, 10);
2638 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2639 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2640
2641 /* validating srv_check_state */
2642 p = NULL;
2643 errno = 0;
2644 srv_check_state = strtol(params[9], &p, 10);
2645 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2646 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2647 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2648
2649 /* validating srv_agent_state */
2650 p = NULL;
2651 errno = 0;
2652 srv_agent_state = strtol(params[10], &p, 10);
2653 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2654 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2655 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2656
2657 /* validating bk_f_forced_id */
2658 p = NULL;
2659 errno = 0;
2660 bk_f_forced_id = strtol(params[11], &p, 10);
2661 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2662 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2663
2664 /* validating srv_f_forced_id */
2665 p = NULL;
2666 errno = 0;
2667 srv_f_forced_id = strtol(params[12], &p, 10);
2668 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2669 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2670
2671
2672 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002673 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002674 goto out;
2675
2676 /* recover operational state and apply it to this server
2677 * and all servers tracking this one */
2678 switch (srv_op_state) {
2679 case SRV_ST_STOPPED:
2680 srv->check.health = 0;
2681 srv_set_stopped(srv, "changed from server-state after a reload");
2682 break;
2683 case SRV_ST_STARTING:
2684 srv->state = srv_op_state;
2685 break;
2686 case SRV_ST_STOPPING:
2687 srv->check.health = srv->check.rise + srv->check.fall - 1;
2688 srv_set_stopping(srv, "changed from server-state after a reload");
2689 break;
2690 case SRV_ST_RUNNING:
2691 srv->check.health = srv->check.rise + srv->check.fall - 1;
2692 srv_set_running(srv, "");
2693 break;
2694 }
2695
2696 /* When applying server state, the following rules apply:
2697 * - in case of a configuration change, we apply the setting from the new
2698 * configuration, regardless of old running state
2699 * - if no configuration change, we apply old running state only if old running
2700 * state is different from new configuration state
2701 */
2702 /* configuration has changed */
2703 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
2704 if (srv->admin & SRV_ADMF_CMAINT)
2705 srv_adm_set_maint(srv);
2706 else
2707 srv_adm_set_ready(srv);
2708 }
2709 /* configuration is the same, let's compate old running state and new conf state */
2710 else {
2711 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
2712 srv_adm_set_maint(srv);
2713 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
2714 srv_adm_set_ready(srv);
2715 }
2716 /* apply drain mode if server is currently enabled */
2717 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
2718 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002719 * (srv->iweight is the weight set up in configuration).
2720 * There are two possible reasons for FDRAIN to have been present :
2721 * - previous config weight was zero
2722 * - "set server b/s drain" was sent to the CLI
2723 *
2724 * In the first case, we simply want to drop this drain state
2725 * if the new weight is not zero anymore, meaning the administrator
2726 * has intentionally turned the weight back to a positive value to
2727 * enable the server again after an operation. In the second case,
2728 * the drain state was forced on the CLI regardless of the config's
2729 * weight so we don't want a change to the config weight to lose this
2730 * status. What this means is :
2731 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2732 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002733 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002734 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002735 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002736 }
2737
2738 srv->last_change = date.tv_sec - srv_last_time_change;
2739 srv->check.status = srv_check_status;
2740 srv->check.result = srv_check_result;
2741 srv->check.health = srv_check_health;
2742
2743 /* Only case we want to apply is removing ENABLED flag which could have been
2744 * done by the "disable health" command over the stats socket
2745 */
2746 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2747 (srv_check_state & CHK_ST_CONFIGURED) &&
2748 !(srv_check_state & CHK_ST_ENABLED))
2749 srv->check.state &= ~CHK_ST_ENABLED;
2750
2751 /* Only case we want to apply is removing ENABLED flag which could have been
2752 * done by the "disable agent" command over the stats socket
2753 */
2754 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2755 (srv_agent_state & CHK_ST_CONFIGURED) &&
2756 !(srv_agent_state & CHK_ST_ENABLED))
2757 srv->agent.state &= ~CHK_ST_ENABLED;
2758
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002759 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2760 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002761 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002762 * It means that a configuration file change has precedence over a unix socket change
2763 * for server's weight
2764 *
2765 * by default, HAProxy applies the following weight when parsing the configuration
2766 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002767 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002768 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002769 srv->uweight = srv_uweight;
2770 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002771 server_recalc_eweight(srv);
2772
Willy Tarreaue5a60682016-11-09 14:54:53 +01002773 /* load server IP address */
2774 srv->lastaddr = strdup(params[0]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002775 break;
2776 default:
2777 chunk_appendf(msg, ", version '%d' not supported", version);
2778 }
2779
2780 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002781 if (msg->len) {
2782 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002783 Warning("server-state application failed for server '%s/%s'%s",
2784 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002785 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002786}
2787
2788/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2789 * For each proxy, it does the following:
2790 * - opens its server state file (either one or local one)
2791 * - read whole file, line by line
2792 * - analyse each line to check if it matches our current backend:
2793 * - backend name matches
2794 * - backend id matches if id is forced and name doesn't match
2795 * - if the server pointed by the line is found, then state is applied
2796 *
2797 * If the running backend uuid or id differs from the state file, then HAProxy reports
2798 * a warning.
2799 */
2800void apply_server_state(void)
2801{
2802 char *cur, *end;
2803 char mybuf[SRV_STATE_LINE_MAXLEN];
2804 int mybuflen;
2805 char *params[SRV_STATE_FILE_MAX_FIELDS];
2806 char *srv_params[SRV_STATE_FILE_MAX_FIELDS];
2807 int arg, srv_arg, version, diff;
2808 FILE *f;
2809 char *filepath;
2810 char globalfilepath[MAXPATHLEN + 1];
2811 char localfilepath[MAXPATHLEN + 1];
2812 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2813 extern struct proxy *proxy;
2814 struct proxy *curproxy, *bk;
2815 struct server *srv;
2816
2817 globalfilepathlen = 0;
2818 /* create the globalfilepath variable */
2819 if (global.server_state_file) {
2820 /* absolute path or no base directory provided */
2821 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2822 len = strlen(global.server_state_file);
2823 if (len > MAXPATHLEN) {
2824 globalfilepathlen = 0;
2825 goto globalfileerror;
2826 }
2827 memcpy(globalfilepath, global.server_state_file, len);
2828 globalfilepath[len] = '\0';
2829 globalfilepathlen = len;
2830 }
2831 else if (global.server_state_base) {
2832 len = strlen(global.server_state_base);
2833 globalfilepathlen += len;
2834
2835 if (globalfilepathlen > MAXPATHLEN) {
2836 globalfilepathlen = 0;
2837 goto globalfileerror;
2838 }
2839 strncpy(globalfilepath, global.server_state_base, len);
2840 globalfilepath[globalfilepathlen] = 0;
2841
2842 /* append a slash if needed */
2843 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2844 if (globalfilepathlen + 1 > MAXPATHLEN) {
2845 globalfilepathlen = 0;
2846 goto globalfileerror;
2847 }
2848 globalfilepath[globalfilepathlen++] = '/';
2849 }
2850
2851 len = strlen(global.server_state_file);
2852 if (globalfilepathlen + len > MAXPATHLEN) {
2853 globalfilepathlen = 0;
2854 goto globalfileerror;
2855 }
2856 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2857 globalfilepathlen += len;
2858 globalfilepath[globalfilepathlen++] = 0;
2859 }
2860 }
2861 globalfileerror:
2862 if (globalfilepathlen == 0)
2863 globalfilepath[0] = '\0';
2864
2865 /* read servers state from local file */
2866 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2867 /* servers are only in backends */
2868 if (!(curproxy->cap & PR_CAP_BE))
2869 continue;
2870 fileopenerr = 0;
2871 filepath = NULL;
2872
2873 /* search server state file path and name */
2874 switch (curproxy->load_server_state_from_file) {
2875 /* read servers state from global file */
2876 case PR_SRV_STATE_FILE_GLOBAL:
2877 /* there was an error while generating global server state file path */
2878 if (globalfilepathlen == 0)
2879 continue;
2880 filepath = globalfilepath;
2881 fileopenerr = 1;
2882 break;
2883 /* this backend has its own file */
2884 case PR_SRV_STATE_FILE_LOCAL:
2885 localfilepathlen = 0;
2886 localfilepath[0] = '\0';
2887 len = 0;
2888 /* create the localfilepath variable */
2889 /* absolute path or no base directory provided */
2890 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2891 len = strlen(curproxy->server_state_file_name);
2892 if (len > MAXPATHLEN) {
2893 localfilepathlen = 0;
2894 goto localfileerror;
2895 }
2896 memcpy(localfilepath, curproxy->server_state_file_name, len);
2897 localfilepath[len] = '\0';
2898 localfilepathlen = len;
2899 }
2900 else if (global.server_state_base) {
2901 len = strlen(global.server_state_base);
2902 localfilepathlen += len;
2903
2904 if (localfilepathlen > MAXPATHLEN) {
2905 localfilepathlen = 0;
2906 goto localfileerror;
2907 }
2908 strncpy(localfilepath, global.server_state_base, len);
2909 localfilepath[localfilepathlen] = 0;
2910
2911 /* append a slash if needed */
2912 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
2913 if (localfilepathlen + 1 > MAXPATHLEN) {
2914 localfilepathlen = 0;
2915 goto localfileerror;
2916 }
2917 localfilepath[localfilepathlen++] = '/';
2918 }
2919
2920 len = strlen(curproxy->server_state_file_name);
2921 if (localfilepathlen + len > MAXPATHLEN) {
2922 localfilepathlen = 0;
2923 goto localfileerror;
2924 }
2925 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
2926 localfilepathlen += len;
2927 localfilepath[localfilepathlen++] = 0;
2928 }
2929 filepath = localfilepath;
2930 localfileerror:
2931 if (localfilepathlen == 0)
2932 localfilepath[0] = '\0';
2933
2934 break;
2935 case PR_SRV_STATE_FILE_NONE:
2936 default:
2937 continue;
2938 }
2939
2940 /* preload global state file */
2941 errno = 0;
2942 f = fopen(filepath, "r");
2943 if (errno && fileopenerr)
2944 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
2945 if (!f)
2946 continue;
2947
2948 mybuf[0] = '\0';
2949 mybuflen = 0;
2950 version = 0;
2951
2952 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01002953 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
2954 Warning("Can't read first line of the server state file '%s'\n", filepath);
2955 goto fileclose;
2956 }
2957
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002958 cur = mybuf;
2959 version = atoi(cur);
2960 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
2961 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01002962 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002963
2964 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
2965 int bk_f_forced_id = 0;
2966 int check_id = 0;
2967 int check_name = 0;
2968
2969 mybuflen = strlen(mybuf);
2970 cur = mybuf;
2971 end = cur + mybuflen;
2972
2973 bk = NULL;
2974 srv = NULL;
2975
2976 /* we need at least one character */
2977 if (mybuflen == 0)
2978 continue;
2979
2980 /* ignore blank characters at the beginning of the line */
2981 while (isspace(*cur))
2982 ++cur;
2983
2984 if (cur == end)
2985 continue;
2986
2987 /* ignore comment line */
2988 if (*cur == '#')
2989 continue;
2990
2991 /* we're now ready to move the line into *srv_params[] */
2992 params[0] = cur;
2993 arg = 1;
2994 srv_arg = 0;
2995 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
2996 if (isspace(*cur)) {
2997 *cur = '\0';
2998 ++cur;
2999 while (isspace(*cur))
3000 ++cur;
3001 switch (version) {
3002 case 1:
3003 /*
3004 * srv_addr: params[4] => srv_params[0]
3005 * srv_op_state: params[5] => srv_params[1]
3006 * srv_admin_state: params[6] => srv_params[2]
3007 * srv_uweight: params[7] => srv_params[3]
3008 * srv_iweight: params[8] => srv_params[4]
3009 * srv_last_time_change: params[9] => srv_params[5]
3010 * srv_check_status: params[10] => srv_params[6]
3011 * srv_check_result: params[11] => srv_params[7]
3012 * srv_check_health: params[12] => srv_params[8]
3013 * srv_check_state: params[13] => srv_params[9]
3014 * srv_agent_state: params[14] => srv_params[10]
3015 * bk_f_forced_id: params[15] => srv_params[11]
3016 * srv_f_forced_id: params[16] => srv_params[12]
3017 */
3018 if (arg >= 4) {
3019 srv_params[srv_arg] = cur;
3020 ++srv_arg;
3021 }
3022 break;
3023 }
3024
3025 params[arg] = cur;
3026 ++arg;
3027 }
3028 else {
3029 ++cur;
3030 }
3031 }
3032
3033 /* if line is incomplete line, then ignore it.
3034 * otherwise, update useful flags */
3035 switch (version) {
3036 case 1:
3037 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3038 continue;
3039 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3040 check_id = (atoi(params[0]) == curproxy->uuid);
3041 check_name = (strcmp(curproxy->id, params[1]) == 0);
3042 break;
3043 }
3044
3045 diff = 0;
3046 bk = curproxy;
3047
3048 /* if backend can't be found, let's continue */
3049 if (!check_id && !check_name)
3050 continue;
3051 else if (!check_id && check_name) {
3052 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3053 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3054 }
3055 else if (check_id && !check_name) {
3056 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3057 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3058 /* if name doesn't match, we still want to update curproxy if the backend id
3059 * was forced in previous the previous configuration */
3060 if (!bk_f_forced_id)
3061 continue;
3062 }
3063
3064 /* look for the server by its id: param[2] */
3065 /* else look for the server by its name: param[3] */
3066 diff = 0;
3067 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3068
3069 if (!srv) {
3070 /* if no server found, then warning and continue with next line */
3071 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3072 params[3], params[2], params[0], params[1]);
3073 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3074 params[3], params[2], params[0], params[1]);
3075 continue;
3076 }
3077 else if (diff & PR_FBM_MISMATCH_ID) {
3078 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);
3079 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);
3080 }
3081 else if (diff & PR_FBM_MISMATCH_NAME) {
3082 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);
3083 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);
3084 }
3085
3086 /* now we can proceed with server's state update */
3087 srv_update_state(srv, version, srv_params);
3088 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003089fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003090 fclose(f);
3091 }
3092}
3093
Simon Horman7d09b9a2013-02-12 10:45:51 +09003094/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003095 * update a server's current IP address.
3096 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3097 * ip is in network format.
3098 * updater is a string which contains an information about the requester of the update.
3099 * updater is used if not NULL.
3100 *
3101 * A log line and a stderr warning message is generated based on server's backend options.
3102 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003103int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003104{
3105 /* generates a log line and a warning on stderr */
3106 if (1) {
3107 /* book enough space for both IPv4 and IPv6 */
3108 char oldip[INET6_ADDRSTRLEN];
3109 char newip[INET6_ADDRSTRLEN];
3110
3111 memset(oldip, '\0', INET6_ADDRSTRLEN);
3112 memset(newip, '\0', INET6_ADDRSTRLEN);
3113
3114 /* copy old IP address in a string */
3115 switch (s->addr.ss_family) {
3116 case AF_INET:
3117 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3118 break;
3119 case AF_INET6:
3120 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3121 break;
3122 };
3123
3124 /* copy new IP address in a string */
3125 switch (ip_sin_family) {
3126 case AF_INET:
3127 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3128 break;
3129 case AF_INET6:
3130 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3131 break;
3132 };
3133
3134 /* save log line into a buffer */
3135 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3136 s->proxy->id, s->id, oldip, newip, updater);
3137
3138 /* write the buffer on stderr */
3139 Warning("%s.\n", trash.str);
3140
3141 /* send a log */
3142 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3143 }
3144
3145 /* save the new IP family */
3146 s->addr.ss_family = ip_sin_family;
3147 /* save the new IP address */
3148 switch (ip_sin_family) {
3149 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003150 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003151 break;
3152 case AF_INET6:
3153 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3154 break;
3155 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003156 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003157
3158 return 0;
3159}
3160
3161/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003162 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3163 *
3164 * Caller can pass its name through <updater> to get it integrated in the response message
3165 * returned by the function.
3166 *
3167 * The function first does the following, in that order:
3168 * - validates the new addr and/or port
3169 * - checks if an update is required (new IP or port is different than current ones)
3170 * - checks the update is allowed:
3171 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3172 * - allow all changes if no CHECKS are configured
3173 * - if CHECK is configured:
3174 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3175 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3176 * conditions are met
3177 */
3178const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3179{
3180 struct sockaddr_storage sa;
3181 int ret, port_change_required;
3182 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003183 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003184 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003185 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003186
3187 msg = get_trash_chunk();
3188 chunk_reset(msg);
3189
3190 if (addr) {
3191 memset(&sa, 0, sizeof(struct sockaddr_storage));
3192 if (str2ip2(addr, &sa, 0) == NULL) {
3193 chunk_printf(msg, "Invalid addr '%s'", addr);
3194 goto out;
3195 }
3196
3197 /* changes are allowed on AF_INET* families only */
3198 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3199 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3200 goto out;
3201 }
3202
3203 /* collecting data currently setup */
3204 memset(current_addr, '\0', sizeof(current_addr));
3205 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3206 /* changes are allowed on AF_INET* families only */
3207 if ((ret != AF_INET) && (ret != AF_INET6)) {
3208 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3209 goto out;
3210 }
3211
3212 /* applying ADDR changes if required and allowed
3213 * ipcmp returns 0 when both ADDR are the same
3214 */
3215 if (ipcmp(&s->addr, &sa) == 0) {
3216 chunk_appendf(msg, "no need to change the addr");
3217 goto port;
3218 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003219 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003220 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003221
3222 /* we also need to update check's ADDR only if it uses the server's one */
3223 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003224 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003225 }
3226
3227 /* we also need to update agent ADDR only if it use the server's one */
3228 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003229 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003230 }
3231
3232 /* update report for caller */
3233 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3234 }
3235
3236 port:
3237 if (port) {
3238 char sign = '\0';
3239 char *endptr;
3240
3241 if (addr)
3242 chunk_appendf(msg, ", ");
3243
3244 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003245 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003246
3247 /* check if PORT change is required */
3248 port_change_required = 0;
3249
3250 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003251 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003252 new_port = strtol(port, &endptr, 10);
3253 if ((errno != 0) || (port == endptr)) {
3254 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3255 goto out;
3256 }
3257
3258 /* check if caller triggers a port mapped or offset */
3259 if (sign == '-' || (sign == '+')) {
3260 /* check if server currently uses port map */
3261 if (!(s->flags & SRV_F_MAPPORTS)) {
3262 /* switch from fixed port to port map mandatorily triggers
3263 * a port change */
3264 port_change_required = 1;
3265 /* check is configured
3266 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3267 * prevent PORT change if check doesn't have it's dedicated port while switching
3268 * to port mapping */
3269 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3270 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.");
3271 goto out;
3272 }
3273 }
3274 /* we're already using port maps */
3275 else {
3276 port_change_required = current_port != new_port;
3277 }
3278 }
3279 /* fixed port */
3280 else {
3281 port_change_required = current_port != new_port;
3282 }
3283
3284 /* applying PORT changes if required and update response message */
3285 if (port_change_required) {
3286 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003287 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003288 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003289
3290 /* prepare message */
3291 chunk_appendf(msg, "port changed from '");
3292 if (s->flags & SRV_F_MAPPORTS)
3293 chunk_appendf(msg, "+");
3294 chunk_appendf(msg, "%d' to '", current_port);
3295
3296 if (sign == '-') {
3297 s->flags |= SRV_F_MAPPORTS;
3298 chunk_appendf(msg, "%c", sign);
3299 /* just use for result output */
3300 new_port = -new_port;
3301 }
3302 else if (sign == '+') {
3303 s->flags |= SRV_F_MAPPORTS;
3304 chunk_appendf(msg, "%c", sign);
3305 }
3306 else {
3307 s->flags &= ~SRV_F_MAPPORTS;
3308 }
3309
3310 chunk_appendf(msg, "%d'", new_port);
3311
3312 /* we also need to update health checks port only if it uses server's realport */
3313 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3314 s->check.port = new_port;
3315 }
3316 }
3317 else {
3318 chunk_appendf(msg, "no need to change the port");
3319 }
3320 }
3321
3322out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003323 if (changed)
3324 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003325 if (updater)
3326 chunk_appendf(msg, " by '%s'", updater);
3327 chunk_appendf(msg, "\n");
3328 return msg->str;
3329}
3330
3331
3332/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003333 * update server status based on result of name resolution
3334 * returns:
3335 * 0 if server status is updated
3336 * 1 if server status has not changed
3337 */
3338int snr_update_srv_status(struct server *s)
3339{
3340 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003341 struct dns_resolvers *resolvers;
3342
3343 resolvers = resolution->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003344
3345 switch (resolution->status) {
3346 case RSLV_STATUS_NONE:
3347 /* status when HAProxy has just (re)started */
3348 trigger_resolution(s);
3349 break;
3350
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003351 case RSLV_STATUS_VALID:
3352 /*
3353 * resume health checks
3354 * server will be turned back on if health check is safe
3355 */
3356 if (!(s->admin & SRV_ADMF_RMAINT))
3357 return 1;
3358 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3359 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3360 s->proxy->id, s->id);
3361
3362 Warning("%s.\n", trash.str);
3363 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3364 return 0;
3365
3366 case RSLV_STATUS_NX:
3367 /* stop server if resolution is NX for a long enough period */
3368 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
3369 if (s->admin & SRV_ADMF_RMAINT)
3370 return 1;
3371 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3372 return 0;
3373 }
3374 break;
3375
3376 case RSLV_STATUS_TIMEOUT:
3377 /* stop server if resolution is TIMEOUT for a long enough period */
3378 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
3379 if (s->admin & SRV_ADMF_RMAINT)
3380 return 1;
3381 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3382 return 0;
3383 }
3384 break;
3385
3386 case RSLV_STATUS_REFUSED:
3387 /* stop server if resolution is REFUSED for a long enough period */
3388 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
3389 if (s->admin & SRV_ADMF_RMAINT)
3390 return 1;
3391 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3392 return 0;
3393 }
3394 break;
3395
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003396 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003397 /* stop server if resolution is in unmatched error for a long enough period */
3398 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
3399 if (s->admin & SRV_ADMF_RMAINT)
3400 return 1;
3401 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3402 return 0;
3403 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003404 break;
3405 }
3406
3407 return 1;
3408}
3409
3410/*
3411 * Server Name Resolution valid response callback
3412 * It expects:
3413 * - <nameserver>: the name server which answered the valid response
3414 * - <response>: buffer containing a valid DNS response
3415 * - <response_len>: size of <response>
3416 * It performs the following actions:
3417 * - ignore response if current ip found and server family not met
3418 * - update with first new ip found if family is met and current IP is not found
3419 * returns:
3420 * 0 on error
3421 * 1 when no error or safe ignore
3422 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003423int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003424{
3425 struct server *s;
3426 void *serverip, *firstip;
3427 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003428 int ret;
3429 struct chunk *chk = get_trash_chunk();
3430
3431 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003432 firstip = NULL; /* pointer to the first valid response found */
3433 /* it will be used as the new IP if a change is required */
3434 firstip_sin_family = AF_UNSPEC;
3435 serverip = NULL; /* current server IP address */
3436
3437 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003438 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003439
3440 /* initializing server IP pointer */
3441 server_sin_family = s->addr.ss_family;
3442 switch (server_sin_family) {
3443 case AF_INET:
3444 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3445 break;
3446
3447 case AF_INET6:
3448 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3449 break;
3450
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003451 case AF_UNSPEC:
3452 break;
3453
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003454 default:
3455 goto invalid;
3456 }
3457
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003458 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01003459 serverip, server_sin_family, &firstip,
3460 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003461
3462 switch (ret) {
3463 case DNS_UPD_NO:
3464 if (resolution->status != RSLV_STATUS_VALID) {
3465 resolution->status = RSLV_STATUS_VALID;
3466 resolution->last_status_change = now_ms;
3467 }
3468 goto stop_resolution;
3469
3470 case DNS_UPD_SRVIP_NOT_FOUND:
3471 goto save_ip;
3472
3473 case DNS_UPD_CNAME:
3474 if (resolution->status != RSLV_STATUS_VALID) {
3475 resolution->status = RSLV_STATUS_VALID;
3476 resolution->last_status_change = now_ms;
3477 }
3478 goto invalid;
3479
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003480 case DNS_UPD_NO_IP_FOUND:
3481 if (resolution->status != RSLV_STATUS_OTHER) {
3482 resolution->status = RSLV_STATUS_OTHER;
3483 resolution->last_status_change = now_ms;
3484 }
3485 goto stop_resolution;
3486
Baptiste Assmannfad03182015-10-28 02:03:32 +01003487 case DNS_UPD_NAME_ERROR:
3488 /* if this is not the last expected response, we ignore it */
3489 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
3490 return 0;
3491 /* update resolution status to OTHER error type */
3492 if (resolution->status != RSLV_STATUS_OTHER) {
3493 resolution->status = RSLV_STATUS_OTHER;
3494 resolution->last_status_change = now_ms;
3495 }
3496 goto stop_resolution;
3497
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003498 default:
3499 goto invalid;
3500
3501 }
3502
3503 save_ip:
3504 nameserver->counters.update += 1;
3505 if (resolution->status != RSLV_STATUS_VALID) {
3506 resolution->status = RSLV_STATUS_VALID;
3507 resolution->last_status_change = now_ms;
3508 }
3509
3510 /* save the first ip we found */
3511 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3512 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3513
3514 stop_resolution:
3515 /* update last resolution date and time */
3516 resolution->last_resolution = now_ms;
3517 /* reset current status flag */
3518 resolution->step = RSLV_STEP_NONE;
3519 /* reset values */
3520 dns_reset_resolution(resolution);
3521
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003522 dns_update_resolvers_timeout(nameserver->resolvers);
3523
3524 snr_update_srv_status(s);
3525 return 0;
3526
3527 invalid:
3528 nameserver->counters.invalid += 1;
3529 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3530 goto stop_resolution;
3531
3532 snr_update_srv_status(s);
3533 return 0;
3534}
3535
3536/*
3537 * Server Name Resolution error management callback
3538 * returns:
3539 * 0 on error
3540 * 1 when no error or safe ignore
3541 */
3542int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
3543{
3544 struct server *s;
3545 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003546 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003547
3548 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003549 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003550 resolvers = resolution->resolvers;
3551
3552 /* can be ignored if this is not the last response */
3553 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3554 return 1;
3555 }
3556
3557 switch (error_code) {
3558 case DNS_RESP_INVALID:
3559 case DNS_RESP_WRONG_NAME:
3560 if (resolution->status != RSLV_STATUS_INVALID) {
3561 resolution->status = RSLV_STATUS_INVALID;
3562 resolution->last_status_change = now_ms;
3563 }
3564 break;
3565
3566 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02003567 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003568 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02003569 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02003570 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01003571 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
3572 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02003573
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003574 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003575 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003576 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003577 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02003578 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003579 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02003580 }
3581 else if (res_preferred_afinet) {
3582 /* fallback from A to AAAA */
3583 resolution->query_type = DNS_RTYPE_AAAA;
3584 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003585 else {
3586 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01003587 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003588 resolution->query_type = DNS_RTYPE_A;
3589 } else {
3590 resolution->query_type = DNS_RTYPE_AAAA;
3591 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02003592 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003593
3594 dns_send_query(resolution);
3595
3596 /*
3597 * move the resolution to the last element of the FIFO queue
3598 * and update timeout wakeup based on the new first entry
3599 */
3600 if (dns_check_resolution_queue(resolvers) > 1) {
3601 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02003602 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003603 /* ex first resolution goes to the end of the queue */
3604 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
3605 }
3606 dns_update_resolvers_timeout(resolvers);
3607 goto leave;
3608 }
3609 else {
3610 if (resolution->status != RSLV_STATUS_OTHER) {
3611 resolution->status = RSLV_STATUS_OTHER;
3612 resolution->last_status_change = now_ms;
3613 }
3614 }
3615 break;
3616
3617 case DNS_RESP_NX_DOMAIN:
3618 if (resolution->status != RSLV_STATUS_NX) {
3619 resolution->status = RSLV_STATUS_NX;
3620 resolution->last_status_change = now_ms;
3621 }
3622 break;
3623
3624 case DNS_RESP_REFUSED:
3625 if (resolution->status != RSLV_STATUS_REFUSED) {
3626 resolution->status = RSLV_STATUS_REFUSED;
3627 resolution->last_status_change = now_ms;
3628 }
3629 break;
3630
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003631 case DNS_RESP_TIMEOUT:
3632 if (resolution->status != RSLV_STATUS_TIMEOUT) {
3633 resolution->status = RSLV_STATUS_TIMEOUT;
3634 resolution->last_status_change = now_ms;
3635 }
3636 break;
3637 }
3638
3639 /* update last resolution date and time */
3640 resolution->last_resolution = now_ms;
3641 /* reset current status flag */
3642 resolution->step = RSLV_STEP_NONE;
3643 /* reset values */
3644 dns_reset_resolution(resolution);
3645
3646 LIST_DEL(&resolution->list);
3647 dns_update_resolvers_timeout(resolvers);
3648
3649 leave:
3650 snr_update_srv_status(s);
3651 return 1;
3652}
3653
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003654/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3655 * resolver. This is suited for initial address configuration. Returns 0 on
3656 * success otherwise a non-zero error code. In case of error, *err_code, if
3657 * not NULL, is filled up.
3658 */
3659int srv_set_addr_via_libc(struct server *srv, int *err_code)
3660{
3661 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003662 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003663 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003664 return 1;
3665 }
3666 return 0;
3667}
3668
3669/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3670 * from the state file. This is suited for initial address configuration.
3671 * Returns 0 on success otherwise a non-zero error code. In case of error,
3672 * *err_code, if not NULL, is filled up.
3673 */
3674static int srv_apply_lastaddr(struct server *srv, int *err_code)
3675{
3676 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3677 if (err_code)
3678 *err_code |= ERR_WARN;
3679 return 1;
3680 }
3681 return 0;
3682}
3683
Willy Tarreau25e51522016-11-04 15:10:17 +01003684/* returns 0 if no error, otherwise a combination of ERR_* flags */
3685static int srv_iterate_initaddr(struct server *srv)
3686{
3687 int return_code = 0;
3688 int err_code;
3689 unsigned int methods;
3690
3691 methods = srv->init_addr_methods;
3692 if (!methods) { // default to "last,libc"
3693 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3694 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3695 }
3696
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003697 /* "-dr" : always append "none" so that server addresses resolution
3698 * failures are silently ignored, this is convenient to validate some
3699 * configs out of their environment.
3700 */
3701 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3702 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3703
Willy Tarreau25e51522016-11-04 15:10:17 +01003704 while (methods) {
3705 err_code = 0;
3706 switch (srv_get_next_initaddr(&methods)) {
3707 case SRV_IADDR_LAST:
3708 if (!srv->lastaddr)
3709 continue;
3710 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003711 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003712 return_code |= err_code;
3713 break;
3714
3715 case SRV_IADDR_LIBC:
3716 if (!srv->hostname)
3717 continue;
3718 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003719 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003720 return_code |= err_code;
3721 break;
3722
Willy Tarreau37ebe122016-11-04 15:17:58 +01003723 case SRV_IADDR_NONE:
3724 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003725 if (return_code) {
3726 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3727 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3728 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003729 return return_code;
3730
Willy Tarreau4310d362016-11-02 15:05:56 +01003731 case SRV_IADDR_IP:
3732 ipcpy(&srv->init_addr, &srv->addr);
3733 if (return_code) {
3734 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3735 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3736 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003737 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003738
Willy Tarreau25e51522016-11-04 15:10:17 +01003739 default: /* unhandled method */
3740 break;
3741 }
3742 }
3743
3744 if (!return_code) {
3745 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3746 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3747 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003748 else {
3749 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3750 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3751 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003752
3753 return_code |= ERR_ALERT | ERR_FATAL;
3754 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003755out:
3756 srv_set_dyncookie(srv);
3757 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003758}
3759
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003760/*
3761 * This function parses all backends and all servers within each backend
3762 * and performs servers' addr resolution based on information provided by:
3763 * - configuration file
3764 * - server-state file (states provided by an 'old' haproxy process)
3765 *
3766 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3767 */
3768int srv_init_addr(void)
3769{
3770 struct proxy *curproxy;
3771 int return_code = 0;
3772
3773 curproxy = proxy;
3774 while (curproxy) {
3775 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003776
3777 /* servers are in backend only */
3778 if (!(curproxy->cap & PR_CAP_BE))
3779 goto srv_init_addr_next;
3780
Willy Tarreau25e51522016-11-04 15:10:17 +01003781 for (srv = curproxy->srv; srv; srv = srv->next)
3782 if (srv->hostname)
3783 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003784
3785 srv_init_addr_next:
3786 curproxy = curproxy->next;
3787 }
3788
3789 return return_code;
3790}
3791
Willy Tarreau21b069d2016-11-23 17:15:08 +01003792/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3793 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003794 * 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 +01003795 * used for CLI commands requiring a server name.
3796 * Important: the <arg> is modified to remove the '/'.
3797 */
3798struct server *cli_find_server(struct appctx *appctx, char *arg)
3799{
3800 struct proxy *px;
3801 struct server *sv;
3802 char *line;
3803
3804 /* split "backend/server" and make <line> point to server */
3805 for (line = arg; *line; line++)
3806 if (*line == '/') {
3807 *line++ = '\0';
3808 break;
3809 }
3810
3811 if (!*line || !*arg) {
3812 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003813 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003814 return NULL;
3815 }
3816
3817 if (!get_backend_server(arg, line, &px, &sv)) {
3818 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003819 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003820 return NULL;
3821 }
3822
3823 if (px->state == PR_STSTOPPED) {
3824 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003825 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003826 return NULL;
3827 }
3828
3829 return sv;
3830}
3831
William Lallemand222baf22016-11-19 02:00:33 +01003832
3833static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
3834{
3835 struct server *sv;
3836 const char *warning;
3837
3838 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3839 return 1;
3840
3841 sv = cli_find_server(appctx, args[2]);
3842 if (!sv)
3843 return 1;
3844
3845 if (strcmp(args[3], "weight") == 0) {
3846 warning = server_parse_weight_change_request(sv, args[4]);
3847 if (warning) {
3848 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003849 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003850 }
3851 }
3852 else if (strcmp(args[3], "state") == 0) {
3853 if (strcmp(args[4], "ready") == 0)
3854 srv_adm_set_ready(sv);
3855 else if (strcmp(args[4], "drain") == 0)
3856 srv_adm_set_drain(sv);
3857 else if (strcmp(args[4], "maint") == 0)
3858 srv_adm_set_maint(sv);
3859 else {
3860 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003861 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003862 }
3863 }
3864 else if (strcmp(args[3], "health") == 0) {
3865 if (sv->track) {
3866 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003867 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003868 }
3869 else if (strcmp(args[4], "up") == 0) {
3870 sv->check.health = sv->check.rise + sv->check.fall - 1;
3871 srv_set_running(sv, "changed from CLI");
3872 }
3873 else if (strcmp(args[4], "stopping") == 0) {
3874 sv->check.health = sv->check.rise + sv->check.fall - 1;
3875 srv_set_stopping(sv, "changed from CLI");
3876 }
3877 else if (strcmp(args[4], "down") == 0) {
3878 sv->check.health = 0;
3879 srv_set_stopped(sv, "changed from CLI");
3880 }
3881 else {
3882 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003883 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003884 }
3885 }
3886 else if (strcmp(args[3], "agent") == 0) {
3887 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3888 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003889 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003890 }
3891 else if (strcmp(args[4], "up") == 0) {
3892 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
3893 srv_set_running(sv, "changed from CLI");
3894 }
3895 else if (strcmp(args[4], "down") == 0) {
3896 sv->agent.health = 0;
3897 srv_set_stopped(sv, "changed from CLI");
3898 }
3899 else {
3900 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003901 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003902 }
3903 }
Misiek2da082d2017-01-09 09:40:42 +01003904 else if (strcmp(args[3], "agent-addr") == 0) {
3905 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3906 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3907 appctx->st0 = CLI_ST_PRINT;
3908 } else {
3909 if (str2ip(args[4], &sv->agent.addr) == NULL) {
3910 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
3911 appctx->st0 = CLI_ST_PRINT;
3912 }
3913 }
3914 }
3915 else if (strcmp(args[3], "agent-send") == 0) {
3916 if (!(sv->agent.state & CHK_ST_ENABLED)) {
3917 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
3918 appctx->st0 = CLI_ST_PRINT;
3919 } else {
3920 char *nss = strdup(args[4]);
3921 if (!nss) {
3922 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
3923 appctx->st0 = CLI_ST_PRINT;
3924 } else {
3925 free(sv->agent.send_string);
3926 sv->agent.send_string = nss;
3927 sv->agent.send_string_len = strlen(args[4]);
3928 }
3929 }
3930 }
William Lallemand222baf22016-11-19 02:00:33 +01003931 else if (strcmp(args[3], "check-port") == 0) {
3932 int i = 0;
3933 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
3934 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003935 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003936 }
3937 if ((i < 0) || (i > 65535)) {
3938 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003939 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003940 }
3941 /* prevent the update of port to 0 if MAPPORTS are in use */
3942 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
3943 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003944 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003945 return 1;
3946 }
3947 sv->check.port = i;
3948 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003949 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003950 }
3951 else if (strcmp(args[3], "addr") == 0) {
3952 char *addr = NULL;
3953 char *port = NULL;
3954 if (strlen(args[4]) == 0) {
3955 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003956 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003957 return 1;
3958 }
3959 else {
3960 addr = args[4];
3961 }
3962 if (strcmp(args[5], "port") == 0) {
3963 port = args[6];
3964 }
3965 warning = update_server_addr_port(sv, addr, port, "stats socket command");
3966 if (warning) {
3967 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003968 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003969 }
3970 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
3971 }
3972 else {
3973 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 +01003974 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003975 }
3976 return 1;
3977}
3978
William Lallemand6b160942016-11-22 12:34:35 +01003979static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
3980{
3981 struct stream_interface *si = appctx->owner;
3982 struct proxy *px;
3983 struct server *sv;
3984 char *line;
3985
3986
3987 /* split "backend/server" and make <line> point to server */
3988 for (line = args[2]; *line; line++)
3989 if (*line == '/') {
3990 *line++ = '\0';
3991 break;
3992 }
3993
3994 if (!*line) {
3995 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003996 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01003997 return 1;
3998 }
3999
4000 if (!get_backend_server(args[2], line, &px, &sv)) {
4001 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004002 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004003 return 1;
4004 }
4005
4006 /* return server's effective weight at the moment */
4007 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004008 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004009 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004010 return 0;
4011 }
William Lallemand6b160942016-11-22 12:34:35 +01004012 return 1;
4013}
4014
4015static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4016{
4017 struct server *sv;
4018 const char *warning;
4019
4020 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4021 return 1;
4022
4023 sv = cli_find_server(appctx, args[2]);
4024 if (!sv)
4025 return 1;
4026
4027 warning = server_parse_weight_change_request(sv, args[3]);
4028 if (warning) {
4029 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004030 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004031 }
4032 return 1;
4033}
4034
Willy Tarreaub8026272016-11-23 11:26:56 +01004035/* parse a "set maxconn server" command. It always returns 1. */
4036static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4037{
4038 struct server *sv;
4039 const char *warning;
4040
4041 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4042 return 1;
4043
4044 sv = cli_find_server(appctx, args[3]);
4045 if (!sv)
4046 return 1;
4047
4048 warning = server_parse_maxconn_change_request(sv, args[4]);
4049 if (warning) {
4050 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004051 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004052 }
4053 return 1;
4054}
William Lallemand6b160942016-11-22 12:34:35 +01004055
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004056/* parse a "disable agent" command. It always returns 1. */
4057static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4058{
4059 struct server *sv;
4060
4061 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4062 return 1;
4063
4064 sv = cli_find_server(appctx, args[2]);
4065 if (!sv)
4066 return 1;
4067
4068 sv->agent.state &= ~CHK_ST_ENABLED;
4069 return 1;
4070}
4071
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004072/* parse a "disable health" command. It always returns 1. */
4073static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4074{
4075 struct server *sv;
4076
4077 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4078 return 1;
4079
4080 sv = cli_find_server(appctx, args[2]);
4081 if (!sv)
4082 return 1;
4083
4084 sv->check.state &= ~CHK_ST_ENABLED;
4085 return 1;
4086}
4087
Willy Tarreauffb4d582016-11-24 12:47:00 +01004088/* parse a "disable server" command. It always returns 1. */
4089static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4090{
4091 struct server *sv;
4092
4093 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4094 return 1;
4095
4096 sv = cli_find_server(appctx, args[2]);
4097 if (!sv)
4098 return 1;
4099
4100 srv_adm_set_maint(sv);
4101 return 1;
4102}
4103
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004104/* parse a "enable agent" command. It always returns 1. */
4105static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4106{
4107 struct server *sv;
4108
4109 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4110 return 1;
4111
4112 sv = cli_find_server(appctx, args[2]);
4113 if (!sv)
4114 return 1;
4115
4116 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
4117 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004118 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004119 return 1;
4120 }
4121
4122 sv->agent.state |= CHK_ST_ENABLED;
4123 return 1;
4124}
4125
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004126/* parse a "enable health" command. It always returns 1. */
4127static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4128{
4129 struct server *sv;
4130
4131 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4132 return 1;
4133
4134 sv = cli_find_server(appctx, args[2]);
4135 if (!sv)
4136 return 1;
4137
4138 sv->check.state |= CHK_ST_ENABLED;
4139 return 1;
4140}
4141
Willy Tarreauffb4d582016-11-24 12:47:00 +01004142/* parse a "enable server" command. It always returns 1. */
4143static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4144{
4145 struct server *sv;
4146
4147 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4148 return 1;
4149
4150 sv = cli_find_server(appctx, args[2]);
4151 if (!sv)
4152 return 1;
4153
4154 srv_adm_set_ready(sv);
4155 return 1;
4156}
4157
William Lallemand222baf22016-11-19 02:00:33 +01004158/* register cli keywords */
4159static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004160 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004161 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004162 { { "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 +01004163 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004164 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004165 { { "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 +01004166 { { "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 +01004167 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004168 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4169 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4170
William Lallemand222baf22016-11-19 02:00:33 +01004171 {{},}
4172}};
4173
4174__attribute__((constructor))
4175static void __server_init(void)
4176{
4177 cli_register_kw(&cli_kws);
4178}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004179
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004180/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004181 * Local variables:
4182 * c-indent-level: 8
4183 * c-basic-offset: 8
4184 * End:
4185 */