blob: b5904e93af192bd0cbffaba6041fcb61f652ddb9 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
Olivier Houchard4e694042017-03-14 20:01:29 +010017#include <import/xxhash.h>
18
Willy Tarreau272adea2014-03-31 10:39:59 +020019#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020020#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020021#include <common/errors.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010022#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020023#include <common/time.h>
24
William Lallemand222baf22016-11-19 02:00:33 +010025#include <types/applet.h>
26#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020027#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010028#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020029#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010030#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020031
William Lallemand222baf22016-11-19 02:00:33 +010032#include <proto/applet.h>
33#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090034#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020035#include <proto/port_range.h>
36#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020037#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010038#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020039#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020040#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010041#include <proto/stream_interface.h>
42#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020043#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020044#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010045#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020046
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);
Frédéric Lécailleb418c122017-04-26 11:24:02 +020049static int srv_set_fqdn(struct server *srv, const char *fqdn);
Willy Tarreaubaaee002006-06-26 02:48:02 +020050
Willy Tarreau21faa912012-10-10 08:27:36 +020051/* List head of all known server keywords */
52static struct srv_kw_list srv_keywords = {
53 .list = LIST_HEAD_INIT(srv_keywords.list)
54};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020055
Simon Hormana3608442013-11-01 16:46:15 +090056int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020057{
Emeric Brun52a91d32017-08-31 14:41:55 +020058 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020059 return s->down_time;
60
61 return now.tv_sec - s->last_change + s->down_time;
62}
Willy Tarreaubaaee002006-06-26 02:48:02 +020063
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050064int srv_lastsession(const struct server *s)
65{
66 if (s->counters.last_sess)
67 return now.tv_sec - s->counters.last_sess;
68
69 return -1;
70}
71
Simon Horman4a741432013-02-23 15:35:38 +090072int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020073{
Simon Horman4a741432013-02-23 15:35:38 +090074 const struct server *s = check->server;
75
Willy Tarreauff5ae352013-12-11 20:36:34 +010076 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090077 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010078
Emeric Brun52a91d32017-08-31 14:41:55 +020079 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090080 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010081
Simon Horman4a741432013-02-23 15:35:38 +090082 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010083}
84
Olivier Houchard4e694042017-03-14 20:01:29 +010085void srv_set_dyncookie(struct server *s)
86{
87 struct proxy *p = s->proxy;
88 struct server *tmpserv;
89 char *tmpbuf;
90 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010091 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +010092 size_t buffer_len;
93 int addr_len;
94 int port;
95
96 if ((s->flags & SRV_F_COOKIESET) ||
97 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
98 s->proxy->dyncookie_key == NULL)
99 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100100 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100101
102 if (s->addr.ss_family != AF_INET &&
103 s->addr.ss_family != AF_INET6)
104 return;
105 /*
106 * Buffer to calculate the cookie value.
107 * The buffer contains the secret key + the server IP address
108 * + the TCP port.
109 */
110 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
111 /*
112 * The TCP port should use only 2 bytes, but is stored in
113 * an unsigned int in struct server, so let's use 4, to be
114 * on the safe side.
115 */
116 buffer_len = key_len + addr_len + 4;
117 tmpbuf = trash.str;
118 memcpy(tmpbuf, p->dyncookie_key, key_len);
119 memcpy(&(tmpbuf[key_len]),
120 s->addr.ss_family == AF_INET ?
121 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
122 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
123 addr_len);
124 /*
125 * Make sure it's the same across all the load balancers,
126 * no matter their endianness.
127 */
128 port = htonl(s->svc_port);
129 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
130 hash_value = XXH64(tmpbuf, buffer_len, 0);
131 memprintf(&s->cookie, "%016llx", hash_value);
132 if (!s->cookie)
133 return;
134 s->cklen = 16;
135 /*
136 * Check that we did not get a hash collision.
137 * Unlikely, but it can happen.
138 */
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200139 for (tmpserv = p->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);
Olivier Houchard4e694042017-03-14 20:01:29 +0100148 }
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200149 }
Olivier Houchard4e694042017-03-14 20:01:29 +0100150}
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{
Emeric Brun52a91d32017-08-31 14:41:55 +0200320 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
321 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100322 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{
Emeric Brun52a91d32017-08-31 14:41:55 +0200331 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
332 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100333 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) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200796 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200797 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;
Emeric Brun52a91d32017-08-31 14:41:55 +0200823 int srv_was_stopping = (s->next_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
Emeric Brun52a91d32017-08-31 14:41:55 +0200827 if ((s->next_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200828 return;
829
830 s->last_change = now.tv_sec;
Emeric Brun52a91d32017-08-31 14:41:55 +0200831 s->next_state = SRV_ST_STOPPED;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200832 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
Emeric Brun52a91d32017-08-31 14:41:55 +0200878 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200879 return;
880
Emeric Brun52a91d32017-08-31 14:41:55 +0200881 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200882 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
Emeric Brun52a91d32017-08-31 14:41:55 +0200890 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200891 s->down_time += now.tv_sec - s->last_change;
892
893 s->last_change = now.tv_sec;
894
Emeric Brun52a91d32017-08-31 14:41:55 +0200895 s->next_state = SRV_ST_STARTING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200896 if (s->slowstart > 0)
897 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
898 else
Emeric Brun52a91d32017-08-31 14:41:55 +0200899 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200900
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) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200909 !(s->flags & SRV_F_BACKUP) && s->next_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
Emeric Brun52a91d32017-08-31 14:41:55 +0200943 if (s->next_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200944 return;
945
Emeric Brun52a91d32017-08-31 14:41:55 +0200946 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200947 return;
948
949 s->last_change = now.tv_sec;
Emeric Brun52a91d32017-08-31 14:41:55 +0200950 s->next_state = SRV_ST_STOPPING;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200951 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 */
Emeric Brun52a91d32017-08-31 14:41:55 +0200994 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200995 return;
996
Emeric Brun52a91d32017-08-31 14:41:55 +0200997 s->next_admin |= mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200998
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200999 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001000 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1001 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001002 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
Emeric Brun52a91d32017-08-31 14:41:55 +02001011 if (s->next_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 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001025 int srv_was_stopping = (s->next_state == SRV_ST_STOPPING) || (s->next_admin & SRV_ADMF_DRAIN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001026 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;
Emeric Brun52a91d32017-08-31 14:41:55 +02001030 s->next_state = SRV_ST_STOPPED;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001031 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 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001064 if ((mode & SRV_ADMF_DRAIN) && !(s->next_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 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001118 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001119 return;
1120
Emeric Brun52a91d32017-08-31 14:41:55 +02001121 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001122
Emeric Brun52a91d32017-08-31 14:41:55 +02001123 if (s->next_admin & SRV_ADMF_MAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001124 /* 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
Emeric Brun52a91d32017-08-31 14:41:55 +02001183 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001184 (!(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))) {
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001186
Willy Tarreau6fb4ba32017-09-21 17:37:38 +02001187 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
1188 if (s->last_change < now.tv_sec) // ignore negative times
1189 s->down_time += now.tv_sec - s->last_change;
1190
Emeric Brune1e39472017-09-21 15:45:44 +02001191 s->last_change = now.tv_sec;
Emeric Brun52a91d32017-08-31 14:41:55 +02001192 s->next_state = SRV_ST_STOPPING;
Willy Tarreau6fb4ba32017-09-21 17:37:38 +02001193 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001194 else {
Emeric Brune1e39472017-09-21 15:45:44 +02001195 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1196 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1197 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1198 s->proxy->last_change = now.tv_sec;
1199 }
1200
1201 if (s->last_change < now.tv_sec) // ignore negative times
1202 s->down_time += now.tv_sec - s->last_change;
1203
1204 s->last_change = now.tv_sec;
Emeric Brun52a91d32017-08-31 14:41:55 +02001205 s->next_state = SRV_ST_STARTING;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001206 if (s->slowstart > 0)
1207 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
1208 else
Emeric Brun52a91d32017-08-31 14:41:55 +02001209 s->next_state = SRV_ST_RUNNING;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001210
Emeric Brune1e39472017-09-21 15:45:44 +02001211 server_recalc_eweight(s);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001212
Emeric Brune1e39472017-09-21 15:45:44 +02001213 /* If the server is set with "on-marked-up shutdown-backup-sessions",
1214 * and it's not a backup server and its effective weight is > 0,
1215 * then it can accept new connections, so we shut down all streams
1216 * on all backup servers.
1217 */
1218 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
1219 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
1220 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
1221
1222 /* check if we can handle some connections queued at the proxy. We
1223 * will take as many as we can handle.
1224 */
1225 xferred = pendconn_grab_from_px(s);
1226 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001227
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001228 }
1229
1230 if (mode & SRV_ADMF_FMAINT) {
1231 chunk_printf(&trash,
1232 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
1233 s->flags & SRV_F_BACKUP ? "Backup " : "",
1234 s->proxy->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +02001235 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1236 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001237 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001238 else if (mode & SRV_ADMF_RMAINT) {
1239 chunk_printf(&trash,
1240 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
1241 s->flags & SRV_F_BACKUP ? "Backup " : "",
1242 s->proxy->id, s->id, s->hostname,
Emeric Brun52a91d32017-08-31 14:41:55 +02001243 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1244 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaue6599732016-11-07 15:42:33 +01001245 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001246 else {
1247 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001248 "%sServer %s/%s is %s/%s (leaving maintenance)",
1249 s->flags & SRV_F_BACKUP ? "Backup " : "",
1250 s->proxy->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +02001251 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1252 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001253 srv_append_status(&trash, s, NULL, xferred, 0);
1254 }
1255 Warning("%s.\n", trash.str);
1256 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1257 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001258 else if ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001259 /* remaining in drain mode after removing one of its flags */
1260
1261 if (mode & SRV_ADMF_FDRAIN) {
1262 chunk_printf(&trash,
1263 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001264 s->flags & SRV_F_BACKUP ? "Backup " : "",
1265 s->proxy->id, s->id);
1266
1267 if (s->track) /* normally it's mandatory here */
1268 chunk_appendf(&trash, " via %s/%s",
1269 s->track->proxy->id, s->track->id);
1270 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001271 else {
1272 chunk_printf(&trash,
1273 "%sServer %s/%s remains in forced drain mode",
1274 s->flags & SRV_F_BACKUP ? "Backup " : "",
1275 s->proxy->id, s->id);
1276 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001277 Warning("%s.\n", trash.str);
1278 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001279 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001280 else if (mode & SRV_ADMF_DRAIN) {
1281 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001282 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1283 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1284 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1285 s->proxy->last_change = now.tv_sec;
1286 }
1287
1288 if (s->last_change < now.tv_sec) // ignore negative times
1289 s->down_time += now.tv_sec - s->last_change;
1290 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001291 server_recalc_eweight(s);
1292
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001293 if (mode & SRV_ADMF_FDRAIN) {
1294 chunk_printf(&trash,
1295 "%sServer %s/%s is %s (leaving forced drain)",
1296 s->flags & SRV_F_BACKUP ? "Backup " : "",
1297 s->proxy->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +02001298 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001299 }
1300 else {
1301 chunk_printf(&trash,
1302 "%sServer %s/%s is %s (leaving drain)",
1303 s->flags & SRV_F_BACKUP ? "Backup " : "",
1304 s->proxy->id, s->id,
Emeric Brun52a91d32017-08-31 14:41:55 +02001305 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001306 if (s->track) /* normally it's mandatory here */
1307 chunk_appendf(&trash, " via %s/%s",
1308 s->track->proxy->id, s->track->id);
1309 }
1310 Warning("%s.\n", trash.str);
1311 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001312 }
1313
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001314 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001315 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1316 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001317 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001318
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001319 if (mode & SRV_ADMF_MAINT)
1320 mode = SRV_ADMF_IMAINT;
1321 else if (mode & SRV_ADMF_DRAIN)
1322 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001323
1324 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001325 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001326}
1327
Willy Tarreau757478e2016-11-03 19:22:19 +01001328/* principle: propagate maint and drain to tracking servers. This is useful
1329 * upon startup so that inherited states are correct.
1330 */
1331static void srv_propagate_admin_state(struct server *srv)
1332{
1333 struct server *srv2;
1334
1335 if (!srv->trackers)
1336 return;
1337
1338 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001339 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001340 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001341
Emeric Brun52a91d32017-08-31 14:41:55 +02001342 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001343 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001344 }
1345}
1346
1347/* Compute and propagate the admin states for all servers in proxy <px>.
1348 * Only servers *not* tracking another one are considered, because other
1349 * ones will be handled when the server they track is visited.
1350 */
1351void srv_compute_all_admin_states(struct proxy *px)
1352{
1353 struct server *srv;
1354
1355 for (srv = px->srv; srv; srv = srv->next) {
1356 if (srv->track)
1357 continue;
1358 srv_propagate_admin_state(srv);
1359 }
1360}
1361
Willy Tarreaudff55432012-10-10 17:51:05 +02001362/* Note: must not be declared <const> as its list will be overwritten.
1363 * Please take care of keeping this list alphabetically sorted, doing so helps
1364 * all code contributors.
1365 * Optional keywords are also declared with a NULL ->parse() function so that
1366 * the config parser can report an appropriate error when a known keyword was
1367 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001368 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001369 */
1370static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001371 { "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 +01001372 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001373 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001374 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001375 { "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 +01001376 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001377 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1378 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001379 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001380 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001381 { "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 +01001382 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001383 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001384 { "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 +01001385 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1386 { "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 +01001387 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001388 { "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 +01001389 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001390 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1391 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1 }, /* Enforce use of PROXY V2 protocol */
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001392 { "source", srv_parse_source, -1, 1 }, /* Set the source address to be used to connect to the server */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001393 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001394 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001395 { NULL, NULL, 0 },
1396}};
1397
1398__attribute__((constructor))
1399static void __listener_init(void)
1400{
1401 srv_register_keywords(&srv_kws);
1402}
1403
Willy Tarreau004e0452013-11-21 11:22:01 +01001404/* Recomputes the server's eweight based on its state, uweight, the current time,
1405 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1406 * state is automatically disabled if the time is elapsed.
1407 */
1408void server_recalc_eweight(struct server *sv)
1409{
1410 struct proxy *px = sv->proxy;
1411 unsigned w;
1412
1413 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1414 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001415 if (sv->next_state == SRV_ST_STARTING)
1416 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001417 }
1418
1419 /* We must take care of not pushing the server to full throttle during slow starts.
1420 * It must also start immediately, at least at the minimal step when leaving maintenance.
1421 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001422 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001423 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1424 else
1425 w = px->lbprm.wdiv;
1426
Emeric Brun52a91d32017-08-31 14:41:55 +02001427 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001428
1429 /* now propagate the status change to any LB algorithms */
1430 if (px->lbprm.update_server_eweight)
1431 px->lbprm.update_server_eweight(sv);
Emeric Brun52a91d32017-08-31 14:41:55 +02001432 else if (srv_willbe_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +01001433 if (px->lbprm.set_server_status_up)
1434 px->lbprm.set_server_status_up(sv);
1435 }
1436 else {
1437 if (px->lbprm.set_server_status_down)
1438 px->lbprm.set_server_status_down(sv);
1439 }
1440}
1441
Willy Tarreaubaaee002006-06-26 02:48:02 +02001442/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001443 * Parses weight_str and configures sv accordingly.
1444 * Returns NULL on success, error message string otherwise.
1445 */
1446const char *server_parse_weight_change_request(struct server *sv,
1447 const char *weight_str)
1448{
1449 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001450 long int w;
1451 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001452
1453 px = sv->proxy;
1454
1455 /* if the weight is terminated with '%', it is set relative to
1456 * the initial weight, otherwise it is absolute.
1457 */
1458 if (!*weight_str)
1459 return "Require <weight> or <weight%>.\n";
1460
Simon Hormanb796afa2013-02-12 10:45:53 +09001461 w = strtol(weight_str, &end, 10);
1462 if (end == weight_str)
1463 return "Empty weight string empty or preceded by garbage";
1464 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001465 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001466 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001467 /* Avoid integer overflow */
1468 if (w > 25600)
1469 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001470 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001471 if (w > 256)
1472 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001473 }
1474 else if (w < 0 || w > 256)
1475 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001476 else if (end[0] != '\0')
1477 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001478
1479 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1480 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1481
1482 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001483 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001484
1485 return NULL;
1486}
1487
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001488/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001489 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1490 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001491 * Returns:
1492 * - error string on error
1493 * - NULL on success
1494 */
1495const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001496 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001497{
1498 unsigned char ip[INET6_ADDRSTRLEN];
1499
1500 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001501 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001502 return NULL;
1503 }
1504 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001505 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001506 return NULL;
1507 }
1508
1509 return "Could not understand IP address format.\n";
1510}
1511
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001512const char *server_parse_maxconn_change_request(struct server *sv,
1513 const char *maxconn_str)
1514{
1515 long int v;
1516 char *end;
1517
1518 if (!*maxconn_str)
1519 return "Require <maxconn>.\n";
1520
1521 v = strtol(maxconn_str, &end, 10);
1522 if (end == maxconn_str)
1523 return "maxconn string empty or preceded by garbage";
1524 else if (end[0] != '\0')
1525 return "Trailing garbage in maxconn string";
1526
1527 if (sv->maxconn == sv->minconn) { // static maxconn
1528 sv->maxconn = sv->minconn = v;
1529 } else { // dynamic maxconn
1530 sv->maxconn = v;
1531 }
1532
1533 if (may_dequeue_tasks(sv, sv->proxy))
1534 process_srv_queue(sv);
1535
1536 return NULL;
1537}
1538
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001539#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001540static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1541 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001542{
1543 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001544 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001545 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001546 NULL,
1547 };
1548
1549 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001550 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001551
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001552 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1553}
1554
1555static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1556{
1557 struct sample_expr *expr;
1558
1559 expr = srv_sni_sample_parse_expr(newsrv, px, px->conf.file, px->conf.line, err);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001560 if (!expr) {
1561 memprintf(err, "error detected while parsing sni expression : %s", *err);
1562 return ERR_ALERT | ERR_FATAL;
1563 }
1564
1565 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1566 memprintf(err, "error detected while parsing sni expression : "
1567 " fetch method '%s' extracts information from '%s', "
1568 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001569 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001570 return ERR_ALERT | ERR_FATAL;
1571 }
1572
1573 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1574 release_sample_expr(newsrv->ssl_ctx.sni);
1575 newsrv->ssl_ctx.sni = expr;
1576
1577 return 0;
1578}
1579#endif
1580
1581static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1582{
1583 if (err && *err) {
1584 indent_msg(err, 2);
1585 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1586 }
1587 else
1588 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1589 file, linenum, args[0], args[1], args[cur_arg]);
1590}
1591
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001592static void srv_conn_src_sport_range_cpy(struct server *srv,
1593 struct server *src)
1594{
1595 int range_sz;
1596
1597 range_sz = src->conn_src.sport_range->size;
1598 if (range_sz > 0) {
1599 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1600 if (srv->conn_src.sport_range != NULL) {
1601 int i;
1602
1603 for (i = 0; i < range_sz; i++) {
1604 srv->conn_src.sport_range->ports[i] =
1605 src->conn_src.sport_range->ports[i];
1606 }
1607 }
1608 }
1609}
1610
1611/*
1612 * Copy <src> server connection source settings to <srv> server everything needed.
1613 */
1614static void srv_conn_src_cpy(struct server *srv, struct server *src)
1615{
1616 srv->conn_src.opts = src->conn_src.opts;
1617 srv->conn_src.source_addr = src->conn_src.source_addr;
1618
1619 /* Source port range copy. */
1620 if (src->conn_src.sport_range != NULL)
1621 srv_conn_src_sport_range_cpy(srv, src);
1622
1623#ifdef CONFIG_HAP_TRANSPARENT
1624 if (src->conn_src.bind_hdr_name != NULL) {
1625 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1626 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1627 }
1628 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1629 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1630#endif
1631 if (src->conn_src.iface_name != NULL)
1632 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1633}
1634
1635/*
1636 * Copy <src> server SSL settings to <srv> server allocating
1637 * everything needed.
1638 */
1639#if defined(USE_OPENSSL)
1640static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1641{
1642 if (src->ssl_ctx.ca_file != NULL)
1643 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1644 if (src->ssl_ctx.crl_file != NULL)
1645 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1646 if (src->ssl_ctx.client_crt != NULL)
1647 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1648
1649 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1650
1651 if (src->ssl_ctx.verify_host != NULL)
1652 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1653 if (src->ssl_ctx.ciphers != NULL)
1654 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1655 if (src->sni_expr != NULL)
1656 srv->sni_expr = strdup(src->sni_expr);
1657}
1658#endif
1659
1660/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001661 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001662 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001663 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001664 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001665static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001666{
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001667 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001668 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001669
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001670 free(srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001671 srv->hostname = strdup(hostname);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001672
Baptiste Assmann42746372017-05-03 12:12:02 +02001673 srv->hostname_dn_len = dns_str_to_dn_label_len(hostname);
1674 srv->hostname_dn = calloc(srv->hostname_dn_len + 1, sizeof(char));
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001675
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001676 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001677 goto err;
1678
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001679 if (!dns_str_to_dn_label(srv->hostname,
Baptiste Assmann42746372017-05-03 12:12:02 +02001680 srv->hostname_dn,
1681 srv->hostname_dn_len + 1))
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001682 goto err;
1683
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001684 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001685
1686 err:
1687 free(srv->hostname);
1688 srv->hostname = NULL;
Baptiste Assmann42746372017-05-03 12:12:02 +02001689 free(srv->hostname_dn);
1690 srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001691 return -1;
1692}
1693
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001694/*
1695 * Free the link between a server and its resolution.
1696 * It also performs the following tasks:
1697 * - check if resolution can be moved back in the resolvers' pool
1698 * (and do it)
1699 * - move resolution's hostname_dn and hostname_dn_len to the next requester
1700 * available (when applied)
1701 */
Baptiste Assmann747359e2017-08-14 10:37:46 +02001702void srv_free_from_resolution(struct server *srv)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001703{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001704 struct dns_requester *requester;
1705 int count;
1706
1707 /* check if we can move the resolution back to the pool.
1708 * if <count> is greater than 1, then we can't */
1709 count = 0;
1710 list_for_each_entry(requester, &srv->resolution->requester.wait, list) {
1711 ++count;
1712 if (count > 1)
1713 break;
1714 }
1715 list_for_each_entry(requester, &srv->resolution->requester.curr, list) {
1716 ++count;
1717 if (count > 1)
1718 break;
1719 }
1720 if (count <= 1) {
1721 /* move the resolution back to the pool */
1722 dns_resolution_free(srv->resolvers, srv->resolution);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001723 return;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001724 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001725
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001726 dns_rm_requester_from_resolution(srv->dns_requester, srv->resolution);
1727
1728 return;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001729}
1730
1731/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001732 * Copy <src> server settings to <srv> server allocating
1733 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001734 * This function is not supposed to be called at any time, but only
1735 * during server settings parsing or during server allocations from
1736 * a server template, and just after having calloc()'ed a new server.
1737 * So, <src> may only be a default server (when parsing server settings)
1738 * or a server template (during server allocations from a server template).
1739 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1740 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001741 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001742static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001743{
1744 /* Connection source settings copy */
1745 srv_conn_src_cpy(srv, src);
1746
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001747 if (srv_tmpl) {
1748 srv->addr = src->addr;
1749 srv->svc_port = src->svc_port;
1750 }
1751
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001752 srv->pp_opts = src->pp_opts;
1753 if (src->rdr_pfx != NULL) {
1754 srv->rdr_pfx = strdup(src->rdr_pfx);
1755 srv->rdr_len = src->rdr_len;
1756 }
1757 if (src->cookie != NULL) {
1758 srv->cookie = strdup(src->cookie);
1759 srv->cklen = src->cklen;
1760 }
1761 srv->use_ssl = src->use_ssl;
1762 srv->check.addr = srv->agent.addr = src->check.addr;
1763 srv->check.use_ssl = src->check.use_ssl;
1764 srv->check.port = src->check.port;
1765 /* Note: 'flags' field has potentially been already initialized. */
1766 srv->flags |= src->flags;
1767 srv->do_check = src->do_check;
1768 srv->do_agent = src->do_agent;
1769 if (srv->check.port)
1770 srv->flags |= SRV_F_CHECKPORT;
1771 srv->check.inter = src->check.inter;
1772 srv->check.fastinter = src->check.fastinter;
1773 srv->check.downinter = src->check.downinter;
1774 srv->agent.use_ssl = src->agent.use_ssl;
1775 srv->agent.port = src->agent.port;
1776 if (src->agent.send_string != NULL)
1777 srv->agent.send_string = strdup(src->agent.send_string);
1778 srv->agent.send_string_len = src->agent.send_string_len;
1779 srv->agent.inter = src->agent.inter;
1780 srv->agent.fastinter = src->agent.fastinter;
1781 srv->agent.downinter = src->agent.downinter;
1782 srv->maxqueue = src->maxqueue;
1783 srv->minconn = src->minconn;
1784 srv->maxconn = src->maxconn;
1785 srv->slowstart = src->slowstart;
1786 srv->observe = src->observe;
1787 srv->onerror = src->onerror;
1788 srv->onmarkeddown = src->onmarkeddown;
1789 srv->onmarkedup = src->onmarkedup;
1790 if (src->trackit != NULL)
1791 srv->trackit = strdup(src->trackit);
1792 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1793 srv->uweight = srv->iweight = src->iweight;
1794
1795 srv->check.send_proxy = src->check.send_proxy;
1796 /* health: up, but will fall down at first failure */
1797 srv->check.rise = srv->check.health = src->check.rise;
1798 srv->check.fall = src->check.fall;
1799
1800 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001801 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1802 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1803 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001804 srv->check.state |= CHK_ST_PAUSED;
1805 srv->check.health = 0;
1806 }
1807
1808 /* health: up but will fall down at first failure */
1809 srv->agent.rise = srv->agent.health = src->agent.rise;
1810 srv->agent.fall = src->agent.fall;
1811
1812 if (src->resolvers_id != NULL)
1813 srv->resolvers_id = strdup(src->resolvers_id);
1814 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1815 if (srv->dns_opts.family_prio == AF_UNSPEC)
1816 srv->dns_opts.family_prio = AF_INET6;
1817 memcpy(srv->dns_opts.pref_net,
1818 src->dns_opts.pref_net,
1819 sizeof srv->dns_opts.pref_net);
1820 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1821
1822 srv->init_addr_methods = src->init_addr_methods;
1823 srv->init_addr = src->init_addr;
1824#if defined(USE_OPENSSL)
1825 srv_ssl_settings_cpy(srv, src);
1826#endif
1827#ifdef TCP_USER_TIMEOUT
1828 srv->tcp_ut = src->tcp_ut;
1829#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001830 if (srv_tmpl)
1831 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001832}
1833
1834static struct server *new_server(struct proxy *proxy)
1835{
1836 struct server *srv;
1837
1838 srv = calloc(1, sizeof *srv);
1839 if (!srv)
1840 return NULL;
1841
1842 srv->obj_type = OBJ_TYPE_SERVER;
1843 srv->proxy = proxy;
1844 LIST_INIT(&srv->actconns);
1845 LIST_INIT(&srv->pendconns);
1846 LIST_INIT(&srv->priv_conns);
1847 LIST_INIT(&srv->idle_conns);
1848 LIST_INIT(&srv->safe_conns);
1849
Emeric Brun52a91d32017-08-31 14:41:55 +02001850 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001851 srv->last_change = now.tv_sec;
1852
1853 srv->check.status = HCHK_STATUS_INI;
1854 srv->check.server = srv;
1855 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1856
1857 srv->agent.status = HCHK_STATUS_INI;
1858 srv->agent.server = srv;
1859 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1860
1861 return srv;
1862}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001863
1864/*
1865 * Validate <srv> server health-check settings.
1866 * Returns 0 if everything is OK, -1 if not.
1867 */
1868static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1869{
1870 struct tcpcheck_rule *r = NULL;
1871 struct list *l;
1872
1873 /*
1874 * We need at least a service port, a check port or the first tcp-check rule must
1875 * be a 'connect' one when checking an IPv4/IPv6 server.
1876 */
1877 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1878 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1879 return 0;
1880
1881 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1882 if (!r) {
1883 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1884 "Check has been disabled.\n",
1885 file, linenum, srv->id);
1886 return -1;
1887 }
1888
1889 /* search the first action (connect / send / expect) in the list */
1890 l = &srv->proxy->tcpcheck_rules;
1891 list_for_each_entry(r, l, list) {
1892 if (r->action != TCPCHK_ACT_COMMENT)
1893 break;
1894 }
1895
1896 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1897 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1898 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1899 file, linenum, srv->id);
1900 return -1;
1901 }
1902
1903 /* scan the tcp-check ruleset to ensure a port has been configured */
1904 l = &srv->proxy->tcpcheck_rules;
1905 list_for_each_entry(r, l, list) {
1906 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1907 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1908 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1909 file, linenum, srv->id);
1910 return -1;
1911 }
1912 }
1913
1914 return 0;
1915}
1916
1917/*
1918 * Initialize <srv> health-check structure.
1919 * Returns the error string in case of memory allocation failure, NULL if not.
1920 */
1921static const char *do_health_check_init(struct server *srv, int check_type, int state)
1922{
1923 const char *ret;
1924
1925 if (!srv->do_check)
1926 return NULL;
1927
1928 ret = init_check(&srv->check, check_type);
1929 if (ret)
1930 return ret;
1931
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001932 srv->check.state |= state;
1933 global.maxsock++;
1934
1935 return NULL;
1936}
1937
1938static int server_health_check_init(const char *file, int linenum,
1939 struct server *srv, struct proxy *curproxy)
1940{
1941 const char *ret;
1942
1943 if (!srv->do_check)
1944 return 0;
1945
1946 if (srv->trackit) {
1947 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1948 file, linenum);
1949 return ERR_ALERT | ERR_FATAL;
1950 }
1951
1952 if (server_healthcheck_validate(file, linenum, srv) < 0)
1953 return ERR_ALERT | ERR_ABORT;
1954
1955 /* note: check type will be set during the config review phase */
1956 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1957 if (ret) {
1958 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1959 return ERR_ALERT | ERR_ABORT;
1960 }
1961
1962 return 0;
1963}
1964
1965/*
1966 * Initialize <srv> agent check structure.
1967 * Returns the error string in case of memory allocation failure, NULL if not.
1968 */
1969static const char *do_server_agent_check_init(struct server *srv, int state)
1970{
1971 const char *ret;
1972
1973 if (!srv->do_agent)
1974 return NULL;
1975
1976 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1977 if (ret)
1978 return ret;
1979
1980 if (!srv->agent.inter)
1981 srv->agent.inter = srv->check.inter;
1982
1983 srv->agent.state |= state;
1984 global.maxsock++;
1985
1986 return NULL;
1987}
1988
1989static int server_agent_check_init(const char *file, int linenum,
1990 struct server *srv, struct proxy *curproxy)
1991{
1992 const char *ret;
1993
1994 if (!srv->do_agent)
1995 return 0;
1996
1997 if (!srv->agent.port) {
1998 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1999 file, linenum, srv->id);
2000 return ERR_ALERT | ERR_FATAL;
2001 }
2002
2003 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
2004 if (ret) {
2005 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2006 return ERR_ALERT | ERR_ABORT;
2007 }
2008
2009 return 0;
2010}
2011
2012#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2013static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
2014 struct server *srv, struct proxy *proxy)
2015{
2016 int ret;
2017 char *err = NULL;
2018
2019 if (!srv->sni_expr)
2020 return 0;
2021
2022 ret = server_parse_sni_expr(srv, proxy, &err);
2023 if (!ret)
2024 return 0;
2025
2026 display_parser_err(file, linenum, args, cur_arg, &err);
2027 free(err);
2028
2029 return ret;
2030}
2031#endif
2032
2033/*
2034 * Server initializations finalization.
2035 * Initialize health check, agent check and SNI expression if enabled.
2036 * Must not be called for a default server instance.
2037 */
2038static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2039 struct server *srv, struct proxy *px)
2040{
2041 int ret;
2042
2043 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2044 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2045 return ret;
2046 }
2047
2048#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2049 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2050 return ret;
2051#endif
2052
2053 if (srv->flags & SRV_F_BACKUP)
2054 px->srv_bck++;
2055 else
2056 px->srv_act++;
2057 srv_lb_commit_status(srv);
2058
2059 return 0;
2060}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002061
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002062/*
2063 * Parse as much as possible such a range string argument: low[-high]
2064 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2065 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2066 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2067 * Returns 0 if succeeded, -1 if not.
2068 */
2069static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2070{
2071 char *nb_high_arg;
2072
2073 *nb_high = 0;
2074 chunk_printf(&trash, "%s", arg);
2075 *nb_low = atoi(trash.str);
2076
2077 if ((nb_high_arg = strchr(trash.str, '-'))) {
2078 *nb_high_arg++ = '\0';
2079 *nb_high = atoi(nb_high_arg);
2080 }
2081 else {
2082 *nb_high += *nb_low;
2083 *nb_low = 1;
2084 }
2085
2086 if (*nb_low < 0 || *nb_high < *nb_low)
2087 return -1;
2088
2089 return 0;
2090}
2091
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002092static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2093{
2094 chunk_printf(&trash, "%s%d", prefix, nb);
2095 free(srv->id);
2096 srv->id = strdup(trash.str);
2097}
2098
2099/*
2100 * Initialize as much as possible servers from <srv> server template.
2101 * Note that a server template is a special server with
2102 * a few different parameters than a server which has
2103 * been parsed mostly the same way as a server.
2104 * Returns the number of servers succesfully allocated,
2105 * 'srv' template included.
2106 */
2107static int server_template_init(struct server *srv, struct proxy *px)
2108{
2109 int i;
2110 struct server *newsrv;
2111
2112 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2113 int check_init_state;
2114 int agent_init_state;
2115
2116 newsrv = new_server(px);
2117 if (!newsrv)
2118 goto err;
2119
2120 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002121 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002122#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2123 if (newsrv->sni_expr) {
2124 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2125 if (!newsrv->ssl_ctx.sni)
2126 goto err;
2127 }
2128#endif
2129 /* Set this new server ID. */
2130 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2131
2132 /* Initial checks states. */
2133 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2134 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2135
2136 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2137 do_server_agent_check_init(newsrv, agent_init_state))
2138 goto err;
2139
2140 /* Linked backwards first. This will be restablished after parsing. */
2141 newsrv->next = px->srv;
2142 px->srv = newsrv;
2143 }
2144 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2145
2146 return i - srv->tmpl_info.nb_low;
2147
2148 err:
2149 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2150 if (newsrv) {
2151#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2152 release_sample_expr(newsrv->ssl_ctx.sni);
2153#endif
2154 free_check(&newsrv->agent);
2155 free_check(&newsrv->check);
2156 }
2157 free(newsrv);
2158 return i - srv->tmpl_info.nb_low;
2159}
2160
Willy Tarreau272adea2014-03-31 10:39:59 +02002161int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
2162{
2163 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002164 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002165 char *errmsg = NULL;
2166 int err_code = 0;
2167 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002168 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002169
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002170 if (!strcmp(args[0], "server") ||
2171 !strcmp(args[0], "default-server") ||
2172 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002173 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002174 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002175 int srv = !defsrv && !strcmp(args[0], "server");
2176 int srv_tmpl = !defsrv && !srv;
2177 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002178
2179 if (!defsrv && curproxy == defproxy) {
2180 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
2181 err_code |= ERR_ALERT | ERR_FATAL;
2182 goto out;
2183 }
2184 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
2185 err_code |= ERR_ALERT | ERR_FATAL;
2186
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002187 /* There is no mandatory first arguments for default server. */
2188 if (srv) {
2189 if (!*args[2]) {
2190 /* 'server' line number of argument check. */
2191 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
2192 file, linenum, args[0]);
2193 err_code |= ERR_ALERT | ERR_FATAL;
2194 goto out;
2195 }
2196
2197 err = invalid_char(args[1]);
2198 }
2199 else if (srv_tmpl) {
2200 if (!*args[3]) {
2201 /* 'server-template' line number of argument check. */
2202 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2203 file, linenum, args[0]);
2204 err_code |= ERR_ALERT | ERR_FATAL;
2205 goto out;
2206 }
2207
2208 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002209 }
2210
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002211 if (err) {
2212 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
2213 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002214 err_code |= ERR_ALERT | ERR_FATAL;
2215 goto out;
2216 }
2217
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002218 cur_arg = 2;
2219 if (srv_tmpl) {
2220 /* Parse server-template <nb | range> arg. */
2221 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
2222 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
2223 file, linenum, args[0], args[cur_arg]);
2224 err_code |= ERR_ALERT | ERR_FATAL;
2225 goto out;
2226 }
2227 cur_arg++;
2228 }
2229
Willy Tarreau272adea2014-03-31 10:39:59 +02002230 if (!defsrv) {
2231 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002232 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002233 struct protocol *proto;
2234
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002235 newsrv = new_server(curproxy);
2236 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002237 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2238 err_code |= ERR_ALERT | ERR_ABORT;
2239 goto out;
2240 }
2241
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002242 if (srv_tmpl) {
2243 newsrv->tmpl_info.nb_low = tmpl_range_low;
2244 newsrv->tmpl_info.nb_high = tmpl_range_high;
2245 }
2246
Willy Tarreau272adea2014-03-31 10:39:59 +02002247 /* the servers are linked backwards first */
2248 newsrv->next = curproxy->srv;
2249 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002250 newsrv->conf.file = strdup(file);
2251 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002252 /* Note: for a server template, its id is its prefix.
2253 * This is a temporary id which will be used for server allocations to come
2254 * after parsing.
2255 */
2256 if (srv)
2257 newsrv->id = strdup(args[1]);
2258 else
2259 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002260
2261 /* several ways to check the port component :
2262 * - IP => port=+0, relative (IPv4 only)
2263 * - IP: => port=+0, relative
2264 * - IP:N => port=N, absolute
2265 * - IP:+N => port=+N, relative
2266 * - IP:-N => port=-N, relative
2267 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002268 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002269 if (!sk) {
2270 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
2271 err_code |= ERR_ALERT | ERR_FATAL;
2272 goto out;
2273 }
2274
2275 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002276 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002277 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
2278 file, linenum, args[0], args[1]);
2279 err_code |= ERR_ALERT | ERR_FATAL;
2280 goto out;
2281 }
2282
2283 if (!port1 || !port2) {
2284 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002285 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002286 }
2287 else if (port1 != port2) {
2288 /* port range */
2289 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
2290 file, linenum, args[0], args[1], args[2]);
2291 err_code |= ERR_ALERT | ERR_FATAL;
2292 goto out;
2293 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002294
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002295 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002296 if (fqdn) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002297 if (fqdn[0] == '_') {
2298 struct dns_srvrq *srvrq = NULL;
2299 int found = 0;
2300 /* SRV record */
2301 /* Check if a SRV request already exists, and if not, create it */
2302 list_for_each_entry(srvrq, &curproxy->srvrq_list, list) {
2303 if (!strcmp(srvrq->name, fqdn)) {
2304 found = 1;
2305 break;
2306 }
2307 }
2308 if (found == 0) {
2309 int hostname_dn_len;
2310
2311 srvrq = calloc(1, sizeof(*srvrq));
2312 if (!srvrq) {
2313 Alert("Failed to allocate memory");
2314 err_code = ERR_ALERT | ERR_FATAL;
2315 goto out;
2316 }
2317 srvrq->obj_type = OBJ_TYPE_SRVRQ;
2318 srvrq->proxy = proxy;
2319 srvrq->name = strdup(fqdn);
2320 srvrq->inter = 2000;
2321 hostname_dn_len = dns_str_to_dn_label_len(fqdn);
2322 if (hostname_dn_len == -1) {
2323 Alert("Failed to parse domaine name '%s'", fqdn);
2324 err_code = ERR_ALERT | ERR_FATAL;
2325 goto out;
2326 }
2327 srvrq->hostname_dn = malloc(hostname_dn_len + 1);
2328 srvrq->hostname_dn_len = hostname_dn_len;
2329 if (!srvrq->hostname_dn) {
2330 Alert("Failed to alloc memory");
2331 err_code = ERR_ALERT | ERR_FATAL;
2332 goto out;
2333 }
2334 if (!dns_str_to_dn_label(fqdn,
2335 srvrq->hostname_dn,
2336 hostname_dn_len + 1)) {
2337 Alert("Failed to parse domain name '%s'", fqdn);
2338 err_code = ERR_ALERT | ERR_FATAL;
2339 goto out;
2340 }
2341 LIST_ADDQ(&proxy->srvrq_list, &srvrq->list);
2342
2343 }
2344 newsrv->srvrq = srvrq;
2345
2346
2347 } else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
2348 Alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
2349 file, linenum, newsrv->id);
2350 err_code |= ERR_ALERT | ERR_FATAL;
2351 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002352 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002353 }
2354
Willy Tarreau272adea2014-03-31 10:39:59 +02002355 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002356 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002357
Olivier Houchard8da5f982017-08-04 18:35:36 +02002358 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002359 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002360 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002361 err_code |= ERR_ALERT | ERR_FATAL;
2362 goto out;
2363 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002364
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002365 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002366 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002367 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002368 } else {
2369 newsrv = &curproxy->defsrv;
2370 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002371 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002372 }
2373
2374 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002375 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002376 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2377 if (err) {
2378 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2379 file, linenum, *err, newsrv->id);
2380 err_code |= ERR_ALERT | ERR_FATAL;
2381 goto out;
2382 }
2383 if (val <= 0) {
2384 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2385 file, linenum, val, args[cur_arg], newsrv->id);
2386 err_code |= ERR_ALERT | ERR_FATAL;
2387 goto out;
2388 }
2389 newsrv->agent.inter = val;
2390 cur_arg += 2;
2391 }
Misiekea849332017-01-09 09:39:51 +01002392 else if (!strcmp(args[cur_arg], "agent-addr")) {
2393 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2394 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2395 goto out;
2396 }
2397
2398 cur_arg += 2;
2399 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002400 else if (!strcmp(args[cur_arg], "agent-port")) {
2401 global.maxsock++;
2402 newsrv->agent.port = atol(args[cur_arg + 1]);
2403 cur_arg += 2;
2404 }
James Brown55f9ff12015-10-21 18:19:05 -07002405 else if (!strcmp(args[cur_arg], "agent-send")) {
2406 global.maxsock++;
2407 free(newsrv->agent.send_string);
2408 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2409 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2410 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2411 cur_arg += 2;
2412 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002413 else if (!strcmp(args[cur_arg], "init-addr")) {
2414 char *p, *end;
2415 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002416 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002417
2418 newsrv->init_addr_methods = 0;
2419 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2420
2421 for (p = args[cur_arg + 1]; *p; p = end) {
2422 /* cut on next comma */
2423 for (end = p; *end && *end != ','; end++);
2424 if (*end)
2425 *(end++) = 0;
2426
Willy Tarreau4310d362016-11-02 15:05:56 +01002427 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002428 if (!strcmp(p, "libc")) {
2429 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2430 }
2431 else if (!strcmp(p, "last")) {
2432 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2433 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002434 else if (!strcmp(p, "none")) {
2435 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2436 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002437 else if (str2ip2(p, &sa, 0)) {
2438 if (is_addr(&newsrv->init_addr)) {
2439 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2440 file, linenum, args[cur_arg], p);
2441 err_code |= ERR_ALERT | ERR_FATAL;
2442 goto out;
2443 }
2444 newsrv->init_addr = sa;
2445 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2446 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002447 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002448 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002449 file, linenum, args[cur_arg], p);
2450 err_code |= ERR_ALERT | ERR_FATAL;
2451 goto out;
2452 }
2453 if (!done) {
2454 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2455 file, linenum, args[cur_arg], p);
2456 err_code |= ERR_ALERT | ERR_FATAL;
2457 goto out;
2458 }
2459 }
2460 cur_arg += 2;
2461 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002462 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002463 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002464 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2465 cur_arg += 2;
2466 }
2467 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2468 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002469 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002470 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002471 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002472 else {
2473 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2474 file, linenum, args[cur_arg]);
2475 err_code |= ERR_ALERT | ERR_FATAL;
2476 goto out;
2477 }
2478 cur_arg += 2;
2479 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002480 else if (!strcmp(args[cur_arg], "resolve-net")) {
2481 char *p, *e;
2482 unsigned char mask;
2483 struct dns_options *opt;
2484
2485 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
2486 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2487 file, linenum, args[cur_arg]);
2488 err_code |= ERR_ALERT | ERR_FATAL;
2489 goto out;
2490 }
2491
2492 opt = &newsrv->dns_opts;
2493
2494 /* Split arguments by comma, and convert it from ipv4 or ipv6
2495 * string network in in_addr or in6_addr.
2496 */
2497 p = args[cur_arg + 1];
2498 e = p;
2499 while (*p != '\0') {
2500 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002501 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002502 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2503 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2504 err_code |= ERR_ALERT | ERR_FATAL;
2505 goto out;
2506 }
2507 /* look for end or comma. */
2508 while (*e != ',' && *e != '\0')
2509 e++;
2510 if (*e == ',') {
2511 *e = '\0';
2512 e++;
2513 }
2514 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2515 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2516 /* Try to convert input string from ipv4 or ipv6 network. */
2517 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2518 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2519 &mask)) {
2520 /* Try to convert input string from ipv6 network. */
2521 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2522 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2523 } else {
2524 /* All network conversions fail, retrun error. */
2525 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2526 file, linenum, args[cur_arg], p);
2527 err_code |= ERR_ALERT | ERR_FATAL;
2528 goto out;
2529 }
2530 opt->pref_net_nb++;
2531 p = e;
2532 }
2533
2534 cur_arg += 2;
2535 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002536 else if (!strcmp(args[cur_arg], "rise")) {
2537 if (!*args[cur_arg + 1]) {
2538 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2539 file, linenum, args[cur_arg]);
2540 err_code |= ERR_ALERT | ERR_FATAL;
2541 goto out;
2542 }
2543
2544 newsrv->check.rise = atol(args[cur_arg + 1]);
2545 if (newsrv->check.rise <= 0) {
2546 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2547 file, linenum, args[cur_arg]);
2548 err_code |= ERR_ALERT | ERR_FATAL;
2549 goto out;
2550 }
2551
2552 if (newsrv->check.health)
2553 newsrv->check.health = newsrv->check.rise;
2554 cur_arg += 2;
2555 }
2556 else if (!strcmp(args[cur_arg], "fall")) {
2557 newsrv->check.fall = atol(args[cur_arg + 1]);
2558
2559 if (!*args[cur_arg + 1]) {
2560 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2561 file, linenum, args[cur_arg]);
2562 err_code |= ERR_ALERT | ERR_FATAL;
2563 goto out;
2564 }
2565
2566 if (newsrv->check.fall <= 0) {
2567 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2568 file, linenum, args[cur_arg]);
2569 err_code |= ERR_ALERT | ERR_FATAL;
2570 goto out;
2571 }
2572
2573 cur_arg += 2;
2574 }
2575 else if (!strcmp(args[cur_arg], "inter")) {
2576 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2577 if (err) {
2578 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2579 file, linenum, *err, newsrv->id);
2580 err_code |= ERR_ALERT | ERR_FATAL;
2581 goto out;
2582 }
2583 if (val <= 0) {
2584 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2585 file, linenum, val, args[cur_arg], newsrv->id);
2586 err_code |= ERR_ALERT | ERR_FATAL;
2587 goto out;
2588 }
2589 newsrv->check.inter = val;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002590 if (newsrv->srvrq)
2591 newsrv->srvrq->inter = val;
Willy Tarreau272adea2014-03-31 10:39:59 +02002592 cur_arg += 2;
2593 }
2594 else if (!strcmp(args[cur_arg], "fastinter")) {
2595 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2596 if (err) {
2597 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2598 file, linenum, *err, newsrv->id);
2599 err_code |= ERR_ALERT | ERR_FATAL;
2600 goto out;
2601 }
2602 if (val <= 0) {
2603 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2604 file, linenum, val, args[cur_arg], newsrv->id);
2605 err_code |= ERR_ALERT | ERR_FATAL;
2606 goto out;
2607 }
2608 newsrv->check.fastinter = val;
2609 cur_arg += 2;
2610 }
2611 else if (!strcmp(args[cur_arg], "downinter")) {
2612 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2613 if (err) {
2614 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2615 file, linenum, *err, newsrv->id);
2616 err_code |= ERR_ALERT | ERR_FATAL;
2617 goto out;
2618 }
2619 if (val <= 0) {
2620 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2621 file, linenum, val, args[cur_arg], newsrv->id);
2622 err_code |= ERR_ALERT | ERR_FATAL;
2623 goto out;
2624 }
2625 newsrv->check.downinter = val;
2626 cur_arg += 2;
2627 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002628 else if (!strcmp(args[cur_arg], "port")) {
2629 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002630 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002631 cur_arg += 2;
2632 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002633 else if (!strcmp(args[cur_arg], "weight")) {
2634 int w;
2635 w = atol(args[cur_arg + 1]);
2636 if (w < 0 || w > SRV_UWGHT_MAX) {
2637 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2638 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2639 err_code |= ERR_ALERT | ERR_FATAL;
2640 goto out;
2641 }
2642 newsrv->uweight = newsrv->iweight = w;
2643 cur_arg += 2;
2644 }
2645 else if (!strcmp(args[cur_arg], "minconn")) {
2646 newsrv->minconn = atol(args[cur_arg + 1]);
2647 cur_arg += 2;
2648 }
2649 else if (!strcmp(args[cur_arg], "maxconn")) {
2650 newsrv->maxconn = atol(args[cur_arg + 1]);
2651 cur_arg += 2;
2652 }
2653 else if (!strcmp(args[cur_arg], "maxqueue")) {
2654 newsrv->maxqueue = atol(args[cur_arg + 1]);
2655 cur_arg += 2;
2656 }
2657 else if (!strcmp(args[cur_arg], "slowstart")) {
2658 /* slowstart is stored in seconds */
2659 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2660 if (err) {
2661 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2662 file, linenum, *err, newsrv->id);
2663 err_code |= ERR_ALERT | ERR_FATAL;
2664 goto out;
2665 }
2666 newsrv->slowstart = (val + 999) / 1000;
2667 cur_arg += 2;
2668 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002669 else if (!strcmp(args[cur_arg], "on-error")) {
2670 if (!strcmp(args[cur_arg + 1], "fastinter"))
2671 newsrv->onerror = HANA_ONERR_FASTINTER;
2672 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2673 newsrv->onerror = HANA_ONERR_FAILCHK;
2674 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2675 newsrv->onerror = HANA_ONERR_SUDDTH;
2676 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2677 newsrv->onerror = HANA_ONERR_MARKDWN;
2678 else {
2679 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2680 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2681 file, linenum, args[cur_arg], args[cur_arg + 1]);
2682 err_code |= ERR_ALERT | ERR_FATAL;
2683 goto out;
2684 }
2685
2686 cur_arg += 2;
2687 }
2688 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2689 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2690 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2691 else {
2692 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2693 file, linenum, args[cur_arg], args[cur_arg + 1]);
2694 err_code |= ERR_ALERT | ERR_FATAL;
2695 goto out;
2696 }
2697
2698 cur_arg += 2;
2699 }
2700 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2701 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2702 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2703 else {
2704 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2705 file, linenum, args[cur_arg], args[cur_arg + 1]);
2706 err_code |= ERR_ALERT | ERR_FATAL;
2707 goto out;
2708 }
2709
2710 cur_arg += 2;
2711 }
2712 else if (!strcmp(args[cur_arg], "error-limit")) {
2713 if (!*args[cur_arg + 1]) {
2714 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2715 file, linenum, args[cur_arg]);
2716 err_code |= ERR_ALERT | ERR_FATAL;
2717 goto out;
2718 }
2719
2720 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2721
2722 if (newsrv->consecutive_errors_limit <= 0) {
2723 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2724 file, linenum, args[cur_arg]);
2725 err_code |= ERR_ALERT | ERR_FATAL;
2726 goto out;
2727 }
2728 cur_arg += 2;
2729 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002730 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002731 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2732 file, linenum, "usesrc", "source");
2733 err_code |= ERR_ALERT | ERR_FATAL;
2734 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002735 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002736 else {
2737 static int srv_dumped;
2738 struct srv_kw *kw;
2739 char *err;
2740
2741 kw = srv_find_kw(args[cur_arg]);
2742 if (kw) {
2743 char *err = NULL;
2744 int code;
2745
2746 if (!kw->parse) {
2747 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2748 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002749 if (kw->skip != -1)
2750 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002751 err_code |= ERR_ALERT | ERR_FATAL;
2752 goto out;
2753 }
2754
2755 if (defsrv && !kw->default_ok) {
2756 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2757 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002758 if (kw->skip != -1)
2759 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002760 err_code |= ERR_ALERT;
2761 continue;
2762 }
2763
2764 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2765 err_code |= code;
2766
2767 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002768 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002769 if (code & ERR_FATAL) {
2770 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002771 if (kw->skip != -1)
2772 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002773 goto out;
2774 }
2775 }
2776 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002777 if (kw->skip != -1)
2778 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002779 continue;
2780 }
2781
2782 err = NULL;
2783 if (!srv_dumped) {
2784 srv_dump_kws(&err);
2785 indent_msg(&err, 4);
2786 srv_dumped = 1;
2787 }
2788
2789 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2790 file, linenum, args[0], args[1], args[cur_arg],
2791 err ? " Registered keywords :" : "", err ? err : "");
2792 free(err);
2793
2794 err_code |= ERR_ALERT | ERR_FATAL;
2795 goto out;
2796 }
2797 }
2798
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002799 if (!defsrv)
2800 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2801 if (err_code & ERR_FATAL)
2802 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002803 if (srv_tmpl)
2804 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002805 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002806 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002807 return 0;
2808
2809 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002810 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002811 free(errmsg);
2812 return err_code;
2813}
2814
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002815/* Returns a pointer to the first server matching either id <id>.
2816 * NULL is returned if no match is found.
2817 * the lookup is performed in the backend <bk>
2818 */
2819struct server *server_find_by_id(struct proxy *bk, int id)
2820{
2821 struct eb32_node *eb32;
2822 struct server *curserver;
2823
2824 if (!bk || (id ==0))
2825 return NULL;
2826
2827 /* <bk> has no backend capabilities, so it can't have a server */
2828 if (!(bk->cap & PR_CAP_BE))
2829 return NULL;
2830
2831 curserver = NULL;
2832
2833 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2834 if (eb32)
2835 curserver = container_of(eb32, struct server, conf.id);
2836
2837 return curserver;
2838}
2839
2840/* Returns a pointer to the first server matching either name <name>, or id
2841 * if <name> starts with a '#'. NULL is returned if no match is found.
2842 * the lookup is performed in the backend <bk>
2843 */
2844struct server *server_find_by_name(struct proxy *bk, const char *name)
2845{
2846 struct server *curserver;
2847
2848 if (!bk || !name)
2849 return NULL;
2850
2851 /* <bk> has no backend capabilities, so it can't have a server */
2852 if (!(bk->cap & PR_CAP_BE))
2853 return NULL;
2854
2855 curserver = NULL;
2856 if (*name == '#') {
2857 curserver = server_find_by_id(bk, atoi(name + 1));
2858 if (curserver)
2859 return curserver;
2860 }
2861 else {
2862 curserver = bk->srv;
2863
2864 while (curserver && (strcmp(curserver->id, name) != 0))
2865 curserver = curserver->next;
2866
2867 if (curserver)
2868 return curserver;
2869 }
2870
2871 return NULL;
2872}
2873
2874struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2875{
2876 struct server *byname;
2877 struct server *byid;
2878
2879 if (!name && !id)
2880 return NULL;
2881
2882 if (diff)
2883 *diff = 0;
2884
2885 byname = byid = NULL;
2886
2887 if (name) {
2888 byname = server_find_by_name(bk, name);
2889 if (byname && (!id || byname->puid == id))
2890 return byname;
2891 }
2892
2893 /* remaining possibilities :
2894 * - name not set
2895 * - name set but not found
2896 * - name found but ID doesn't match
2897 */
2898 if (id) {
2899 byid = server_find_by_id(bk, id);
2900 if (byid) {
2901 if (byname) {
2902 /* use id only if forced by configuration */
2903 if (byid->flags & SRV_F_FORCED_ID) {
2904 if (diff)
2905 *diff |= 2;
2906 return byid;
2907 }
2908 else {
2909 if (diff)
2910 *diff |= 1;
2911 return byname;
2912 }
2913 }
2914
2915 /* remaining possibilities:
2916 * - name not set
2917 * - name set but not found
2918 */
2919 if (name && diff)
2920 *diff |= 2;
2921 return byid;
2922 }
2923
2924 /* id bot found */
2925 if (byname) {
2926 if (diff)
2927 *diff |= 1;
2928 return byname;
2929 }
2930 }
2931
2932 return NULL;
2933}
2934
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002935/* Update a server state using the parameters available in the params list */
2936static void srv_update_state(struct server *srv, int version, char **params)
2937{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002938 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002939 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002940
2941 /* fields since version 1
2942 * and common to all other upcoming versions
2943 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002944 enum srv_state srv_op_state;
2945 enum srv_admin srv_admin_state;
2946 unsigned srv_uweight, srv_iweight;
2947 unsigned long srv_last_time_change;
2948 short srv_check_status;
2949 enum chk_result srv_check_result;
2950 int srv_check_health;
2951 int srv_check_state, srv_agent_state;
2952 int bk_f_forced_id;
2953 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002954 int fqdn_set_by_cli;
2955 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002956 const char *port_str;
2957 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002958
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002959 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002960 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002961 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002962 switch (version) {
2963 case 1:
2964 /*
2965 * now we can proceed with server's state update:
2966 * srv_addr: params[0]
2967 * srv_op_state: params[1]
2968 * srv_admin_state: params[2]
2969 * srv_uweight: params[3]
2970 * srv_iweight: params[4]
2971 * srv_last_time_change: params[5]
2972 * srv_check_status: params[6]
2973 * srv_check_result: params[7]
2974 * srv_check_health: params[8]
2975 * srv_check_state: params[9]
2976 * srv_agent_state: params[10]
2977 * bk_f_forced_id: params[11]
2978 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002979 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002980 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002981 */
2982
2983 /* validating srv_op_state */
2984 p = NULL;
2985 errno = 0;
2986 srv_op_state = strtol(params[1], &p, 10);
2987 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2988 (srv_op_state != SRV_ST_STOPPED &&
2989 srv_op_state != SRV_ST_STARTING &&
2990 srv_op_state != SRV_ST_RUNNING &&
2991 srv_op_state != SRV_ST_STOPPING)) {
2992 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2993 }
2994
2995 /* validating srv_admin_state */
2996 p = NULL;
2997 errno = 0;
2998 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002999 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01003000
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003001 /* inherited statuses will be recomputed later.
3002 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
3003 */
3004 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01003005
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003006 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
3007 (srv_admin_state != 0 &&
3008 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003009 srv_admin_state != SRV_ADMF_CMAINT &&
3010 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01003011 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01003012 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003013 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
3014 }
3015
3016 /* validating srv_uweight */
3017 p = NULL;
3018 errno = 0;
3019 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003020 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003021 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
3022
3023 /* validating srv_iweight */
3024 p = NULL;
3025 errno = 0;
3026 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003027 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003028 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
3029
3030 /* validating srv_last_time_change */
3031 p = NULL;
3032 errno = 0;
3033 srv_last_time_change = strtol(params[5], &p, 10);
3034 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
3035 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
3036
3037 /* validating srv_check_status */
3038 p = NULL;
3039 errno = 0;
3040 srv_check_status = strtol(params[6], &p, 10);
3041 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3042 (srv_check_status >= HCHK_STATUS_SIZE))
3043 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3044
3045 /* validating srv_check_result */
3046 p = NULL;
3047 errno = 0;
3048 srv_check_result = strtol(params[7], &p, 10);
3049 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3050 (srv_check_result != CHK_RES_UNKNOWN &&
3051 srv_check_result != CHK_RES_NEUTRAL &&
3052 srv_check_result != CHK_RES_FAILED &&
3053 srv_check_result != CHK_RES_PASSED &&
3054 srv_check_result != CHK_RES_CONDPASS)) {
3055 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3056 }
3057
3058 /* validating srv_check_health */
3059 p = NULL;
3060 errno = 0;
3061 srv_check_health = strtol(params[8], &p, 10);
3062 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3063 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3064
3065 /* validating srv_check_state */
3066 p = NULL;
3067 errno = 0;
3068 srv_check_state = strtol(params[9], &p, 10);
3069 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3070 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3071 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3072
3073 /* validating srv_agent_state */
3074 p = NULL;
3075 errno = 0;
3076 srv_agent_state = strtol(params[10], &p, 10);
3077 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3078 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3079 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3080
3081 /* validating bk_f_forced_id */
3082 p = NULL;
3083 errno = 0;
3084 bk_f_forced_id = strtol(params[11], &p, 10);
3085 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3086 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3087
3088 /* validating srv_f_forced_id */
3089 p = NULL;
3090 errno = 0;
3091 srv_f_forced_id = strtol(params[12], &p, 10);
3092 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3093 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3094
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003095 /* validating srv_fqdn */
3096 fqdn = params[13];
3097 if (fqdn && *fqdn == '-')
3098 fqdn = NULL;
3099 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3100 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3101 fqdn = NULL;
3102 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003103
Frédéric Lécaille31694712017-08-01 08:47:19 +02003104 port_str = params[14];
3105 if (port_str) {
3106 port = strl2uic(port_str, strlen(port_str));
3107 if (port > USHRT_MAX) {
3108 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3109 port_str = NULL;
3110 }
3111 }
3112
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003113 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02003114 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003115 goto out;
3116
3117 /* recover operational state and apply it to this server
3118 * and all servers tracking this one */
3119 switch (srv_op_state) {
3120 case SRV_ST_STOPPED:
3121 srv->check.health = 0;
3122 srv_set_stopped(srv, "changed from server-state after a reload");
3123 break;
3124 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02003125 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003126 break;
3127 case SRV_ST_STOPPING:
3128 srv->check.health = srv->check.rise + srv->check.fall - 1;
3129 srv_set_stopping(srv, "changed from server-state after a reload");
3130 break;
3131 case SRV_ST_RUNNING:
3132 srv->check.health = srv->check.rise + srv->check.fall - 1;
3133 srv_set_running(srv, "");
3134 break;
3135 }
3136
3137 /* When applying server state, the following rules apply:
3138 * - in case of a configuration change, we apply the setting from the new
3139 * configuration, regardless of old running state
3140 * - if no configuration change, we apply old running state only if old running
3141 * state is different from new configuration state
3142 */
3143 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003144 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3145 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003146 srv_adm_set_maint(srv);
3147 else
3148 srv_adm_set_ready(srv);
3149 }
3150 /* configuration is the same, let's compate old running state and new conf state */
3151 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003152 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003153 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003154 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003155 srv_adm_set_ready(srv);
3156 }
3157 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003158 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003159 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003160 * (srv->iweight is the weight set up in configuration).
3161 * There are two possible reasons for FDRAIN to have been present :
3162 * - previous config weight was zero
3163 * - "set server b/s drain" was sent to the CLI
3164 *
3165 * In the first case, we simply want to drop this drain state
3166 * if the new weight is not zero anymore, meaning the administrator
3167 * has intentionally turned the weight back to a positive value to
3168 * enable the server again after an operation. In the second case,
3169 * the drain state was forced on the CLI regardless of the config's
3170 * weight so we don't want a change to the config weight to lose this
3171 * status. What this means is :
3172 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3173 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003174 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003175 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003176 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003177 }
3178
3179 srv->last_change = date.tv_sec - srv_last_time_change;
3180 srv->check.status = srv_check_status;
3181 srv->check.result = srv_check_result;
3182 srv->check.health = srv_check_health;
3183
3184 /* Only case we want to apply is removing ENABLED flag which could have been
3185 * done by the "disable health" command over the stats socket
3186 */
3187 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3188 (srv_check_state & CHK_ST_CONFIGURED) &&
3189 !(srv_check_state & CHK_ST_ENABLED))
3190 srv->check.state &= ~CHK_ST_ENABLED;
3191
3192 /* Only case we want to apply is removing ENABLED flag which could have been
3193 * done by the "disable agent" command over the stats socket
3194 */
3195 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3196 (srv_agent_state & CHK_ST_CONFIGURED) &&
3197 !(srv_agent_state & CHK_ST_ENABLED))
3198 srv->agent.state &= ~CHK_ST_ENABLED;
3199
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003200 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3201 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003202 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003203 * It means that a configuration file change has precedence over a unix socket change
3204 * for server's weight
3205 *
3206 * by default, HAProxy applies the following weight when parsing the configuration
3207 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003208 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003209 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003210 srv->uweight = srv_uweight;
3211 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003212 server_recalc_eweight(srv);
3213
Willy Tarreaue5a60682016-11-09 14:54:53 +01003214 /* load server IP address */
3215 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003216
3217 if (fqdn && srv->hostname) {
3218 if (!strcmp(srv->hostname, fqdn)) {
3219 /* Here we reset the 'set from stats socket FQDN' flag
3220 * to support such transitions:
3221 * Let's say initial FQDN value is foo1 (in configuration file).
3222 * - FQDN changed from stats socket, from foo1 to foo2 value,
3223 * - FQDN changed again from file configuration (with the same previous value
3224 set from stats socket, from foo1 to foo2 value),
3225 * - reload for any other reason than a FQDN modification,
3226 * the configuration file FQDN matches the fqdn server state file value.
3227 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
3228 * any futher FQDN modification.
3229 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003230 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003231 }
3232 else {
3233 /* If the FDQN has been changed from stats socket,
3234 * apply fqdn state file value (which is the value set
3235 * from stats socket).
3236 */
3237 if (fqdn_set_by_cli) {
3238 srv_set_fqdn(srv, fqdn);
Emeric Brun52a91d32017-08-31 14:41:55 +02003239 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003240 }
3241 }
3242 }
3243
Frédéric Lécaille31694712017-08-01 08:47:19 +02003244 if (port_str)
3245 srv->svc_port = port;
3246
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003247 break;
3248 default:
3249 chunk_appendf(msg, ", version '%d' not supported", version);
3250 }
3251
3252 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003253 if (msg->len) {
3254 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02003255 Warning("server-state application failed for server '%s/%s'%s",
3256 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003257 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003258}
3259
3260/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3261 * For each proxy, it does the following:
3262 * - opens its server state file (either one or local one)
3263 * - read whole file, line by line
3264 * - analyse each line to check if it matches our current backend:
3265 * - backend name matches
3266 * - backend id matches if id is forced and name doesn't match
3267 * - if the server pointed by the line is found, then state is applied
3268 *
3269 * If the running backend uuid or id differs from the state file, then HAProxy reports
3270 * a warning.
3271 */
3272void apply_server_state(void)
3273{
3274 char *cur, *end;
3275 char mybuf[SRV_STATE_LINE_MAXLEN];
3276 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003277 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3278 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003279 int arg, srv_arg, version, diff;
3280 FILE *f;
3281 char *filepath;
3282 char globalfilepath[MAXPATHLEN + 1];
3283 char localfilepath[MAXPATHLEN + 1];
3284 int len, fileopenerr, globalfilepathlen, localfilepathlen;
3285 extern struct proxy *proxy;
3286 struct proxy *curproxy, *bk;
3287 struct server *srv;
3288
3289 globalfilepathlen = 0;
3290 /* create the globalfilepath variable */
3291 if (global.server_state_file) {
3292 /* absolute path or no base directory provided */
3293 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3294 len = strlen(global.server_state_file);
3295 if (len > MAXPATHLEN) {
3296 globalfilepathlen = 0;
3297 goto globalfileerror;
3298 }
3299 memcpy(globalfilepath, global.server_state_file, len);
3300 globalfilepath[len] = '\0';
3301 globalfilepathlen = len;
3302 }
3303 else if (global.server_state_base) {
3304 len = strlen(global.server_state_base);
3305 globalfilepathlen += len;
3306
3307 if (globalfilepathlen > MAXPATHLEN) {
3308 globalfilepathlen = 0;
3309 goto globalfileerror;
3310 }
3311 strncpy(globalfilepath, global.server_state_base, len);
3312 globalfilepath[globalfilepathlen] = 0;
3313
3314 /* append a slash if needed */
3315 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3316 if (globalfilepathlen + 1 > MAXPATHLEN) {
3317 globalfilepathlen = 0;
3318 goto globalfileerror;
3319 }
3320 globalfilepath[globalfilepathlen++] = '/';
3321 }
3322
3323 len = strlen(global.server_state_file);
3324 if (globalfilepathlen + len > MAXPATHLEN) {
3325 globalfilepathlen = 0;
3326 goto globalfileerror;
3327 }
3328 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3329 globalfilepathlen += len;
3330 globalfilepath[globalfilepathlen++] = 0;
3331 }
3332 }
3333 globalfileerror:
3334 if (globalfilepathlen == 0)
3335 globalfilepath[0] = '\0';
3336
3337 /* read servers state from local file */
3338 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3339 /* servers are only in backends */
3340 if (!(curproxy->cap & PR_CAP_BE))
3341 continue;
3342 fileopenerr = 0;
3343 filepath = NULL;
3344
3345 /* search server state file path and name */
3346 switch (curproxy->load_server_state_from_file) {
3347 /* read servers state from global file */
3348 case PR_SRV_STATE_FILE_GLOBAL:
3349 /* there was an error while generating global server state file path */
3350 if (globalfilepathlen == 0)
3351 continue;
3352 filepath = globalfilepath;
3353 fileopenerr = 1;
3354 break;
3355 /* this backend has its own file */
3356 case PR_SRV_STATE_FILE_LOCAL:
3357 localfilepathlen = 0;
3358 localfilepath[0] = '\0';
3359 len = 0;
3360 /* create the localfilepath variable */
3361 /* absolute path or no base directory provided */
3362 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3363 len = strlen(curproxy->server_state_file_name);
3364 if (len > MAXPATHLEN) {
3365 localfilepathlen = 0;
3366 goto localfileerror;
3367 }
3368 memcpy(localfilepath, curproxy->server_state_file_name, len);
3369 localfilepath[len] = '\0';
3370 localfilepathlen = len;
3371 }
3372 else if (global.server_state_base) {
3373 len = strlen(global.server_state_base);
3374 localfilepathlen += len;
3375
3376 if (localfilepathlen > MAXPATHLEN) {
3377 localfilepathlen = 0;
3378 goto localfileerror;
3379 }
3380 strncpy(localfilepath, global.server_state_base, len);
3381 localfilepath[localfilepathlen] = 0;
3382
3383 /* append a slash if needed */
3384 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3385 if (localfilepathlen + 1 > MAXPATHLEN) {
3386 localfilepathlen = 0;
3387 goto localfileerror;
3388 }
3389 localfilepath[localfilepathlen++] = '/';
3390 }
3391
3392 len = strlen(curproxy->server_state_file_name);
3393 if (localfilepathlen + len > MAXPATHLEN) {
3394 localfilepathlen = 0;
3395 goto localfileerror;
3396 }
3397 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3398 localfilepathlen += len;
3399 localfilepath[localfilepathlen++] = 0;
3400 }
3401 filepath = localfilepath;
3402 localfileerror:
3403 if (localfilepathlen == 0)
3404 localfilepath[0] = '\0';
3405
3406 break;
3407 case PR_SRV_STATE_FILE_NONE:
3408 default:
3409 continue;
3410 }
3411
3412 /* preload global state file */
3413 errno = 0;
3414 f = fopen(filepath, "r");
3415 if (errno && fileopenerr)
3416 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3417 if (!f)
3418 continue;
3419
3420 mybuf[0] = '\0';
3421 mybuflen = 0;
3422 version = 0;
3423
3424 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003425 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3426 Warning("Can't read first line of the server state file '%s'\n", filepath);
3427 goto fileclose;
3428 }
3429
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003430 cur = mybuf;
3431 version = atoi(cur);
3432 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3433 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003434 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003435
3436 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3437 int bk_f_forced_id = 0;
3438 int check_id = 0;
3439 int check_name = 0;
3440
3441 mybuflen = strlen(mybuf);
3442 cur = mybuf;
3443 end = cur + mybuflen;
3444
3445 bk = NULL;
3446 srv = NULL;
3447
3448 /* we need at least one character */
3449 if (mybuflen == 0)
3450 continue;
3451
3452 /* ignore blank characters at the beginning of the line */
3453 while (isspace(*cur))
3454 ++cur;
3455
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003456 /* Ignore empty or commented lines */
3457 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003458 continue;
3459
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003460 /* truncated lines */
3461 if (mybuf[mybuflen - 1] != '\n') {
3462 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003463 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003464 }
3465
3466 /* Removes trailing '\n' */
3467 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003468
3469 /* we're now ready to move the line into *srv_params[] */
3470 params[0] = cur;
3471 arg = 1;
3472 srv_arg = 0;
3473 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3474 if (isspace(*cur)) {
3475 *cur = '\0';
3476 ++cur;
3477 while (isspace(*cur))
3478 ++cur;
3479 switch (version) {
3480 case 1:
3481 /*
3482 * srv_addr: params[4] => srv_params[0]
3483 * srv_op_state: params[5] => srv_params[1]
3484 * srv_admin_state: params[6] => srv_params[2]
3485 * srv_uweight: params[7] => srv_params[3]
3486 * srv_iweight: params[8] => srv_params[4]
3487 * srv_last_time_change: params[9] => srv_params[5]
3488 * srv_check_status: params[10] => srv_params[6]
3489 * srv_check_result: params[11] => srv_params[7]
3490 * srv_check_health: params[12] => srv_params[8]
3491 * srv_check_state: params[13] => srv_params[9]
3492 * srv_agent_state: params[14] => srv_params[10]
3493 * bk_f_forced_id: params[15] => srv_params[11]
3494 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003495 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003496 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003497 */
3498 if (arg >= 4) {
3499 srv_params[srv_arg] = cur;
3500 ++srv_arg;
3501 }
3502 break;
3503 }
3504
3505 params[arg] = cur;
3506 ++arg;
3507 }
3508 else {
3509 ++cur;
3510 }
3511 }
3512
3513 /* if line is incomplete line, then ignore it.
3514 * otherwise, update useful flags */
3515 switch (version) {
3516 case 1:
3517 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3518 continue;
3519 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3520 check_id = (atoi(params[0]) == curproxy->uuid);
3521 check_name = (strcmp(curproxy->id, params[1]) == 0);
3522 break;
3523 }
3524
3525 diff = 0;
3526 bk = curproxy;
3527
3528 /* if backend can't be found, let's continue */
3529 if (!check_id && !check_name)
3530 continue;
3531 else if (!check_id && check_name) {
3532 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3533 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3534 }
3535 else if (check_id && !check_name) {
3536 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3537 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3538 /* if name doesn't match, we still want to update curproxy if the backend id
3539 * was forced in previous the previous configuration */
3540 if (!bk_f_forced_id)
3541 continue;
3542 }
3543
3544 /* look for the server by its id: param[2] */
3545 /* else look for the server by its name: param[3] */
3546 diff = 0;
3547 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3548
3549 if (!srv) {
3550 /* if no server found, then warning and continue with next line */
3551 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3552 params[3], params[2], params[0], params[1]);
3553 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3554 params[3], params[2], params[0], params[1]);
3555 continue;
3556 }
3557 else if (diff & PR_FBM_MISMATCH_ID) {
3558 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);
3559 send_log(bk, LOG_NOTICE, "In backend '%s' (id: %d): server ID mismatch: from server state file: '%s', from running config %d\n", bk->id, bk->uuid, params[2], srv->puid);
Frédéric Lécaille0bedb8a2017-06-15 14:09:10 +02003560 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003561 }
3562 else if (diff & PR_FBM_MISMATCH_NAME) {
3563 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);
3564 send_log(bk, LOG_NOTICE, "In backend '%s' (id: %d): server name mismatch: from server state file: '%s', from running config '%s'\n", bk->id, bk->uuid, params[3], srv->id);
Frédéric Lécaille0bedb8a2017-06-15 14:09:10 +02003565 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003566 }
3567
3568 /* now we can proceed with server's state update */
3569 srv_update_state(srv, version, srv_params);
3570 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003571fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003572 fclose(f);
3573 }
3574}
3575
Simon Horman7d09b9a2013-02-12 10:45:51 +09003576/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003577 * update a server's current IP address.
3578 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3579 * ip is in network format.
3580 * updater is a string which contains an information about the requester of the update.
3581 * updater is used if not NULL.
3582 *
3583 * A log line and a stderr warning message is generated based on server's backend options.
3584 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003585int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003586{
3587 /* generates a log line and a warning on stderr */
3588 if (1) {
3589 /* book enough space for both IPv4 and IPv6 */
3590 char oldip[INET6_ADDRSTRLEN];
3591 char newip[INET6_ADDRSTRLEN];
3592
3593 memset(oldip, '\0', INET6_ADDRSTRLEN);
3594 memset(newip, '\0', INET6_ADDRSTRLEN);
3595
3596 /* copy old IP address in a string */
3597 switch (s->addr.ss_family) {
3598 case AF_INET:
3599 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3600 break;
3601 case AF_INET6:
3602 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3603 break;
3604 };
3605
3606 /* copy new IP address in a string */
3607 switch (ip_sin_family) {
3608 case AF_INET:
3609 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3610 break;
3611 case AF_INET6:
3612 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3613 break;
3614 };
3615
3616 /* save log line into a buffer */
3617 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3618 s->proxy->id, s->id, oldip, newip, updater);
3619
3620 /* write the buffer on stderr */
3621 Warning("%s.\n", trash.str);
3622
3623 /* send a log */
3624 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3625 }
3626
3627 /* save the new IP family */
3628 s->addr.ss_family = ip_sin_family;
3629 /* save the new IP address */
3630 switch (ip_sin_family) {
3631 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003632 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003633 break;
3634 case AF_INET6:
3635 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3636 break;
3637 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003638 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003639
3640 return 0;
3641}
3642
3643/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003644 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3645 *
3646 * Caller can pass its name through <updater> to get it integrated in the response message
3647 * returned by the function.
3648 *
3649 * The function first does the following, in that order:
3650 * - validates the new addr and/or port
3651 * - checks if an update is required (new IP or port is different than current ones)
3652 * - checks the update is allowed:
3653 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3654 * - allow all changes if no CHECKS are configured
3655 * - if CHECK is configured:
3656 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3657 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3658 * conditions are met
3659 */
3660const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3661{
3662 struct sockaddr_storage sa;
3663 int ret, port_change_required;
3664 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003665 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003666 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003667 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003668
3669 msg = get_trash_chunk();
3670 chunk_reset(msg);
3671
3672 if (addr) {
3673 memset(&sa, 0, sizeof(struct sockaddr_storage));
3674 if (str2ip2(addr, &sa, 0) == NULL) {
3675 chunk_printf(msg, "Invalid addr '%s'", addr);
3676 goto out;
3677 }
3678
3679 /* changes are allowed on AF_INET* families only */
3680 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3681 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3682 goto out;
3683 }
3684
3685 /* collecting data currently setup */
3686 memset(current_addr, '\0', sizeof(current_addr));
3687 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3688 /* changes are allowed on AF_INET* families only */
3689 if ((ret != AF_INET) && (ret != AF_INET6)) {
3690 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3691 goto out;
3692 }
3693
3694 /* applying ADDR changes if required and allowed
3695 * ipcmp returns 0 when both ADDR are the same
3696 */
3697 if (ipcmp(&s->addr, &sa) == 0) {
3698 chunk_appendf(msg, "no need to change the addr");
3699 goto port;
3700 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003701 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003702 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003703
3704 /* we also need to update check's ADDR only if it uses the server's one */
3705 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003706 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003707 }
3708
3709 /* we also need to update agent ADDR only if it use the server's one */
3710 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003711 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003712 }
3713
3714 /* update report for caller */
3715 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3716 }
3717
3718 port:
3719 if (port) {
3720 char sign = '\0';
3721 char *endptr;
3722
3723 if (addr)
3724 chunk_appendf(msg, ", ");
3725
3726 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003727 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003728
3729 /* check if PORT change is required */
3730 port_change_required = 0;
3731
3732 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003733 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003734 new_port = strtol(port, &endptr, 10);
3735 if ((errno != 0) || (port == endptr)) {
3736 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3737 goto out;
3738 }
3739
3740 /* check if caller triggers a port mapped or offset */
3741 if (sign == '-' || (sign == '+')) {
3742 /* check if server currently uses port map */
3743 if (!(s->flags & SRV_F_MAPPORTS)) {
3744 /* switch from fixed port to port map mandatorily triggers
3745 * a port change */
3746 port_change_required = 1;
3747 /* check is configured
3748 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3749 * prevent PORT change if check doesn't have it's dedicated port while switching
3750 * to port mapping */
3751 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3752 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.");
3753 goto out;
3754 }
3755 }
3756 /* we're already using port maps */
3757 else {
3758 port_change_required = current_port != new_port;
3759 }
3760 }
3761 /* fixed port */
3762 else {
3763 port_change_required = current_port != new_port;
3764 }
3765
3766 /* applying PORT changes if required and update response message */
3767 if (port_change_required) {
3768 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003769 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003770 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003771
3772 /* prepare message */
3773 chunk_appendf(msg, "port changed from '");
3774 if (s->flags & SRV_F_MAPPORTS)
3775 chunk_appendf(msg, "+");
3776 chunk_appendf(msg, "%d' to '", current_port);
3777
3778 if (sign == '-') {
3779 s->flags |= SRV_F_MAPPORTS;
3780 chunk_appendf(msg, "%c", sign);
3781 /* just use for result output */
3782 new_port = -new_port;
3783 }
3784 else if (sign == '+') {
3785 s->flags |= SRV_F_MAPPORTS;
3786 chunk_appendf(msg, "%c", sign);
3787 }
3788 else {
3789 s->flags &= ~SRV_F_MAPPORTS;
3790 }
3791
3792 chunk_appendf(msg, "%d'", new_port);
3793
3794 /* we also need to update health checks port only if it uses server's realport */
3795 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3796 s->check.port = new_port;
3797 }
3798 }
3799 else {
3800 chunk_appendf(msg, "no need to change the port");
3801 }
3802 }
3803
3804out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003805 if (changed)
3806 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003807 if (updater)
3808 chunk_appendf(msg, " by '%s'", updater);
3809 chunk_appendf(msg, "\n");
3810 return msg->str;
3811}
3812
3813
3814/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003815 * update server status based on result of name resolution
3816 * returns:
3817 * 0 if server status is updated
3818 * 1 if server status has not changed
3819 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003820int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003821{
3822 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann42746372017-05-03 12:12:02 +02003823 struct dns_resolvers *resolvers = s->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003824
3825 switch (resolution->status) {
3826 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003827 /* status when HAProxy has just (re)started.
3828 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003829 break;
3830
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003831 case RSLV_STATUS_VALID:
3832 /*
3833 * resume health checks
3834 * server will be turned back on if health check is safe
3835 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003836 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003837 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003838 return 1;
3839 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3840 "No IP for server ");
3841 return (0);
3842 }
Emeric Brun52a91d32017-08-31 14:41:55 +02003843 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003844 return 1;
3845 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3846 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3847 s->proxy->id, s->id);
3848
3849 Warning("%s.\n", trash.str);
3850 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3851 return 0;
3852
3853 case RSLV_STATUS_NX:
3854 /* stop server if resolution is NX for a long enough period */
3855 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003856 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003857 return 1;
3858 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3859 return 0;
3860 }
3861 break;
3862
3863 case RSLV_STATUS_TIMEOUT:
3864 /* stop server if resolution is TIMEOUT for a long enough period */
3865 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003866 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003867 return 1;
3868 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3869 return 0;
3870 }
3871 break;
3872
3873 case RSLV_STATUS_REFUSED:
3874 /* stop server if resolution is REFUSED for a long enough period */
3875 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003876 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003877 return 1;
3878 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3879 return 0;
3880 }
3881 break;
3882
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003883 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003884 /* stop server if resolution is in unmatched error for a long enough period */
3885 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003886 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003887 return 1;
3888 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3889 return 0;
3890 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003891 break;
3892 }
3893
3894 return 1;
3895}
3896
3897/*
3898 * Server Name Resolution valid response callback
3899 * It expects:
3900 * - <nameserver>: the name server which answered the valid response
3901 * - <response>: buffer containing a valid DNS response
3902 * - <response_len>: size of <response>
3903 * It performs the following actions:
3904 * - ignore response if current ip found and server family not met
3905 * - update with first new ip found if family is met and current IP is not found
3906 * returns:
3907 * 0 on error
3908 * 1 when no error or safe ignore
3909 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003910int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003911{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003912 struct server *s = NULL;
3913 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003914 void *serverip, *firstip;
3915 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003916 int ret;
3917 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003918 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003919
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003920 s = objt_server(requester->requester);
3921 if (!s)
3922 return 1;
3923
3924 resolution = s->resolution;
3925
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003926 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003927 firstip = NULL; /* pointer to the first valid response found */
3928 /* it will be used as the new IP if a change is required */
3929 firstip_sin_family = AF_UNSPEC;
3930 serverip = NULL; /* current server IP address */
3931
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003932 /* initializing server IP pointer */
3933 server_sin_family = s->addr.ss_family;
3934 switch (server_sin_family) {
3935 case AF_INET:
3936 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3937 break;
3938
3939 case AF_INET6:
3940 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3941 break;
3942
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003943 case AF_UNSPEC:
3944 break;
3945
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003946 default:
3947 goto invalid;
3948 }
3949
Baptiste Assmann729c9012017-05-22 15:13:10 +02003950 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003951 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003952 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003953
3954 switch (ret) {
3955 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003956 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003957
3958 case DNS_UPD_SRVIP_NOT_FOUND:
3959 goto save_ip;
3960
3961 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003962 goto invalid;
3963
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003964 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003965 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003966 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003967
Baptiste Assmannfad03182015-10-28 02:03:32 +01003968 case DNS_UPD_NAME_ERROR:
3969 /* if this is not the last expected response, we ignore it */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003970 if (nameserver && (resolution->nb_responses < nameserver->resolvers->count_nameservers))
Baptiste Assmannfad03182015-10-28 02:03:32 +01003971 return 0;
3972 /* update resolution status to OTHER error type */
3973 if (resolution->status != RSLV_STATUS_OTHER) {
3974 resolution->status = RSLV_STATUS_OTHER;
3975 resolution->last_status_change = now_ms;
3976 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003977 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003978
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003979 default:
3980 goto invalid;
3981
3982 }
3983
3984 save_ip:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003985 if (nameserver)
3986 nameserver->counters.update += 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003987
3988 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003989 if (nameserver)
3990 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3991 else
3992 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003993 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3994
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003995 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003996 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003997 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003998
3999 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004000 if (nameserver) {
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004001 nameserver->counters.invalid += 1;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004002 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
4003 goto update_status;
4004 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004005
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004006 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004007 return 0;
4008}
4009
4010/*
4011 * Server Name Resolution error management callback
4012 * returns:
4013 * 0 on error
4014 * 1 when no error or safe ignore
4015 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004016int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004017{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004018 struct server *s = NULL;
4019 struct dns_resolution *resolution = NULL;
4020 struct dns_resolvers *resolvers = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004021
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004022 s = objt_server(requester->requester);
4023 if (!s)
4024 return 1;
4025
4026 resolution = s->resolution;
Baptiste Assmann42746372017-05-03 12:12:02 +02004027 resolvers = s->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004028
4029 /* can be ignored if this is not the last response */
4030 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
4031 return 1;
4032 }
4033
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004034 snr_update_srv_status(s, 0);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004035 return 1;
4036}
4037
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004038/*
4039 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004040 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004041 * It returns a pointer to the first server found or NULL if <ip> is not yet
4042 * assigned.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004043 */
4044struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4045{
4046 struct server *tmpsrv;
4047 struct proxy *be;
4048
4049 if (!srv)
4050 return NULL;
4051
4052 be = srv->proxy;
4053 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
4054 /* We want to compare the IP in the record with the IP of the servers in the
4055 * same backend, only if:
4056 * * DNS resolution is enabled on the server
4057 * * the hostname used for the resolution by our server is the same than the
4058 * one used for the server found in the backend
4059 * * the server found in the backend is not our current server
4060 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004061 if ((tmpsrv->hostname_dn == NULL) ||
4062 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4063 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004064 (srv->puid == tmpsrv->puid))
4065 continue;
4066
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004067 /* If the server has been taken down, don't consider it */
Emeric Brun52a91d32017-08-31 14:41:55 +02004068 if (tmpsrv->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004069 continue;
4070
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004071 /* At this point, we have 2 different servers using the same DNS hostname
4072 * for their respective resolution.
4073 */
4074 if (*ip_family == tmpsrv->addr.ss_family &&
4075 ((tmpsrv->addr.ss_family == AF_INET &&
4076 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4077 (tmpsrv->addr.ss_family == AF_INET6 &&
4078 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
4079 return tmpsrv;
4080 }
4081 }
4082
4083 return NULL;
4084}
4085
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004086/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4087 * resolver. This is suited for initial address configuration. Returns 0 on
4088 * success otherwise a non-zero error code. In case of error, *err_code, if
4089 * not NULL, is filled up.
4090 */
4091int srv_set_addr_via_libc(struct server *srv, int *err_code)
4092{
4093 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004094 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004095 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004096 return 1;
4097 }
4098 return 0;
4099}
4100
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004101/* Set the server's FDQN (->hostname) from <hostname>.
4102 * Returns -1 if failed, 0 if not.
4103 */
4104int srv_set_fqdn(struct server *srv, const char *hostname)
4105{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004106 struct dns_resolution *resolution;
4107 int hostname_dn_len;
Olivier Houchard8da5f982017-08-04 18:35:36 +02004108 int did_set_reso = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004109
4110 /* run time DNS resolution was not active for this server
4111 * and we can't enable it at run time for now.
4112 */
4113 if (!srv->dns_requester)
4114 return -1;
4115
4116 chunk_reset(&trash);
4117
4118 /* check if hostname is really a hostname and if we have enough
4119 * room to save it in its domain name format
4120 */
4121 hostname_dn_len = dns_str_to_dn_label_len(hostname);
4122 if (hostname_dn_len == -1 || hostname_dn_len + 1 > trash.size)
4123 return -1;
4124
4125 if (!dns_str_to_dn_label(hostname,
4126 trash.str,
4127 hostname_dn_len + 1))
4128 return -1;
4129
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004130
Olivier Houchard8da5f982017-08-04 18:35:36 +02004131 if (srv->resolution->hostname_dn) {
4132 /* get a resolution from the curr or wait queues, or a brand new one from the pool */
4133 resolution = dns_resolution_list_get(srv->resolvers, trash.str, srv->dns_requester->prefered_query_type);
4134 if (!resolution)
4135 return -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004136
Olivier Houchard8da5f982017-08-04 18:35:36 +02004137 /* in this case, the new hostanme is the same than the old one */
4138 if (srv->resolution == resolution && srv->hostname)
4139 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004140
Olivier Houchard8da5f982017-08-04 18:35:36 +02004141 /* first, we need to unlink our server from its current resolution */
4142 srv_free_from_resolution(srv);
4143 } else {
Baptiste Assmann6fb81922017-08-14 16:35:45 +02004144 /* this server's fqdn has been set by a SRV record */
4145 resolution = dns_resolution_list_get(srv->resolvers, trash.str, srv->dns_requester->prefered_query_type);
4146 srv_free_from_resolution(srv);
4147 srv->resolution = resolution;
4148 if (resolution->hostname_dn == NULL) {
4149 resolution->last_resolution = now_ms;
4150 did_set_reso = 1;
4151 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02004152 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004153
4154 /* now we update server's parameters */
4155 free(srv->hostname);
4156 free(srv->hostname_dn);
4157 srv->hostname = strdup(hostname);
4158 srv->hostname_dn = strdup(trash.str);
4159 srv->hostname_dn_len = hostname_dn_len;
4160 if (!srv->hostname || !srv->hostname_dn)
4161 return -1;
Olivier Houchard8da5f982017-08-04 18:35:36 +02004162 if (did_set_reso) {
4163 resolution->query_type = srv->dns_requester->prefered_query_type;
4164 resolution->hostname_dn = srv->hostname_dn;
4165 resolution->hostname_dn_len = hostname_dn_len;
4166 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004167
4168 /* then we can link srv to its new resolution */
4169 dns_link_resolution(srv, OBJ_TYPE_SERVER, resolution);
4170
4171 return 0;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004172}
4173
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004174/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4175 * from the state file. This is suited for initial address configuration.
4176 * Returns 0 on success otherwise a non-zero error code. In case of error,
4177 * *err_code, if not NULL, is filled up.
4178 */
4179static int srv_apply_lastaddr(struct server *srv, int *err_code)
4180{
4181 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4182 if (err_code)
4183 *err_code |= ERR_WARN;
4184 return 1;
4185 }
4186 return 0;
4187}
4188
Willy Tarreau25e51522016-11-04 15:10:17 +01004189/* returns 0 if no error, otherwise a combination of ERR_* flags */
4190static int srv_iterate_initaddr(struct server *srv)
4191{
4192 int return_code = 0;
4193 int err_code;
4194 unsigned int methods;
4195
4196 methods = srv->init_addr_methods;
4197 if (!methods) { // default to "last,libc"
4198 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4199 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4200 }
4201
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004202 /* "-dr" : always append "none" so that server addresses resolution
4203 * failures are silently ignored, this is convenient to validate some
4204 * configs out of their environment.
4205 */
4206 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4207 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4208
Willy Tarreau25e51522016-11-04 15:10:17 +01004209 while (methods) {
4210 err_code = 0;
4211 switch (srv_get_next_initaddr(&methods)) {
4212 case SRV_IADDR_LAST:
4213 if (!srv->lastaddr)
4214 continue;
4215 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004216 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004217 return_code |= err_code;
4218 break;
4219
4220 case SRV_IADDR_LIBC:
4221 if (!srv->hostname)
4222 continue;
4223 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004224 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004225 return_code |= err_code;
4226 break;
4227
Willy Tarreau37ebe122016-11-04 15:17:58 +01004228 case SRV_IADDR_NONE:
4229 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004230 if (return_code) {
4231 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4232 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4233 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004234 return return_code;
4235
Willy Tarreau4310d362016-11-02 15:05:56 +01004236 case SRV_IADDR_IP:
4237 ipcpy(&srv->init_addr, &srv->addr);
4238 if (return_code) {
4239 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4240 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4241 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004242 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004243
Willy Tarreau25e51522016-11-04 15:10:17 +01004244 default: /* unhandled method */
4245 break;
4246 }
4247 }
4248
4249 if (!return_code) {
4250 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
4251 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4252 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004253 else {
4254 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
4255 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4256 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004257
4258 return_code |= ERR_ALERT | ERR_FATAL;
4259 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004260out:
4261 srv_set_dyncookie(srv);
4262 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004263}
4264
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004265/*
4266 * This function parses all backends and all servers within each backend
4267 * and performs servers' addr resolution based on information provided by:
4268 * - configuration file
4269 * - server-state file (states provided by an 'old' haproxy process)
4270 *
4271 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4272 */
4273int srv_init_addr(void)
4274{
4275 struct proxy *curproxy;
4276 int return_code = 0;
4277
4278 curproxy = proxy;
4279 while (curproxy) {
4280 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004281
4282 /* servers are in backend only */
4283 if (!(curproxy->cap & PR_CAP_BE))
4284 goto srv_init_addr_next;
4285
Willy Tarreau25e51522016-11-04 15:10:17 +01004286 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004287 if (srv->hostname)
4288 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004289
4290 srv_init_addr_next:
4291 curproxy = curproxy->next;
4292 }
4293
4294 return return_code;
4295}
4296
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004297const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater)
4298{
4299
4300 struct chunk *msg;
4301
4302 msg = get_trash_chunk();
4303 chunk_reset(msg);
4304
Olivier Houchard8da5f982017-08-04 18:35:36 +02004305 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004306 chunk_appendf(msg, "no need to change the FDQN");
4307 goto out;
4308 }
4309
4310 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4311 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4312 goto out;
4313 }
4314
4315 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4316 server->proxy->id, server->id, server->hostname, fqdn);
4317
4318 if (srv_set_fqdn(server, fqdn) < 0) {
4319 chunk_reset(msg);
4320 chunk_appendf(msg, "could not update %s/%s FQDN",
4321 server->proxy->id, server->id);
4322 goto out;
4323 }
4324
4325 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004326 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004327
4328 out:
4329 if (updater)
4330 chunk_appendf(msg, " by '%s'", updater);
4331 chunk_appendf(msg, "\n");
4332
4333 return msg->str;
4334}
4335
4336
Willy Tarreau21b069d2016-11-23 17:15:08 +01004337/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4338 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004339 * 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 +01004340 * used for CLI commands requiring a server name.
4341 * Important: the <arg> is modified to remove the '/'.
4342 */
4343struct server *cli_find_server(struct appctx *appctx, char *arg)
4344{
4345 struct proxy *px;
4346 struct server *sv;
4347 char *line;
4348
4349 /* split "backend/server" and make <line> point to server */
4350 for (line = arg; *line; line++)
4351 if (*line == '/') {
4352 *line++ = '\0';
4353 break;
4354 }
4355
4356 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004357 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004358 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004359 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004360 return NULL;
4361 }
4362
4363 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004364 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004365 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004366 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004367 return NULL;
4368 }
4369
4370 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004371 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004372 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004373 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004374 return NULL;
4375 }
4376
4377 return sv;
4378}
4379
William Lallemand222baf22016-11-19 02:00:33 +01004380
4381static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4382{
4383 struct server *sv;
4384 const char *warning;
4385
4386 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4387 return 1;
4388
4389 sv = cli_find_server(appctx, args[2]);
4390 if (!sv)
4391 return 1;
4392
4393 if (strcmp(args[3], "weight") == 0) {
4394 warning = server_parse_weight_change_request(sv, args[4]);
4395 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004396 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004397 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004398 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004399 }
4400 }
4401 else if (strcmp(args[3], "state") == 0) {
4402 if (strcmp(args[4], "ready") == 0)
4403 srv_adm_set_ready(sv);
4404 else if (strcmp(args[4], "drain") == 0)
4405 srv_adm_set_drain(sv);
4406 else if (strcmp(args[4], "maint") == 0)
4407 srv_adm_set_maint(sv);
4408 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004409 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004410 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004411 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004412 }
4413 }
4414 else if (strcmp(args[3], "health") == 0) {
4415 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004416 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004417 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004418 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004419 }
4420 else if (strcmp(args[4], "up") == 0) {
4421 sv->check.health = sv->check.rise + sv->check.fall - 1;
4422 srv_set_running(sv, "changed from CLI");
4423 }
4424 else if (strcmp(args[4], "stopping") == 0) {
4425 sv->check.health = sv->check.rise + sv->check.fall - 1;
4426 srv_set_stopping(sv, "changed from CLI");
4427 }
4428 else if (strcmp(args[4], "down") == 0) {
4429 sv->check.health = 0;
4430 srv_set_stopped(sv, "changed from CLI");
4431 }
4432 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004433 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004434 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004435 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004436 }
4437 }
4438 else if (strcmp(args[3], "agent") == 0) {
4439 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004440 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004441 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004442 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004443 }
4444 else if (strcmp(args[4], "up") == 0) {
4445 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4446 srv_set_running(sv, "changed from CLI");
4447 }
4448 else if (strcmp(args[4], "down") == 0) {
4449 sv->agent.health = 0;
4450 srv_set_stopped(sv, "changed from CLI");
4451 }
4452 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004453 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004454 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004455 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004456 }
4457 }
Misiek2da082d2017-01-09 09:40:42 +01004458 else if (strcmp(args[3], "agent-addr") == 0) {
4459 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004460 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004461 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4462 appctx->st0 = CLI_ST_PRINT;
4463 } else {
4464 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004465 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004466 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4467 appctx->st0 = CLI_ST_PRINT;
4468 }
4469 }
4470 }
4471 else if (strcmp(args[3], "agent-send") == 0) {
4472 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004473 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004474 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4475 appctx->st0 = CLI_ST_PRINT;
4476 } else {
4477 char *nss = strdup(args[4]);
4478 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004479 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004480 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4481 appctx->st0 = CLI_ST_PRINT;
4482 } else {
4483 free(sv->agent.send_string);
4484 sv->agent.send_string = nss;
4485 sv->agent.send_string_len = strlen(args[4]);
4486 }
4487 }
4488 }
William Lallemand222baf22016-11-19 02:00:33 +01004489 else if (strcmp(args[3], "check-port") == 0) {
4490 int i = 0;
4491 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004492 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004493 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004494 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004495 }
4496 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004497 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004498 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004499 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004500 }
4501 /* prevent the update of port to 0 if MAPPORTS are in use */
4502 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004503 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004504 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004505 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004506 return 1;
4507 }
4508 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004509 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004510 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004511 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004512 }
4513 else if (strcmp(args[3], "addr") == 0) {
4514 char *addr = NULL;
4515 char *port = NULL;
4516 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004517 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004518 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004519 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004520 return 1;
4521 }
4522 else {
4523 addr = args[4];
4524 }
4525 if (strcmp(args[5], "port") == 0) {
4526 port = args[6];
4527 }
4528 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4529 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004530 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004531 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004532 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004533 }
4534 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4535 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004536 else if (strcmp(args[3], "fqdn") == 0) {
4537 if (!*args[4]) {
4538 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4539 appctx->st0 = CLI_ST_PRINT;
4540 return 1;
4541 }
4542 warning = update_server_fqdn(sv, args[4], "stats socket command");
4543 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004544 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004545 appctx->ctx.cli.msg = warning;
4546 appctx->st0 = CLI_ST_PRINT;
4547 }
4548 }
William Lallemand222baf22016-11-19 02:00:33 +01004549 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004550 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004551 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight', 'addr', 'fqdn' and 'check-port'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004552 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004553 }
4554 return 1;
4555}
4556
William Lallemand6b160942016-11-22 12:34:35 +01004557static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4558{
4559 struct stream_interface *si = appctx->owner;
4560 struct proxy *px;
4561 struct server *sv;
4562 char *line;
4563
4564
4565 /* split "backend/server" and make <line> point to server */
4566 for (line = args[2]; *line; line++)
4567 if (*line == '/') {
4568 *line++ = '\0';
4569 break;
4570 }
4571
4572 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004573 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004574 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004575 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004576 return 1;
4577 }
4578
4579 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004580 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004581 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004582 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004583 return 1;
4584 }
4585
4586 /* return server's effective weight at the moment */
4587 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004588 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004589 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004590 return 0;
4591 }
William Lallemand6b160942016-11-22 12:34:35 +01004592 return 1;
4593}
4594
4595static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4596{
4597 struct server *sv;
4598 const char *warning;
4599
4600 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4601 return 1;
4602
4603 sv = cli_find_server(appctx, args[2]);
4604 if (!sv)
4605 return 1;
4606
4607 warning = server_parse_weight_change_request(sv, args[3]);
4608 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004609 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004610 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004611 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004612 }
4613 return 1;
4614}
4615
Willy Tarreaub8026272016-11-23 11:26:56 +01004616/* parse a "set maxconn server" command. It always returns 1. */
4617static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4618{
4619 struct server *sv;
4620 const char *warning;
4621
4622 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4623 return 1;
4624
4625 sv = cli_find_server(appctx, args[3]);
4626 if (!sv)
4627 return 1;
4628
4629 warning = server_parse_maxconn_change_request(sv, args[4]);
4630 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004631 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004632 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004633 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004634 }
4635 return 1;
4636}
William Lallemand6b160942016-11-22 12:34:35 +01004637
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004638/* parse a "disable agent" command. It always returns 1. */
4639static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4640{
4641 struct server *sv;
4642
4643 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4644 return 1;
4645
4646 sv = cli_find_server(appctx, args[2]);
4647 if (!sv)
4648 return 1;
4649
4650 sv->agent.state &= ~CHK_ST_ENABLED;
4651 return 1;
4652}
4653
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004654/* parse a "disable health" command. It always returns 1. */
4655static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4656{
4657 struct server *sv;
4658
4659 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4660 return 1;
4661
4662 sv = cli_find_server(appctx, args[2]);
4663 if (!sv)
4664 return 1;
4665
4666 sv->check.state &= ~CHK_ST_ENABLED;
4667 return 1;
4668}
4669
Willy Tarreauffb4d582016-11-24 12:47:00 +01004670/* parse a "disable server" command. It always returns 1. */
4671static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4672{
4673 struct server *sv;
4674
4675 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4676 return 1;
4677
4678 sv = cli_find_server(appctx, args[2]);
4679 if (!sv)
4680 return 1;
4681
4682 srv_adm_set_maint(sv);
4683 return 1;
4684}
4685
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004686/* parse a "enable agent" command. It always returns 1. */
4687static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4688{
4689 struct server *sv;
4690
4691 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4692 return 1;
4693
4694 sv = cli_find_server(appctx, args[2]);
4695 if (!sv)
4696 return 1;
4697
4698 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004699 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004700 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004701 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004702 return 1;
4703 }
4704
4705 sv->agent.state |= CHK_ST_ENABLED;
4706 return 1;
4707}
4708
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004709/* parse a "enable health" command. It always returns 1. */
4710static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4711{
4712 struct server *sv;
4713
4714 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4715 return 1;
4716
4717 sv = cli_find_server(appctx, args[2]);
4718 if (!sv)
4719 return 1;
4720
4721 sv->check.state |= CHK_ST_ENABLED;
4722 return 1;
4723}
4724
Willy Tarreauffb4d582016-11-24 12:47:00 +01004725/* parse a "enable server" command. It always returns 1. */
4726static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4727{
4728 struct server *sv;
4729
4730 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4731 return 1;
4732
4733 sv = cli_find_server(appctx, args[2]);
4734 if (!sv)
4735 return 1;
4736
4737 srv_adm_set_ready(sv);
4738 return 1;
4739}
4740
William Lallemand222baf22016-11-19 02:00:33 +01004741/* register cli keywords */
4742static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004743 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004744 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004745 { { "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 +01004746 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004747 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004748 { { "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 +01004749 { { "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 +01004750 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004751 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4752 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4753
William Lallemand222baf22016-11-19 02:00:33 +01004754 {{},}
4755}};
4756
4757__attribute__((constructor))
4758static void __server_init(void)
4759{
4760 cli_register_kw(&cli_kws);
4761}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004762
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004763/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004764 * Local variables:
4765 * c-indent-level: 8
4766 * c-basic-offset: 8
4767 * End:
4768 */