blob: 917ed09a38fc17b6a4cb1178b1faf24c07c7c383 [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{
Willy Tarreau892337c2014-05-13 23:41:20 +020058 if ((s->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
Willy Tarreau892337c2014-05-13 23:41:20 +020079 if ((s->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{
320 newsrv->admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
321 newsrv->state = SRV_ST_STOPPED;
322 newsrv->check.state |= CHK_ST_PAUSED;
323 newsrv->check.health = 0;
324 return 0;
325}
326
327/* Parse the "enabled" server keyword */
328static int srv_parse_enabled(char **args, int *cur_arg,
329 struct proxy *curproxy, struct server *newsrv, char **err)
330{
331 newsrv->admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
332 newsrv->state = SRV_ST_RUNNING;
333 newsrv->check.state &= ~CHK_ST_PAUSED;
334 newsrv->check.health = newsrv->check.rise;
335 return 0;
336}
337
Willy Tarreaudff55432012-10-10 17:51:05 +0200338/* parse the "id" server keyword */
339static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
340{
341 struct eb32_node *node;
342
343 if (!*args[*cur_arg + 1]) {
344 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
345 return ERR_ALERT | ERR_FATAL;
346 }
347
348 newsrv->puid = atol(args[*cur_arg + 1]);
349 newsrv->conf.id.key = newsrv->puid;
350
351 if (newsrv->puid <= 0) {
352 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
353 return ERR_ALERT | ERR_FATAL;
354 }
355
356 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
357 if (node) {
358 struct server *target = container_of(node, struct server, conf.id);
359 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
360 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
361 target->id);
362 return ERR_ALERT | ERR_FATAL;
363 }
364
365 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200366 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200367 return 0;
368}
369
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100370/* Parse the "namespace" server keyword */
371static int srv_parse_namespace(char **args, int *cur_arg,
372 struct proxy *curproxy, struct server *newsrv, char **err)
373{
374#ifdef CONFIG_HAP_NS
375 char *arg;
376
377 arg = args[*cur_arg + 1];
378 if (!*arg) {
379 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
380 return ERR_ALERT | ERR_FATAL;
381 }
382
383 if (!strcmp(arg, "*")) {
384 /* Use the namespace associated with the connection (if present). */
385 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
386 return 0;
387 }
388
389 /*
390 * As this parser may be called several times for the same 'default-server'
391 * object, or for a new 'server' instance deriving from a 'default-server'
392 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
393 */
394 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
395
396 newsrv->netns = netns_store_lookup(arg, strlen(arg));
397 if (!newsrv->netns)
398 newsrv->netns = netns_store_insert(arg);
399
400 if (!newsrv->netns) {
401 memprintf(err, "Cannot open namespace '%s'", arg);
402 return ERR_ALERT | ERR_FATAL;
403 }
404
405 return 0;
406#else
407 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
408 return ERR_ALERT | ERR_FATAL;
409#endif
410}
411
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100412/* Parse the "no-agent-check" server keyword */
413static int srv_parse_no_agent_check(char **args, int *cur_arg,
414 struct proxy *curproxy, struct server *newsrv, char **err)
415{
416 free_check(&newsrv->agent);
417 newsrv->agent.inter = 0;
418 newsrv->agent.port = 0;
419 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
420 newsrv->do_agent = 0;
421 return 0;
422}
423
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100424/* Parse the "no-backup" server keyword */
425static int srv_parse_no_backup(char **args, int *cur_arg,
426 struct proxy *curproxy, struct server *newsrv, char **err)
427{
428 newsrv->flags &= ~SRV_F_BACKUP;
429 return 0;
430}
431
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100432/* Parse the "no-check" server keyword */
433static int srv_parse_no_check(char **args, int *cur_arg,
434 struct proxy *curproxy, struct server *newsrv, char **err)
435{
436 free_check(&newsrv->check);
437 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
438 newsrv->do_check = 0;
439 return 0;
440}
441
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100442/* Parse the "no-check-send-proxy" server keyword */
443static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
444 struct proxy *curproxy, struct server *newsrv, char **err)
445{
446 newsrv->check.send_proxy = 0;
447 return 0;
448}
449
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100450/* Disable server PROXY protocol flags. */
451static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
452{
453 srv->pp_opts &= ~flags;
454 return 0;
455}
456
457/* Parse the "no-send-proxy" server keyword */
458static int srv_parse_no_send_proxy(char **args, int *cur_arg,
459 struct proxy *curproxy, struct server *newsrv, char **err)
460{
461 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
462}
463
464/* Parse the "no-send-proxy-v2" server keyword */
465static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
466 struct proxy *curproxy, struct server *newsrv, char **err)
467{
468 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
469}
470
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100471/* Parse the "non-stick" server keyword */
472static int srv_parse_non_stick(char **args, int *cur_arg,
473 struct proxy *curproxy, struct server *newsrv, char **err)
474{
475 newsrv->flags |= SRV_F_NON_STICK;
476 return 0;
477}
478
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100479/* Enable server PROXY protocol flags. */
480static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
481{
482 srv->pp_opts |= flags;
483 return 0;
484}
485
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100486/* Parse the "observe" server keyword */
487static int srv_parse_observe(char **args, int *cur_arg,
488 struct proxy *curproxy, struct server *newsrv, char **err)
489{
490 char *arg;
491
492 arg = args[*cur_arg + 1];
493 if (!*arg) {
494 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
495 return ERR_ALERT | ERR_FATAL;
496 }
497
498 if (!strcmp(arg, "none")) {
499 newsrv->observe = HANA_OBS_NONE;
500 }
501 else if (!strcmp(arg, "layer4")) {
502 newsrv->observe = HANA_OBS_LAYER4;
503 }
504 else if (!strcmp(arg, "layer7")) {
505 if (curproxy->mode != PR_MODE_HTTP) {
506 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
507 return ERR_ALERT;
508 }
509 newsrv->observe = HANA_OBS_LAYER7;
510 }
511 else {
512 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
513 "but got '%s'\n", args[*cur_arg], arg);
514 return ERR_ALERT | ERR_FATAL;
515 }
516
517 return 0;
518}
519
Frédéric Lécaille16186232017-03-14 16:42:49 +0100520/* Parse the "redir" server keyword */
521static int srv_parse_redir(char **args, int *cur_arg,
522 struct proxy *curproxy, struct server *newsrv, char **err)
523{
524 char *arg;
525
526 arg = args[*cur_arg + 1];
527 if (!*arg) {
528 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
529 return ERR_ALERT | ERR_FATAL;
530 }
531
532 free(newsrv->rdr_pfx);
533 newsrv->rdr_pfx = strdup(arg);
534 newsrv->rdr_len = strlen(arg);
535
536 return 0;
537}
538
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100539/* Parse the "send-proxy" server keyword */
540static int srv_parse_send_proxy(char **args, int *cur_arg,
541 struct proxy *curproxy, struct server *newsrv, char **err)
542{
543 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
544}
545
546/* Parse the "send-proxy-v2" server keyword */
547static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
548 struct proxy *curproxy, struct server *newsrv, char **err)
549{
550 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
551}
552
Frédéric Lécailledba97072017-03-17 15:33:50 +0100553
554/* Parse the "source" server keyword */
555static int srv_parse_source(char **args, int *cur_arg,
556 struct proxy *curproxy, struct server *newsrv, char **err)
557{
558 char *errmsg;
559 int port_low, port_high;
560 struct sockaddr_storage *sk;
561 struct protocol *proto;
562
563 errmsg = NULL;
564
565 if (!*args[*cur_arg + 1]) {
566 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
567 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
568 goto err;
569 }
570
571 /* 'sk' is statically allocated (no need to be freed). */
572 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
573 if (!sk) {
574 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
575 goto err;
576 }
577
578 proto = protocol_by_family(sk->ss_family);
579 if (!proto || !proto->connect) {
580 Alert("'%s %s' : connect() not supported for this address family.\n",
581 args[*cur_arg], args[*cur_arg + 1]);
582 goto err;
583 }
584
585 newsrv->conn_src.opts |= CO_SRC_BIND;
586 newsrv->conn_src.source_addr = *sk;
587
588 if (port_low != port_high) {
589 int i;
590
591 if (!port_low || !port_high) {
592 Alert("'%s' does not support port offsets (found '%s').\n",
593 args[*cur_arg], args[*cur_arg + 1]);
594 goto err;
595 }
596
597 if (port_low <= 0 || port_low > 65535 ||
598 port_high <= 0 || port_high > 65535 ||
599 port_low > port_high) {
600 Alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
601 goto err;
602 }
603 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
604 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
605 newsrv->conn_src.sport_range->ports[i] = port_low + i;
606 }
607
608 *cur_arg += 2;
609 while (*(args[*cur_arg])) {
610 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
611#if defined(CONFIG_HAP_TRANSPARENT)
612 if (!*args[*cur_arg + 1]) {
613 Alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
614 "or 'hdr_ip(name,#)' as argument.\n");
615 goto err;
616 }
617 if (!strcmp(args[*cur_arg + 1], "client")) {
618 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
619 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
620 }
621 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
622 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
623 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
624 }
625 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
626 char *name, *end;
627
628 name = args[*cur_arg + 1] + 7;
629 while (isspace(*name))
630 name++;
631
632 end = name;
633 while (*end && !isspace(*end) && *end != ',' && *end != ')')
634 end++;
635
636 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
637 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
638 free(newsrv->conn_src.bind_hdr_name);
639 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
640 newsrv->conn_src.bind_hdr_len = end - name;
641 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
642 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
643 newsrv->conn_src.bind_hdr_occ = -1;
644
645 /* now look for an occurrence number */
646 while (isspace(*end))
647 end++;
648 if (*end == ',') {
649 end++;
650 name = end;
651 if (*end == '-')
652 end++;
653 while (isdigit((int)*end))
654 end++;
655 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
656 }
657
658 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
659 Alert("usesrc hdr_ip(name,num) does not support negative"
660 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
661 goto err;
662 }
663 }
664 else {
665 struct sockaddr_storage *sk;
666 int port1, port2;
667
668 /* 'sk' is statically allocated (no need to be freed). */
669 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
670 if (!sk) {
671 Alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
672 goto err;
673 }
674
675 proto = protocol_by_family(sk->ss_family);
676 if (!proto || !proto->connect) {
677 Alert("'%s %s' : connect() not supported for this address family.\n",
678 args[*cur_arg], args[*cur_arg + 1]);
679 goto err;
680 }
681
682 if (port1 != port2) {
683 Alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
684 args[*cur_arg], args[*cur_arg + 1]);
685 goto err;
686 }
687 newsrv->conn_src.tproxy_addr = *sk;
688 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
689 }
690 global.last_checks |= LSTCHK_NETADM;
691 *cur_arg += 2;
692 continue;
693#else /* no TPROXY support */
694 Alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
695 goto err;
696#endif /* defined(CONFIG_HAP_TRANSPARENT) */
697 } /* "usesrc" */
698
699 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
700#ifdef SO_BINDTODEVICE
701 if (!*args[*cur_arg + 1]) {
702 Alert("'%s' : missing interface name.\n", args[0]);
703 goto err;
704 }
705 free(newsrv->conn_src.iface_name);
706 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
707 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
708 global.last_checks |= LSTCHK_NETADM;
709#else
710 Alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
711 goto err;
712#endif
713 *cur_arg += 2;
714 continue;
715 }
716 /* this keyword in not an option of "source" */
717 break;
718 } /* while */
719
720 return 0;
721
722 err:
723 free(errmsg);
724 return ERR_ALERT | ERR_FATAL;
725}
726
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100727/* Parse the "stick" server keyword */
728static int srv_parse_stick(char **args, int *cur_arg,
729 struct proxy *curproxy, struct server *newsrv, char **err)
730{
731 newsrv->flags &= ~SRV_F_NON_STICK;
732 return 0;
733}
734
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100735/* Parse the "track" server keyword */
736static int srv_parse_track(char **args, int *cur_arg,
737 struct proxy *curproxy, struct server *newsrv, char **err)
738{
739 char *arg;
740
741 arg = args[*cur_arg + 1];
742 if (!*arg) {
743 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
744 return ERR_ALERT | ERR_FATAL;
745 }
746
747 free(newsrv->trackit);
748 newsrv->trackit = strdup(arg);
749
750 return 0;
751}
752
Frédéric Lécailledba97072017-03-17 15:33:50 +0100753
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200754/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200755 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200756 * shutdown.
757 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200758void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200759{
Willy Tarreau87b09662015-04-03 00:22:06 +0200760 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200761
Willy Tarreau87b09662015-04-03 00:22:06 +0200762 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
763 if (stream->srv_conn == srv)
764 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200765}
766
767/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200768 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200769 * the reason for the shutdown.
770 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200771void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200772{
773 struct server *srv;
774
775 for (srv = px->srv; srv != NULL; srv = srv->next)
776 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200777 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200778}
779
Willy Tarreaubda92272014-05-20 21:55:30 +0200780/* Appends some information to a message string related to a server going UP or
781 * DOWN. If both <forced> and <reason> are null and the server tracks another
782 * one, a "via" information will be provided to know where the status came from.
783 * If <reason> is non-null, the entire string will be appended after a comma and
784 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200785 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200786 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200787 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200788void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200789{
Willy Tarreaubda92272014-05-20 21:55:30 +0200790 if (reason)
791 chunk_appendf(msg, ", %s", reason);
792 else if (!forced && s->track)
793 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200794
795 if (xferred >= 0) {
796 if (s->state == SRV_ST_STOPPED)
797 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
798 " %d sessions active, %d requeued, %d remaining in queue",
799 s->proxy->srv_act, s->proxy->srv_bck,
800 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
801 s->cur_sess, xferred, s->nbpend);
802 else
803 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
804 " %d sessions requeued, %d total in queue",
805 s->proxy->srv_act, s->proxy->srv_bck,
806 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
807 xferred, s->nbpend);
808 }
809}
810
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200811/* Marks server <s> down, regardless of its checks' statuses, notifies by all
812 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200813 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200814 * recomputes the number of servers, but not the map. Maintenance servers are
815 * ignored. It reports <reason> if non-null as the reason for going down. Note
816 * that it makes use of the trash to build the log strings, so <reason> must
817 * not be placed there.
818 */
819void srv_set_stopped(struct server *s, const char *reason)
820{
821 struct server *srv;
822 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
823 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900824 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200825 int xferred;
826
827 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
828 return;
829
830 s->last_change = now.tv_sec;
831 s->state = SRV_ST_STOPPED;
832 if (s->proxy->lbprm.set_server_status_down)
833 s->proxy->lbprm.set_server_status_down(s);
834
835 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200836 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200837
Willy Tarreau87b09662015-04-03 00:22:06 +0200838 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200839 * a connection. Those which are redispatchable will be queued
840 * to another server or to the proxy itself.
841 */
842 xferred = pendconn_redistribute(s);
843
844 chunk_printf(&trash,
845 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
846 s->proxy->id, s->id);
847
848 srv_append_status(&trash, s, reason, xferred, 0);
849 Warning("%s.\n", trash.str);
850
851 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900852 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
853 send_log(s->proxy, log_level, "%s.\n", trash.str);
854 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200855
856 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
857 set_backend_down(s->proxy);
858
859 s->counters.down_trans++;
860
861 for (srv = s->trackers; srv; srv = srv->tracknext)
862 srv_set_stopped(srv, NULL);
863}
864
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200865/* Marks server <s> up regardless of its checks' statuses and provided it isn't
866 * in maintenance. Notifies by all available means, recounts the remaining
867 * servers on the proxy and tries to grab requests from the proxy. It
868 * automatically recomputes the number of servers, but not the map. Maintenance
869 * servers are ignored. It reports <reason> if non-null as the reason for going
870 * up. Note that it makes use of the trash to build the log strings, so <reason>
871 * must not be placed there.
872 */
873void srv_set_running(struct server *s, const char *reason)
874{
875 struct server *srv;
876 int xferred;
877
878 if (s->admin & SRV_ADMF_MAINT)
879 return;
880
881 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
882 return;
883
884 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
885 if (s->proxy->last_change < now.tv_sec) // ignore negative times
886 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
887 s->proxy->last_change = now.tv_sec;
888 }
889
890 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
891 s->down_time += now.tv_sec - s->last_change;
892
893 s->last_change = now.tv_sec;
894
895 s->state = SRV_ST_STARTING;
896 if (s->slowstart > 0)
897 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
898 else
899 s->state = SRV_ST_RUNNING;
900
901 server_recalc_eweight(s);
902
903 /* If the server is set with "on-marked-up shutdown-backup-sessions",
904 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200905 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200906 * on all backup servers.
907 */
908 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
909 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200910 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200911
912 /* check if we can handle some connections queued at the proxy. We
913 * will take as many as we can handle.
914 */
915 xferred = pendconn_grab_from_px(s);
916
917 chunk_printf(&trash,
918 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
919 s->proxy->id, s->id);
920
921 srv_append_status(&trash, s, reason, xferred, 0);
922 Warning("%s.\n", trash.str);
923 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900924 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200925
926 for (srv = s->trackers; srv; srv = srv->tracknext)
927 srv_set_running(srv, NULL);
928}
929
Willy Tarreau8eb77842014-05-21 13:54:57 +0200930/* Marks server <s> stopping regardless of its checks' statuses and provided it
931 * isn't in maintenance. Notifies by all available means, recounts the remaining
932 * servers on the proxy and tries to grab requests from the proxy. It
933 * automatically recomputes the number of servers, but not the map. Maintenance
934 * servers are ignored. It reports <reason> if non-null as the reason for going
935 * up. Note that it makes use of the trash to build the log strings, so <reason>
936 * must not be placed there.
937 */
938void srv_set_stopping(struct server *s, const char *reason)
939{
940 struct server *srv;
941 int xferred;
942
943 if (s->admin & SRV_ADMF_MAINT)
944 return;
945
946 if (s->state == SRV_ST_STOPPING)
947 return;
948
949 s->last_change = now.tv_sec;
950 s->state = SRV_ST_STOPPING;
951 if (s->proxy->lbprm.set_server_status_down)
952 s->proxy->lbprm.set_server_status_down(s);
953
Willy Tarreau87b09662015-04-03 00:22:06 +0200954 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200955 * a connection. Those which are redispatchable will be queued
956 * to another server or to the proxy itself.
957 */
958 xferred = pendconn_redistribute(s);
959
960 chunk_printf(&trash,
961 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
962 s->proxy->id, s->id);
963
964 srv_append_status(&trash, s, reason, xferred, 0);
965
966 Warning("%s.\n", trash.str);
967 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
968
969 if (!s->proxy->srv_bck && !s->proxy->srv_act)
970 set_backend_down(s->proxy);
971
972 for (srv = s->trackers; srv; srv = srv->tracknext)
973 srv_set_stopping(srv, NULL);
974}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200975
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200976/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
977 * enforce either maint mode or drain mode. It is not allowed to set more than
978 * one flag at once. The equivalent "inherited" flag is propagated to all
979 * tracking servers. Maintenance mode disables health checks (but not agent
980 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100981 * is done. If <cause> is non-null, it will be displayed at the end of the log
982 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200983 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100984void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200985{
986 struct check *check = &s->check;
987 struct server *srv;
988 int xferred;
989
990 if (!mode)
991 return;
992
993 /* stop going down as soon as we meet a server already in the same state */
994 if (s->admin & mode)
995 return;
996
997 s->admin |= mode;
998
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200999 /* stop going down if the equivalent flag was already present (forced or inherited) */
1000 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
1001 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
1002 return;
1003
1004 /* Maintenance must also disable health checks */
1005 if (mode & SRV_ADMF_MAINT) {
1006 if (s->check.state & CHK_ST_ENABLED) {
1007 s->check.state |= CHK_ST_PAUSED;
1008 check->health = 0;
1009 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001010
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001011 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001012 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +01001013 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
1014 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
1015 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001016
Willy Tarreaubda92272014-05-20 21:55:30 +02001017 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001018
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001019 if (!(global.mode & MODE_STARTING)) {
1020 Warning("%s.\n", trash.str);
1021 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1022 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001023 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001024 else { /* server was still running */
1025 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
1026 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1027
1028 check->health = 0; /* failure */
1029 s->last_change = now.tv_sec;
1030 s->state = SRV_ST_STOPPED;
1031 if (s->proxy->lbprm.set_server_status_down)
1032 s->proxy->lbprm.set_server_status_down(s);
1033
1034 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001035 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001036
Willy Tarreau87b09662015-04-03 00:22:06 +02001037 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001038 * a connection. Those which are redispatchable will be queued
1039 * to another server or to the proxy itself.
1040 */
1041 xferred = pendconn_redistribute(s);
1042
1043 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +01001044 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001045 s->flags & SRV_F_BACKUP ? "Backup " : "",
Willy Tarreau8b428482016-11-07 15:53:43 +01001046 s->proxy->id, s->id,
1047 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001048
1049 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
1050
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001051 if (!(global.mode & MODE_STARTING)) {
1052 Warning("%s.\n", trash.str);
1053 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
1054 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001055
1056 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1057 set_backend_down(s->proxy);
1058
1059 s->counters.down_trans++;
1060 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001061 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001062
1063 /* drain state is applied only if not yet in maint */
1064 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001065 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1066
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001067 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001068 if (s->proxy->lbprm.set_server_status_down)
1069 s->proxy->lbprm.set_server_status_down(s);
1070
Willy Tarreau87b09662015-04-03 00:22:06 +02001071 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001072 * a connection. Those which are redispatchable will be queued
1073 * to another server or to the proxy itself.
1074 */
1075 xferred = pendconn_redistribute(s);
1076
Willy Tarreau8b428482016-11-07 15:53:43 +01001077 chunk_printf(&trash, "%sServer %s/%s enters drain state%s%s%s",
1078 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
1079 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001080
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001081 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001082
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001083 if (!(global.mode & MODE_STARTING)) {
1084 Warning("%s.\n", trash.str);
1085 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1086 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
1087 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001088 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1089 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001090 }
1091
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001092 /* compute the inherited flag to propagate */
1093 if (mode & SRV_ADMF_MAINT)
1094 mode = SRV_ADMF_IMAINT;
1095 else if (mode & SRV_ADMF_DRAIN)
1096 mode = SRV_ADMF_IDRAIN;
1097
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001098 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +01001099 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001100}
1101
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001102/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1103 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1104 * than one flag at once. The equivalent "inherited" flag is propagated to all
1105 * tracking servers. Leaving maintenance mode re-enables health checks. When
1106 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001107 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001108void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001109{
1110 struct check *check = &s->check;
1111 struct server *srv;
1112 int xferred = -1;
1113
1114 if (!mode)
1115 return;
1116
1117 /* stop going down as soon as we see the flag is not there anymore */
1118 if (!(s->admin & mode))
1119 return;
1120
1121 s->admin &= ~mode;
1122
1123 if (s->admin & SRV_ADMF_MAINT) {
1124 /* remaining in maintenance mode, let's inform precisely about the
1125 * situation.
1126 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001127 if (mode & SRV_ADMF_FMAINT) {
1128 chunk_printf(&trash,
1129 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
1130 s->flags & SRV_F_BACKUP ? "Backup " : "",
1131 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001132
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001133 if (s->track) /* normally it's mandatory here */
1134 chunk_appendf(&trash, " via %s/%s",
1135 s->track->proxy->id, s->track->id);
1136 Warning("%s.\n", trash.str);
1137 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1138 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001139 if (mode & SRV_ADMF_RMAINT) {
1140 chunk_printf(&trash,
1141 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
1142 s->flags & SRV_F_BACKUP ? "Backup " : "",
1143 s->proxy->id, s->id, s->hostname);
1144
1145 if (s->track) /* normally it's mandatory here */
1146 chunk_appendf(&trash, " via %s/%s",
1147 s->track->proxy->id, s->track->id);
1148 Warning("%s.\n", trash.str);
1149 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1150 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001151 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001152 chunk_printf(&trash,
1153 "%sServer %s/%s remains in forced maintenance",
1154 s->flags & SRV_F_BACKUP ? "Backup " : "",
1155 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001156 Warning("%s.\n", trash.str);
1157 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1158 }
1159 /* don't report anything when leaving drain mode and remaining in maintenance */
1160 }
1161 else if (mode & SRV_ADMF_MAINT) {
1162 /* OK here we're leaving maintenance, we have many things to check,
1163 * because the server might possibly be coming back up depending on
1164 * its state. In practice, leaving maintenance means that we should
1165 * immediately turn to UP (more or less the slowstart) under the
1166 * following conditions :
1167 * - server is neither checked nor tracked
1168 * - server tracks another server which is not checked
1169 * - server tracks another server which is already up
1170 * Which sums up as something simpler :
1171 * "either the tracking server is up or the server's checks are disabled
1172 * or up". Otherwise we only re-enable health checks. There's a special
1173 * case associated to the stopping state which can be inherited. Note
1174 * that the server might still be in drain mode, which is naturally dealt
1175 * with by the lower level functions.
1176 */
1177
1178 if (s->check.state & CHK_ST_ENABLED) {
1179 s->check.state &= ~CHK_ST_PAUSED;
1180 check->health = check->rise; /* start OK but check immediately */
1181 }
1182
1183 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
1184 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
1185 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
1186 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1187 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1188 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1189 s->proxy->last_change = now.tv_sec;
1190 }
1191
1192 if (s->last_change < now.tv_sec) // ignore negative times
1193 s->down_time += now.tv_sec - s->last_change;
1194 s->last_change = now.tv_sec;
1195
1196 if (s->track && s->track->state == SRV_ST_STOPPING)
1197 s->state = SRV_ST_STOPPING;
1198 else {
1199 s->state = SRV_ST_STARTING;
1200 if (s->slowstart > 0)
1201 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
1202 else
1203 s->state = SRV_ST_RUNNING;
1204 }
1205
1206 server_recalc_eweight(s);
1207
1208 /* If the server is set with "on-marked-up shutdown-backup-sessions",
1209 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +02001210 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001211 * on all backup servers.
1212 */
1213 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
1214 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001215 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001216
1217 /* check if we can handle some connections queued at the proxy. We
1218 * will take as many as we can handle.
1219 */
1220 xferred = pendconn_grab_from_px(s);
1221 }
1222
1223 if (mode & SRV_ADMF_FMAINT) {
1224 chunk_printf(&trash,
1225 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
1226 s->flags & SRV_F_BACKUP ? "Backup " : "",
1227 s->proxy->id, s->id,
1228 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1229 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001230 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001231 else if (mode & SRV_ADMF_RMAINT) {
1232 chunk_printf(&trash,
1233 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
1234 s->flags & SRV_F_BACKUP ? "Backup " : "",
1235 s->proxy->id, s->id, s->hostname,
1236 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1237 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
1238 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001239 else {
1240 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001241 "%sServer %s/%s is %s/%s (leaving maintenance)",
1242 s->flags & SRV_F_BACKUP ? "Backup " : "",
1243 s->proxy->id, s->id,
1244 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1245 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
1246 srv_append_status(&trash, s, NULL, xferred, 0);
1247 }
1248 Warning("%s.\n", trash.str);
1249 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1250 }
1251 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
1252 /* remaining in drain mode after removing one of its flags */
1253
1254 if (mode & SRV_ADMF_FDRAIN) {
1255 chunk_printf(&trash,
1256 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001257 s->flags & SRV_F_BACKUP ? "Backup " : "",
1258 s->proxy->id, s->id);
1259
1260 if (s->track) /* normally it's mandatory here */
1261 chunk_appendf(&trash, " via %s/%s",
1262 s->track->proxy->id, s->track->id);
1263 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001264 else {
1265 chunk_printf(&trash,
1266 "%sServer %s/%s remains in forced drain mode",
1267 s->flags & SRV_F_BACKUP ? "Backup " : "",
1268 s->proxy->id, s->id);
1269 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001270 Warning("%s.\n", trash.str);
1271 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001272 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001273 else if (mode & SRV_ADMF_DRAIN) {
1274 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001275 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1276 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1277 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1278 s->proxy->last_change = now.tv_sec;
1279 }
1280
1281 if (s->last_change < now.tv_sec) // ignore negative times
1282 s->down_time += now.tv_sec - s->last_change;
1283 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001284 server_recalc_eweight(s);
1285
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001286 if (mode & SRV_ADMF_FDRAIN) {
1287 chunk_printf(&trash,
1288 "%sServer %s/%s is %s (leaving forced drain)",
1289 s->flags & SRV_F_BACKUP ? "Backup " : "",
1290 s->proxy->id, s->id,
1291 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
1292 }
1293 else {
1294 chunk_printf(&trash,
1295 "%sServer %s/%s is %s (leaving drain)",
1296 s->flags & SRV_F_BACKUP ? "Backup " : "",
1297 s->proxy->id, s->id,
1298 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
1299 if (s->track) /* normally it's mandatory here */
1300 chunk_appendf(&trash, " via %s/%s",
1301 s->track->proxy->id, s->track->id);
1302 }
1303 Warning("%s.\n", trash.str);
1304 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001305 }
1306
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001307 /* stop going down if the equivalent flag is still present (forced or inherited) */
1308 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
1309 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
1310 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001311
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001312 if (mode & SRV_ADMF_MAINT)
1313 mode = SRV_ADMF_IMAINT;
1314 else if (mode & SRV_ADMF_DRAIN)
1315 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001316
1317 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001318 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001319}
1320
Willy Tarreau757478e2016-11-03 19:22:19 +01001321/* principle: propagate maint and drain to tracking servers. This is useful
1322 * upon startup so that inherited states are correct.
1323 */
1324static void srv_propagate_admin_state(struct server *srv)
1325{
1326 struct server *srv2;
1327
1328 if (!srv->trackers)
1329 return;
1330
1331 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
1332 if (srv->admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001333 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001334
1335 if (srv->admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001336 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001337 }
1338}
1339
1340/* Compute and propagate the admin states for all servers in proxy <px>.
1341 * Only servers *not* tracking another one are considered, because other
1342 * ones will be handled when the server they track is visited.
1343 */
1344void srv_compute_all_admin_states(struct proxy *px)
1345{
1346 struct server *srv;
1347
1348 for (srv = px->srv; srv; srv = srv->next) {
1349 if (srv->track)
1350 continue;
1351 srv_propagate_admin_state(srv);
1352 }
1353}
1354
Willy Tarreaudff55432012-10-10 17:51:05 +02001355/* Note: must not be declared <const> as its list will be overwritten.
1356 * Please take care of keeping this list alphabetically sorted, doing so helps
1357 * all code contributors.
1358 * Optional keywords are also declared with a NULL ->parse() function so that
1359 * the config parser can report an appropriate error when a known keyword was
1360 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001361 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001362 */
1363static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001364 { "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 +01001365 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001366 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001367 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001368 { "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 +01001369 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001370 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1371 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001372 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001373 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001374 { "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 +01001375 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001376 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001377 { "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 +01001378 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1379 { "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 +01001380 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001381 { "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 +01001382 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001383 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1384 { "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 +02001385 { "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 +01001386 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001387 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001388 { NULL, NULL, 0 },
1389}};
1390
1391__attribute__((constructor))
1392static void __listener_init(void)
1393{
1394 srv_register_keywords(&srv_kws);
1395}
1396
Willy Tarreau004e0452013-11-21 11:22:01 +01001397/* Recomputes the server's eweight based on its state, uweight, the current time,
1398 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1399 * state is automatically disabled if the time is elapsed.
1400 */
1401void server_recalc_eweight(struct server *sv)
1402{
1403 struct proxy *px = sv->proxy;
1404 unsigned w;
1405
1406 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1407 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +02001408 if (sv->state == SRV_ST_STARTING)
1409 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001410 }
1411
1412 /* We must take care of not pushing the server to full throttle during slow starts.
1413 * It must also start immediately, at least at the minimal step when leaving maintenance.
1414 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001415 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001416 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1417 else
1418 w = px->lbprm.wdiv;
1419
1420 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
1421
1422 /* now propagate the status change to any LB algorithms */
1423 if (px->lbprm.update_server_eweight)
1424 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +02001425 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +01001426 if (px->lbprm.set_server_status_up)
1427 px->lbprm.set_server_status_up(sv);
1428 }
1429 else {
1430 if (px->lbprm.set_server_status_down)
1431 px->lbprm.set_server_status_down(sv);
1432 }
1433}
1434
Willy Tarreaubaaee002006-06-26 02:48:02 +02001435/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001436 * Parses weight_str and configures sv accordingly.
1437 * Returns NULL on success, error message string otherwise.
1438 */
1439const char *server_parse_weight_change_request(struct server *sv,
1440 const char *weight_str)
1441{
1442 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001443 long int w;
1444 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001445
1446 px = sv->proxy;
1447
1448 /* if the weight is terminated with '%', it is set relative to
1449 * the initial weight, otherwise it is absolute.
1450 */
1451 if (!*weight_str)
1452 return "Require <weight> or <weight%>.\n";
1453
Simon Hormanb796afa2013-02-12 10:45:53 +09001454 w = strtol(weight_str, &end, 10);
1455 if (end == weight_str)
1456 return "Empty weight string empty or preceded by garbage";
1457 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001458 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001459 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001460 /* Avoid integer overflow */
1461 if (w > 25600)
1462 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001463 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001464 if (w > 256)
1465 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001466 }
1467 else if (w < 0 || w > 256)
1468 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001469 else if (end[0] != '\0')
1470 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001471
1472 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1473 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1474
1475 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001476 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001477
1478 return NULL;
1479}
1480
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001481/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001482 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1483 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001484 * Returns:
1485 * - error string on error
1486 * - NULL on success
1487 */
1488const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001489 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001490{
1491 unsigned char ip[INET6_ADDRSTRLEN];
1492
1493 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001494 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001495 return NULL;
1496 }
1497 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001498 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001499 return NULL;
1500 }
1501
1502 return "Could not understand IP address format.\n";
1503}
1504
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001505const char *server_parse_maxconn_change_request(struct server *sv,
1506 const char *maxconn_str)
1507{
1508 long int v;
1509 char *end;
1510
1511 if (!*maxconn_str)
1512 return "Require <maxconn>.\n";
1513
1514 v = strtol(maxconn_str, &end, 10);
1515 if (end == maxconn_str)
1516 return "maxconn string empty or preceded by garbage";
1517 else if (end[0] != '\0')
1518 return "Trailing garbage in maxconn string";
1519
1520 if (sv->maxconn == sv->minconn) { // static maxconn
1521 sv->maxconn = sv->minconn = v;
1522 } else { // dynamic maxconn
1523 sv->maxconn = v;
1524 }
1525
1526 if (may_dequeue_tasks(sv, sv->proxy))
1527 process_srv_queue(sv);
1528
1529 return NULL;
1530}
1531
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001532#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001533static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1534 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001535{
1536 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001537 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001538 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001539 NULL,
1540 };
1541
1542 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001543 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001544
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001545 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1546}
1547
1548static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1549{
1550 struct sample_expr *expr;
1551
1552 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 +01001553 if (!expr) {
1554 memprintf(err, "error detected while parsing sni expression : %s", *err);
1555 return ERR_ALERT | ERR_FATAL;
1556 }
1557
1558 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1559 memprintf(err, "error detected while parsing sni expression : "
1560 " fetch method '%s' extracts information from '%s', "
1561 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001562 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001563 return ERR_ALERT | ERR_FATAL;
1564 }
1565
1566 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1567 release_sample_expr(newsrv->ssl_ctx.sni);
1568 newsrv->ssl_ctx.sni = expr;
1569
1570 return 0;
1571}
1572#endif
1573
1574static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1575{
1576 if (err && *err) {
1577 indent_msg(err, 2);
1578 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1579 }
1580 else
1581 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1582 file, linenum, args[0], args[1], args[cur_arg]);
1583}
1584
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001585static void srv_conn_src_sport_range_cpy(struct server *srv,
1586 struct server *src)
1587{
1588 int range_sz;
1589
1590 range_sz = src->conn_src.sport_range->size;
1591 if (range_sz > 0) {
1592 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1593 if (srv->conn_src.sport_range != NULL) {
1594 int i;
1595
1596 for (i = 0; i < range_sz; i++) {
1597 srv->conn_src.sport_range->ports[i] =
1598 src->conn_src.sport_range->ports[i];
1599 }
1600 }
1601 }
1602}
1603
1604/*
1605 * Copy <src> server connection source settings to <srv> server everything needed.
1606 */
1607static void srv_conn_src_cpy(struct server *srv, struct server *src)
1608{
1609 srv->conn_src.opts = src->conn_src.opts;
1610 srv->conn_src.source_addr = src->conn_src.source_addr;
1611
1612 /* Source port range copy. */
1613 if (src->conn_src.sport_range != NULL)
1614 srv_conn_src_sport_range_cpy(srv, src);
1615
1616#ifdef CONFIG_HAP_TRANSPARENT
1617 if (src->conn_src.bind_hdr_name != NULL) {
1618 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1619 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1620 }
1621 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1622 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1623#endif
1624 if (src->conn_src.iface_name != NULL)
1625 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1626}
1627
1628/*
1629 * Copy <src> server SSL settings to <srv> server allocating
1630 * everything needed.
1631 */
1632#if defined(USE_OPENSSL)
1633static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1634{
1635 if (src->ssl_ctx.ca_file != NULL)
1636 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1637 if (src->ssl_ctx.crl_file != NULL)
1638 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1639 if (src->ssl_ctx.client_crt != NULL)
1640 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1641
1642 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1643
1644 if (src->ssl_ctx.verify_host != NULL)
1645 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1646 if (src->ssl_ctx.ciphers != NULL)
1647 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1648 if (src->sni_expr != NULL)
1649 srv->sni_expr = strdup(src->sni_expr);
1650}
1651#endif
1652
1653/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001654 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001655 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001656 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001657 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001658static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001659{
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001660 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001661 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001662
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001663 free(srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001664 srv->hostname = strdup(hostname);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001665
Baptiste Assmann42746372017-05-03 12:12:02 +02001666 srv->hostname_dn_len = dns_str_to_dn_label_len(hostname);
1667 srv->hostname_dn = calloc(srv->hostname_dn_len + 1, sizeof(char));
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001668
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001669 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001670 goto err;
1671
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001672 if (!dns_str_to_dn_label(srv->hostname,
Baptiste Assmann42746372017-05-03 12:12:02 +02001673 srv->hostname_dn,
1674 srv->hostname_dn_len + 1))
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001675 goto err;
1676
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001677 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001678
1679 err:
1680 free(srv->hostname);
1681 srv->hostname = NULL;
Baptiste Assmann42746372017-05-03 12:12:02 +02001682 free(srv->hostname_dn);
1683 srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001684 return -1;
1685}
1686
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001687/*
1688 * Free the link between a server and its resolution.
1689 * It also performs the following tasks:
1690 * - check if resolution can be moved back in the resolvers' pool
1691 * (and do it)
1692 * - move resolution's hostname_dn and hostname_dn_len to the next requester
1693 * available (when applied)
1694 */
Baptiste Assmann747359e2017-08-14 10:37:46 +02001695void srv_free_from_resolution(struct server *srv)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001696{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001697 struct dns_requester *requester;
1698 int count;
1699
1700 /* check if we can move the resolution back to the pool.
1701 * if <count> is greater than 1, then we can't */
1702 count = 0;
1703 list_for_each_entry(requester, &srv->resolution->requester.wait, list) {
1704 ++count;
1705 if (count > 1)
1706 break;
1707 }
1708 list_for_each_entry(requester, &srv->resolution->requester.curr, list) {
1709 ++count;
1710 if (count > 1)
1711 break;
1712 }
1713 if (count <= 1) {
1714 /* move the resolution back to the pool */
1715 dns_resolution_free(srv->resolvers, srv->resolution);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001716 return;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001717 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001718
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001719 dns_rm_requester_from_resolution(srv->dns_requester, srv->resolution);
1720
1721 return;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001722}
1723
1724/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001725 * Copy <src> server settings to <srv> server allocating
1726 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001727 * This function is not supposed to be called at any time, but only
1728 * during server settings parsing or during server allocations from
1729 * a server template, and just after having calloc()'ed a new server.
1730 * So, <src> may only be a default server (when parsing server settings)
1731 * or a server template (during server allocations from a server template).
1732 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1733 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001734 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001735static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001736{
1737 /* Connection source settings copy */
1738 srv_conn_src_cpy(srv, src);
1739
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001740 if (srv_tmpl) {
1741 srv->addr = src->addr;
1742 srv->svc_port = src->svc_port;
1743 }
1744
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001745 srv->pp_opts = src->pp_opts;
1746 if (src->rdr_pfx != NULL) {
1747 srv->rdr_pfx = strdup(src->rdr_pfx);
1748 srv->rdr_len = src->rdr_len;
1749 }
1750 if (src->cookie != NULL) {
1751 srv->cookie = strdup(src->cookie);
1752 srv->cklen = src->cklen;
1753 }
1754 srv->use_ssl = src->use_ssl;
1755 srv->check.addr = srv->agent.addr = src->check.addr;
1756 srv->check.use_ssl = src->check.use_ssl;
1757 srv->check.port = src->check.port;
1758 /* Note: 'flags' field has potentially been already initialized. */
1759 srv->flags |= src->flags;
1760 srv->do_check = src->do_check;
1761 srv->do_agent = src->do_agent;
1762 if (srv->check.port)
1763 srv->flags |= SRV_F_CHECKPORT;
1764 srv->check.inter = src->check.inter;
1765 srv->check.fastinter = src->check.fastinter;
1766 srv->check.downinter = src->check.downinter;
1767 srv->agent.use_ssl = src->agent.use_ssl;
1768 srv->agent.port = src->agent.port;
1769 if (src->agent.send_string != NULL)
1770 srv->agent.send_string = strdup(src->agent.send_string);
1771 srv->agent.send_string_len = src->agent.send_string_len;
1772 srv->agent.inter = src->agent.inter;
1773 srv->agent.fastinter = src->agent.fastinter;
1774 srv->agent.downinter = src->agent.downinter;
1775 srv->maxqueue = src->maxqueue;
1776 srv->minconn = src->minconn;
1777 srv->maxconn = src->maxconn;
1778 srv->slowstart = src->slowstart;
1779 srv->observe = src->observe;
1780 srv->onerror = src->onerror;
1781 srv->onmarkeddown = src->onmarkeddown;
1782 srv->onmarkedup = src->onmarkedup;
1783 if (src->trackit != NULL)
1784 srv->trackit = strdup(src->trackit);
1785 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1786 srv->uweight = srv->iweight = src->iweight;
1787
1788 srv->check.send_proxy = src->check.send_proxy;
1789 /* health: up, but will fall down at first failure */
1790 srv->check.rise = srv->check.health = src->check.rise;
1791 srv->check.fall = src->check.fall;
1792
1793 /* Here we check if 'disabled' is the default server state */
1794 if (src->admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1795 srv->admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1796 srv->state = SRV_ST_STOPPED;
1797 srv->check.state |= CHK_ST_PAUSED;
1798 srv->check.health = 0;
1799 }
1800
1801 /* health: up but will fall down at first failure */
1802 srv->agent.rise = srv->agent.health = src->agent.rise;
1803 srv->agent.fall = src->agent.fall;
1804
1805 if (src->resolvers_id != NULL)
1806 srv->resolvers_id = strdup(src->resolvers_id);
1807 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1808 if (srv->dns_opts.family_prio == AF_UNSPEC)
1809 srv->dns_opts.family_prio = AF_INET6;
1810 memcpy(srv->dns_opts.pref_net,
1811 src->dns_opts.pref_net,
1812 sizeof srv->dns_opts.pref_net);
1813 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1814
1815 srv->init_addr_methods = src->init_addr_methods;
1816 srv->init_addr = src->init_addr;
1817#if defined(USE_OPENSSL)
1818 srv_ssl_settings_cpy(srv, src);
1819#endif
1820#ifdef TCP_USER_TIMEOUT
1821 srv->tcp_ut = src->tcp_ut;
1822#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001823 if (srv_tmpl)
1824 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001825}
1826
1827static struct server *new_server(struct proxy *proxy)
1828{
1829 struct server *srv;
1830
1831 srv = calloc(1, sizeof *srv);
1832 if (!srv)
1833 return NULL;
1834
1835 srv->obj_type = OBJ_TYPE_SERVER;
1836 srv->proxy = proxy;
1837 LIST_INIT(&srv->actconns);
1838 LIST_INIT(&srv->pendconns);
1839 LIST_INIT(&srv->priv_conns);
1840 LIST_INIT(&srv->idle_conns);
1841 LIST_INIT(&srv->safe_conns);
1842
1843 srv->state = SRV_ST_RUNNING; /* early server setup */
1844 srv->last_change = now.tv_sec;
1845
1846 srv->check.status = HCHK_STATUS_INI;
1847 srv->check.server = srv;
1848 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1849
1850 srv->agent.status = HCHK_STATUS_INI;
1851 srv->agent.server = srv;
1852 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1853
1854 return srv;
1855}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001856
1857/*
1858 * Validate <srv> server health-check settings.
1859 * Returns 0 if everything is OK, -1 if not.
1860 */
1861static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1862{
1863 struct tcpcheck_rule *r = NULL;
1864 struct list *l;
1865
1866 /*
1867 * We need at least a service port, a check port or the first tcp-check rule must
1868 * be a 'connect' one when checking an IPv4/IPv6 server.
1869 */
1870 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1871 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1872 return 0;
1873
1874 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1875 if (!r) {
1876 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1877 "Check has been disabled.\n",
1878 file, linenum, srv->id);
1879 return -1;
1880 }
1881
1882 /* search the first action (connect / send / expect) in the list */
1883 l = &srv->proxy->tcpcheck_rules;
1884 list_for_each_entry(r, l, list) {
1885 if (r->action != TCPCHK_ACT_COMMENT)
1886 break;
1887 }
1888
1889 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1890 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1891 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1892 file, linenum, srv->id);
1893 return -1;
1894 }
1895
1896 /* scan the tcp-check ruleset to ensure a port has been configured */
1897 l = &srv->proxy->tcpcheck_rules;
1898 list_for_each_entry(r, l, list) {
1899 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1900 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1901 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1902 file, linenum, srv->id);
1903 return -1;
1904 }
1905 }
1906
1907 return 0;
1908}
1909
1910/*
1911 * Initialize <srv> health-check structure.
1912 * Returns the error string in case of memory allocation failure, NULL if not.
1913 */
1914static const char *do_health_check_init(struct server *srv, int check_type, int state)
1915{
1916 const char *ret;
1917
1918 if (!srv->do_check)
1919 return NULL;
1920
1921 ret = init_check(&srv->check, check_type);
1922 if (ret)
1923 return ret;
1924
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001925 srv->check.state |= state;
1926 global.maxsock++;
1927
1928 return NULL;
1929}
1930
1931static int server_health_check_init(const char *file, int linenum,
1932 struct server *srv, struct proxy *curproxy)
1933{
1934 const char *ret;
1935
1936 if (!srv->do_check)
1937 return 0;
1938
1939 if (srv->trackit) {
1940 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1941 file, linenum);
1942 return ERR_ALERT | ERR_FATAL;
1943 }
1944
1945 if (server_healthcheck_validate(file, linenum, srv) < 0)
1946 return ERR_ALERT | ERR_ABORT;
1947
1948 /* note: check type will be set during the config review phase */
1949 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1950 if (ret) {
1951 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1952 return ERR_ALERT | ERR_ABORT;
1953 }
1954
1955 return 0;
1956}
1957
1958/*
1959 * Initialize <srv> agent check structure.
1960 * Returns the error string in case of memory allocation failure, NULL if not.
1961 */
1962static const char *do_server_agent_check_init(struct server *srv, int state)
1963{
1964 const char *ret;
1965
1966 if (!srv->do_agent)
1967 return NULL;
1968
1969 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1970 if (ret)
1971 return ret;
1972
1973 if (!srv->agent.inter)
1974 srv->agent.inter = srv->check.inter;
1975
1976 srv->agent.state |= state;
1977 global.maxsock++;
1978
1979 return NULL;
1980}
1981
1982static int server_agent_check_init(const char *file, int linenum,
1983 struct server *srv, struct proxy *curproxy)
1984{
1985 const char *ret;
1986
1987 if (!srv->do_agent)
1988 return 0;
1989
1990 if (!srv->agent.port) {
1991 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1992 file, linenum, srv->id);
1993 return ERR_ALERT | ERR_FATAL;
1994 }
1995
1996 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1997 if (ret) {
1998 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1999 return ERR_ALERT | ERR_ABORT;
2000 }
2001
2002 return 0;
2003}
2004
2005#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2006static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
2007 struct server *srv, struct proxy *proxy)
2008{
2009 int ret;
2010 char *err = NULL;
2011
2012 if (!srv->sni_expr)
2013 return 0;
2014
2015 ret = server_parse_sni_expr(srv, proxy, &err);
2016 if (!ret)
2017 return 0;
2018
2019 display_parser_err(file, linenum, args, cur_arg, &err);
2020 free(err);
2021
2022 return ret;
2023}
2024#endif
2025
2026/*
2027 * Server initializations finalization.
2028 * Initialize health check, agent check and SNI expression if enabled.
2029 * Must not be called for a default server instance.
2030 */
2031static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2032 struct server *srv, struct proxy *px)
2033{
2034 int ret;
2035
2036 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2037 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2038 return ret;
2039 }
2040
2041#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2042 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2043 return ret;
2044#endif
2045
2046 if (srv->flags & SRV_F_BACKUP)
2047 px->srv_bck++;
2048 else
2049 px->srv_act++;
2050 srv_lb_commit_status(srv);
2051
2052 return 0;
2053}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002054
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002055/*
2056 * Parse as much as possible such a range string argument: low[-high]
2057 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2058 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2059 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2060 * Returns 0 if succeeded, -1 if not.
2061 */
2062static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2063{
2064 char *nb_high_arg;
2065
2066 *nb_high = 0;
2067 chunk_printf(&trash, "%s", arg);
2068 *nb_low = atoi(trash.str);
2069
2070 if ((nb_high_arg = strchr(trash.str, '-'))) {
2071 *nb_high_arg++ = '\0';
2072 *nb_high = atoi(nb_high_arg);
2073 }
2074 else {
2075 *nb_high += *nb_low;
2076 *nb_low = 1;
2077 }
2078
2079 if (*nb_low < 0 || *nb_high < *nb_low)
2080 return -1;
2081
2082 return 0;
2083}
2084
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002085static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2086{
2087 chunk_printf(&trash, "%s%d", prefix, nb);
2088 free(srv->id);
2089 srv->id = strdup(trash.str);
2090}
2091
2092/*
2093 * Initialize as much as possible servers from <srv> server template.
2094 * Note that a server template is a special server with
2095 * a few different parameters than a server which has
2096 * been parsed mostly the same way as a server.
2097 * Returns the number of servers succesfully allocated,
2098 * 'srv' template included.
2099 */
2100static int server_template_init(struct server *srv, struct proxy *px)
2101{
2102 int i;
2103 struct server *newsrv;
2104
2105 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2106 int check_init_state;
2107 int agent_init_state;
2108
2109 newsrv = new_server(px);
2110 if (!newsrv)
2111 goto err;
2112
2113 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002114 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002115#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2116 if (newsrv->sni_expr) {
2117 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2118 if (!newsrv->ssl_ctx.sni)
2119 goto err;
2120 }
2121#endif
2122 /* Set this new server ID. */
2123 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2124
2125 /* Initial checks states. */
2126 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2127 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2128
2129 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2130 do_server_agent_check_init(newsrv, agent_init_state))
2131 goto err;
2132
2133 /* Linked backwards first. This will be restablished after parsing. */
2134 newsrv->next = px->srv;
2135 px->srv = newsrv;
2136 }
2137 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2138
2139 return i - srv->tmpl_info.nb_low;
2140
2141 err:
2142 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2143 if (newsrv) {
2144#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2145 release_sample_expr(newsrv->ssl_ctx.sni);
2146#endif
2147 free_check(&newsrv->agent);
2148 free_check(&newsrv->check);
2149 }
2150 free(newsrv);
2151 return i - srv->tmpl_info.nb_low;
2152}
2153
Willy Tarreau272adea2014-03-31 10:39:59 +02002154int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
2155{
2156 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002157 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002158 char *errmsg = NULL;
2159 int err_code = 0;
2160 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002161 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002162
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002163 if (!strcmp(args[0], "server") ||
2164 !strcmp(args[0], "default-server") ||
2165 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002166 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002167 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002168 int srv = !defsrv && !strcmp(args[0], "server");
2169 int srv_tmpl = !defsrv && !srv;
2170 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002171
2172 if (!defsrv && curproxy == defproxy) {
2173 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
2174 err_code |= ERR_ALERT | ERR_FATAL;
2175 goto out;
2176 }
2177 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
2178 err_code |= ERR_ALERT | ERR_FATAL;
2179
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002180 /* There is no mandatory first arguments for default server. */
2181 if (srv) {
2182 if (!*args[2]) {
2183 /* 'server' line number of argument check. */
2184 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
2185 file, linenum, args[0]);
2186 err_code |= ERR_ALERT | ERR_FATAL;
2187 goto out;
2188 }
2189
2190 err = invalid_char(args[1]);
2191 }
2192 else if (srv_tmpl) {
2193 if (!*args[3]) {
2194 /* 'server-template' line number of argument check. */
2195 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2196 file, linenum, args[0]);
2197 err_code |= ERR_ALERT | ERR_FATAL;
2198 goto out;
2199 }
2200
2201 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002202 }
2203
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002204 if (err) {
2205 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
2206 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002207 err_code |= ERR_ALERT | ERR_FATAL;
2208 goto out;
2209 }
2210
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002211 cur_arg = 2;
2212 if (srv_tmpl) {
2213 /* Parse server-template <nb | range> arg. */
2214 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
2215 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
2216 file, linenum, args[0], args[cur_arg]);
2217 err_code |= ERR_ALERT | ERR_FATAL;
2218 goto out;
2219 }
2220 cur_arg++;
2221 }
2222
Willy Tarreau272adea2014-03-31 10:39:59 +02002223 if (!defsrv) {
2224 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002225 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002226 struct protocol *proto;
2227
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002228 newsrv = new_server(curproxy);
2229 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002230 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2231 err_code |= ERR_ALERT | ERR_ABORT;
2232 goto out;
2233 }
2234
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002235 if (srv_tmpl) {
2236 newsrv->tmpl_info.nb_low = tmpl_range_low;
2237 newsrv->tmpl_info.nb_high = tmpl_range_high;
2238 }
2239
Willy Tarreau272adea2014-03-31 10:39:59 +02002240 /* the servers are linked backwards first */
2241 newsrv->next = curproxy->srv;
2242 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002243 newsrv->conf.file = strdup(file);
2244 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002245 /* Note: for a server template, its id is its prefix.
2246 * This is a temporary id which will be used for server allocations to come
2247 * after parsing.
2248 */
2249 if (srv)
2250 newsrv->id = strdup(args[1]);
2251 else
2252 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002253
2254 /* several ways to check the port component :
2255 * - IP => port=+0, relative (IPv4 only)
2256 * - IP: => port=+0, relative
2257 * - IP:N => port=N, absolute
2258 * - IP:+N => port=+N, relative
2259 * - IP:-N => port=-N, relative
2260 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002261 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002262 if (!sk) {
2263 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
2264 err_code |= ERR_ALERT | ERR_FATAL;
2265 goto out;
2266 }
2267
2268 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002269 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002270 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
2271 file, linenum, args[0], args[1]);
2272 err_code |= ERR_ALERT | ERR_FATAL;
2273 goto out;
2274 }
2275
2276 if (!port1 || !port2) {
2277 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002278 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002279 }
2280 else if (port1 != port2) {
2281 /* port range */
2282 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
2283 file, linenum, args[0], args[1], args[2]);
2284 err_code |= ERR_ALERT | ERR_FATAL;
2285 goto out;
2286 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002287
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002288 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002289 if (fqdn) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002290 if (fqdn[0] == '_') {
2291 struct dns_srvrq *srvrq = NULL;
2292 int found = 0;
2293 /* SRV record */
2294 /* Check if a SRV request already exists, and if not, create it */
2295 list_for_each_entry(srvrq, &curproxy->srvrq_list, list) {
2296 if (!strcmp(srvrq->name, fqdn)) {
2297 found = 1;
2298 break;
2299 }
2300 }
2301 if (found == 0) {
2302 int hostname_dn_len;
2303
2304 srvrq = calloc(1, sizeof(*srvrq));
2305 if (!srvrq) {
2306 Alert("Failed to allocate memory");
2307 err_code = ERR_ALERT | ERR_FATAL;
2308 goto out;
2309 }
2310 srvrq->obj_type = OBJ_TYPE_SRVRQ;
2311 srvrq->proxy = proxy;
2312 srvrq->name = strdup(fqdn);
2313 srvrq->inter = 2000;
2314 hostname_dn_len = dns_str_to_dn_label_len(fqdn);
2315 if (hostname_dn_len == -1) {
2316 Alert("Failed to parse domaine name '%s'", fqdn);
2317 err_code = ERR_ALERT | ERR_FATAL;
2318 goto out;
2319 }
2320 srvrq->hostname_dn = malloc(hostname_dn_len + 1);
2321 srvrq->hostname_dn_len = hostname_dn_len;
2322 if (!srvrq->hostname_dn) {
2323 Alert("Failed to alloc memory");
2324 err_code = ERR_ALERT | ERR_FATAL;
2325 goto out;
2326 }
2327 if (!dns_str_to_dn_label(fqdn,
2328 srvrq->hostname_dn,
2329 hostname_dn_len + 1)) {
2330 Alert("Failed to parse domain name '%s'", fqdn);
2331 err_code = ERR_ALERT | ERR_FATAL;
2332 goto out;
2333 }
2334 LIST_ADDQ(&proxy->srvrq_list, &srvrq->list);
2335
2336 }
2337 newsrv->srvrq = srvrq;
2338
2339
2340 } else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
2341 Alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
2342 file, linenum, newsrv->id);
2343 err_code |= ERR_ALERT | ERR_FATAL;
2344 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002345 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002346 }
2347
Willy Tarreau272adea2014-03-31 10:39:59 +02002348 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002349 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002350
Olivier Houchard8da5f982017-08-04 18:35:36 +02002351 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002352 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002353 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002354 err_code |= ERR_ALERT | ERR_FATAL;
2355 goto out;
2356 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002357
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002358 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002359 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002360 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002361 } else {
2362 newsrv = &curproxy->defsrv;
2363 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002364 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002365 }
2366
2367 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002368 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002369 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2370 if (err) {
2371 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2372 file, linenum, *err, newsrv->id);
2373 err_code |= ERR_ALERT | ERR_FATAL;
2374 goto out;
2375 }
2376 if (val <= 0) {
2377 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2378 file, linenum, val, args[cur_arg], newsrv->id);
2379 err_code |= ERR_ALERT | ERR_FATAL;
2380 goto out;
2381 }
2382 newsrv->agent.inter = val;
2383 cur_arg += 2;
2384 }
Misiekea849332017-01-09 09:39:51 +01002385 else if (!strcmp(args[cur_arg], "agent-addr")) {
2386 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2387 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2388 goto out;
2389 }
2390
2391 cur_arg += 2;
2392 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002393 else if (!strcmp(args[cur_arg], "agent-port")) {
2394 global.maxsock++;
2395 newsrv->agent.port = atol(args[cur_arg + 1]);
2396 cur_arg += 2;
2397 }
James Brown55f9ff12015-10-21 18:19:05 -07002398 else if (!strcmp(args[cur_arg], "agent-send")) {
2399 global.maxsock++;
2400 free(newsrv->agent.send_string);
2401 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2402 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2403 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2404 cur_arg += 2;
2405 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002406 else if (!strcmp(args[cur_arg], "init-addr")) {
2407 char *p, *end;
2408 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002409 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002410
2411 newsrv->init_addr_methods = 0;
2412 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2413
2414 for (p = args[cur_arg + 1]; *p; p = end) {
2415 /* cut on next comma */
2416 for (end = p; *end && *end != ','; end++);
2417 if (*end)
2418 *(end++) = 0;
2419
Willy Tarreau4310d362016-11-02 15:05:56 +01002420 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002421 if (!strcmp(p, "libc")) {
2422 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2423 }
2424 else if (!strcmp(p, "last")) {
2425 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2426 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002427 else if (!strcmp(p, "none")) {
2428 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2429 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002430 else if (str2ip2(p, &sa, 0)) {
2431 if (is_addr(&newsrv->init_addr)) {
2432 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2433 file, linenum, args[cur_arg], p);
2434 err_code |= ERR_ALERT | ERR_FATAL;
2435 goto out;
2436 }
2437 newsrv->init_addr = sa;
2438 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2439 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002440 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002441 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002442 file, linenum, args[cur_arg], p);
2443 err_code |= ERR_ALERT | ERR_FATAL;
2444 goto out;
2445 }
2446 if (!done) {
2447 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2448 file, linenum, args[cur_arg], p);
2449 err_code |= ERR_ALERT | ERR_FATAL;
2450 goto out;
2451 }
2452 }
2453 cur_arg += 2;
2454 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002455 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002456 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002457 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2458 cur_arg += 2;
2459 }
2460 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2461 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002462 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002463 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002464 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002465 else {
2466 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2467 file, linenum, args[cur_arg]);
2468 err_code |= ERR_ALERT | ERR_FATAL;
2469 goto out;
2470 }
2471 cur_arg += 2;
2472 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002473 else if (!strcmp(args[cur_arg], "resolve-net")) {
2474 char *p, *e;
2475 unsigned char mask;
2476 struct dns_options *opt;
2477
2478 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
2479 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2480 file, linenum, args[cur_arg]);
2481 err_code |= ERR_ALERT | ERR_FATAL;
2482 goto out;
2483 }
2484
2485 opt = &newsrv->dns_opts;
2486
2487 /* Split arguments by comma, and convert it from ipv4 or ipv6
2488 * string network in in_addr or in6_addr.
2489 */
2490 p = args[cur_arg + 1];
2491 e = p;
2492 while (*p != '\0') {
2493 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002494 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002495 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2496 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2497 err_code |= ERR_ALERT | ERR_FATAL;
2498 goto out;
2499 }
2500 /* look for end or comma. */
2501 while (*e != ',' && *e != '\0')
2502 e++;
2503 if (*e == ',') {
2504 *e = '\0';
2505 e++;
2506 }
2507 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2508 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2509 /* Try to convert input string from ipv4 or ipv6 network. */
2510 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2511 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2512 &mask)) {
2513 /* Try to convert input string from ipv6 network. */
2514 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2515 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2516 } else {
2517 /* All network conversions fail, retrun error. */
2518 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2519 file, linenum, args[cur_arg], p);
2520 err_code |= ERR_ALERT | ERR_FATAL;
2521 goto out;
2522 }
2523 opt->pref_net_nb++;
2524 p = e;
2525 }
2526
2527 cur_arg += 2;
2528 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002529 else if (!strcmp(args[cur_arg], "rise")) {
2530 if (!*args[cur_arg + 1]) {
2531 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2532 file, linenum, args[cur_arg]);
2533 err_code |= ERR_ALERT | ERR_FATAL;
2534 goto out;
2535 }
2536
2537 newsrv->check.rise = atol(args[cur_arg + 1]);
2538 if (newsrv->check.rise <= 0) {
2539 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2540 file, linenum, args[cur_arg]);
2541 err_code |= ERR_ALERT | ERR_FATAL;
2542 goto out;
2543 }
2544
2545 if (newsrv->check.health)
2546 newsrv->check.health = newsrv->check.rise;
2547 cur_arg += 2;
2548 }
2549 else if (!strcmp(args[cur_arg], "fall")) {
2550 newsrv->check.fall = atol(args[cur_arg + 1]);
2551
2552 if (!*args[cur_arg + 1]) {
2553 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2554 file, linenum, args[cur_arg]);
2555 err_code |= ERR_ALERT | ERR_FATAL;
2556 goto out;
2557 }
2558
2559 if (newsrv->check.fall <= 0) {
2560 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2561 file, linenum, args[cur_arg]);
2562 err_code |= ERR_ALERT | ERR_FATAL;
2563 goto out;
2564 }
2565
2566 cur_arg += 2;
2567 }
2568 else if (!strcmp(args[cur_arg], "inter")) {
2569 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2570 if (err) {
2571 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2572 file, linenum, *err, newsrv->id);
2573 err_code |= ERR_ALERT | ERR_FATAL;
2574 goto out;
2575 }
2576 if (val <= 0) {
2577 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2578 file, linenum, val, args[cur_arg], newsrv->id);
2579 err_code |= ERR_ALERT | ERR_FATAL;
2580 goto out;
2581 }
2582 newsrv->check.inter = val;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002583 if (newsrv->srvrq)
2584 newsrv->srvrq->inter = val;
Willy Tarreau272adea2014-03-31 10:39:59 +02002585 cur_arg += 2;
2586 }
2587 else if (!strcmp(args[cur_arg], "fastinter")) {
2588 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2589 if (err) {
2590 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2591 file, linenum, *err, newsrv->id);
2592 err_code |= ERR_ALERT | ERR_FATAL;
2593 goto out;
2594 }
2595 if (val <= 0) {
2596 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2597 file, linenum, val, args[cur_arg], newsrv->id);
2598 err_code |= ERR_ALERT | ERR_FATAL;
2599 goto out;
2600 }
2601 newsrv->check.fastinter = val;
2602 cur_arg += 2;
2603 }
2604 else if (!strcmp(args[cur_arg], "downinter")) {
2605 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2606 if (err) {
2607 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2608 file, linenum, *err, newsrv->id);
2609 err_code |= ERR_ALERT | ERR_FATAL;
2610 goto out;
2611 }
2612 if (val <= 0) {
2613 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2614 file, linenum, val, args[cur_arg], newsrv->id);
2615 err_code |= ERR_ALERT | ERR_FATAL;
2616 goto out;
2617 }
2618 newsrv->check.downinter = val;
2619 cur_arg += 2;
2620 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002621 else if (!strcmp(args[cur_arg], "port")) {
2622 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002623 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002624 cur_arg += 2;
2625 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002626 else if (!strcmp(args[cur_arg], "weight")) {
2627 int w;
2628 w = atol(args[cur_arg + 1]);
2629 if (w < 0 || w > SRV_UWGHT_MAX) {
2630 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2631 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2632 err_code |= ERR_ALERT | ERR_FATAL;
2633 goto out;
2634 }
2635 newsrv->uweight = newsrv->iweight = w;
2636 cur_arg += 2;
2637 }
2638 else if (!strcmp(args[cur_arg], "minconn")) {
2639 newsrv->minconn = atol(args[cur_arg + 1]);
2640 cur_arg += 2;
2641 }
2642 else if (!strcmp(args[cur_arg], "maxconn")) {
2643 newsrv->maxconn = atol(args[cur_arg + 1]);
2644 cur_arg += 2;
2645 }
2646 else if (!strcmp(args[cur_arg], "maxqueue")) {
2647 newsrv->maxqueue = atol(args[cur_arg + 1]);
2648 cur_arg += 2;
2649 }
2650 else if (!strcmp(args[cur_arg], "slowstart")) {
2651 /* slowstart is stored in seconds */
2652 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2653 if (err) {
2654 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2655 file, linenum, *err, newsrv->id);
2656 err_code |= ERR_ALERT | ERR_FATAL;
2657 goto out;
2658 }
2659 newsrv->slowstart = (val + 999) / 1000;
2660 cur_arg += 2;
2661 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002662 else if (!strcmp(args[cur_arg], "on-error")) {
2663 if (!strcmp(args[cur_arg + 1], "fastinter"))
2664 newsrv->onerror = HANA_ONERR_FASTINTER;
2665 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2666 newsrv->onerror = HANA_ONERR_FAILCHK;
2667 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2668 newsrv->onerror = HANA_ONERR_SUDDTH;
2669 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2670 newsrv->onerror = HANA_ONERR_MARKDWN;
2671 else {
2672 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2673 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2674 file, linenum, args[cur_arg], args[cur_arg + 1]);
2675 err_code |= ERR_ALERT | ERR_FATAL;
2676 goto out;
2677 }
2678
2679 cur_arg += 2;
2680 }
2681 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2682 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2683 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2684 else {
2685 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2686 file, linenum, args[cur_arg], args[cur_arg + 1]);
2687 err_code |= ERR_ALERT | ERR_FATAL;
2688 goto out;
2689 }
2690
2691 cur_arg += 2;
2692 }
2693 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2694 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2695 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2696 else {
2697 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2698 file, linenum, args[cur_arg], args[cur_arg + 1]);
2699 err_code |= ERR_ALERT | ERR_FATAL;
2700 goto out;
2701 }
2702
2703 cur_arg += 2;
2704 }
2705 else if (!strcmp(args[cur_arg], "error-limit")) {
2706 if (!*args[cur_arg + 1]) {
2707 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2708 file, linenum, args[cur_arg]);
2709 err_code |= ERR_ALERT | ERR_FATAL;
2710 goto out;
2711 }
2712
2713 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2714
2715 if (newsrv->consecutive_errors_limit <= 0) {
2716 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2717 file, linenum, args[cur_arg]);
2718 err_code |= ERR_ALERT | ERR_FATAL;
2719 goto out;
2720 }
2721 cur_arg += 2;
2722 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002723 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002724 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2725 file, linenum, "usesrc", "source");
2726 err_code |= ERR_ALERT | ERR_FATAL;
2727 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002728 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002729 else {
2730 static int srv_dumped;
2731 struct srv_kw *kw;
2732 char *err;
2733
2734 kw = srv_find_kw(args[cur_arg]);
2735 if (kw) {
2736 char *err = NULL;
2737 int code;
2738
2739 if (!kw->parse) {
2740 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2741 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002742 if (kw->skip != -1)
2743 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002744 err_code |= ERR_ALERT | ERR_FATAL;
2745 goto out;
2746 }
2747
2748 if (defsrv && !kw->default_ok) {
2749 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2750 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002751 if (kw->skip != -1)
2752 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002753 err_code |= ERR_ALERT;
2754 continue;
2755 }
2756
2757 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2758 err_code |= code;
2759
2760 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002761 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002762 if (code & ERR_FATAL) {
2763 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002764 if (kw->skip != -1)
2765 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002766 goto out;
2767 }
2768 }
2769 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002770 if (kw->skip != -1)
2771 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002772 continue;
2773 }
2774
2775 err = NULL;
2776 if (!srv_dumped) {
2777 srv_dump_kws(&err);
2778 indent_msg(&err, 4);
2779 srv_dumped = 1;
2780 }
2781
2782 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2783 file, linenum, args[0], args[1], args[cur_arg],
2784 err ? " Registered keywords :" : "", err ? err : "");
2785 free(err);
2786
2787 err_code |= ERR_ALERT | ERR_FATAL;
2788 goto out;
2789 }
2790 }
2791
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002792 if (!defsrv)
2793 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2794 if (err_code & ERR_FATAL)
2795 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002796 if (srv_tmpl)
2797 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002798 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002799 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002800 return 0;
2801
2802 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002803 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002804 free(errmsg);
2805 return err_code;
2806}
2807
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002808/* Returns a pointer to the first server matching either id <id>.
2809 * NULL is returned if no match is found.
2810 * the lookup is performed in the backend <bk>
2811 */
2812struct server *server_find_by_id(struct proxy *bk, int id)
2813{
2814 struct eb32_node *eb32;
2815 struct server *curserver;
2816
2817 if (!bk || (id ==0))
2818 return NULL;
2819
2820 /* <bk> has no backend capabilities, so it can't have a server */
2821 if (!(bk->cap & PR_CAP_BE))
2822 return NULL;
2823
2824 curserver = NULL;
2825
2826 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2827 if (eb32)
2828 curserver = container_of(eb32, struct server, conf.id);
2829
2830 return curserver;
2831}
2832
2833/* Returns a pointer to the first server matching either name <name>, or id
2834 * if <name> starts with a '#'. NULL is returned if no match is found.
2835 * the lookup is performed in the backend <bk>
2836 */
2837struct server *server_find_by_name(struct proxy *bk, const char *name)
2838{
2839 struct server *curserver;
2840
2841 if (!bk || !name)
2842 return NULL;
2843
2844 /* <bk> has no backend capabilities, so it can't have a server */
2845 if (!(bk->cap & PR_CAP_BE))
2846 return NULL;
2847
2848 curserver = NULL;
2849 if (*name == '#') {
2850 curserver = server_find_by_id(bk, atoi(name + 1));
2851 if (curserver)
2852 return curserver;
2853 }
2854 else {
2855 curserver = bk->srv;
2856
2857 while (curserver && (strcmp(curserver->id, name) != 0))
2858 curserver = curserver->next;
2859
2860 if (curserver)
2861 return curserver;
2862 }
2863
2864 return NULL;
2865}
2866
2867struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2868{
2869 struct server *byname;
2870 struct server *byid;
2871
2872 if (!name && !id)
2873 return NULL;
2874
2875 if (diff)
2876 *diff = 0;
2877
2878 byname = byid = NULL;
2879
2880 if (name) {
2881 byname = server_find_by_name(bk, name);
2882 if (byname && (!id || byname->puid == id))
2883 return byname;
2884 }
2885
2886 /* remaining possibilities :
2887 * - name not set
2888 * - name set but not found
2889 * - name found but ID doesn't match
2890 */
2891 if (id) {
2892 byid = server_find_by_id(bk, id);
2893 if (byid) {
2894 if (byname) {
2895 /* use id only if forced by configuration */
2896 if (byid->flags & SRV_F_FORCED_ID) {
2897 if (diff)
2898 *diff |= 2;
2899 return byid;
2900 }
2901 else {
2902 if (diff)
2903 *diff |= 1;
2904 return byname;
2905 }
2906 }
2907
2908 /* remaining possibilities:
2909 * - name not set
2910 * - name set but not found
2911 */
2912 if (name && diff)
2913 *diff |= 2;
2914 return byid;
2915 }
2916
2917 /* id bot found */
2918 if (byname) {
2919 if (diff)
2920 *diff |= 1;
2921 return byname;
2922 }
2923 }
2924
2925 return NULL;
2926}
2927
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002928/* Update a server state using the parameters available in the params list */
2929static void srv_update_state(struct server *srv, int version, char **params)
2930{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002931 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002932 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002933
2934 /* fields since version 1
2935 * and common to all other upcoming versions
2936 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002937 enum srv_state srv_op_state;
2938 enum srv_admin srv_admin_state;
2939 unsigned srv_uweight, srv_iweight;
2940 unsigned long srv_last_time_change;
2941 short srv_check_status;
2942 enum chk_result srv_check_result;
2943 int srv_check_health;
2944 int srv_check_state, srv_agent_state;
2945 int bk_f_forced_id;
2946 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002947 int fqdn_set_by_cli;
2948 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002949 const char *port_str;
2950 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002951
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002952 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002953 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002954 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002955 switch (version) {
2956 case 1:
2957 /*
2958 * now we can proceed with server's state update:
2959 * srv_addr: params[0]
2960 * srv_op_state: params[1]
2961 * srv_admin_state: params[2]
2962 * srv_uweight: params[3]
2963 * srv_iweight: params[4]
2964 * srv_last_time_change: params[5]
2965 * srv_check_status: params[6]
2966 * srv_check_result: params[7]
2967 * srv_check_health: params[8]
2968 * srv_check_state: params[9]
2969 * srv_agent_state: params[10]
2970 * bk_f_forced_id: params[11]
2971 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002972 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002973 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002974 */
2975
2976 /* validating srv_op_state */
2977 p = NULL;
2978 errno = 0;
2979 srv_op_state = strtol(params[1], &p, 10);
2980 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2981 (srv_op_state != SRV_ST_STOPPED &&
2982 srv_op_state != SRV_ST_STARTING &&
2983 srv_op_state != SRV_ST_RUNNING &&
2984 srv_op_state != SRV_ST_STOPPING)) {
2985 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2986 }
2987
2988 /* validating srv_admin_state */
2989 p = NULL;
2990 errno = 0;
2991 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002992 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002993
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002994 /* inherited statuses will be recomputed later.
2995 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2996 */
2997 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002998
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002999 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
3000 (srv_admin_state != 0 &&
3001 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003002 srv_admin_state != SRV_ADMF_CMAINT &&
3003 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01003004 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01003005 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003006 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
3007 }
3008
3009 /* validating srv_uweight */
3010 p = NULL;
3011 errno = 0;
3012 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003013 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003014 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
3015
3016 /* validating srv_iweight */
3017 p = NULL;
3018 errno = 0;
3019 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003020 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003021 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
3022
3023 /* validating srv_last_time_change */
3024 p = NULL;
3025 errno = 0;
3026 srv_last_time_change = strtol(params[5], &p, 10);
3027 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
3028 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
3029
3030 /* validating srv_check_status */
3031 p = NULL;
3032 errno = 0;
3033 srv_check_status = strtol(params[6], &p, 10);
3034 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3035 (srv_check_status >= HCHK_STATUS_SIZE))
3036 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3037
3038 /* validating srv_check_result */
3039 p = NULL;
3040 errno = 0;
3041 srv_check_result = strtol(params[7], &p, 10);
3042 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3043 (srv_check_result != CHK_RES_UNKNOWN &&
3044 srv_check_result != CHK_RES_NEUTRAL &&
3045 srv_check_result != CHK_RES_FAILED &&
3046 srv_check_result != CHK_RES_PASSED &&
3047 srv_check_result != CHK_RES_CONDPASS)) {
3048 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3049 }
3050
3051 /* validating srv_check_health */
3052 p = NULL;
3053 errno = 0;
3054 srv_check_health = strtol(params[8], &p, 10);
3055 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3056 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3057
3058 /* validating srv_check_state */
3059 p = NULL;
3060 errno = 0;
3061 srv_check_state = strtol(params[9], &p, 10);
3062 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3063 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3064 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3065
3066 /* validating srv_agent_state */
3067 p = NULL;
3068 errno = 0;
3069 srv_agent_state = strtol(params[10], &p, 10);
3070 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3071 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3072 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3073
3074 /* validating bk_f_forced_id */
3075 p = NULL;
3076 errno = 0;
3077 bk_f_forced_id = strtol(params[11], &p, 10);
3078 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3079 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3080
3081 /* validating srv_f_forced_id */
3082 p = NULL;
3083 errno = 0;
3084 srv_f_forced_id = strtol(params[12], &p, 10);
3085 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3086 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3087
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003088 /* validating srv_fqdn */
3089 fqdn = params[13];
3090 if (fqdn && *fqdn == '-')
3091 fqdn = NULL;
3092 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3093 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3094 fqdn = NULL;
3095 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003096
Frédéric Lécaille31694712017-08-01 08:47:19 +02003097 port_str = params[14];
3098 if (port_str) {
3099 port = strl2uic(port_str, strlen(port_str));
3100 if (port > USHRT_MAX) {
3101 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3102 port_str = NULL;
3103 }
3104 }
3105
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003106 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02003107 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003108 goto out;
3109
3110 /* recover operational state and apply it to this server
3111 * and all servers tracking this one */
3112 switch (srv_op_state) {
3113 case SRV_ST_STOPPED:
3114 srv->check.health = 0;
3115 srv_set_stopped(srv, "changed from server-state after a reload");
3116 break;
3117 case SRV_ST_STARTING:
3118 srv->state = srv_op_state;
3119 break;
3120 case SRV_ST_STOPPING:
3121 srv->check.health = srv->check.rise + srv->check.fall - 1;
3122 srv_set_stopping(srv, "changed from server-state after a reload");
3123 break;
3124 case SRV_ST_RUNNING:
3125 srv->check.health = srv->check.rise + srv->check.fall - 1;
3126 srv_set_running(srv, "");
3127 break;
3128 }
3129
3130 /* When applying server state, the following rules apply:
3131 * - in case of a configuration change, we apply the setting from the new
3132 * configuration, regardless of old running state
3133 * - if no configuration change, we apply old running state only if old running
3134 * state is different from new configuration state
3135 */
3136 /* configuration has changed */
3137 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
3138 if (srv->admin & SRV_ADMF_CMAINT)
3139 srv_adm_set_maint(srv);
3140 else
3141 srv_adm_set_ready(srv);
3142 }
3143 /* configuration is the same, let's compate old running state and new conf state */
3144 else {
3145 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
3146 srv_adm_set_maint(srv);
3147 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
3148 srv_adm_set_ready(srv);
3149 }
3150 /* apply drain mode if server is currently enabled */
3151 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
3152 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003153 * (srv->iweight is the weight set up in configuration).
3154 * There are two possible reasons for FDRAIN to have been present :
3155 * - previous config weight was zero
3156 * - "set server b/s drain" was sent to the CLI
3157 *
3158 * In the first case, we simply want to drop this drain state
3159 * if the new weight is not zero anymore, meaning the administrator
3160 * has intentionally turned the weight back to a positive value to
3161 * enable the server again after an operation. In the second case,
3162 * the drain state was forced on the CLI regardless of the config's
3163 * weight so we don't want a change to the config weight to lose this
3164 * status. What this means is :
3165 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3166 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003167 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003168 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003169 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003170 }
3171
3172 srv->last_change = date.tv_sec - srv_last_time_change;
3173 srv->check.status = srv_check_status;
3174 srv->check.result = srv_check_result;
3175 srv->check.health = srv_check_health;
3176
3177 /* Only case we want to apply is removing ENABLED flag which could have been
3178 * done by the "disable health" command over the stats socket
3179 */
3180 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3181 (srv_check_state & CHK_ST_CONFIGURED) &&
3182 !(srv_check_state & CHK_ST_ENABLED))
3183 srv->check.state &= ~CHK_ST_ENABLED;
3184
3185 /* Only case we want to apply is removing ENABLED flag which could have been
3186 * done by the "disable agent" command over the stats socket
3187 */
3188 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3189 (srv_agent_state & CHK_ST_CONFIGURED) &&
3190 !(srv_agent_state & CHK_ST_ENABLED))
3191 srv->agent.state &= ~CHK_ST_ENABLED;
3192
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003193 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3194 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003195 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003196 * It means that a configuration file change has precedence over a unix socket change
3197 * for server's weight
3198 *
3199 * by default, HAProxy applies the following weight when parsing the configuration
3200 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003201 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003202 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003203 srv->uweight = srv_uweight;
3204 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003205 server_recalc_eweight(srv);
3206
Willy Tarreaue5a60682016-11-09 14:54:53 +01003207 /* load server IP address */
3208 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003209
3210 if (fqdn && srv->hostname) {
3211 if (!strcmp(srv->hostname, fqdn)) {
3212 /* Here we reset the 'set from stats socket FQDN' flag
3213 * to support such transitions:
3214 * Let's say initial FQDN value is foo1 (in configuration file).
3215 * - FQDN changed from stats socket, from foo1 to foo2 value,
3216 * - FQDN changed again from file configuration (with the same previous value
3217 set from stats socket, from foo1 to foo2 value),
3218 * - reload for any other reason than a FQDN modification,
3219 * the configuration file FQDN matches the fqdn server state file value.
3220 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
3221 * any futher FQDN modification.
3222 */
3223 srv->admin &= ~SRV_ADMF_HMAINT;
3224 }
3225 else {
3226 /* If the FDQN has been changed from stats socket,
3227 * apply fqdn state file value (which is the value set
3228 * from stats socket).
3229 */
3230 if (fqdn_set_by_cli) {
3231 srv_set_fqdn(srv, fqdn);
3232 srv->admin |= SRV_ADMF_HMAINT;
3233 }
3234 }
3235 }
3236
Frédéric Lécaille31694712017-08-01 08:47:19 +02003237 if (port_str)
3238 srv->svc_port = port;
3239
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003240 break;
3241 default:
3242 chunk_appendf(msg, ", version '%d' not supported", version);
3243 }
3244
3245 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003246 if (msg->len) {
3247 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02003248 Warning("server-state application failed for server '%s/%s'%s",
3249 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003250 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003251}
3252
3253/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3254 * For each proxy, it does the following:
3255 * - opens its server state file (either one or local one)
3256 * - read whole file, line by line
3257 * - analyse each line to check if it matches our current backend:
3258 * - backend name matches
3259 * - backend id matches if id is forced and name doesn't match
3260 * - if the server pointed by the line is found, then state is applied
3261 *
3262 * If the running backend uuid or id differs from the state file, then HAProxy reports
3263 * a warning.
3264 */
3265void apply_server_state(void)
3266{
3267 char *cur, *end;
3268 char mybuf[SRV_STATE_LINE_MAXLEN];
3269 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003270 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3271 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003272 int arg, srv_arg, version, diff;
3273 FILE *f;
3274 char *filepath;
3275 char globalfilepath[MAXPATHLEN + 1];
3276 char localfilepath[MAXPATHLEN + 1];
3277 int len, fileopenerr, globalfilepathlen, localfilepathlen;
3278 extern struct proxy *proxy;
3279 struct proxy *curproxy, *bk;
3280 struct server *srv;
3281
3282 globalfilepathlen = 0;
3283 /* create the globalfilepath variable */
3284 if (global.server_state_file) {
3285 /* absolute path or no base directory provided */
3286 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3287 len = strlen(global.server_state_file);
3288 if (len > MAXPATHLEN) {
3289 globalfilepathlen = 0;
3290 goto globalfileerror;
3291 }
3292 memcpy(globalfilepath, global.server_state_file, len);
3293 globalfilepath[len] = '\0';
3294 globalfilepathlen = len;
3295 }
3296 else if (global.server_state_base) {
3297 len = strlen(global.server_state_base);
3298 globalfilepathlen += len;
3299
3300 if (globalfilepathlen > MAXPATHLEN) {
3301 globalfilepathlen = 0;
3302 goto globalfileerror;
3303 }
3304 strncpy(globalfilepath, global.server_state_base, len);
3305 globalfilepath[globalfilepathlen] = 0;
3306
3307 /* append a slash if needed */
3308 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3309 if (globalfilepathlen + 1 > MAXPATHLEN) {
3310 globalfilepathlen = 0;
3311 goto globalfileerror;
3312 }
3313 globalfilepath[globalfilepathlen++] = '/';
3314 }
3315
3316 len = strlen(global.server_state_file);
3317 if (globalfilepathlen + len > MAXPATHLEN) {
3318 globalfilepathlen = 0;
3319 goto globalfileerror;
3320 }
3321 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3322 globalfilepathlen += len;
3323 globalfilepath[globalfilepathlen++] = 0;
3324 }
3325 }
3326 globalfileerror:
3327 if (globalfilepathlen == 0)
3328 globalfilepath[0] = '\0';
3329
3330 /* read servers state from local file */
3331 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3332 /* servers are only in backends */
3333 if (!(curproxy->cap & PR_CAP_BE))
3334 continue;
3335 fileopenerr = 0;
3336 filepath = NULL;
3337
3338 /* search server state file path and name */
3339 switch (curproxy->load_server_state_from_file) {
3340 /* read servers state from global file */
3341 case PR_SRV_STATE_FILE_GLOBAL:
3342 /* there was an error while generating global server state file path */
3343 if (globalfilepathlen == 0)
3344 continue;
3345 filepath = globalfilepath;
3346 fileopenerr = 1;
3347 break;
3348 /* this backend has its own file */
3349 case PR_SRV_STATE_FILE_LOCAL:
3350 localfilepathlen = 0;
3351 localfilepath[0] = '\0';
3352 len = 0;
3353 /* create the localfilepath variable */
3354 /* absolute path or no base directory provided */
3355 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3356 len = strlen(curproxy->server_state_file_name);
3357 if (len > MAXPATHLEN) {
3358 localfilepathlen = 0;
3359 goto localfileerror;
3360 }
3361 memcpy(localfilepath, curproxy->server_state_file_name, len);
3362 localfilepath[len] = '\0';
3363 localfilepathlen = len;
3364 }
3365 else if (global.server_state_base) {
3366 len = strlen(global.server_state_base);
3367 localfilepathlen += len;
3368
3369 if (localfilepathlen > MAXPATHLEN) {
3370 localfilepathlen = 0;
3371 goto localfileerror;
3372 }
3373 strncpy(localfilepath, global.server_state_base, len);
3374 localfilepath[localfilepathlen] = 0;
3375
3376 /* append a slash if needed */
3377 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3378 if (localfilepathlen + 1 > MAXPATHLEN) {
3379 localfilepathlen = 0;
3380 goto localfileerror;
3381 }
3382 localfilepath[localfilepathlen++] = '/';
3383 }
3384
3385 len = strlen(curproxy->server_state_file_name);
3386 if (localfilepathlen + len > MAXPATHLEN) {
3387 localfilepathlen = 0;
3388 goto localfileerror;
3389 }
3390 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3391 localfilepathlen += len;
3392 localfilepath[localfilepathlen++] = 0;
3393 }
3394 filepath = localfilepath;
3395 localfileerror:
3396 if (localfilepathlen == 0)
3397 localfilepath[0] = '\0';
3398
3399 break;
3400 case PR_SRV_STATE_FILE_NONE:
3401 default:
3402 continue;
3403 }
3404
3405 /* preload global state file */
3406 errno = 0;
3407 f = fopen(filepath, "r");
3408 if (errno && fileopenerr)
3409 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3410 if (!f)
3411 continue;
3412
3413 mybuf[0] = '\0';
3414 mybuflen = 0;
3415 version = 0;
3416
3417 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003418 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3419 Warning("Can't read first line of the server state file '%s'\n", filepath);
3420 goto fileclose;
3421 }
3422
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003423 cur = mybuf;
3424 version = atoi(cur);
3425 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3426 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003427 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003428
3429 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3430 int bk_f_forced_id = 0;
3431 int check_id = 0;
3432 int check_name = 0;
3433
3434 mybuflen = strlen(mybuf);
3435 cur = mybuf;
3436 end = cur + mybuflen;
3437
3438 bk = NULL;
3439 srv = NULL;
3440
3441 /* we need at least one character */
3442 if (mybuflen == 0)
3443 continue;
3444
3445 /* ignore blank characters at the beginning of the line */
3446 while (isspace(*cur))
3447 ++cur;
3448
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003449 /* Ignore empty or commented lines */
3450 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003451 continue;
3452
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003453 /* truncated lines */
3454 if (mybuf[mybuflen - 1] != '\n') {
3455 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003456 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003457 }
3458
3459 /* Removes trailing '\n' */
3460 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003461
3462 /* we're now ready to move the line into *srv_params[] */
3463 params[0] = cur;
3464 arg = 1;
3465 srv_arg = 0;
3466 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3467 if (isspace(*cur)) {
3468 *cur = '\0';
3469 ++cur;
3470 while (isspace(*cur))
3471 ++cur;
3472 switch (version) {
3473 case 1:
3474 /*
3475 * srv_addr: params[4] => srv_params[0]
3476 * srv_op_state: params[5] => srv_params[1]
3477 * srv_admin_state: params[6] => srv_params[2]
3478 * srv_uweight: params[7] => srv_params[3]
3479 * srv_iweight: params[8] => srv_params[4]
3480 * srv_last_time_change: params[9] => srv_params[5]
3481 * srv_check_status: params[10] => srv_params[6]
3482 * srv_check_result: params[11] => srv_params[7]
3483 * srv_check_health: params[12] => srv_params[8]
3484 * srv_check_state: params[13] => srv_params[9]
3485 * srv_agent_state: params[14] => srv_params[10]
3486 * bk_f_forced_id: params[15] => srv_params[11]
3487 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003488 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003489 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003490 */
3491 if (arg >= 4) {
3492 srv_params[srv_arg] = cur;
3493 ++srv_arg;
3494 }
3495 break;
3496 }
3497
3498 params[arg] = cur;
3499 ++arg;
3500 }
3501 else {
3502 ++cur;
3503 }
3504 }
3505
3506 /* if line is incomplete line, then ignore it.
3507 * otherwise, update useful flags */
3508 switch (version) {
3509 case 1:
3510 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3511 continue;
3512 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3513 check_id = (atoi(params[0]) == curproxy->uuid);
3514 check_name = (strcmp(curproxy->id, params[1]) == 0);
3515 break;
3516 }
3517
3518 diff = 0;
3519 bk = curproxy;
3520
3521 /* if backend can't be found, let's continue */
3522 if (!check_id && !check_name)
3523 continue;
3524 else if (!check_id && check_name) {
3525 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3526 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3527 }
3528 else if (check_id && !check_name) {
3529 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3530 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3531 /* if name doesn't match, we still want to update curproxy if the backend id
3532 * was forced in previous the previous configuration */
3533 if (!bk_f_forced_id)
3534 continue;
3535 }
3536
3537 /* look for the server by its id: param[2] */
3538 /* else look for the server by its name: param[3] */
3539 diff = 0;
3540 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3541
3542 if (!srv) {
3543 /* if no server found, then warning and continue with next line */
3544 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3545 params[3], params[2], params[0], params[1]);
3546 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3547 params[3], params[2], params[0], params[1]);
3548 continue;
3549 }
3550 else if (diff & PR_FBM_MISMATCH_ID) {
3551 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);
3552 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 +02003553 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003554 }
3555 else if (diff & PR_FBM_MISMATCH_NAME) {
3556 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);
3557 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 +02003558 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003559 }
3560
3561 /* now we can proceed with server's state update */
3562 srv_update_state(srv, version, srv_params);
3563 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003564fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003565 fclose(f);
3566 }
3567}
3568
Simon Horman7d09b9a2013-02-12 10:45:51 +09003569/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003570 * update a server's current IP address.
3571 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3572 * ip is in network format.
3573 * updater is a string which contains an information about the requester of the update.
3574 * updater is used if not NULL.
3575 *
3576 * A log line and a stderr warning message is generated based on server's backend options.
3577 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003578int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003579{
3580 /* generates a log line and a warning on stderr */
3581 if (1) {
3582 /* book enough space for both IPv4 and IPv6 */
3583 char oldip[INET6_ADDRSTRLEN];
3584 char newip[INET6_ADDRSTRLEN];
3585
3586 memset(oldip, '\0', INET6_ADDRSTRLEN);
3587 memset(newip, '\0', INET6_ADDRSTRLEN);
3588
3589 /* copy old IP address in a string */
3590 switch (s->addr.ss_family) {
3591 case AF_INET:
3592 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3593 break;
3594 case AF_INET6:
3595 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3596 break;
3597 };
3598
3599 /* copy new IP address in a string */
3600 switch (ip_sin_family) {
3601 case AF_INET:
3602 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3603 break;
3604 case AF_INET6:
3605 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3606 break;
3607 };
3608
3609 /* save log line into a buffer */
3610 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3611 s->proxy->id, s->id, oldip, newip, updater);
3612
3613 /* write the buffer on stderr */
3614 Warning("%s.\n", trash.str);
3615
3616 /* send a log */
3617 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3618 }
3619
3620 /* save the new IP family */
3621 s->addr.ss_family = ip_sin_family;
3622 /* save the new IP address */
3623 switch (ip_sin_family) {
3624 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003625 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003626 break;
3627 case AF_INET6:
3628 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3629 break;
3630 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003631 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003632
3633 return 0;
3634}
3635
3636/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003637 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3638 *
3639 * Caller can pass its name through <updater> to get it integrated in the response message
3640 * returned by the function.
3641 *
3642 * The function first does the following, in that order:
3643 * - validates the new addr and/or port
3644 * - checks if an update is required (new IP or port is different than current ones)
3645 * - checks the update is allowed:
3646 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3647 * - allow all changes if no CHECKS are configured
3648 * - if CHECK is configured:
3649 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3650 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3651 * conditions are met
3652 */
3653const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3654{
3655 struct sockaddr_storage sa;
3656 int ret, port_change_required;
3657 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003658 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003659 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003660 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003661
3662 msg = get_trash_chunk();
3663 chunk_reset(msg);
3664
3665 if (addr) {
3666 memset(&sa, 0, sizeof(struct sockaddr_storage));
3667 if (str2ip2(addr, &sa, 0) == NULL) {
3668 chunk_printf(msg, "Invalid addr '%s'", addr);
3669 goto out;
3670 }
3671
3672 /* changes are allowed on AF_INET* families only */
3673 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3674 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3675 goto out;
3676 }
3677
3678 /* collecting data currently setup */
3679 memset(current_addr, '\0', sizeof(current_addr));
3680 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3681 /* changes are allowed on AF_INET* families only */
3682 if ((ret != AF_INET) && (ret != AF_INET6)) {
3683 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3684 goto out;
3685 }
3686
3687 /* applying ADDR changes if required and allowed
3688 * ipcmp returns 0 when both ADDR are the same
3689 */
3690 if (ipcmp(&s->addr, &sa) == 0) {
3691 chunk_appendf(msg, "no need to change the addr");
3692 goto port;
3693 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003694 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003695 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003696
3697 /* we also need to update check's ADDR only if it uses the server's one */
3698 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003699 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003700 }
3701
3702 /* we also need to update agent ADDR only if it use the server's one */
3703 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003704 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003705 }
3706
3707 /* update report for caller */
3708 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3709 }
3710
3711 port:
3712 if (port) {
3713 char sign = '\0';
3714 char *endptr;
3715
3716 if (addr)
3717 chunk_appendf(msg, ", ");
3718
3719 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003720 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003721
3722 /* check if PORT change is required */
3723 port_change_required = 0;
3724
3725 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003726 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003727 new_port = strtol(port, &endptr, 10);
3728 if ((errno != 0) || (port == endptr)) {
3729 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3730 goto out;
3731 }
3732
3733 /* check if caller triggers a port mapped or offset */
3734 if (sign == '-' || (sign == '+')) {
3735 /* check if server currently uses port map */
3736 if (!(s->flags & SRV_F_MAPPORTS)) {
3737 /* switch from fixed port to port map mandatorily triggers
3738 * a port change */
3739 port_change_required = 1;
3740 /* check is configured
3741 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3742 * prevent PORT change if check doesn't have it's dedicated port while switching
3743 * to port mapping */
3744 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3745 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.");
3746 goto out;
3747 }
3748 }
3749 /* we're already using port maps */
3750 else {
3751 port_change_required = current_port != new_port;
3752 }
3753 }
3754 /* fixed port */
3755 else {
3756 port_change_required = current_port != new_port;
3757 }
3758
3759 /* applying PORT changes if required and update response message */
3760 if (port_change_required) {
3761 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003762 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003763 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003764
3765 /* prepare message */
3766 chunk_appendf(msg, "port changed from '");
3767 if (s->flags & SRV_F_MAPPORTS)
3768 chunk_appendf(msg, "+");
3769 chunk_appendf(msg, "%d' to '", current_port);
3770
3771 if (sign == '-') {
3772 s->flags |= SRV_F_MAPPORTS;
3773 chunk_appendf(msg, "%c", sign);
3774 /* just use for result output */
3775 new_port = -new_port;
3776 }
3777 else if (sign == '+') {
3778 s->flags |= SRV_F_MAPPORTS;
3779 chunk_appendf(msg, "%c", sign);
3780 }
3781 else {
3782 s->flags &= ~SRV_F_MAPPORTS;
3783 }
3784
3785 chunk_appendf(msg, "%d'", new_port);
3786
3787 /* we also need to update health checks port only if it uses server's realport */
3788 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3789 s->check.port = new_port;
3790 }
3791 }
3792 else {
3793 chunk_appendf(msg, "no need to change the port");
3794 }
3795 }
3796
3797out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003798 if (changed)
3799 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003800 if (updater)
3801 chunk_appendf(msg, " by '%s'", updater);
3802 chunk_appendf(msg, "\n");
3803 return msg->str;
3804}
3805
3806
3807/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003808 * update server status based on result of name resolution
3809 * returns:
3810 * 0 if server status is updated
3811 * 1 if server status has not changed
3812 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003813int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003814{
3815 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann42746372017-05-03 12:12:02 +02003816 struct dns_resolvers *resolvers = s->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003817
3818 switch (resolution->status) {
3819 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003820 /* status when HAProxy has just (re)started.
3821 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003822 break;
3823
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003824 case RSLV_STATUS_VALID:
3825 /*
3826 * resume health checks
3827 * server will be turned back on if health check is safe
3828 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003829 if (has_no_ip) {
3830 if (s->admin & SRV_ADMF_RMAINT)
3831 return 1;
3832 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3833 "No IP for server ");
3834 return (0);
3835 }
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003836 if (!(s->admin & SRV_ADMF_RMAINT))
3837 return 1;
3838 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3839 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3840 s->proxy->id, s->id);
3841
3842 Warning("%s.\n", trash.str);
3843 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3844 return 0;
3845
3846 case RSLV_STATUS_NX:
3847 /* stop server if resolution is NX for a long enough period */
3848 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
3849 if (s->admin & SRV_ADMF_RMAINT)
3850 return 1;
3851 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3852 return 0;
3853 }
3854 break;
3855
3856 case RSLV_STATUS_TIMEOUT:
3857 /* stop server if resolution is TIMEOUT for a long enough period */
3858 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
3859 if (s->admin & SRV_ADMF_RMAINT)
3860 return 1;
3861 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3862 return 0;
3863 }
3864 break;
3865
3866 case RSLV_STATUS_REFUSED:
3867 /* stop server if resolution is REFUSED for a long enough period */
3868 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
3869 if (s->admin & SRV_ADMF_RMAINT)
3870 return 1;
3871 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3872 return 0;
3873 }
3874 break;
3875
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003876 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003877 /* stop server if resolution is in unmatched error for a long enough period */
3878 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
3879 if (s->admin & SRV_ADMF_RMAINT)
3880 return 1;
3881 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3882 return 0;
3883 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003884 break;
3885 }
3886
3887 return 1;
3888}
3889
3890/*
3891 * Server Name Resolution valid response callback
3892 * It expects:
3893 * - <nameserver>: the name server which answered the valid response
3894 * - <response>: buffer containing a valid DNS response
3895 * - <response_len>: size of <response>
3896 * It performs the following actions:
3897 * - ignore response if current ip found and server family not met
3898 * - update with first new ip found if family is met and current IP is not found
3899 * returns:
3900 * 0 on error
3901 * 1 when no error or safe ignore
3902 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003903int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003904{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003905 struct server *s = NULL;
3906 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003907 void *serverip, *firstip;
3908 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003909 int ret;
3910 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003911 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003912
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003913 s = objt_server(requester->requester);
3914 if (!s)
3915 return 1;
3916
3917 resolution = s->resolution;
3918
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003919 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003920 firstip = NULL; /* pointer to the first valid response found */
3921 /* it will be used as the new IP if a change is required */
3922 firstip_sin_family = AF_UNSPEC;
3923 serverip = NULL; /* current server IP address */
3924
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003925 /* initializing server IP pointer */
3926 server_sin_family = s->addr.ss_family;
3927 switch (server_sin_family) {
3928 case AF_INET:
3929 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3930 break;
3931
3932 case AF_INET6:
3933 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3934 break;
3935
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003936 case AF_UNSPEC:
3937 break;
3938
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003939 default:
3940 goto invalid;
3941 }
3942
Baptiste Assmann729c9012017-05-22 15:13:10 +02003943 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003944 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003945 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003946
3947 switch (ret) {
3948 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003949 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003950
3951 case DNS_UPD_SRVIP_NOT_FOUND:
3952 goto save_ip;
3953
3954 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003955 goto invalid;
3956
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003957 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003958 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003959 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003960
Baptiste Assmannfad03182015-10-28 02:03:32 +01003961 case DNS_UPD_NAME_ERROR:
3962 /* if this is not the last expected response, we ignore it */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003963 if (nameserver && (resolution->nb_responses < nameserver->resolvers->count_nameservers))
Baptiste Assmannfad03182015-10-28 02:03:32 +01003964 return 0;
3965 /* update resolution status to OTHER error type */
3966 if (resolution->status != RSLV_STATUS_OTHER) {
3967 resolution->status = RSLV_STATUS_OTHER;
3968 resolution->last_status_change = now_ms;
3969 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003970 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003971
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003972 default:
3973 goto invalid;
3974
3975 }
3976
3977 save_ip:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003978 if (nameserver)
3979 nameserver->counters.update += 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003980
3981 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003982 if (nameserver)
3983 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3984 else
3985 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003986 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3987
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003988 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003989 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003990 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003991
3992 invalid:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003993 if (nameserver)
3994 nameserver->counters.invalid += 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003995 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003996 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003997
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003998 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003999 return 0;
4000}
4001
4002/*
4003 * Server Name Resolution error management callback
4004 * returns:
4005 * 0 on error
4006 * 1 when no error or safe ignore
4007 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004008int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004009{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004010 struct server *s = NULL;
4011 struct dns_resolution *resolution = NULL;
4012 struct dns_resolvers *resolvers = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004013
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004014 s = objt_server(requester->requester);
4015 if (!s)
4016 return 1;
4017
4018 resolution = s->resolution;
Baptiste Assmann42746372017-05-03 12:12:02 +02004019 resolvers = s->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004020
4021 /* can be ignored if this is not the last response */
4022 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
4023 return 1;
4024 }
4025
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004026 snr_update_srv_status(s, 0);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004027 return 1;
4028}
4029
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004030/*
4031 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004032 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004033 * It returns a pointer to the first server found or NULL if <ip> is not yet
4034 * assigned.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004035 */
4036struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4037{
4038 struct server *tmpsrv;
4039 struct proxy *be;
4040
4041 if (!srv)
4042 return NULL;
4043
4044 be = srv->proxy;
4045 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
4046 /* We want to compare the IP in the record with the IP of the servers in the
4047 * same backend, only if:
4048 * * DNS resolution is enabled on the server
4049 * * the hostname used for the resolution by our server is the same than the
4050 * one used for the server found in the backend
4051 * * the server found in the backend is not our current server
4052 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004053 if ((tmpsrv->hostname_dn == NULL) ||
4054 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4055 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004056 (srv->puid == tmpsrv->puid))
4057 continue;
4058
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004059 /* If the server has been taken down, don't consider it */
4060 if (tmpsrv->admin & SRV_ADMF_RMAINT)
4061 continue;
4062
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004063 /* At this point, we have 2 different servers using the same DNS hostname
4064 * for their respective resolution.
4065 */
4066 if (*ip_family == tmpsrv->addr.ss_family &&
4067 ((tmpsrv->addr.ss_family == AF_INET &&
4068 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4069 (tmpsrv->addr.ss_family == AF_INET6 &&
4070 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
4071 return tmpsrv;
4072 }
4073 }
4074
4075 return NULL;
4076}
4077
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004078/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4079 * resolver. This is suited for initial address configuration. Returns 0 on
4080 * success otherwise a non-zero error code. In case of error, *err_code, if
4081 * not NULL, is filled up.
4082 */
4083int srv_set_addr_via_libc(struct server *srv, int *err_code)
4084{
4085 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004086 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004087 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004088 return 1;
4089 }
4090 return 0;
4091}
4092
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004093/* Set the server's FDQN (->hostname) from <hostname>.
4094 * Returns -1 if failed, 0 if not.
4095 */
4096int srv_set_fqdn(struct server *srv, const char *hostname)
4097{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004098 struct dns_resolution *resolution;
4099 int hostname_dn_len;
Olivier Houchard8da5f982017-08-04 18:35:36 +02004100 int did_set_reso = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004101
4102 /* run time DNS resolution was not active for this server
4103 * and we can't enable it at run time for now.
4104 */
4105 if (!srv->dns_requester)
4106 return -1;
4107
4108 chunk_reset(&trash);
4109
4110 /* check if hostname is really a hostname and if we have enough
4111 * room to save it in its domain name format
4112 */
4113 hostname_dn_len = dns_str_to_dn_label_len(hostname);
4114 if (hostname_dn_len == -1 || hostname_dn_len + 1 > trash.size)
4115 return -1;
4116
4117 if (!dns_str_to_dn_label(hostname,
4118 trash.str,
4119 hostname_dn_len + 1))
4120 return -1;
4121
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004122
Olivier Houchard8da5f982017-08-04 18:35:36 +02004123 if (srv->resolution->hostname_dn) {
4124 /* get a resolution from the curr or wait queues, or a brand new one from the pool */
4125 resolution = dns_resolution_list_get(srv->resolvers, trash.str, srv->dns_requester->prefered_query_type);
4126 if (!resolution)
4127 return -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004128
Olivier Houchard8da5f982017-08-04 18:35:36 +02004129 /* in this case, the new hostanme is the same than the old one */
4130 if (srv->resolution == resolution && srv->hostname)
4131 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004132
Olivier Houchard8da5f982017-08-04 18:35:36 +02004133 /* first, we need to unlink our server from its current resolution */
4134 srv_free_from_resolution(srv);
4135 } else {
Baptiste Assmann6fb81922017-08-14 16:35:45 +02004136 /* this server's fqdn has been set by a SRV record */
4137 resolution = dns_resolution_list_get(srv->resolvers, trash.str, srv->dns_requester->prefered_query_type);
4138 srv_free_from_resolution(srv);
4139 srv->resolution = resolution;
4140 if (resolution->hostname_dn == NULL) {
4141 resolution->last_resolution = now_ms;
4142 did_set_reso = 1;
4143 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02004144 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004145
4146 /* now we update server's parameters */
4147 free(srv->hostname);
4148 free(srv->hostname_dn);
4149 srv->hostname = strdup(hostname);
4150 srv->hostname_dn = strdup(trash.str);
4151 srv->hostname_dn_len = hostname_dn_len;
4152 if (!srv->hostname || !srv->hostname_dn)
4153 return -1;
Olivier Houchard8da5f982017-08-04 18:35:36 +02004154 if (did_set_reso) {
4155 resolution->query_type = srv->dns_requester->prefered_query_type;
4156 resolution->hostname_dn = srv->hostname_dn;
4157 resolution->hostname_dn_len = hostname_dn_len;
4158 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004159
4160 /* then we can link srv to its new resolution */
4161 dns_link_resolution(srv, OBJ_TYPE_SERVER, resolution);
4162
4163 return 0;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004164}
4165
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004166/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4167 * from the state file. This is suited for initial address configuration.
4168 * Returns 0 on success otherwise a non-zero error code. In case of error,
4169 * *err_code, if not NULL, is filled up.
4170 */
4171static int srv_apply_lastaddr(struct server *srv, int *err_code)
4172{
4173 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4174 if (err_code)
4175 *err_code |= ERR_WARN;
4176 return 1;
4177 }
4178 return 0;
4179}
4180
Willy Tarreau25e51522016-11-04 15:10:17 +01004181/* returns 0 if no error, otherwise a combination of ERR_* flags */
4182static int srv_iterate_initaddr(struct server *srv)
4183{
4184 int return_code = 0;
4185 int err_code;
4186 unsigned int methods;
4187
4188 methods = srv->init_addr_methods;
4189 if (!methods) { // default to "last,libc"
4190 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4191 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4192 }
4193
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004194 /* "-dr" : always append "none" so that server addresses resolution
4195 * failures are silently ignored, this is convenient to validate some
4196 * configs out of their environment.
4197 */
4198 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4199 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4200
Willy Tarreau25e51522016-11-04 15:10:17 +01004201 while (methods) {
4202 err_code = 0;
4203 switch (srv_get_next_initaddr(&methods)) {
4204 case SRV_IADDR_LAST:
4205 if (!srv->lastaddr)
4206 continue;
4207 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004208 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004209 return_code |= err_code;
4210 break;
4211
4212 case SRV_IADDR_LIBC:
4213 if (!srv->hostname)
4214 continue;
4215 if (srv_set_addr_via_libc(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
Willy Tarreau37ebe122016-11-04 15:17:58 +01004220 case SRV_IADDR_NONE:
4221 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004222 if (return_code) {
4223 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4224 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4225 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004226 return return_code;
4227
Willy Tarreau4310d362016-11-02 15:05:56 +01004228 case SRV_IADDR_IP:
4229 ipcpy(&srv->init_addr, &srv->addr);
4230 if (return_code) {
4231 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4232 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4233 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004234 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004235
Willy Tarreau25e51522016-11-04 15:10:17 +01004236 default: /* unhandled method */
4237 break;
4238 }
4239 }
4240
4241 if (!return_code) {
4242 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
4243 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4244 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004245 else {
4246 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
4247 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4248 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004249
4250 return_code |= ERR_ALERT | ERR_FATAL;
4251 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004252out:
4253 srv_set_dyncookie(srv);
4254 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004255}
4256
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004257/*
4258 * This function parses all backends and all servers within each backend
4259 * and performs servers' addr resolution based on information provided by:
4260 * - configuration file
4261 * - server-state file (states provided by an 'old' haproxy process)
4262 *
4263 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4264 */
4265int srv_init_addr(void)
4266{
4267 struct proxy *curproxy;
4268 int return_code = 0;
4269
4270 curproxy = proxy;
4271 while (curproxy) {
4272 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004273
4274 /* servers are in backend only */
4275 if (!(curproxy->cap & PR_CAP_BE))
4276 goto srv_init_addr_next;
4277
Willy Tarreau25e51522016-11-04 15:10:17 +01004278 for (srv = curproxy->srv; srv; srv = srv->next)
4279 if (srv->hostname)
4280 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004281
4282 srv_init_addr_next:
4283 curproxy = curproxy->next;
4284 }
4285
4286 return return_code;
4287}
4288
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004289const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater)
4290{
4291
4292 struct chunk *msg;
4293
4294 msg = get_trash_chunk();
4295 chunk_reset(msg);
4296
Olivier Houchard8da5f982017-08-04 18:35:36 +02004297 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004298 chunk_appendf(msg, "no need to change the FDQN");
4299 goto out;
4300 }
4301
4302 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4303 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4304 goto out;
4305 }
4306
4307 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4308 server->proxy->id, server->id, server->hostname, fqdn);
4309
4310 if (srv_set_fqdn(server, fqdn) < 0) {
4311 chunk_reset(msg);
4312 chunk_appendf(msg, "could not update %s/%s FQDN",
4313 server->proxy->id, server->id);
4314 goto out;
4315 }
4316
4317 /* Flag as FQDN set from stats socket. */
4318 server->admin |= SRV_ADMF_HMAINT;
4319
4320 out:
4321 if (updater)
4322 chunk_appendf(msg, " by '%s'", updater);
4323 chunk_appendf(msg, "\n");
4324
4325 return msg->str;
4326}
4327
4328
Willy Tarreau21b069d2016-11-23 17:15:08 +01004329/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4330 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004331 * 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 +01004332 * used for CLI commands requiring a server name.
4333 * Important: the <arg> is modified to remove the '/'.
4334 */
4335struct server *cli_find_server(struct appctx *appctx, char *arg)
4336{
4337 struct proxy *px;
4338 struct server *sv;
4339 char *line;
4340
4341 /* split "backend/server" and make <line> point to server */
4342 for (line = arg; *line; line++)
4343 if (*line == '/') {
4344 *line++ = '\0';
4345 break;
4346 }
4347
4348 if (!*line || !*arg) {
4349 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004350 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004351 return NULL;
4352 }
4353
4354 if (!get_backend_server(arg, line, &px, &sv)) {
4355 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004356 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004357 return NULL;
4358 }
4359
4360 if (px->state == PR_STSTOPPED) {
4361 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004362 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004363 return NULL;
4364 }
4365
4366 return sv;
4367}
4368
William Lallemand222baf22016-11-19 02:00:33 +01004369
4370static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4371{
4372 struct server *sv;
4373 const char *warning;
4374
4375 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4376 return 1;
4377
4378 sv = cli_find_server(appctx, args[2]);
4379 if (!sv)
4380 return 1;
4381
4382 if (strcmp(args[3], "weight") == 0) {
4383 warning = server_parse_weight_change_request(sv, args[4]);
4384 if (warning) {
4385 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004386 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004387 }
4388 }
4389 else if (strcmp(args[3], "state") == 0) {
4390 if (strcmp(args[4], "ready") == 0)
4391 srv_adm_set_ready(sv);
4392 else if (strcmp(args[4], "drain") == 0)
4393 srv_adm_set_drain(sv);
4394 else if (strcmp(args[4], "maint") == 0)
4395 srv_adm_set_maint(sv);
4396 else {
4397 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
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], "health") == 0) {
4402 if (sv->track) {
4403 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004404 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004405 }
4406 else if (strcmp(args[4], "up") == 0) {
4407 sv->check.health = sv->check.rise + sv->check.fall - 1;
4408 srv_set_running(sv, "changed from CLI");
4409 }
4410 else if (strcmp(args[4], "stopping") == 0) {
4411 sv->check.health = sv->check.rise + sv->check.fall - 1;
4412 srv_set_stopping(sv, "changed from CLI");
4413 }
4414 else if (strcmp(args[4], "down") == 0) {
4415 sv->check.health = 0;
4416 srv_set_stopped(sv, "changed from CLI");
4417 }
4418 else {
4419 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004420 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004421 }
4422 }
4423 else if (strcmp(args[3], "agent") == 0) {
4424 if (!(sv->agent.state & CHK_ST_ENABLED)) {
4425 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004426 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004427 }
4428 else if (strcmp(args[4], "up") == 0) {
4429 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4430 srv_set_running(sv, "changed from CLI");
4431 }
4432 else if (strcmp(args[4], "down") == 0) {
4433 sv->agent.health = 0;
4434 srv_set_stopped(sv, "changed from CLI");
4435 }
4436 else {
4437 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004438 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004439 }
4440 }
Misiek2da082d2017-01-09 09:40:42 +01004441 else if (strcmp(args[3], "agent-addr") == 0) {
4442 if (!(sv->agent.state & CHK_ST_ENABLED)) {
4443 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4444 appctx->st0 = CLI_ST_PRINT;
4445 } else {
4446 if (str2ip(args[4], &sv->agent.addr) == NULL) {
4447 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4448 appctx->st0 = CLI_ST_PRINT;
4449 }
4450 }
4451 }
4452 else if (strcmp(args[3], "agent-send") == 0) {
4453 if (!(sv->agent.state & CHK_ST_ENABLED)) {
4454 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4455 appctx->st0 = CLI_ST_PRINT;
4456 } else {
4457 char *nss = strdup(args[4]);
4458 if (!nss) {
4459 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4460 appctx->st0 = CLI_ST_PRINT;
4461 } else {
4462 free(sv->agent.send_string);
4463 sv->agent.send_string = nss;
4464 sv->agent.send_string_len = strlen(args[4]);
4465 }
4466 }
4467 }
William Lallemand222baf22016-11-19 02:00:33 +01004468 else if (strcmp(args[3], "check-port") == 0) {
4469 int i = 0;
4470 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
4471 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004472 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004473 }
4474 if ((i < 0) || (i > 65535)) {
4475 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004476 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004477 }
4478 /* prevent the update of port to 0 if MAPPORTS are in use */
4479 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
4480 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004481 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004482 return 1;
4483 }
4484 sv->check.port = i;
4485 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004486 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004487 }
4488 else if (strcmp(args[3], "addr") == 0) {
4489 char *addr = NULL;
4490 char *port = NULL;
4491 if (strlen(args[4]) == 0) {
4492 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004493 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004494 return 1;
4495 }
4496 else {
4497 addr = args[4];
4498 }
4499 if (strcmp(args[5], "port") == 0) {
4500 port = args[6];
4501 }
4502 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4503 if (warning) {
4504 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004505 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004506 }
4507 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4508 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004509 else if (strcmp(args[3], "fqdn") == 0) {
4510 if (!*args[4]) {
4511 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4512 appctx->st0 = CLI_ST_PRINT;
4513 return 1;
4514 }
4515 warning = update_server_fqdn(sv, args[4], "stats socket command");
4516 if (warning) {
4517 appctx->ctx.cli.msg = warning;
4518 appctx->st0 = CLI_ST_PRINT;
4519 }
4520 }
William Lallemand222baf22016-11-19 02:00:33 +01004521 else {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004522 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 +01004523 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004524 }
4525 return 1;
4526}
4527
William Lallemand6b160942016-11-22 12:34:35 +01004528static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4529{
4530 struct stream_interface *si = appctx->owner;
4531 struct proxy *px;
4532 struct server *sv;
4533 char *line;
4534
4535
4536 /* split "backend/server" and make <line> point to server */
4537 for (line = args[2]; *line; line++)
4538 if (*line == '/') {
4539 *line++ = '\0';
4540 break;
4541 }
4542
4543 if (!*line) {
4544 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004545 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004546 return 1;
4547 }
4548
4549 if (!get_backend_server(args[2], line, &px, &sv)) {
4550 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004551 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004552 return 1;
4553 }
4554
4555 /* return server's effective weight at the moment */
4556 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004557 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004558 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004559 return 0;
4560 }
William Lallemand6b160942016-11-22 12:34:35 +01004561 return 1;
4562}
4563
4564static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4565{
4566 struct server *sv;
4567 const char *warning;
4568
4569 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4570 return 1;
4571
4572 sv = cli_find_server(appctx, args[2]);
4573 if (!sv)
4574 return 1;
4575
4576 warning = server_parse_weight_change_request(sv, args[3]);
4577 if (warning) {
4578 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004579 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004580 }
4581 return 1;
4582}
4583
Willy Tarreaub8026272016-11-23 11:26:56 +01004584/* parse a "set maxconn server" command. It always returns 1. */
4585static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4586{
4587 struct server *sv;
4588 const char *warning;
4589
4590 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4591 return 1;
4592
4593 sv = cli_find_server(appctx, args[3]);
4594 if (!sv)
4595 return 1;
4596
4597 warning = server_parse_maxconn_change_request(sv, args[4]);
4598 if (warning) {
4599 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004600 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004601 }
4602 return 1;
4603}
William Lallemand6b160942016-11-22 12:34:35 +01004604
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004605/* parse a "disable agent" command. It always returns 1. */
4606static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4607{
4608 struct server *sv;
4609
4610 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4611 return 1;
4612
4613 sv = cli_find_server(appctx, args[2]);
4614 if (!sv)
4615 return 1;
4616
4617 sv->agent.state &= ~CHK_ST_ENABLED;
4618 return 1;
4619}
4620
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004621/* parse a "disable health" command. It always returns 1. */
4622static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4623{
4624 struct server *sv;
4625
4626 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4627 return 1;
4628
4629 sv = cli_find_server(appctx, args[2]);
4630 if (!sv)
4631 return 1;
4632
4633 sv->check.state &= ~CHK_ST_ENABLED;
4634 return 1;
4635}
4636
Willy Tarreauffb4d582016-11-24 12:47:00 +01004637/* parse a "disable server" command. It always returns 1. */
4638static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4639{
4640 struct server *sv;
4641
4642 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4643 return 1;
4644
4645 sv = cli_find_server(appctx, args[2]);
4646 if (!sv)
4647 return 1;
4648
4649 srv_adm_set_maint(sv);
4650 return 1;
4651}
4652
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004653/* parse a "enable agent" command. It always returns 1. */
4654static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4655{
4656 struct server *sv;
4657
4658 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4659 return 1;
4660
4661 sv = cli_find_server(appctx, args[2]);
4662 if (!sv)
4663 return 1;
4664
4665 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
4666 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004667 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004668 return 1;
4669 }
4670
4671 sv->agent.state |= CHK_ST_ENABLED;
4672 return 1;
4673}
4674
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004675/* parse a "enable health" command. It always returns 1. */
4676static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4677{
4678 struct server *sv;
4679
4680 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4681 return 1;
4682
4683 sv = cli_find_server(appctx, args[2]);
4684 if (!sv)
4685 return 1;
4686
4687 sv->check.state |= CHK_ST_ENABLED;
4688 return 1;
4689}
4690
Willy Tarreauffb4d582016-11-24 12:47:00 +01004691/* parse a "enable server" command. It always returns 1. */
4692static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4693{
4694 struct server *sv;
4695
4696 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4697 return 1;
4698
4699 sv = cli_find_server(appctx, args[2]);
4700 if (!sv)
4701 return 1;
4702
4703 srv_adm_set_ready(sv);
4704 return 1;
4705}
4706
William Lallemand222baf22016-11-19 02:00:33 +01004707/* register cli keywords */
4708static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004709 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004710 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004711 { { "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 +01004712 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004713 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004714 { { "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 +01004715 { { "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 +01004716 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004717 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4718 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4719
William Lallemand222baf22016-11-19 02:00:33 +01004720 {{},}
4721}};
4722
4723__attribute__((constructor))
4724static void __server_init(void)
4725{
4726 cli_register_kw(&cli_kws);
4727}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004728
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004729/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004730 * Local variables:
4731 * c-indent-level: 8
4732 * c-basic-offset: 8
4733 * End:
4734 */