blob: d26c1187647b2acb7da07763ff2b673b20eafd35 [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);
Baptiste Assmann9d41fe72017-05-04 12:13:31 +020050static void srv_free_dns_resolution(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020051
Willy Tarreau21faa912012-10-10 08:27:36 +020052/* List head of all known server keywords */
53static struct srv_kw_list srv_keywords = {
54 .list = LIST_HEAD_INIT(srv_keywords.list)
55};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020056
Simon Hormana3608442013-11-01 16:46:15 +090057int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020058{
Willy Tarreau892337c2014-05-13 23:41:20 +020059 if ((s->state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020060 return s->down_time;
61
62 return now.tv_sec - s->last_change + s->down_time;
63}
Willy Tarreaubaaee002006-06-26 02:48:02 +020064
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050065int srv_lastsession(const struct server *s)
66{
67 if (s->counters.last_sess)
68 return now.tv_sec - s->counters.last_sess;
69
70 return -1;
71}
72
Simon Horman4a741432013-02-23 15:35:38 +090073int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020074{
Simon Horman4a741432013-02-23 15:35:38 +090075 const struct server *s = check->server;
76
Willy Tarreauff5ae352013-12-11 20:36:34 +010077 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090078 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010079
Willy Tarreau892337c2014-05-13 23:41:20 +020080 if ((s->state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090081 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010082
Simon Horman4a741432013-02-23 15:35:38 +090083 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010084}
85
Olivier Houchard4e694042017-03-14 20:01:29 +010086void srv_set_dyncookie(struct server *s)
87{
88 struct proxy *p = s->proxy;
89 struct server *tmpserv;
90 char *tmpbuf;
91 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010092 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +010093 size_t buffer_len;
94 int addr_len;
95 int port;
96
97 if ((s->flags & SRV_F_COOKIESET) ||
98 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
99 s->proxy->dyncookie_key == NULL)
100 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100101 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100102
103 if (s->addr.ss_family != AF_INET &&
104 s->addr.ss_family != AF_INET6)
105 return;
106 /*
107 * Buffer to calculate the cookie value.
108 * The buffer contains the secret key + the server IP address
109 * + the TCP port.
110 */
111 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
112 /*
113 * The TCP port should use only 2 bytes, but is stored in
114 * an unsigned int in struct server, so let's use 4, to be
115 * on the safe side.
116 */
117 buffer_len = key_len + addr_len + 4;
118 tmpbuf = trash.str;
119 memcpy(tmpbuf, p->dyncookie_key, key_len);
120 memcpy(&(tmpbuf[key_len]),
121 s->addr.ss_family == AF_INET ?
122 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
123 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
124 addr_len);
125 /*
126 * Make sure it's the same across all the load balancers,
127 * no matter their endianness.
128 */
129 port = htonl(s->svc_port);
130 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
131 hash_value = XXH64(tmpbuf, buffer_len, 0);
132 memprintf(&s->cookie, "%016llx", hash_value);
133 if (!s->cookie)
134 return;
135 s->cklen = 16;
136 /*
137 * Check that we did not get a hash collision.
138 * Unlikely, but it can happen.
139 */
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200140 for (tmpserv = p->srv; tmpserv != NULL;
141 tmpserv = tmpserv->next) {
142 if (tmpserv == s)
143 continue;
144 if (tmpserv->cookie &&
145 strcmp(tmpserv->cookie, s->cookie) == 0) {
146 Warning("We generated two equal cookies for two different servers.\n"
147 "Please change the secret key for '%s'.\n",
148 s->proxy->id);
Olivier Houchard4e694042017-03-14 20:01:29 +0100149 }
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200150 }
Olivier Houchard4e694042017-03-14 20:01:29 +0100151}
152
Willy Tarreau21faa912012-10-10 08:27:36 +0200153/*
154 * Registers the server keyword list <kwl> as a list of valid keywords for next
155 * parsing sessions.
156 */
157void srv_register_keywords(struct srv_kw_list *kwl)
158{
159 LIST_ADDQ(&srv_keywords.list, &kwl->list);
160}
161
162/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
163 * keyword is found with a NULL ->parse() function, then an attempt is made to
164 * find one with a valid ->parse() function. This way it is possible to declare
165 * platform-dependant, known keywords as NULL, then only declare them as valid
166 * if some options are met. Note that if the requested keyword contains an
167 * opening parenthesis, everything from this point is ignored.
168 */
169struct srv_kw *srv_find_kw(const char *kw)
170{
171 int index;
172 const char *kwend;
173 struct srv_kw_list *kwl;
174 struct srv_kw *ret = NULL;
175
176 kwend = strchr(kw, '(');
177 if (!kwend)
178 kwend = kw + strlen(kw);
179
180 list_for_each_entry(kwl, &srv_keywords.list, list) {
181 for (index = 0; kwl->kw[index].kw != NULL; index++) {
182 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
183 kwl->kw[index].kw[kwend-kw] == 0) {
184 if (kwl->kw[index].parse)
185 return &kwl->kw[index]; /* found it !*/
186 else
187 ret = &kwl->kw[index]; /* may be OK */
188 }
189 }
190 }
191 return ret;
192}
193
194/* Dumps all registered "server" keywords to the <out> string pointer. The
195 * unsupported keywords are only dumped if their supported form was not
196 * found.
197 */
198void srv_dump_kws(char **out)
199{
200 struct srv_kw_list *kwl;
201 int index;
202
203 *out = NULL;
204 list_for_each_entry(kwl, &srv_keywords.list, list) {
205 for (index = 0; kwl->kw[index].kw != NULL; index++) {
206 if (kwl->kw[index].parse ||
207 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
208 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
209 kwl->scope,
210 kwl->kw[index].kw,
211 kwl->kw[index].skip ? " <arg>" : "",
212 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
213 kwl->kw[index].parse ? "" : " (not supported)");
214 }
215 }
216 }
217}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100218
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100219/* Parse the "addr" server keyword */
220static int srv_parse_addr(char **args, int *cur_arg,
221 struct proxy *curproxy, struct server *newsrv, char **err)
222{
223 char *errmsg, *arg;
224 struct sockaddr_storage *sk;
225 int port1, port2;
226 struct protocol *proto;
227
228 errmsg = NULL;
229 arg = args[*cur_arg + 1];
230
231 if (!*arg) {
232 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
233 goto err;
234 }
235
236 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
237 if (!sk) {
238 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
239 goto err;
240 }
241
242 proto = protocol_by_family(sk->ss_family);
243 if (!proto || !proto->connect) {
244 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
245 args[*cur_arg], arg);
246 goto err;
247 }
248
249 if (port1 != port2) {
250 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
251 args[*cur_arg], arg);
252 goto err;
253 }
254
255 newsrv->check.addr = newsrv->agent.addr = *sk;
256 newsrv->flags |= SRV_F_CHECKADDR;
257 newsrv->flags |= SRV_F_AGENTADDR;
258
259 return 0;
260
261 err:
262 free(errmsg);
263 return ERR_ALERT | ERR_FATAL;
264}
265
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100266/* Parse the "agent-check" server keyword */
267static int srv_parse_agent_check(char **args, int *cur_arg,
268 struct proxy *curproxy, struct server *newsrv, char **err)
269{
270 newsrv->do_agent = 1;
271 return 0;
272}
273
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100274/* Parse the "backup" server keyword */
275static int srv_parse_backup(char **args, int *cur_arg,
276 struct proxy *curproxy, struct server *newsrv, char **err)
277{
278 newsrv->flags |= SRV_F_BACKUP;
279 return 0;
280}
281
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100282/* Parse the "check" server keyword */
283static int srv_parse_check(char **args, int *cur_arg,
284 struct proxy *curproxy, struct server *newsrv, char **err)
285{
286 newsrv->do_check = 1;
287 return 0;
288}
289
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100290/* Parse the "check-send-proxy" server keyword */
291static int srv_parse_check_send_proxy(char **args, int *cur_arg,
292 struct proxy *curproxy, struct server *newsrv, char **err)
293{
294 newsrv->check.send_proxy = 1;
295 return 0;
296}
297
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100298/* Parse the "cookie" server keyword */
299static int srv_parse_cookie(char **args, int *cur_arg,
300 struct proxy *curproxy, struct server *newsrv, char **err)
301{
302 char *arg;
303
304 arg = args[*cur_arg + 1];
305 if (!*arg) {
306 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
307 return ERR_ALERT | ERR_FATAL;
308 }
309
310 free(newsrv->cookie);
311 newsrv->cookie = strdup(arg);
312 newsrv->cklen = strlen(arg);
313 newsrv->flags |= SRV_F_COOKIESET;
314 return 0;
315}
316
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100317/* Parse the "disabled" server keyword */
318static int srv_parse_disabled(char **args, int *cur_arg,
319 struct proxy *curproxy, struct server *newsrv, char **err)
320{
321 newsrv->admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
322 newsrv->state = SRV_ST_STOPPED;
323 newsrv->check.state |= CHK_ST_PAUSED;
324 newsrv->check.health = 0;
325 return 0;
326}
327
328/* Parse the "enabled" server keyword */
329static int srv_parse_enabled(char **args, int *cur_arg,
330 struct proxy *curproxy, struct server *newsrv, char **err)
331{
332 newsrv->admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
333 newsrv->state = SRV_ST_RUNNING;
334 newsrv->check.state &= ~CHK_ST_PAUSED;
335 newsrv->check.health = newsrv->check.rise;
336 return 0;
337}
338
Willy Tarreaudff55432012-10-10 17:51:05 +0200339/* parse the "id" server keyword */
340static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
341{
342 struct eb32_node *node;
343
344 if (!*args[*cur_arg + 1]) {
345 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
346 return ERR_ALERT | ERR_FATAL;
347 }
348
349 newsrv->puid = atol(args[*cur_arg + 1]);
350 newsrv->conf.id.key = newsrv->puid;
351
352 if (newsrv->puid <= 0) {
353 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
354 return ERR_ALERT | ERR_FATAL;
355 }
356
357 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
358 if (node) {
359 struct server *target = container_of(node, struct server, conf.id);
360 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
361 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
362 target->id);
363 return ERR_ALERT | ERR_FATAL;
364 }
365
366 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200367 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200368 return 0;
369}
370
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100371/* Parse the "namespace" server keyword */
372static int srv_parse_namespace(char **args, int *cur_arg,
373 struct proxy *curproxy, struct server *newsrv, char **err)
374{
375#ifdef CONFIG_HAP_NS
376 char *arg;
377
378 arg = args[*cur_arg + 1];
379 if (!*arg) {
380 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
381 return ERR_ALERT | ERR_FATAL;
382 }
383
384 if (!strcmp(arg, "*")) {
385 /* Use the namespace associated with the connection (if present). */
386 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
387 return 0;
388 }
389
390 /*
391 * As this parser may be called several times for the same 'default-server'
392 * object, or for a new 'server' instance deriving from a 'default-server'
393 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
394 */
395 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
396
397 newsrv->netns = netns_store_lookup(arg, strlen(arg));
398 if (!newsrv->netns)
399 newsrv->netns = netns_store_insert(arg);
400
401 if (!newsrv->netns) {
402 memprintf(err, "Cannot open namespace '%s'", arg);
403 return ERR_ALERT | ERR_FATAL;
404 }
405
406 return 0;
407#else
408 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
409 return ERR_ALERT | ERR_FATAL;
410#endif
411}
412
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100413/* Parse the "no-agent-check" server keyword */
414static int srv_parse_no_agent_check(char **args, int *cur_arg,
415 struct proxy *curproxy, struct server *newsrv, char **err)
416{
417 free_check(&newsrv->agent);
418 newsrv->agent.inter = 0;
419 newsrv->agent.port = 0;
420 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
421 newsrv->do_agent = 0;
422 return 0;
423}
424
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100425/* Parse the "no-backup" server keyword */
426static int srv_parse_no_backup(char **args, int *cur_arg,
427 struct proxy *curproxy, struct server *newsrv, char **err)
428{
429 newsrv->flags &= ~SRV_F_BACKUP;
430 return 0;
431}
432
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100433/* Parse the "no-check" server keyword */
434static int srv_parse_no_check(char **args, int *cur_arg,
435 struct proxy *curproxy, struct server *newsrv, char **err)
436{
437 free_check(&newsrv->check);
438 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
439 newsrv->do_check = 0;
440 return 0;
441}
442
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100443/* Parse the "no-check-send-proxy" server keyword */
444static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
445 struct proxy *curproxy, struct server *newsrv, char **err)
446{
447 newsrv->check.send_proxy = 0;
448 return 0;
449}
450
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100451/* Disable server PROXY protocol flags. */
452static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
453{
454 srv->pp_opts &= ~flags;
455 return 0;
456}
457
458/* Parse the "no-send-proxy" server keyword */
459static int srv_parse_no_send_proxy(char **args, int *cur_arg,
460 struct proxy *curproxy, struct server *newsrv, char **err)
461{
462 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
463}
464
465/* Parse the "no-send-proxy-v2" server keyword */
466static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
467 struct proxy *curproxy, struct server *newsrv, char **err)
468{
469 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
470}
471
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100472/* Parse the "non-stick" server keyword */
473static int srv_parse_non_stick(char **args, int *cur_arg,
474 struct proxy *curproxy, struct server *newsrv, char **err)
475{
476 newsrv->flags |= SRV_F_NON_STICK;
477 return 0;
478}
479
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100480/* Enable server PROXY protocol flags. */
481static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
482{
483 srv->pp_opts |= flags;
484 return 0;
485}
486
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100487/* Parse the "observe" server keyword */
488static int srv_parse_observe(char **args, int *cur_arg,
489 struct proxy *curproxy, struct server *newsrv, char **err)
490{
491 char *arg;
492
493 arg = args[*cur_arg + 1];
494 if (!*arg) {
495 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
496 return ERR_ALERT | ERR_FATAL;
497 }
498
499 if (!strcmp(arg, "none")) {
500 newsrv->observe = HANA_OBS_NONE;
501 }
502 else if (!strcmp(arg, "layer4")) {
503 newsrv->observe = HANA_OBS_LAYER4;
504 }
505 else if (!strcmp(arg, "layer7")) {
506 if (curproxy->mode != PR_MODE_HTTP) {
507 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
508 return ERR_ALERT;
509 }
510 newsrv->observe = HANA_OBS_LAYER7;
511 }
512 else {
513 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
514 "but got '%s'\n", args[*cur_arg], arg);
515 return ERR_ALERT | ERR_FATAL;
516 }
517
518 return 0;
519}
520
Frédéric Lécaille16186232017-03-14 16:42:49 +0100521/* Parse the "redir" server keyword */
522static int srv_parse_redir(char **args, int *cur_arg,
523 struct proxy *curproxy, struct server *newsrv, char **err)
524{
525 char *arg;
526
527 arg = args[*cur_arg + 1];
528 if (!*arg) {
529 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
530 return ERR_ALERT | ERR_FATAL;
531 }
532
533 free(newsrv->rdr_pfx);
534 newsrv->rdr_pfx = strdup(arg);
535 newsrv->rdr_len = strlen(arg);
536
537 return 0;
538}
539
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100540/* Parse the "send-proxy" server keyword */
541static int srv_parse_send_proxy(char **args, int *cur_arg,
542 struct proxy *curproxy, struct server *newsrv, char **err)
543{
544 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
545}
546
547/* Parse the "send-proxy-v2" server keyword */
548static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
549 struct proxy *curproxy, struct server *newsrv, char **err)
550{
551 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
552}
553
Frédéric Lécailledba97072017-03-17 15:33:50 +0100554
555/* Parse the "source" server keyword */
556static int srv_parse_source(char **args, int *cur_arg,
557 struct proxy *curproxy, struct server *newsrv, char **err)
558{
559 char *errmsg;
560 int port_low, port_high;
561 struct sockaddr_storage *sk;
562 struct protocol *proto;
563
564 errmsg = NULL;
565
566 if (!*args[*cur_arg + 1]) {
567 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
568 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
569 goto err;
570 }
571
572 /* 'sk' is statically allocated (no need to be freed). */
573 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
574 if (!sk) {
575 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
576 goto err;
577 }
578
579 proto = protocol_by_family(sk->ss_family);
580 if (!proto || !proto->connect) {
581 Alert("'%s %s' : connect() not supported for this address family.\n",
582 args[*cur_arg], args[*cur_arg + 1]);
583 goto err;
584 }
585
586 newsrv->conn_src.opts |= CO_SRC_BIND;
587 newsrv->conn_src.source_addr = *sk;
588
589 if (port_low != port_high) {
590 int i;
591
592 if (!port_low || !port_high) {
593 Alert("'%s' does not support port offsets (found '%s').\n",
594 args[*cur_arg], args[*cur_arg + 1]);
595 goto err;
596 }
597
598 if (port_low <= 0 || port_low > 65535 ||
599 port_high <= 0 || port_high > 65535 ||
600 port_low > port_high) {
601 Alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
602 goto err;
603 }
604 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
605 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
606 newsrv->conn_src.sport_range->ports[i] = port_low + i;
607 }
608
609 *cur_arg += 2;
610 while (*(args[*cur_arg])) {
611 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
612#if defined(CONFIG_HAP_TRANSPARENT)
613 if (!*args[*cur_arg + 1]) {
614 Alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
615 "or 'hdr_ip(name,#)' as argument.\n");
616 goto err;
617 }
618 if (!strcmp(args[*cur_arg + 1], "client")) {
619 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
620 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
621 }
622 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
623 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
624 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
625 }
626 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
627 char *name, *end;
628
629 name = args[*cur_arg + 1] + 7;
630 while (isspace(*name))
631 name++;
632
633 end = name;
634 while (*end && !isspace(*end) && *end != ',' && *end != ')')
635 end++;
636
637 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
638 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
639 free(newsrv->conn_src.bind_hdr_name);
640 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
641 newsrv->conn_src.bind_hdr_len = end - name;
642 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
643 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
644 newsrv->conn_src.bind_hdr_occ = -1;
645
646 /* now look for an occurrence number */
647 while (isspace(*end))
648 end++;
649 if (*end == ',') {
650 end++;
651 name = end;
652 if (*end == '-')
653 end++;
654 while (isdigit((int)*end))
655 end++;
656 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
657 }
658
659 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
660 Alert("usesrc hdr_ip(name,num) does not support negative"
661 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
662 goto err;
663 }
664 }
665 else {
666 struct sockaddr_storage *sk;
667 int port1, port2;
668
669 /* 'sk' is statically allocated (no need to be freed). */
670 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
671 if (!sk) {
672 Alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
673 goto err;
674 }
675
676 proto = protocol_by_family(sk->ss_family);
677 if (!proto || !proto->connect) {
678 Alert("'%s %s' : connect() not supported for this address family.\n",
679 args[*cur_arg], args[*cur_arg + 1]);
680 goto err;
681 }
682
683 if (port1 != port2) {
684 Alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
685 args[*cur_arg], args[*cur_arg + 1]);
686 goto err;
687 }
688 newsrv->conn_src.tproxy_addr = *sk;
689 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
690 }
691 global.last_checks |= LSTCHK_NETADM;
692 *cur_arg += 2;
693 continue;
694#else /* no TPROXY support */
695 Alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
696 goto err;
697#endif /* defined(CONFIG_HAP_TRANSPARENT) */
698 } /* "usesrc" */
699
700 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
701#ifdef SO_BINDTODEVICE
702 if (!*args[*cur_arg + 1]) {
703 Alert("'%s' : missing interface name.\n", args[0]);
704 goto err;
705 }
706 free(newsrv->conn_src.iface_name);
707 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
708 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
709 global.last_checks |= LSTCHK_NETADM;
710#else
711 Alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
712 goto err;
713#endif
714 *cur_arg += 2;
715 continue;
716 }
717 /* this keyword in not an option of "source" */
718 break;
719 } /* while */
720
721 return 0;
722
723 err:
724 free(errmsg);
725 return ERR_ALERT | ERR_FATAL;
726}
727
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100728/* Parse the "stick" server keyword */
729static int srv_parse_stick(char **args, int *cur_arg,
730 struct proxy *curproxy, struct server *newsrv, char **err)
731{
732 newsrv->flags &= ~SRV_F_NON_STICK;
733 return 0;
734}
735
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100736/* Parse the "track" server keyword */
737static int srv_parse_track(char **args, int *cur_arg,
738 struct proxy *curproxy, struct server *newsrv, char **err)
739{
740 char *arg;
741
742 arg = args[*cur_arg + 1];
743 if (!*arg) {
744 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
745 return ERR_ALERT | ERR_FATAL;
746 }
747
748 free(newsrv->trackit);
749 newsrv->trackit = strdup(arg);
750
751 return 0;
752}
753
Frédéric Lécailledba97072017-03-17 15:33:50 +0100754
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200755/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200756 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200757 * shutdown.
758 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200759void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200760{
Willy Tarreau87b09662015-04-03 00:22:06 +0200761 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200762
Willy Tarreau87b09662015-04-03 00:22:06 +0200763 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
764 if (stream->srv_conn == srv)
765 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200766}
767
768/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200769 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200770 * the reason for the shutdown.
771 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200772void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200773{
774 struct server *srv;
775
776 for (srv = px->srv; srv != NULL; srv = srv->next)
777 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200778 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200779}
780
Willy Tarreaubda92272014-05-20 21:55:30 +0200781/* Appends some information to a message string related to a server going UP or
782 * DOWN. If both <forced> and <reason> are null and the server tracks another
783 * one, a "via" information will be provided to know where the status came from.
784 * If <reason> is non-null, the entire string will be appended after a comma and
785 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200786 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200787 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200788 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200789void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200790{
Willy Tarreaubda92272014-05-20 21:55:30 +0200791 if (reason)
792 chunk_appendf(msg, ", %s", reason);
793 else if (!forced && s->track)
794 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200795
796 if (xferred >= 0) {
797 if (s->state == SRV_ST_STOPPED)
798 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
799 " %d sessions active, %d requeued, %d remaining in queue",
800 s->proxy->srv_act, s->proxy->srv_bck,
801 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
802 s->cur_sess, xferred, s->nbpend);
803 else
804 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
805 " %d sessions requeued, %d total in queue",
806 s->proxy->srv_act, s->proxy->srv_bck,
807 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
808 xferred, s->nbpend);
809 }
810}
811
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200812/* Marks server <s> down, regardless of its checks' statuses, notifies by all
813 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200814 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200815 * recomputes the number of servers, but not the map. Maintenance servers are
816 * ignored. It reports <reason> if non-null as the reason for going down. Note
817 * that it makes use of the trash to build the log strings, so <reason> must
818 * not be placed there.
819 */
820void srv_set_stopped(struct server *s, const char *reason)
821{
822 struct server *srv;
823 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
824 int srv_was_stopping = (s->state == SRV_ST_STOPPING);
Simon Horman64e34162015-02-06 11:11:57 +0900825 int log_level;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200826 int xferred;
827
828 if ((s->admin & SRV_ADMF_MAINT) || s->state == SRV_ST_STOPPED)
829 return;
830
831 s->last_change = now.tv_sec;
832 s->state = SRV_ST_STOPPED;
833 if (s->proxy->lbprm.set_server_status_down)
834 s->proxy->lbprm.set_server_status_down(s);
835
836 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200837 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200838
Willy Tarreau87b09662015-04-03 00:22:06 +0200839 /* we might have streams queued on this server and waiting for
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200840 * a connection. Those which are redispatchable will be queued
841 * to another server or to the proxy itself.
842 */
843 xferred = pendconn_redistribute(s);
844
845 chunk_printf(&trash,
846 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
847 s->proxy->id, s->id);
848
849 srv_append_status(&trash, s, reason, xferred, 0);
850 Warning("%s.\n", trash.str);
851
852 /* we don't send an alert if the server was previously paused */
Simon Horman64e34162015-02-06 11:11:57 +0900853 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
854 send_log(s->proxy, log_level, "%s.\n", trash.str);
855 send_email_alert(s, log_level, "%s", trash.str);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200856
857 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
858 set_backend_down(s->proxy);
859
860 s->counters.down_trans++;
861
862 for (srv = s->trackers; srv; srv = srv->tracknext)
863 srv_set_stopped(srv, NULL);
864}
865
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200866/* Marks server <s> up regardless of its checks' statuses and provided it isn't
867 * in maintenance. Notifies by all available means, recounts the remaining
868 * servers on the proxy and tries to grab requests from the proxy. It
869 * automatically recomputes the number of servers, but not the map. Maintenance
870 * servers are ignored. It reports <reason> if non-null as the reason for going
871 * up. Note that it makes use of the trash to build the log strings, so <reason>
872 * must not be placed there.
873 */
874void srv_set_running(struct server *s, const char *reason)
875{
876 struct server *srv;
877 int xferred;
878
879 if (s->admin & SRV_ADMF_MAINT)
880 return;
881
882 if (s->state == SRV_ST_STARTING || s->state == SRV_ST_RUNNING)
883 return;
884
885 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
886 if (s->proxy->last_change < now.tv_sec) // ignore negative times
887 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
888 s->proxy->last_change = now.tv_sec;
889 }
890
891 if (s->state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
892 s->down_time += now.tv_sec - s->last_change;
893
894 s->last_change = now.tv_sec;
895
896 s->state = SRV_ST_STARTING;
897 if (s->slowstart > 0)
898 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
899 else
900 s->state = SRV_ST_RUNNING;
901
902 server_recalc_eweight(s);
903
904 /* If the server is set with "on-marked-up shutdown-backup-sessions",
905 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +0200906 * then it can accept new connections, so we shut down all streams
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200907 * on all backup servers.
908 */
909 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
910 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200911 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200912
913 /* check if we can handle some connections queued at the proxy. We
914 * will take as many as we can handle.
915 */
916 xferred = pendconn_grab_from_px(s);
917
918 chunk_printf(&trash,
919 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
920 s->proxy->id, s->id);
921
922 srv_append_status(&trash, s, reason, xferred, 0);
923 Warning("%s.\n", trash.str);
924 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Simon Horman4cd477f2015-04-30 13:10:34 +0900925 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200926
927 for (srv = s->trackers; srv; srv = srv->tracknext)
928 srv_set_running(srv, NULL);
929}
930
Willy Tarreau8eb77842014-05-21 13:54:57 +0200931/* Marks server <s> stopping regardless of its checks' statuses and provided it
932 * isn't in maintenance. Notifies by all available means, recounts the remaining
933 * servers on the proxy and tries to grab requests from the proxy. It
934 * automatically recomputes the number of servers, but not the map. Maintenance
935 * servers are ignored. It reports <reason> if non-null as the reason for going
936 * up. Note that it makes use of the trash to build the log strings, so <reason>
937 * must not be placed there.
938 */
939void srv_set_stopping(struct server *s, const char *reason)
940{
941 struct server *srv;
942 int xferred;
943
944 if (s->admin & SRV_ADMF_MAINT)
945 return;
946
947 if (s->state == SRV_ST_STOPPING)
948 return;
949
950 s->last_change = now.tv_sec;
951 s->state = SRV_ST_STOPPING;
952 if (s->proxy->lbprm.set_server_status_down)
953 s->proxy->lbprm.set_server_status_down(s);
954
Willy Tarreau87b09662015-04-03 00:22:06 +0200955 /* we might have streams queued on this server and waiting for
Willy Tarreau8eb77842014-05-21 13:54:57 +0200956 * a connection. Those which are redispatchable will be queued
957 * to another server or to the proxy itself.
958 */
959 xferred = pendconn_redistribute(s);
960
961 chunk_printf(&trash,
962 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
963 s->proxy->id, s->id);
964
965 srv_append_status(&trash, s, reason, xferred, 0);
966
967 Warning("%s.\n", trash.str);
968 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
969
970 if (!s->proxy->srv_bck && !s->proxy->srv_act)
971 set_backend_down(s->proxy);
972
973 for (srv = s->trackers; srv; srv = srv->tracknext)
974 srv_set_stopping(srv, NULL);
975}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200976
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200977/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
978 * enforce either maint mode or drain mode. It is not allowed to set more than
979 * one flag at once. The equivalent "inherited" flag is propagated to all
980 * tracking servers. Maintenance mode disables health checks (but not agent
981 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100982 * is done. If <cause> is non-null, it will be displayed at the end of the log
983 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200984 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100985void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200986{
987 struct check *check = &s->check;
988 struct server *srv;
989 int xferred;
990
991 if (!mode)
992 return;
993
994 /* stop going down as soon as we meet a server already in the same state */
995 if (s->admin & mode)
996 return;
997
998 s->admin |= mode;
999
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001000 /* stop going down if the equivalent flag was already present (forced or inherited) */
1001 if (((mode & SRV_ADMF_MAINT) && (s->admin & ~mode & SRV_ADMF_MAINT)) ||
1002 ((mode & SRV_ADMF_DRAIN) && (s->admin & ~mode & SRV_ADMF_DRAIN)))
1003 return;
1004
1005 /* Maintenance must also disable health checks */
1006 if (mode & SRV_ADMF_MAINT) {
1007 if (s->check.state & CHK_ST_ENABLED) {
1008 s->check.state |= CHK_ST_PAUSED;
1009 check->health = 0;
1010 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001011
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001012 if (s->state == SRV_ST_STOPPED) { /* server was already down */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001013 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +01001014 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
1015 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
1016 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001017
Willy Tarreaubda92272014-05-20 21:55:30 +02001018 srv_append_status(&trash, s, NULL, -1, (mode & SRV_ADMF_FMAINT));
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001019
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001020 if (!(global.mode & MODE_STARTING)) {
1021 Warning("%s.\n", trash.str);
1022 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1023 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001024 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001025 else { /* server was still running */
1026 int srv_was_stopping = (s->state == SRV_ST_STOPPING) || (s->admin & SRV_ADMF_DRAIN);
1027 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1028
1029 check->health = 0; /* failure */
1030 s->last_change = now.tv_sec;
1031 s->state = SRV_ST_STOPPED;
1032 if (s->proxy->lbprm.set_server_status_down)
1033 s->proxy->lbprm.set_server_status_down(s);
1034
1035 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001036 srv_shutdown_streams(s, SF_ERR_DOWN);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001037
Willy Tarreau87b09662015-04-03 00:22:06 +02001038 /* we might have streams queued on this server and waiting for
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001039 * a connection. Those which are redispatchable will be queued
1040 * to another server or to the proxy itself.
1041 */
1042 xferred = pendconn_redistribute(s);
1043
1044 chunk_printf(&trash,
Willy Tarreau8b428482016-11-07 15:53:43 +01001045 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001046 s->flags & SRV_F_BACKUP ? "Backup " : "",
Willy Tarreau8b428482016-11-07 15:53:43 +01001047 s->proxy->id, s->id,
1048 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001049
1050 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FMAINT));
1051
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001052 if (!(global.mode & MODE_STARTING)) {
1053 Warning("%s.\n", trash.str);
1054 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);
1055 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001056
1057 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1058 set_backend_down(s->proxy);
1059
1060 s->counters.down_trans++;
1061 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001062 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001063
1064 /* drain state is applied only if not yet in maint */
1065 if ((mode & SRV_ADMF_DRAIN) && !(s->admin & SRV_ADMF_MAINT)) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001066 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
1067
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001068 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001069 if (s->proxy->lbprm.set_server_status_down)
1070 s->proxy->lbprm.set_server_status_down(s);
1071
Willy Tarreau87b09662015-04-03 00:22:06 +02001072 /* we might have streams queued on this server and waiting for
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001073 * a connection. Those which are redispatchable will be queued
1074 * to another server or to the proxy itself.
1075 */
1076 xferred = pendconn_redistribute(s);
1077
Willy Tarreau8b428482016-11-07 15:53:43 +01001078 chunk_printf(&trash, "%sServer %s/%s enters drain state%s%s%s",
1079 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
1080 cause ? " (" : "", cause ? cause : "", cause ? ")" : "");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001081
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001082 srv_append_status(&trash, s, NULL, xferred, (mode & SRV_ADMF_FDRAIN));
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001083
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01001084 if (!(global.mode & MODE_STARTING)) {
1085 Warning("%s.\n", trash.str);
1086 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1087 send_email_alert(s, LOG_NOTICE, "%s", trash.str);
1088 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001089 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
1090 set_backend_down(s->proxy);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001091 }
1092
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001093 /* compute the inherited flag to propagate */
1094 if (mode & SRV_ADMF_MAINT)
1095 mode = SRV_ADMF_IMAINT;
1096 else if (mode & SRV_ADMF_DRAIN)
1097 mode = SRV_ADMF_IDRAIN;
1098
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001099 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +01001100 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001101}
1102
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001103/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1104 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1105 * than one flag at once. The equivalent "inherited" flag is propagated to all
1106 * tracking servers. Leaving maintenance mode re-enables health checks. When
1107 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001108 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001109void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001110{
1111 struct check *check = &s->check;
1112 struct server *srv;
1113 int xferred = -1;
1114
1115 if (!mode)
1116 return;
1117
1118 /* stop going down as soon as we see the flag is not there anymore */
1119 if (!(s->admin & mode))
1120 return;
1121
1122 s->admin &= ~mode;
1123
1124 if (s->admin & SRV_ADMF_MAINT) {
1125 /* remaining in maintenance mode, let's inform precisely about the
1126 * situation.
1127 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001128 if (mode & SRV_ADMF_FMAINT) {
1129 chunk_printf(&trash,
1130 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
1131 s->flags & SRV_F_BACKUP ? "Backup " : "",
1132 s->proxy->id, s->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001133
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001134 if (s->track) /* normally it's mandatory here */
1135 chunk_appendf(&trash, " via %s/%s",
1136 s->track->proxy->id, s->track->id);
1137 Warning("%s.\n", trash.str);
1138 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1139 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001140 if (mode & SRV_ADMF_RMAINT) {
1141 chunk_printf(&trash,
1142 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
1143 s->flags & SRV_F_BACKUP ? "Backup " : "",
1144 s->proxy->id, s->id, s->hostname);
1145
1146 if (s->track) /* normally it's mandatory here */
1147 chunk_appendf(&trash, " via %s/%s",
1148 s->track->proxy->id, s->track->id);
1149 Warning("%s.\n", trash.str);
1150 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1151 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001152 else if (mode & SRV_ADMF_IMAINT) {
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001153 chunk_printf(&trash,
1154 "%sServer %s/%s remains in forced maintenance",
1155 s->flags & SRV_F_BACKUP ? "Backup " : "",
1156 s->proxy->id, s->id);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001157 Warning("%s.\n", trash.str);
1158 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1159 }
1160 /* don't report anything when leaving drain mode and remaining in maintenance */
1161 }
1162 else if (mode & SRV_ADMF_MAINT) {
1163 /* OK here we're leaving maintenance, we have many things to check,
1164 * because the server might possibly be coming back up depending on
1165 * its state. In practice, leaving maintenance means that we should
1166 * immediately turn to UP (more or less the slowstart) under the
1167 * following conditions :
1168 * - server is neither checked nor tracked
1169 * - server tracks another server which is not checked
1170 * - server tracks another server which is already up
1171 * Which sums up as something simpler :
1172 * "either the tracking server is up or the server's checks are disabled
1173 * or up". Otherwise we only re-enable health checks. There's a special
1174 * case associated to the stopping state which can be inherited. Note
1175 * that the server might still be in drain mode, which is naturally dealt
1176 * with by the lower level functions.
1177 */
1178
1179 if (s->check.state & CHK_ST_ENABLED) {
1180 s->check.state &= ~CHK_ST_PAUSED;
1181 check->health = check->rise; /* start OK but check immediately */
1182 }
1183
1184 if ((!s->track || s->track->state != SRV_ST_STOPPED) &&
1185 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
1186 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
1187 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1188 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1189 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1190 s->proxy->last_change = now.tv_sec;
1191 }
1192
1193 if (s->last_change < now.tv_sec) // ignore negative times
1194 s->down_time += now.tv_sec - s->last_change;
1195 s->last_change = now.tv_sec;
1196
1197 if (s->track && s->track->state == SRV_ST_STOPPING)
1198 s->state = SRV_ST_STOPPING;
1199 else {
1200 s->state = SRV_ST_STARTING;
1201 if (s->slowstart > 0)
1202 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
1203 else
1204 s->state = SRV_ST_RUNNING;
1205 }
1206
1207 server_recalc_eweight(s);
1208
1209 /* If the server is set with "on-marked-up shutdown-backup-sessions",
1210 * and it's not a backup server and its effective weight is > 0,
Willy Tarreau87b09662015-04-03 00:22:06 +02001211 * then it can accept new connections, so we shut down all streams
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001212 * on all backup servers.
1213 */
1214 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
1215 !(s->flags & SRV_F_BACKUP) && s->eweight)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001216 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001217
1218 /* check if we can handle some connections queued at the proxy. We
1219 * will take as many as we can handle.
1220 */
1221 xferred = pendconn_grab_from_px(s);
1222 }
1223
1224 if (mode & SRV_ADMF_FMAINT) {
1225 chunk_printf(&trash,
1226 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
1227 s->flags & SRV_F_BACKUP ? "Backup " : "",
1228 s->proxy->id, s->id,
1229 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1230 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001231 }
Willy Tarreaue6599732016-11-07 15:42:33 +01001232 else if (mode & SRV_ADMF_RMAINT) {
1233 chunk_printf(&trash,
1234 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
1235 s->flags & SRV_F_BACKUP ? "Backup " : "",
1236 s->proxy->id, s->id, s->hostname,
1237 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1238 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
1239 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001240 else {
1241 chunk_printf(&trash,
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001242 "%sServer %s/%s is %s/%s (leaving maintenance)",
1243 s->flags & SRV_F_BACKUP ? "Backup " : "",
1244 s->proxy->id, s->id,
1245 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP",
1246 (s->admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
1247 srv_append_status(&trash, s, NULL, xferred, 0);
1248 }
1249 Warning("%s.\n", trash.str);
1250 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
1251 }
1252 else if ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)) {
1253 /* remaining in drain mode after removing one of its flags */
1254
1255 if (mode & SRV_ADMF_FDRAIN) {
1256 chunk_printf(&trash,
1257 "%sServer %s/%s is leaving forced drain but remains in drain mode",
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001258 s->flags & SRV_F_BACKUP ? "Backup " : "",
1259 s->proxy->id, s->id);
1260
1261 if (s->track) /* normally it's mandatory here */
1262 chunk_appendf(&trash, " via %s/%s",
1263 s->track->proxy->id, s->track->id);
1264 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001265 else {
1266 chunk_printf(&trash,
1267 "%sServer %s/%s remains in forced drain mode",
1268 s->flags & SRV_F_BACKUP ? "Backup " : "",
1269 s->proxy->id, s->id);
1270 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001271 Warning("%s.\n", trash.str);
1272 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001273 }
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001274 else if (mode & SRV_ADMF_DRAIN) {
1275 /* OK completely leaving drain mode */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001276 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
1277 if (s->proxy->last_change < now.tv_sec) // ignore negative times
1278 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
1279 s->proxy->last_change = now.tv_sec;
1280 }
1281
1282 if (s->last_change < now.tv_sec) // ignore negative times
1283 s->down_time += now.tv_sec - s->last_change;
1284 s->last_change = now.tv_sec;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001285 server_recalc_eweight(s);
1286
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001287 if (mode & SRV_ADMF_FDRAIN) {
1288 chunk_printf(&trash,
1289 "%sServer %s/%s is %s (leaving forced drain)",
1290 s->flags & SRV_F_BACKUP ? "Backup " : "",
1291 s->proxy->id, s->id,
1292 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
1293 }
1294 else {
1295 chunk_printf(&trash,
1296 "%sServer %s/%s is %s (leaving drain)",
1297 s->flags & SRV_F_BACKUP ? "Backup " : "",
1298 s->proxy->id, s->id,
1299 (s->state == SRV_ST_STOPPED) ? "DOWN" : "UP");
1300 if (s->track) /* normally it's mandatory here */
1301 chunk_appendf(&trash, " via %s/%s",
1302 s->track->proxy->id, s->track->id);
1303 }
1304 Warning("%s.\n", trash.str);
1305 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001306 }
1307
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001308 /* stop going down if the equivalent flag is still present (forced or inherited) */
1309 if (((mode & SRV_ADMF_MAINT) && (s->admin & SRV_ADMF_MAINT)) ||
1310 ((mode & SRV_ADMF_DRAIN) && (s->admin & SRV_ADMF_DRAIN)))
1311 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001312
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001313 if (mode & SRV_ADMF_MAINT)
1314 mode = SRV_ADMF_IMAINT;
1315 else if (mode & SRV_ADMF_DRAIN)
1316 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001317
1318 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001319 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001320}
1321
Willy Tarreau757478e2016-11-03 19:22:19 +01001322/* principle: propagate maint and drain to tracking servers. This is useful
1323 * upon startup so that inherited states are correct.
1324 */
1325static void srv_propagate_admin_state(struct server *srv)
1326{
1327 struct server *srv2;
1328
1329 if (!srv->trackers)
1330 return;
1331
1332 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
1333 if (srv->admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001334 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001335
1336 if (srv->admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001337 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001338 }
1339}
1340
1341/* Compute and propagate the admin states for all servers in proxy <px>.
1342 * Only servers *not* tracking another one are considered, because other
1343 * ones will be handled when the server they track is visited.
1344 */
1345void srv_compute_all_admin_states(struct proxy *px)
1346{
1347 struct server *srv;
1348
1349 for (srv = px->srv; srv; srv = srv->next) {
1350 if (srv->track)
1351 continue;
1352 srv_propagate_admin_state(srv);
1353 }
1354}
1355
Willy Tarreaudff55432012-10-10 17:51:05 +02001356/* Note: must not be declared <const> as its list will be overwritten.
1357 * Please take care of keeping this list alphabetically sorted, doing so helps
1358 * all code contributors.
1359 * Optional keywords are also declared with a NULL ->parse() function so that
1360 * the config parser can report an appropriate error when a known keyword was
1361 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001362 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001363 */
1364static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001365 { "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 +01001366 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001367 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001368 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001369 { "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 +01001370 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001371 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1372 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001373 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001374 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001375 { "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 +01001376 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001377 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001378 { "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 +01001379 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1380 { "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 +01001381 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001382 { "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 +01001383 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001384 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1385 { "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 +02001386 { "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 +01001387 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001388 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001389 { NULL, NULL, 0 },
1390}};
1391
1392__attribute__((constructor))
1393static void __listener_init(void)
1394{
1395 srv_register_keywords(&srv_kws);
1396}
1397
Willy Tarreau004e0452013-11-21 11:22:01 +01001398/* Recomputes the server's eweight based on its state, uweight, the current time,
1399 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1400 * state is automatically disabled if the time is elapsed.
1401 */
1402void server_recalc_eweight(struct server *sv)
1403{
1404 struct proxy *px = sv->proxy;
1405 unsigned w;
1406
1407 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1408 /* go to full throttle if the slowstart interval is reached */
Willy Tarreau892337c2014-05-13 23:41:20 +02001409 if (sv->state == SRV_ST_STARTING)
1410 sv->state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001411 }
1412
1413 /* We must take care of not pushing the server to full throttle during slow starts.
1414 * It must also start immediately, at least at the minimal step when leaving maintenance.
1415 */
Willy Tarreau892337c2014-05-13 23:41:20 +02001416 if ((sv->state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001417 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1418 else
1419 w = px->lbprm.wdiv;
1420
1421 sv->eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
1422
1423 /* now propagate the status change to any LB algorithms */
1424 if (px->lbprm.update_server_eweight)
1425 px->lbprm.update_server_eweight(sv);
Willy Tarreau9943d312014-05-22 16:20:59 +02001426 else if (srv_is_usable(sv)) {
Willy Tarreau004e0452013-11-21 11:22:01 +01001427 if (px->lbprm.set_server_status_up)
1428 px->lbprm.set_server_status_up(sv);
1429 }
1430 else {
1431 if (px->lbprm.set_server_status_down)
1432 px->lbprm.set_server_status_down(sv);
1433 }
1434}
1435
Willy Tarreaubaaee002006-06-26 02:48:02 +02001436/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001437 * Parses weight_str and configures sv accordingly.
1438 * Returns NULL on success, error message string otherwise.
1439 */
1440const char *server_parse_weight_change_request(struct server *sv,
1441 const char *weight_str)
1442{
1443 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001444 long int w;
1445 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001446
1447 px = sv->proxy;
1448
1449 /* if the weight is terminated with '%', it is set relative to
1450 * the initial weight, otherwise it is absolute.
1451 */
1452 if (!*weight_str)
1453 return "Require <weight> or <weight%>.\n";
1454
Simon Hormanb796afa2013-02-12 10:45:53 +09001455 w = strtol(weight_str, &end, 10);
1456 if (end == weight_str)
1457 return "Empty weight string empty or preceded by garbage";
1458 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001459 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001460 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001461 /* Avoid integer overflow */
1462 if (w > 25600)
1463 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001464 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001465 if (w > 256)
1466 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001467 }
1468 else if (w < 0 || w > 256)
1469 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001470 else if (end[0] != '\0')
1471 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001472
1473 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1474 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1475
1476 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001477 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001478
1479 return NULL;
1480}
1481
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001482/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001483 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1484 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001485 * Returns:
1486 * - error string on error
1487 * - NULL on success
1488 */
1489const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001490 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001491{
1492 unsigned char ip[INET6_ADDRSTRLEN];
1493
1494 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001495 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001496 return NULL;
1497 }
1498 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001499 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001500 return NULL;
1501 }
1502
1503 return "Could not understand IP address format.\n";
1504}
1505
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001506const char *server_parse_maxconn_change_request(struct server *sv,
1507 const char *maxconn_str)
1508{
1509 long int v;
1510 char *end;
1511
1512 if (!*maxconn_str)
1513 return "Require <maxconn>.\n";
1514
1515 v = strtol(maxconn_str, &end, 10);
1516 if (end == maxconn_str)
1517 return "maxconn string empty or preceded by garbage";
1518 else if (end[0] != '\0')
1519 return "Trailing garbage in maxconn string";
1520
1521 if (sv->maxconn == sv->minconn) { // static maxconn
1522 sv->maxconn = sv->minconn = v;
1523 } else { // dynamic maxconn
1524 sv->maxconn = v;
1525 }
1526
1527 if (may_dequeue_tasks(sv, sv->proxy))
1528 process_srv_queue(sv);
1529
1530 return NULL;
1531}
1532
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001533#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001534static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1535 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001536{
1537 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001538 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001539 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001540 NULL,
1541 };
1542
1543 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001544 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001545
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001546 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1547}
1548
1549static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1550{
1551 struct sample_expr *expr;
1552
1553 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 +01001554 if (!expr) {
1555 memprintf(err, "error detected while parsing sni expression : %s", *err);
1556 return ERR_ALERT | ERR_FATAL;
1557 }
1558
1559 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1560 memprintf(err, "error detected while parsing sni expression : "
1561 " fetch method '%s' extracts information from '%s', "
1562 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001563 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001564 return ERR_ALERT | ERR_FATAL;
1565 }
1566
1567 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1568 release_sample_expr(newsrv->ssl_ctx.sni);
1569 newsrv->ssl_ctx.sni = expr;
1570
1571 return 0;
1572}
1573#endif
1574
1575static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1576{
1577 if (err && *err) {
1578 indent_msg(err, 2);
1579 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1580 }
1581 else
1582 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1583 file, linenum, args[0], args[1], args[cur_arg]);
1584}
1585
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001586static void srv_conn_src_sport_range_cpy(struct server *srv,
1587 struct server *src)
1588{
1589 int range_sz;
1590
1591 range_sz = src->conn_src.sport_range->size;
1592 if (range_sz > 0) {
1593 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1594 if (srv->conn_src.sport_range != NULL) {
1595 int i;
1596
1597 for (i = 0; i < range_sz; i++) {
1598 srv->conn_src.sport_range->ports[i] =
1599 src->conn_src.sport_range->ports[i];
1600 }
1601 }
1602 }
1603}
1604
1605/*
1606 * Copy <src> server connection source settings to <srv> server everything needed.
1607 */
1608static void srv_conn_src_cpy(struct server *srv, struct server *src)
1609{
1610 srv->conn_src.opts = src->conn_src.opts;
1611 srv->conn_src.source_addr = src->conn_src.source_addr;
1612
1613 /* Source port range copy. */
1614 if (src->conn_src.sport_range != NULL)
1615 srv_conn_src_sport_range_cpy(srv, src);
1616
1617#ifdef CONFIG_HAP_TRANSPARENT
1618 if (src->conn_src.bind_hdr_name != NULL) {
1619 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1620 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1621 }
1622 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1623 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1624#endif
1625 if (src->conn_src.iface_name != NULL)
1626 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1627}
1628
1629/*
1630 * Copy <src> server SSL settings to <srv> server allocating
1631 * everything needed.
1632 */
1633#if defined(USE_OPENSSL)
1634static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1635{
1636 if (src->ssl_ctx.ca_file != NULL)
1637 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1638 if (src->ssl_ctx.crl_file != NULL)
1639 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1640 if (src->ssl_ctx.client_crt != NULL)
1641 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1642
1643 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1644
1645 if (src->ssl_ctx.verify_host != NULL)
1646 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1647 if (src->ssl_ctx.ciphers != NULL)
1648 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1649 if (src->sni_expr != NULL)
1650 srv->sni_expr = strdup(src->sni_expr);
1651}
1652#endif
1653
1654/*
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001655 * Allocate <srv> server dns resolution.
1656 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001657 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001658 */
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001659static int srv_alloc_dns_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001660{
1661 char *hostname_dn;
1662 int hostname_dn_len;
1663 struct dns_resolution *dst_dns_rslt;
1664
1665 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001666 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001667
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001668 free(srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001669 srv->hostname = strdup(hostname);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001670 dst_dns_rslt = dns_alloc_resolution();
1671
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001672 hostname_dn_len = dns_str_to_dn_label_len(hostname);
1673 hostname_dn = calloc(hostname_dn_len + 1, sizeof(char));
1674
1675 if (!srv->hostname || !dst_dns_rslt || !hostname_dn)
1676 goto err;
1677
1678 srv->resolution = dst_dns_rslt;
1679 srv->resolution->hostname_dn = hostname_dn;
1680 srv->resolution->hostname_dn_len = hostname_dn_len;
1681
1682 if (!dns_str_to_dn_label(srv->hostname,
1683 srv->resolution->hostname_dn,
1684 srv->resolution->hostname_dn_len + 1))
1685 goto err;
1686
1687 srv->resolution->requester = srv;
1688 srv->resolution->requester_cb = snr_resolution_cb;
1689 srv->resolution->requester_error_cb = snr_resolution_error_cb;
1690 srv->resolution->status = RSLV_STATUS_NONE;
1691 srv->resolution->step = RSLV_STEP_NONE;
1692 /* a first resolution has been done by the configuration parser */
1693 srv->resolution->last_resolution = 0;
1694
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001695 if (srv->resolvers_id) {
1696 struct dns_resolvers *curr_resolvers;
1697 int found = 0;
1698
1699 list_for_each_entry(curr_resolvers, &dns_resolvers, list) {
1700 if (!strcmp(curr_resolvers->id, srv->resolvers_id)) {
1701 found = 1;
1702 break;
1703 }
1704 }
1705 if (!found)
1706 goto err;
1707 srv->resolution->resolvers = curr_resolvers;
1708 }
1709 else
1710 goto err;
1711
1712 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001713
1714 err:
1715 free(srv->hostname);
1716 srv->hostname = NULL;
1717 free(hostname_dn);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001718 dns_free_resolution(dst_dns_rslt);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001719 return -1;
1720}
1721
1722static void srv_free_dns_resolution(struct server *srv)
1723{
1724 if (!srv->resolution)
1725 return;
1726
1727 free(srv->resolution->hostname_dn);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001728 dns_free_resolution(srv->resolution);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001729 srv->resolution = NULL;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001730}
1731
1732/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001733 * Copy <src> server settings to <srv> server allocating
1734 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001735 * This function is not supposed to be called at any time, but only
1736 * during server settings parsing or during server allocations from
1737 * a server template, and just after having calloc()'ed a new server.
1738 * So, <src> may only be a default server (when parsing server settings)
1739 * or a server template (during server allocations from a server template).
1740 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1741 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001742 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001743static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001744{
1745 /* Connection source settings copy */
1746 srv_conn_src_cpy(srv, src);
1747
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001748 if (srv_tmpl) {
1749 srv->addr = src->addr;
1750 srv->svc_port = src->svc_port;
1751 }
1752
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001753 srv->pp_opts = src->pp_opts;
1754 if (src->rdr_pfx != NULL) {
1755 srv->rdr_pfx = strdup(src->rdr_pfx);
1756 srv->rdr_len = src->rdr_len;
1757 }
1758 if (src->cookie != NULL) {
1759 srv->cookie = strdup(src->cookie);
1760 srv->cklen = src->cklen;
1761 }
1762 srv->use_ssl = src->use_ssl;
1763 srv->check.addr = srv->agent.addr = src->check.addr;
1764 srv->check.use_ssl = src->check.use_ssl;
1765 srv->check.port = src->check.port;
1766 /* Note: 'flags' field has potentially been already initialized. */
1767 srv->flags |= src->flags;
1768 srv->do_check = src->do_check;
1769 srv->do_agent = src->do_agent;
1770 if (srv->check.port)
1771 srv->flags |= SRV_F_CHECKPORT;
1772 srv->check.inter = src->check.inter;
1773 srv->check.fastinter = src->check.fastinter;
1774 srv->check.downinter = src->check.downinter;
1775 srv->agent.use_ssl = src->agent.use_ssl;
1776 srv->agent.port = src->agent.port;
1777 if (src->agent.send_string != NULL)
1778 srv->agent.send_string = strdup(src->agent.send_string);
1779 srv->agent.send_string_len = src->agent.send_string_len;
1780 srv->agent.inter = src->agent.inter;
1781 srv->agent.fastinter = src->agent.fastinter;
1782 srv->agent.downinter = src->agent.downinter;
1783 srv->maxqueue = src->maxqueue;
1784 srv->minconn = src->minconn;
1785 srv->maxconn = src->maxconn;
1786 srv->slowstart = src->slowstart;
1787 srv->observe = src->observe;
1788 srv->onerror = src->onerror;
1789 srv->onmarkeddown = src->onmarkeddown;
1790 srv->onmarkedup = src->onmarkedup;
1791 if (src->trackit != NULL)
1792 srv->trackit = strdup(src->trackit);
1793 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1794 srv->uweight = srv->iweight = src->iweight;
1795
1796 srv->check.send_proxy = src->check.send_proxy;
1797 /* health: up, but will fall down at first failure */
1798 srv->check.rise = srv->check.health = src->check.rise;
1799 srv->check.fall = src->check.fall;
1800
1801 /* Here we check if 'disabled' is the default server state */
1802 if (src->admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1803 srv->admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1804 srv->state = SRV_ST_STOPPED;
1805 srv->check.state |= CHK_ST_PAUSED;
1806 srv->check.health = 0;
1807 }
1808
1809 /* health: up but will fall down at first failure */
1810 srv->agent.rise = srv->agent.health = src->agent.rise;
1811 srv->agent.fall = src->agent.fall;
1812
1813 if (src->resolvers_id != NULL)
1814 srv->resolvers_id = strdup(src->resolvers_id);
1815 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1816 if (srv->dns_opts.family_prio == AF_UNSPEC)
1817 srv->dns_opts.family_prio = AF_INET6;
1818 memcpy(srv->dns_opts.pref_net,
1819 src->dns_opts.pref_net,
1820 sizeof srv->dns_opts.pref_net);
1821 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1822
1823 srv->init_addr_methods = src->init_addr_methods;
1824 srv->init_addr = src->init_addr;
1825#if defined(USE_OPENSSL)
1826 srv_ssl_settings_cpy(srv, src);
1827#endif
1828#ifdef TCP_USER_TIMEOUT
1829 srv->tcp_ut = src->tcp_ut;
1830#endif
1831}
1832
1833static struct server *new_server(struct proxy *proxy)
1834{
1835 struct server *srv;
1836
1837 srv = calloc(1, sizeof *srv);
1838 if (!srv)
1839 return NULL;
1840
1841 srv->obj_type = OBJ_TYPE_SERVER;
1842 srv->proxy = proxy;
1843 LIST_INIT(&srv->actconns);
1844 LIST_INIT(&srv->pendconns);
1845 LIST_INIT(&srv->priv_conns);
1846 LIST_INIT(&srv->idle_conns);
1847 LIST_INIT(&srv->safe_conns);
1848
1849 srv->state = SRV_ST_RUNNING; /* early server setup */
1850 srv->last_change = now.tv_sec;
1851
1852 srv->check.status = HCHK_STATUS_INI;
1853 srv->check.server = srv;
1854 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1855
1856 srv->agent.status = HCHK_STATUS_INI;
1857 srv->agent.server = srv;
1858 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1859
1860 return srv;
1861}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001862
1863/*
1864 * Validate <srv> server health-check settings.
1865 * Returns 0 if everything is OK, -1 if not.
1866 */
1867static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1868{
1869 struct tcpcheck_rule *r = NULL;
1870 struct list *l;
1871
1872 /*
1873 * We need at least a service port, a check port or the first tcp-check rule must
1874 * be a 'connect' one when checking an IPv4/IPv6 server.
1875 */
1876 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1877 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1878 return 0;
1879
1880 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1881 if (!r) {
1882 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1883 "Check has been disabled.\n",
1884 file, linenum, srv->id);
1885 return -1;
1886 }
1887
1888 /* search the first action (connect / send / expect) in the list */
1889 l = &srv->proxy->tcpcheck_rules;
1890 list_for_each_entry(r, l, list) {
1891 if (r->action != TCPCHK_ACT_COMMENT)
1892 break;
1893 }
1894
1895 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1896 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1897 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1898 file, linenum, srv->id);
1899 return -1;
1900 }
1901
1902 /* scan the tcp-check ruleset to ensure a port has been configured */
1903 l = &srv->proxy->tcpcheck_rules;
1904 list_for_each_entry(r, l, list) {
1905 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1906 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1907 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1908 file, linenum, srv->id);
1909 return -1;
1910 }
1911 }
1912
1913 return 0;
1914}
1915
1916/*
1917 * Initialize <srv> health-check structure.
1918 * Returns the error string in case of memory allocation failure, NULL if not.
1919 */
1920static const char *do_health_check_init(struct server *srv, int check_type, int state)
1921{
1922 const char *ret;
1923
1924 if (!srv->do_check)
1925 return NULL;
1926
1927 ret = init_check(&srv->check, check_type);
1928 if (ret)
1929 return ret;
1930
1931 if (srv->resolution)
1932 srv->resolution->opts = &srv->dns_opts;
1933
1934 srv->check.state |= state;
1935 global.maxsock++;
1936
1937 return NULL;
1938}
1939
1940static int server_health_check_init(const char *file, int linenum,
1941 struct server *srv, struct proxy *curproxy)
1942{
1943 const char *ret;
1944
1945 if (!srv->do_check)
1946 return 0;
1947
1948 if (srv->trackit) {
1949 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1950 file, linenum);
1951 return ERR_ALERT | ERR_FATAL;
1952 }
1953
1954 if (server_healthcheck_validate(file, linenum, srv) < 0)
1955 return ERR_ALERT | ERR_ABORT;
1956
1957 /* note: check type will be set during the config review phase */
1958 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1959 if (ret) {
1960 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1961 return ERR_ALERT | ERR_ABORT;
1962 }
1963
1964 return 0;
1965}
1966
1967/*
1968 * Initialize <srv> agent check structure.
1969 * Returns the error string in case of memory allocation failure, NULL if not.
1970 */
1971static const char *do_server_agent_check_init(struct server *srv, int state)
1972{
1973 const char *ret;
1974
1975 if (!srv->do_agent)
1976 return NULL;
1977
1978 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1979 if (ret)
1980 return ret;
1981
1982 if (!srv->agent.inter)
1983 srv->agent.inter = srv->check.inter;
1984
1985 srv->agent.state |= state;
1986 global.maxsock++;
1987
1988 return NULL;
1989}
1990
1991static int server_agent_check_init(const char *file, int linenum,
1992 struct server *srv, struct proxy *curproxy)
1993{
1994 const char *ret;
1995
1996 if (!srv->do_agent)
1997 return 0;
1998
1999 if (!srv->agent.port) {
2000 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
2001 file, linenum, srv->id);
2002 return ERR_ALERT | ERR_FATAL;
2003 }
2004
2005 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
2006 if (ret) {
2007 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2008 return ERR_ALERT | ERR_ABORT;
2009 }
2010
2011 return 0;
2012}
2013
2014#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2015static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
2016 struct server *srv, struct proxy *proxy)
2017{
2018 int ret;
2019 char *err = NULL;
2020
2021 if (!srv->sni_expr)
2022 return 0;
2023
2024 ret = server_parse_sni_expr(srv, proxy, &err);
2025 if (!ret)
2026 return 0;
2027
2028 display_parser_err(file, linenum, args, cur_arg, &err);
2029 free(err);
2030
2031 return ret;
2032}
2033#endif
2034
2035/*
2036 * Server initializations finalization.
2037 * Initialize health check, agent check and SNI expression if enabled.
2038 * Must not be called for a default server instance.
2039 */
2040static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2041 struct server *srv, struct proxy *px)
2042{
2043 int ret;
2044
2045 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2046 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2047 return ret;
2048 }
2049
2050#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2051 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2052 return ret;
2053#endif
2054
2055 if (srv->flags & SRV_F_BACKUP)
2056 px->srv_bck++;
2057 else
2058 px->srv_act++;
2059 srv_lb_commit_status(srv);
2060
2061 return 0;
2062}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002063
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002064/*
2065 * Parse as much as possible such a range string argument: low[-high]
2066 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2067 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2068 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2069 * Returns 0 if succeeded, -1 if not.
2070 */
2071static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2072{
2073 char *nb_high_arg;
2074
2075 *nb_high = 0;
2076 chunk_printf(&trash, "%s", arg);
2077 *nb_low = atoi(trash.str);
2078
2079 if ((nb_high_arg = strchr(trash.str, '-'))) {
2080 *nb_high_arg++ = '\0';
2081 *nb_high = atoi(nb_high_arg);
2082 }
2083 else {
2084 *nb_high += *nb_low;
2085 *nb_low = 1;
2086 }
2087
2088 if (*nb_low < 0 || *nb_high < *nb_low)
2089 return -1;
2090
2091 return 0;
2092}
2093
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002094static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2095{
2096 chunk_printf(&trash, "%s%d", prefix, nb);
2097 free(srv->id);
2098 srv->id = strdup(trash.str);
2099}
2100
2101/*
2102 * Initialize as much as possible servers from <srv> server template.
2103 * Note that a server template is a special server with
2104 * a few different parameters than a server which has
2105 * been parsed mostly the same way as a server.
2106 * Returns the number of servers succesfully allocated,
2107 * 'srv' template included.
2108 */
2109static int server_template_init(struct server *srv, struct proxy *px)
2110{
2111 int i;
2112 struct server *newsrv;
2113
2114 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2115 int check_init_state;
2116 int agent_init_state;
2117
2118 newsrv = new_server(px);
2119 if (!newsrv)
2120 goto err;
2121
2122 srv_settings_cpy(newsrv, srv, 1);
2123 srv_alloc_dns_resolution(newsrv, srv->hostname);
2124#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2125 if (newsrv->sni_expr) {
2126 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2127 if (!newsrv->ssl_ctx.sni)
2128 goto err;
2129 }
2130#endif
2131 /* Set this new server ID. */
2132 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2133
2134 /* Initial checks states. */
2135 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2136 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2137
2138 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2139 do_server_agent_check_init(newsrv, agent_init_state))
2140 goto err;
2141
2142 /* Linked backwards first. This will be restablished after parsing. */
2143 newsrv->next = px->srv;
2144 px->srv = newsrv;
2145 }
2146 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2147
2148 return i - srv->tmpl_info.nb_low;
2149
2150 err:
2151 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2152 if (newsrv) {
2153#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2154 release_sample_expr(newsrv->ssl_ctx.sni);
2155#endif
2156 free_check(&newsrv->agent);
2157 free_check(&newsrv->check);
2158 }
2159 free(newsrv);
2160 return i - srv->tmpl_info.nb_low;
2161}
2162
Willy Tarreau272adea2014-03-31 10:39:59 +02002163int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
2164{
2165 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002166 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002167 char *errmsg = NULL;
2168 int err_code = 0;
2169 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002170 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002171
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002172 if (!strcmp(args[0], "server") ||
2173 !strcmp(args[0], "default-server") ||
2174 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002175 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002176 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002177 int srv = !defsrv && !strcmp(args[0], "server");
2178 int srv_tmpl = !defsrv && !srv;
2179 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002180
2181 if (!defsrv && curproxy == defproxy) {
2182 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
2183 err_code |= ERR_ALERT | ERR_FATAL;
2184 goto out;
2185 }
2186 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
2187 err_code |= ERR_ALERT | ERR_FATAL;
2188
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002189 /* There is no mandatory first arguments for default server. */
2190 if (srv) {
2191 if (!*args[2]) {
2192 /* 'server' line number of argument check. */
2193 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
2194 file, linenum, args[0]);
2195 err_code |= ERR_ALERT | ERR_FATAL;
2196 goto out;
2197 }
2198
2199 err = invalid_char(args[1]);
2200 }
2201 else if (srv_tmpl) {
2202 if (!*args[3]) {
2203 /* 'server-template' line number of argument check. */
2204 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2205 file, linenum, args[0]);
2206 err_code |= ERR_ALERT | ERR_FATAL;
2207 goto out;
2208 }
2209
2210 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002211 }
2212
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002213 if (err) {
2214 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
2215 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002216 err_code |= ERR_ALERT | ERR_FATAL;
2217 goto out;
2218 }
2219
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002220 cur_arg = 2;
2221 if (srv_tmpl) {
2222 /* Parse server-template <nb | range> arg. */
2223 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
2224 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
2225 file, linenum, args[0], args[cur_arg]);
2226 err_code |= ERR_ALERT | ERR_FATAL;
2227 goto out;
2228 }
2229 cur_arg++;
2230 }
2231
Willy Tarreau272adea2014-03-31 10:39:59 +02002232 if (!defsrv) {
2233 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002234 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002235 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002236 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +02002237
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002238 newsrv = new_server(curproxy);
2239 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002240 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2241 err_code |= ERR_ALERT | ERR_ABORT;
2242 goto out;
2243 }
2244
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002245 if (srv_tmpl) {
2246 newsrv->tmpl_info.nb_low = tmpl_range_low;
2247 newsrv->tmpl_info.nb_high = tmpl_range_high;
2248 }
2249
Willy Tarreau272adea2014-03-31 10:39:59 +02002250 /* the servers are linked backwards first */
2251 newsrv->next = curproxy->srv;
2252 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002253 newsrv->conf.file = strdup(file);
2254 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002255 /* Note: for a server template, its id is its prefix.
2256 * This is a temporary id which will be used for server allocations to come
2257 * after parsing.
2258 */
2259 if (srv)
2260 newsrv->id = strdup(args[1]);
2261 else
2262 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002263
2264 /* several ways to check the port component :
2265 * - IP => port=+0, relative (IPv4 only)
2266 * - IP: => port=+0, relative
2267 * - IP:N => port=N, absolute
2268 * - IP:+N => port=+N, relative
2269 * - IP:-N => port=-N, relative
2270 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002271 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002272 if (!sk) {
2273 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
2274 err_code |= ERR_ALERT | ERR_FATAL;
2275 goto out;
2276 }
2277
2278 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002279 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002280 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
2281 file, linenum, args[0], args[1]);
2282 err_code |= ERR_ALERT | ERR_FATAL;
2283 goto out;
2284 }
2285
2286 if (!port1 || !port2) {
2287 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002288 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002289 }
2290 else if (port1 != port2) {
2291 /* port range */
2292 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
2293 file, linenum, args[0], args[1], args[2]);
2294 err_code |= ERR_ALERT | ERR_FATAL;
2295 goto out;
2296 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002297
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002298 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +02002299 newsrv->hostname = fqdn;
2300 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002301 goto skip_name_resolution;
2302
Willy Tarreau07101d52015-09-08 16:16:35 +02002303 fqdn = NULL;
Vincent Bernat02779b62016-04-03 13:48:43 +02002304 if ((curr_resolution = calloc(1, sizeof(*curr_resolution))) == NULL)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002305 goto skip_name_resolution;
2306
2307 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
2308 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
2309 goto skip_name_resolution;
2310 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
2311 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002312 file, linenum, args[cur_arg]);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002313 err_code |= ERR_ALERT | ERR_FATAL;
2314 goto out;
2315 }
2316
2317 curr_resolution->requester = newsrv;
2318 curr_resolution->requester_cb = snr_resolution_cb;
2319 curr_resolution->requester_error_cb = snr_resolution_error_cb;
2320 curr_resolution->status = RSLV_STATUS_NONE;
2321 curr_resolution->step = RSLV_STEP_NONE;
2322 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +02002323 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002324 newsrv->resolution = curr_resolution;
2325
2326 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +02002327 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002328 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002329
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002330 if (!newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002331 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002332 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002333 err_code |= ERR_ALERT | ERR_FATAL;
2334 goto out;
2335 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002336
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002337 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002338 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002339 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002340 } else {
2341 newsrv = &curproxy->defsrv;
2342 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002343 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002344 }
2345
2346 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002347 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002348 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2349 if (err) {
2350 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2351 file, linenum, *err, newsrv->id);
2352 err_code |= ERR_ALERT | ERR_FATAL;
2353 goto out;
2354 }
2355 if (val <= 0) {
2356 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2357 file, linenum, val, args[cur_arg], newsrv->id);
2358 err_code |= ERR_ALERT | ERR_FATAL;
2359 goto out;
2360 }
2361 newsrv->agent.inter = val;
2362 cur_arg += 2;
2363 }
Misiekea849332017-01-09 09:39:51 +01002364 else if (!strcmp(args[cur_arg], "agent-addr")) {
2365 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2366 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2367 goto out;
2368 }
2369
2370 cur_arg += 2;
2371 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002372 else if (!strcmp(args[cur_arg], "agent-port")) {
2373 global.maxsock++;
2374 newsrv->agent.port = atol(args[cur_arg + 1]);
2375 cur_arg += 2;
2376 }
James Brown55f9ff12015-10-21 18:19:05 -07002377 else if (!strcmp(args[cur_arg], "agent-send")) {
2378 global.maxsock++;
2379 free(newsrv->agent.send_string);
2380 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2381 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2382 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2383 cur_arg += 2;
2384 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002385 else if (!strcmp(args[cur_arg], "init-addr")) {
2386 char *p, *end;
2387 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002388 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002389
2390 newsrv->init_addr_methods = 0;
2391 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2392
2393 for (p = args[cur_arg + 1]; *p; p = end) {
2394 /* cut on next comma */
2395 for (end = p; *end && *end != ','; end++);
2396 if (*end)
2397 *(end++) = 0;
2398
Willy Tarreau4310d362016-11-02 15:05:56 +01002399 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002400 if (!strcmp(p, "libc")) {
2401 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2402 }
2403 else if (!strcmp(p, "last")) {
2404 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2405 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002406 else if (!strcmp(p, "none")) {
2407 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2408 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002409 else if (str2ip2(p, &sa, 0)) {
2410 if (is_addr(&newsrv->init_addr)) {
2411 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2412 file, linenum, args[cur_arg], p);
2413 err_code |= ERR_ALERT | ERR_FATAL;
2414 goto out;
2415 }
2416 newsrv->init_addr = sa;
2417 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2418 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002419 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002420 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002421 file, linenum, args[cur_arg], p);
2422 err_code |= ERR_ALERT | ERR_FATAL;
2423 goto out;
2424 }
2425 if (!done) {
2426 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2427 file, linenum, args[cur_arg], p);
2428 err_code |= ERR_ALERT | ERR_FATAL;
2429 goto out;
2430 }
2431 }
2432 cur_arg += 2;
2433 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002434 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002435 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002436 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2437 cur_arg += 2;
2438 }
2439 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2440 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002441 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002442 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002443 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002444 else {
2445 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2446 file, linenum, args[cur_arg]);
2447 err_code |= ERR_ALERT | ERR_FATAL;
2448 goto out;
2449 }
2450 cur_arg += 2;
2451 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002452 else if (!strcmp(args[cur_arg], "resolve-net")) {
2453 char *p, *e;
2454 unsigned char mask;
2455 struct dns_options *opt;
2456
2457 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
2458 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2459 file, linenum, args[cur_arg]);
2460 err_code |= ERR_ALERT | ERR_FATAL;
2461 goto out;
2462 }
2463
2464 opt = &newsrv->dns_opts;
2465
2466 /* Split arguments by comma, and convert it from ipv4 or ipv6
2467 * string network in in_addr or in6_addr.
2468 */
2469 p = args[cur_arg + 1];
2470 e = p;
2471 while (*p != '\0') {
2472 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002473 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002474 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2475 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2476 err_code |= ERR_ALERT | ERR_FATAL;
2477 goto out;
2478 }
2479 /* look for end or comma. */
2480 while (*e != ',' && *e != '\0')
2481 e++;
2482 if (*e == ',') {
2483 *e = '\0';
2484 e++;
2485 }
2486 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2487 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2488 /* Try to convert input string from ipv4 or ipv6 network. */
2489 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2490 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2491 &mask)) {
2492 /* Try to convert input string from ipv6 network. */
2493 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2494 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2495 } else {
2496 /* All network conversions fail, retrun error. */
2497 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2498 file, linenum, args[cur_arg], p);
2499 err_code |= ERR_ALERT | ERR_FATAL;
2500 goto out;
2501 }
2502 opt->pref_net_nb++;
2503 p = e;
2504 }
2505
2506 cur_arg += 2;
2507 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002508 else if (!strcmp(args[cur_arg], "rise")) {
2509 if (!*args[cur_arg + 1]) {
2510 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2511 file, linenum, args[cur_arg]);
2512 err_code |= ERR_ALERT | ERR_FATAL;
2513 goto out;
2514 }
2515
2516 newsrv->check.rise = atol(args[cur_arg + 1]);
2517 if (newsrv->check.rise <= 0) {
2518 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2519 file, linenum, args[cur_arg]);
2520 err_code |= ERR_ALERT | ERR_FATAL;
2521 goto out;
2522 }
2523
2524 if (newsrv->check.health)
2525 newsrv->check.health = newsrv->check.rise;
2526 cur_arg += 2;
2527 }
2528 else if (!strcmp(args[cur_arg], "fall")) {
2529 newsrv->check.fall = atol(args[cur_arg + 1]);
2530
2531 if (!*args[cur_arg + 1]) {
2532 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2533 file, linenum, args[cur_arg]);
2534 err_code |= ERR_ALERT | ERR_FATAL;
2535 goto out;
2536 }
2537
2538 if (newsrv->check.fall <= 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 cur_arg += 2;
2546 }
2547 else if (!strcmp(args[cur_arg], "inter")) {
2548 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2549 if (err) {
2550 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2551 file, linenum, *err, newsrv->id);
2552 err_code |= ERR_ALERT | ERR_FATAL;
2553 goto out;
2554 }
2555 if (val <= 0) {
2556 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2557 file, linenum, val, args[cur_arg], newsrv->id);
2558 err_code |= ERR_ALERT | ERR_FATAL;
2559 goto out;
2560 }
2561 newsrv->check.inter = val;
2562 cur_arg += 2;
2563 }
2564 else if (!strcmp(args[cur_arg], "fastinter")) {
2565 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2566 if (err) {
2567 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2568 file, linenum, *err, newsrv->id);
2569 err_code |= ERR_ALERT | ERR_FATAL;
2570 goto out;
2571 }
2572 if (val <= 0) {
2573 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2574 file, linenum, val, args[cur_arg], newsrv->id);
2575 err_code |= ERR_ALERT | ERR_FATAL;
2576 goto out;
2577 }
2578 newsrv->check.fastinter = val;
2579 cur_arg += 2;
2580 }
2581 else if (!strcmp(args[cur_arg], "downinter")) {
2582 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2583 if (err) {
2584 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2585 file, linenum, *err, newsrv->id);
2586 err_code |= ERR_ALERT | ERR_FATAL;
2587 goto out;
2588 }
2589 if (val <= 0) {
2590 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2591 file, linenum, val, args[cur_arg], newsrv->id);
2592 err_code |= ERR_ALERT | ERR_FATAL;
2593 goto out;
2594 }
2595 newsrv->check.downinter = val;
2596 cur_arg += 2;
2597 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002598 else if (!strcmp(args[cur_arg], "port")) {
2599 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002600 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002601 cur_arg += 2;
2602 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002603 else if (!strcmp(args[cur_arg], "weight")) {
2604 int w;
2605 w = atol(args[cur_arg + 1]);
2606 if (w < 0 || w > SRV_UWGHT_MAX) {
2607 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2608 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2609 err_code |= ERR_ALERT | ERR_FATAL;
2610 goto out;
2611 }
2612 newsrv->uweight = newsrv->iweight = w;
2613 cur_arg += 2;
2614 }
2615 else if (!strcmp(args[cur_arg], "minconn")) {
2616 newsrv->minconn = atol(args[cur_arg + 1]);
2617 cur_arg += 2;
2618 }
2619 else if (!strcmp(args[cur_arg], "maxconn")) {
2620 newsrv->maxconn = atol(args[cur_arg + 1]);
2621 cur_arg += 2;
2622 }
2623 else if (!strcmp(args[cur_arg], "maxqueue")) {
2624 newsrv->maxqueue = atol(args[cur_arg + 1]);
2625 cur_arg += 2;
2626 }
2627 else if (!strcmp(args[cur_arg], "slowstart")) {
2628 /* slowstart is stored in seconds */
2629 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2630 if (err) {
2631 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2632 file, linenum, *err, newsrv->id);
2633 err_code |= ERR_ALERT | ERR_FATAL;
2634 goto out;
2635 }
2636 newsrv->slowstart = (val + 999) / 1000;
2637 cur_arg += 2;
2638 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002639 else if (!strcmp(args[cur_arg], "on-error")) {
2640 if (!strcmp(args[cur_arg + 1], "fastinter"))
2641 newsrv->onerror = HANA_ONERR_FASTINTER;
2642 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2643 newsrv->onerror = HANA_ONERR_FAILCHK;
2644 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2645 newsrv->onerror = HANA_ONERR_SUDDTH;
2646 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2647 newsrv->onerror = HANA_ONERR_MARKDWN;
2648 else {
2649 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2650 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2651 file, linenum, args[cur_arg], args[cur_arg + 1]);
2652 err_code |= ERR_ALERT | ERR_FATAL;
2653 goto out;
2654 }
2655
2656 cur_arg += 2;
2657 }
2658 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2659 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2660 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2661 else {
2662 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2663 file, linenum, args[cur_arg], args[cur_arg + 1]);
2664 err_code |= ERR_ALERT | ERR_FATAL;
2665 goto out;
2666 }
2667
2668 cur_arg += 2;
2669 }
2670 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2671 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2672 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2673 else {
2674 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2675 file, linenum, args[cur_arg], args[cur_arg + 1]);
2676 err_code |= ERR_ALERT | ERR_FATAL;
2677 goto out;
2678 }
2679
2680 cur_arg += 2;
2681 }
2682 else if (!strcmp(args[cur_arg], "error-limit")) {
2683 if (!*args[cur_arg + 1]) {
2684 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2685 file, linenum, args[cur_arg]);
2686 err_code |= ERR_ALERT | ERR_FATAL;
2687 goto out;
2688 }
2689
2690 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2691
2692 if (newsrv->consecutive_errors_limit <= 0) {
2693 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2694 file, linenum, args[cur_arg]);
2695 err_code |= ERR_ALERT | ERR_FATAL;
2696 goto out;
2697 }
2698 cur_arg += 2;
2699 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002700 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002701 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2702 file, linenum, "usesrc", "source");
2703 err_code |= ERR_ALERT | ERR_FATAL;
2704 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002705 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002706 else {
2707 static int srv_dumped;
2708 struct srv_kw *kw;
2709 char *err;
2710
2711 kw = srv_find_kw(args[cur_arg]);
2712 if (kw) {
2713 char *err = NULL;
2714 int code;
2715
2716 if (!kw->parse) {
2717 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2718 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002719 if (kw->skip != -1)
2720 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002721 err_code |= ERR_ALERT | ERR_FATAL;
2722 goto out;
2723 }
2724
2725 if (defsrv && !kw->default_ok) {
2726 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2727 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002728 if (kw->skip != -1)
2729 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002730 err_code |= ERR_ALERT;
2731 continue;
2732 }
2733
2734 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2735 err_code |= code;
2736
2737 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002738 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002739 if (code & ERR_FATAL) {
2740 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002741 if (kw->skip != -1)
2742 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002743 goto out;
2744 }
2745 }
2746 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002747 if (kw->skip != -1)
2748 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002749 continue;
2750 }
2751
2752 err = NULL;
2753 if (!srv_dumped) {
2754 srv_dump_kws(&err);
2755 indent_msg(&err, 4);
2756 srv_dumped = 1;
2757 }
2758
2759 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2760 file, linenum, args[0], args[1], args[cur_arg],
2761 err ? " Registered keywords :" : "", err ? err : "");
2762 free(err);
2763
2764 err_code |= ERR_ALERT | ERR_FATAL;
2765 goto out;
2766 }
2767 }
2768
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002769 if (!defsrv)
2770 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2771 if (err_code & ERR_FATAL)
2772 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002773 if (srv_tmpl)
2774 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002775 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002776 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002777 return 0;
2778
2779 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002780 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002781 free(errmsg);
2782 return err_code;
2783}
2784
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002785/* Returns a pointer to the first server matching either id <id>.
2786 * NULL is returned if no match is found.
2787 * the lookup is performed in the backend <bk>
2788 */
2789struct server *server_find_by_id(struct proxy *bk, int id)
2790{
2791 struct eb32_node *eb32;
2792 struct server *curserver;
2793
2794 if (!bk || (id ==0))
2795 return NULL;
2796
2797 /* <bk> has no backend capabilities, so it can't have a server */
2798 if (!(bk->cap & PR_CAP_BE))
2799 return NULL;
2800
2801 curserver = NULL;
2802
2803 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2804 if (eb32)
2805 curserver = container_of(eb32, struct server, conf.id);
2806
2807 return curserver;
2808}
2809
2810/* Returns a pointer to the first server matching either name <name>, or id
2811 * if <name> starts with a '#'. NULL is returned if no match is found.
2812 * the lookup is performed in the backend <bk>
2813 */
2814struct server *server_find_by_name(struct proxy *bk, const char *name)
2815{
2816 struct server *curserver;
2817
2818 if (!bk || !name)
2819 return NULL;
2820
2821 /* <bk> has no backend capabilities, so it can't have a server */
2822 if (!(bk->cap & PR_CAP_BE))
2823 return NULL;
2824
2825 curserver = NULL;
2826 if (*name == '#') {
2827 curserver = server_find_by_id(bk, atoi(name + 1));
2828 if (curserver)
2829 return curserver;
2830 }
2831 else {
2832 curserver = bk->srv;
2833
2834 while (curserver && (strcmp(curserver->id, name) != 0))
2835 curserver = curserver->next;
2836
2837 if (curserver)
2838 return curserver;
2839 }
2840
2841 return NULL;
2842}
2843
2844struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2845{
2846 struct server *byname;
2847 struct server *byid;
2848
2849 if (!name && !id)
2850 return NULL;
2851
2852 if (diff)
2853 *diff = 0;
2854
2855 byname = byid = NULL;
2856
2857 if (name) {
2858 byname = server_find_by_name(bk, name);
2859 if (byname && (!id || byname->puid == id))
2860 return byname;
2861 }
2862
2863 /* remaining possibilities :
2864 * - name not set
2865 * - name set but not found
2866 * - name found but ID doesn't match
2867 */
2868 if (id) {
2869 byid = server_find_by_id(bk, id);
2870 if (byid) {
2871 if (byname) {
2872 /* use id only if forced by configuration */
2873 if (byid->flags & SRV_F_FORCED_ID) {
2874 if (diff)
2875 *diff |= 2;
2876 return byid;
2877 }
2878 else {
2879 if (diff)
2880 *diff |= 1;
2881 return byname;
2882 }
2883 }
2884
2885 /* remaining possibilities:
2886 * - name not set
2887 * - name set but not found
2888 */
2889 if (name && diff)
2890 *diff |= 2;
2891 return byid;
2892 }
2893
2894 /* id bot found */
2895 if (byname) {
2896 if (diff)
2897 *diff |= 1;
2898 return byname;
2899 }
2900 }
2901
2902 return NULL;
2903}
2904
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002905/* Update a server state using the parameters available in the params list */
2906static void srv_update_state(struct server *srv, int version, char **params)
2907{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002908 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002909 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002910
2911 /* fields since version 1
2912 * and common to all other upcoming versions
2913 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002914 enum srv_state srv_op_state;
2915 enum srv_admin srv_admin_state;
2916 unsigned srv_uweight, srv_iweight;
2917 unsigned long srv_last_time_change;
2918 short srv_check_status;
2919 enum chk_result srv_check_result;
2920 int srv_check_health;
2921 int srv_check_state, srv_agent_state;
2922 int bk_f_forced_id;
2923 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002924 int fqdn_set_by_cli;
2925 const char *fqdn;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002926
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002927 fqdn = NULL;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002928 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002929 switch (version) {
2930 case 1:
2931 /*
2932 * now we can proceed with server's state update:
2933 * srv_addr: params[0]
2934 * srv_op_state: params[1]
2935 * srv_admin_state: params[2]
2936 * srv_uweight: params[3]
2937 * srv_iweight: params[4]
2938 * srv_last_time_change: params[5]
2939 * srv_check_status: params[6]
2940 * srv_check_result: params[7]
2941 * srv_check_health: params[8]
2942 * srv_check_state: params[9]
2943 * srv_agent_state: params[10]
2944 * bk_f_forced_id: params[11]
2945 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002946 * srv_fqdn: params[13]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002947 */
2948
2949 /* validating srv_op_state */
2950 p = NULL;
2951 errno = 0;
2952 srv_op_state = strtol(params[1], &p, 10);
2953 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2954 (srv_op_state != SRV_ST_STOPPED &&
2955 srv_op_state != SRV_ST_STARTING &&
2956 srv_op_state != SRV_ST_RUNNING &&
2957 srv_op_state != SRV_ST_STOPPING)) {
2958 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2959 }
2960
2961 /* validating srv_admin_state */
2962 p = NULL;
2963 errno = 0;
2964 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002965 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002966
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002967 /* inherited statuses will be recomputed later.
2968 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2969 */
2970 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002971
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002972 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2973 (srv_admin_state != 0 &&
2974 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002975 srv_admin_state != SRV_ADMF_CMAINT &&
2976 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002977 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002978 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002979 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2980 }
2981
2982 /* validating srv_uweight */
2983 p = NULL;
2984 errno = 0;
2985 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002986 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002987 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2988
2989 /* validating srv_iweight */
2990 p = NULL;
2991 errno = 0;
2992 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002993 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002994 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2995
2996 /* validating srv_last_time_change */
2997 p = NULL;
2998 errno = 0;
2999 srv_last_time_change = strtol(params[5], &p, 10);
3000 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
3001 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
3002
3003 /* validating srv_check_status */
3004 p = NULL;
3005 errno = 0;
3006 srv_check_status = strtol(params[6], &p, 10);
3007 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3008 (srv_check_status >= HCHK_STATUS_SIZE))
3009 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3010
3011 /* validating srv_check_result */
3012 p = NULL;
3013 errno = 0;
3014 srv_check_result = strtol(params[7], &p, 10);
3015 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3016 (srv_check_result != CHK_RES_UNKNOWN &&
3017 srv_check_result != CHK_RES_NEUTRAL &&
3018 srv_check_result != CHK_RES_FAILED &&
3019 srv_check_result != CHK_RES_PASSED &&
3020 srv_check_result != CHK_RES_CONDPASS)) {
3021 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3022 }
3023
3024 /* validating srv_check_health */
3025 p = NULL;
3026 errno = 0;
3027 srv_check_health = strtol(params[8], &p, 10);
3028 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3029 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3030
3031 /* validating srv_check_state */
3032 p = NULL;
3033 errno = 0;
3034 srv_check_state = strtol(params[9], &p, 10);
3035 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3036 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3037 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3038
3039 /* validating srv_agent_state */
3040 p = NULL;
3041 errno = 0;
3042 srv_agent_state = strtol(params[10], &p, 10);
3043 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3044 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3045 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3046
3047 /* validating bk_f_forced_id */
3048 p = NULL;
3049 errno = 0;
3050 bk_f_forced_id = strtol(params[11], &p, 10);
3051 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3052 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3053
3054 /* validating srv_f_forced_id */
3055 p = NULL;
3056 errno = 0;
3057 srv_f_forced_id = strtol(params[12], &p, 10);
3058 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3059 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3060
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003061 /* validating srv_fqdn */
3062 fqdn = params[13];
3063 if (fqdn && *fqdn == '-')
3064 fqdn = NULL;
3065 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3066 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3067 fqdn = NULL;
3068 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003069
3070 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02003071 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003072 goto out;
3073
3074 /* recover operational state and apply it to this server
3075 * and all servers tracking this one */
3076 switch (srv_op_state) {
3077 case SRV_ST_STOPPED:
3078 srv->check.health = 0;
3079 srv_set_stopped(srv, "changed from server-state after a reload");
3080 break;
3081 case SRV_ST_STARTING:
3082 srv->state = srv_op_state;
3083 break;
3084 case SRV_ST_STOPPING:
3085 srv->check.health = srv->check.rise + srv->check.fall - 1;
3086 srv_set_stopping(srv, "changed from server-state after a reload");
3087 break;
3088 case SRV_ST_RUNNING:
3089 srv->check.health = srv->check.rise + srv->check.fall - 1;
3090 srv_set_running(srv, "");
3091 break;
3092 }
3093
3094 /* When applying server state, the following rules apply:
3095 * - in case of a configuration change, we apply the setting from the new
3096 * configuration, regardless of old running state
3097 * - if no configuration change, we apply old running state only if old running
3098 * state is different from new configuration state
3099 */
3100 /* configuration has changed */
3101 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
3102 if (srv->admin & SRV_ADMF_CMAINT)
3103 srv_adm_set_maint(srv);
3104 else
3105 srv_adm_set_ready(srv);
3106 }
3107 /* configuration is the same, let's compate old running state and new conf state */
3108 else {
3109 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
3110 srv_adm_set_maint(srv);
3111 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
3112 srv_adm_set_ready(srv);
3113 }
3114 /* apply drain mode if server is currently enabled */
3115 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
3116 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003117 * (srv->iweight is the weight set up in configuration).
3118 * There are two possible reasons for FDRAIN to have been present :
3119 * - previous config weight was zero
3120 * - "set server b/s drain" was sent to the CLI
3121 *
3122 * In the first case, we simply want to drop this drain state
3123 * if the new weight is not zero anymore, meaning the administrator
3124 * has intentionally turned the weight back to a positive value to
3125 * enable the server again after an operation. In the second case,
3126 * the drain state was forced on the CLI regardless of the config's
3127 * weight so we don't want a change to the config weight to lose this
3128 * status. What this means is :
3129 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3130 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003131 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003132 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003133 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003134 }
3135
3136 srv->last_change = date.tv_sec - srv_last_time_change;
3137 srv->check.status = srv_check_status;
3138 srv->check.result = srv_check_result;
3139 srv->check.health = srv_check_health;
3140
3141 /* Only case we want to apply is removing ENABLED flag which could have been
3142 * done by the "disable health" command over the stats socket
3143 */
3144 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3145 (srv_check_state & CHK_ST_CONFIGURED) &&
3146 !(srv_check_state & CHK_ST_ENABLED))
3147 srv->check.state &= ~CHK_ST_ENABLED;
3148
3149 /* Only case we want to apply is removing ENABLED flag which could have been
3150 * done by the "disable agent" command over the stats socket
3151 */
3152 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3153 (srv_agent_state & CHK_ST_CONFIGURED) &&
3154 !(srv_agent_state & CHK_ST_ENABLED))
3155 srv->agent.state &= ~CHK_ST_ENABLED;
3156
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003157 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3158 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003159 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003160 * It means that a configuration file change has precedence over a unix socket change
3161 * for server's weight
3162 *
3163 * by default, HAProxy applies the following weight when parsing the configuration
3164 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003165 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003166 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003167 srv->uweight = srv_uweight;
3168 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003169 server_recalc_eweight(srv);
3170
Willy Tarreaue5a60682016-11-09 14:54:53 +01003171 /* load server IP address */
3172 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003173
3174 if (fqdn && srv->hostname) {
3175 if (!strcmp(srv->hostname, fqdn)) {
3176 /* Here we reset the 'set from stats socket FQDN' flag
3177 * to support such transitions:
3178 * Let's say initial FQDN value is foo1 (in configuration file).
3179 * - FQDN changed from stats socket, from foo1 to foo2 value,
3180 * - FQDN changed again from file configuration (with the same previous value
3181 set from stats socket, from foo1 to foo2 value),
3182 * - reload for any other reason than a FQDN modification,
3183 * the configuration file FQDN matches the fqdn server state file value.
3184 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
3185 * any futher FQDN modification.
3186 */
3187 srv->admin &= ~SRV_ADMF_HMAINT;
3188 }
3189 else {
3190 /* If the FDQN has been changed from stats socket,
3191 * apply fqdn state file value (which is the value set
3192 * from stats socket).
3193 */
3194 if (fqdn_set_by_cli) {
3195 srv_set_fqdn(srv, fqdn);
3196 srv->admin |= SRV_ADMF_HMAINT;
3197 }
3198 }
3199 }
3200
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003201 break;
3202 default:
3203 chunk_appendf(msg, ", version '%d' not supported", version);
3204 }
3205
3206 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003207 if (msg->len) {
3208 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02003209 Warning("server-state application failed for server '%s/%s'%s",
3210 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003211 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003212}
3213
3214/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3215 * For each proxy, it does the following:
3216 * - opens its server state file (either one or local one)
3217 * - read whole file, line by line
3218 * - analyse each line to check if it matches our current backend:
3219 * - backend name matches
3220 * - backend id matches if id is forced and name doesn't match
3221 * - if the server pointed by the line is found, then state is applied
3222 *
3223 * If the running backend uuid or id differs from the state file, then HAProxy reports
3224 * a warning.
3225 */
3226void apply_server_state(void)
3227{
3228 char *cur, *end;
3229 char mybuf[SRV_STATE_LINE_MAXLEN];
3230 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003231 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3232 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003233 int arg, srv_arg, version, diff;
3234 FILE *f;
3235 char *filepath;
3236 char globalfilepath[MAXPATHLEN + 1];
3237 char localfilepath[MAXPATHLEN + 1];
3238 int len, fileopenerr, globalfilepathlen, localfilepathlen;
3239 extern struct proxy *proxy;
3240 struct proxy *curproxy, *bk;
3241 struct server *srv;
3242
3243 globalfilepathlen = 0;
3244 /* create the globalfilepath variable */
3245 if (global.server_state_file) {
3246 /* absolute path or no base directory provided */
3247 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3248 len = strlen(global.server_state_file);
3249 if (len > MAXPATHLEN) {
3250 globalfilepathlen = 0;
3251 goto globalfileerror;
3252 }
3253 memcpy(globalfilepath, global.server_state_file, len);
3254 globalfilepath[len] = '\0';
3255 globalfilepathlen = len;
3256 }
3257 else if (global.server_state_base) {
3258 len = strlen(global.server_state_base);
3259 globalfilepathlen += len;
3260
3261 if (globalfilepathlen > MAXPATHLEN) {
3262 globalfilepathlen = 0;
3263 goto globalfileerror;
3264 }
3265 strncpy(globalfilepath, global.server_state_base, len);
3266 globalfilepath[globalfilepathlen] = 0;
3267
3268 /* append a slash if needed */
3269 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3270 if (globalfilepathlen + 1 > MAXPATHLEN) {
3271 globalfilepathlen = 0;
3272 goto globalfileerror;
3273 }
3274 globalfilepath[globalfilepathlen++] = '/';
3275 }
3276
3277 len = strlen(global.server_state_file);
3278 if (globalfilepathlen + len > MAXPATHLEN) {
3279 globalfilepathlen = 0;
3280 goto globalfileerror;
3281 }
3282 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3283 globalfilepathlen += len;
3284 globalfilepath[globalfilepathlen++] = 0;
3285 }
3286 }
3287 globalfileerror:
3288 if (globalfilepathlen == 0)
3289 globalfilepath[0] = '\0';
3290
3291 /* read servers state from local file */
3292 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3293 /* servers are only in backends */
3294 if (!(curproxy->cap & PR_CAP_BE))
3295 continue;
3296 fileopenerr = 0;
3297 filepath = NULL;
3298
3299 /* search server state file path and name */
3300 switch (curproxy->load_server_state_from_file) {
3301 /* read servers state from global file */
3302 case PR_SRV_STATE_FILE_GLOBAL:
3303 /* there was an error while generating global server state file path */
3304 if (globalfilepathlen == 0)
3305 continue;
3306 filepath = globalfilepath;
3307 fileopenerr = 1;
3308 break;
3309 /* this backend has its own file */
3310 case PR_SRV_STATE_FILE_LOCAL:
3311 localfilepathlen = 0;
3312 localfilepath[0] = '\0';
3313 len = 0;
3314 /* create the localfilepath variable */
3315 /* absolute path or no base directory provided */
3316 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3317 len = strlen(curproxy->server_state_file_name);
3318 if (len > MAXPATHLEN) {
3319 localfilepathlen = 0;
3320 goto localfileerror;
3321 }
3322 memcpy(localfilepath, curproxy->server_state_file_name, len);
3323 localfilepath[len] = '\0';
3324 localfilepathlen = len;
3325 }
3326 else if (global.server_state_base) {
3327 len = strlen(global.server_state_base);
3328 localfilepathlen += len;
3329
3330 if (localfilepathlen > MAXPATHLEN) {
3331 localfilepathlen = 0;
3332 goto localfileerror;
3333 }
3334 strncpy(localfilepath, global.server_state_base, len);
3335 localfilepath[localfilepathlen] = 0;
3336
3337 /* append a slash if needed */
3338 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3339 if (localfilepathlen + 1 > MAXPATHLEN) {
3340 localfilepathlen = 0;
3341 goto localfileerror;
3342 }
3343 localfilepath[localfilepathlen++] = '/';
3344 }
3345
3346 len = strlen(curproxy->server_state_file_name);
3347 if (localfilepathlen + len > MAXPATHLEN) {
3348 localfilepathlen = 0;
3349 goto localfileerror;
3350 }
3351 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3352 localfilepathlen += len;
3353 localfilepath[localfilepathlen++] = 0;
3354 }
3355 filepath = localfilepath;
3356 localfileerror:
3357 if (localfilepathlen == 0)
3358 localfilepath[0] = '\0';
3359
3360 break;
3361 case PR_SRV_STATE_FILE_NONE:
3362 default:
3363 continue;
3364 }
3365
3366 /* preload global state file */
3367 errno = 0;
3368 f = fopen(filepath, "r");
3369 if (errno && fileopenerr)
3370 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3371 if (!f)
3372 continue;
3373
3374 mybuf[0] = '\0';
3375 mybuflen = 0;
3376 version = 0;
3377
3378 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003379 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3380 Warning("Can't read first line of the server state file '%s'\n", filepath);
3381 goto fileclose;
3382 }
3383
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003384 cur = mybuf;
3385 version = atoi(cur);
3386 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3387 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003388 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003389
3390 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3391 int bk_f_forced_id = 0;
3392 int check_id = 0;
3393 int check_name = 0;
3394
3395 mybuflen = strlen(mybuf);
3396 cur = mybuf;
3397 end = cur + mybuflen;
3398
3399 bk = NULL;
3400 srv = NULL;
3401
3402 /* we need at least one character */
3403 if (mybuflen == 0)
3404 continue;
3405
3406 /* ignore blank characters at the beginning of the line */
3407 while (isspace(*cur))
3408 ++cur;
3409
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003410 /* Ignore empty or commented lines */
3411 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003412 continue;
3413
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003414 /* truncated lines */
3415 if (mybuf[mybuflen - 1] != '\n') {
3416 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003417 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003418 }
3419
3420 /* Removes trailing '\n' */
3421 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003422
3423 /* we're now ready to move the line into *srv_params[] */
3424 params[0] = cur;
3425 arg = 1;
3426 srv_arg = 0;
3427 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3428 if (isspace(*cur)) {
3429 *cur = '\0';
3430 ++cur;
3431 while (isspace(*cur))
3432 ++cur;
3433 switch (version) {
3434 case 1:
3435 /*
3436 * srv_addr: params[4] => srv_params[0]
3437 * srv_op_state: params[5] => srv_params[1]
3438 * srv_admin_state: params[6] => srv_params[2]
3439 * srv_uweight: params[7] => srv_params[3]
3440 * srv_iweight: params[8] => srv_params[4]
3441 * srv_last_time_change: params[9] => srv_params[5]
3442 * srv_check_status: params[10] => srv_params[6]
3443 * srv_check_result: params[11] => srv_params[7]
3444 * srv_check_health: params[12] => srv_params[8]
3445 * srv_check_state: params[13] => srv_params[9]
3446 * srv_agent_state: params[14] => srv_params[10]
3447 * bk_f_forced_id: params[15] => srv_params[11]
3448 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003449 * srv_fqdn: params[17] => srv_params[13]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003450 */
3451 if (arg >= 4) {
3452 srv_params[srv_arg] = cur;
3453 ++srv_arg;
3454 }
3455 break;
3456 }
3457
3458 params[arg] = cur;
3459 ++arg;
3460 }
3461 else {
3462 ++cur;
3463 }
3464 }
3465
3466 /* if line is incomplete line, then ignore it.
3467 * otherwise, update useful flags */
3468 switch (version) {
3469 case 1:
3470 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3471 continue;
3472 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3473 check_id = (atoi(params[0]) == curproxy->uuid);
3474 check_name = (strcmp(curproxy->id, params[1]) == 0);
3475 break;
3476 }
3477
3478 diff = 0;
3479 bk = curproxy;
3480
3481 /* if backend can't be found, let's continue */
3482 if (!check_id && !check_name)
3483 continue;
3484 else if (!check_id && check_name) {
3485 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3486 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3487 }
3488 else if (check_id && !check_name) {
3489 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3490 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3491 /* if name doesn't match, we still want to update curproxy if the backend id
3492 * was forced in previous the previous configuration */
3493 if (!bk_f_forced_id)
3494 continue;
3495 }
3496
3497 /* look for the server by its id: param[2] */
3498 /* else look for the server by its name: param[3] */
3499 diff = 0;
3500 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3501
3502 if (!srv) {
3503 /* if no server found, then warning and continue with next line */
3504 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3505 params[3], params[2], params[0], params[1]);
3506 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3507 params[3], params[2], params[0], params[1]);
3508 continue;
3509 }
3510 else if (diff & PR_FBM_MISMATCH_ID) {
3511 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);
3512 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);
3513 }
3514 else if (diff & PR_FBM_MISMATCH_NAME) {
3515 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);
3516 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);
3517 }
3518
3519 /* now we can proceed with server's state update */
3520 srv_update_state(srv, version, srv_params);
3521 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003522fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003523 fclose(f);
3524 }
3525}
3526
Simon Horman7d09b9a2013-02-12 10:45:51 +09003527/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003528 * update a server's current IP address.
3529 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3530 * ip is in network format.
3531 * updater is a string which contains an information about the requester of the update.
3532 * updater is used if not NULL.
3533 *
3534 * A log line and a stderr warning message is generated based on server's backend options.
3535 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003536int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003537{
3538 /* generates a log line and a warning on stderr */
3539 if (1) {
3540 /* book enough space for both IPv4 and IPv6 */
3541 char oldip[INET6_ADDRSTRLEN];
3542 char newip[INET6_ADDRSTRLEN];
3543
3544 memset(oldip, '\0', INET6_ADDRSTRLEN);
3545 memset(newip, '\0', INET6_ADDRSTRLEN);
3546
3547 /* copy old IP address in a string */
3548 switch (s->addr.ss_family) {
3549 case AF_INET:
3550 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3551 break;
3552 case AF_INET6:
3553 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3554 break;
3555 };
3556
3557 /* copy new IP address in a string */
3558 switch (ip_sin_family) {
3559 case AF_INET:
3560 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3561 break;
3562 case AF_INET6:
3563 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3564 break;
3565 };
3566
3567 /* save log line into a buffer */
3568 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3569 s->proxy->id, s->id, oldip, newip, updater);
3570
3571 /* write the buffer on stderr */
3572 Warning("%s.\n", trash.str);
3573
3574 /* send a log */
3575 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3576 }
3577
3578 /* save the new IP family */
3579 s->addr.ss_family = ip_sin_family;
3580 /* save the new IP address */
3581 switch (ip_sin_family) {
3582 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003583 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003584 break;
3585 case AF_INET6:
3586 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3587 break;
3588 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003589 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003590
3591 return 0;
3592}
3593
3594/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003595 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3596 *
3597 * Caller can pass its name through <updater> to get it integrated in the response message
3598 * returned by the function.
3599 *
3600 * The function first does the following, in that order:
3601 * - validates the new addr and/or port
3602 * - checks if an update is required (new IP or port is different than current ones)
3603 * - checks the update is allowed:
3604 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3605 * - allow all changes if no CHECKS are configured
3606 * - if CHECK is configured:
3607 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3608 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3609 * conditions are met
3610 */
3611const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3612{
3613 struct sockaddr_storage sa;
3614 int ret, port_change_required;
3615 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003616 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003617 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003618 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003619
3620 msg = get_trash_chunk();
3621 chunk_reset(msg);
3622
3623 if (addr) {
3624 memset(&sa, 0, sizeof(struct sockaddr_storage));
3625 if (str2ip2(addr, &sa, 0) == NULL) {
3626 chunk_printf(msg, "Invalid addr '%s'", addr);
3627 goto out;
3628 }
3629
3630 /* changes are allowed on AF_INET* families only */
3631 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3632 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3633 goto out;
3634 }
3635
3636 /* collecting data currently setup */
3637 memset(current_addr, '\0', sizeof(current_addr));
3638 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3639 /* changes are allowed on AF_INET* families only */
3640 if ((ret != AF_INET) && (ret != AF_INET6)) {
3641 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3642 goto out;
3643 }
3644
3645 /* applying ADDR changes if required and allowed
3646 * ipcmp returns 0 when both ADDR are the same
3647 */
3648 if (ipcmp(&s->addr, &sa) == 0) {
3649 chunk_appendf(msg, "no need to change the addr");
3650 goto port;
3651 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003652 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003653 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003654
3655 /* we also need to update check's ADDR only if it uses the server's one */
3656 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003657 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003658 }
3659
3660 /* we also need to update agent ADDR only if it use the server's one */
3661 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003662 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003663 }
3664
3665 /* update report for caller */
3666 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3667 }
3668
3669 port:
3670 if (port) {
3671 char sign = '\0';
3672 char *endptr;
3673
3674 if (addr)
3675 chunk_appendf(msg, ", ");
3676
3677 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003678 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003679
3680 /* check if PORT change is required */
3681 port_change_required = 0;
3682
3683 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003684 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003685 new_port = strtol(port, &endptr, 10);
3686 if ((errno != 0) || (port == endptr)) {
3687 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3688 goto out;
3689 }
3690
3691 /* check if caller triggers a port mapped or offset */
3692 if (sign == '-' || (sign == '+')) {
3693 /* check if server currently uses port map */
3694 if (!(s->flags & SRV_F_MAPPORTS)) {
3695 /* switch from fixed port to port map mandatorily triggers
3696 * a port change */
3697 port_change_required = 1;
3698 /* check is configured
3699 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3700 * prevent PORT change if check doesn't have it's dedicated port while switching
3701 * to port mapping */
3702 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3703 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.");
3704 goto out;
3705 }
3706 }
3707 /* we're already using port maps */
3708 else {
3709 port_change_required = current_port != new_port;
3710 }
3711 }
3712 /* fixed port */
3713 else {
3714 port_change_required = current_port != new_port;
3715 }
3716
3717 /* applying PORT changes if required and update response message */
3718 if (port_change_required) {
3719 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003720 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003721 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003722
3723 /* prepare message */
3724 chunk_appendf(msg, "port changed from '");
3725 if (s->flags & SRV_F_MAPPORTS)
3726 chunk_appendf(msg, "+");
3727 chunk_appendf(msg, "%d' to '", current_port);
3728
3729 if (sign == '-') {
3730 s->flags |= SRV_F_MAPPORTS;
3731 chunk_appendf(msg, "%c", sign);
3732 /* just use for result output */
3733 new_port = -new_port;
3734 }
3735 else if (sign == '+') {
3736 s->flags |= SRV_F_MAPPORTS;
3737 chunk_appendf(msg, "%c", sign);
3738 }
3739 else {
3740 s->flags &= ~SRV_F_MAPPORTS;
3741 }
3742
3743 chunk_appendf(msg, "%d'", new_port);
3744
3745 /* we also need to update health checks port only if it uses server's realport */
3746 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3747 s->check.port = new_port;
3748 }
3749 }
3750 else {
3751 chunk_appendf(msg, "no need to change the port");
3752 }
3753 }
3754
3755out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003756 if (changed)
3757 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003758 if (updater)
3759 chunk_appendf(msg, " by '%s'", updater);
3760 chunk_appendf(msg, "\n");
3761 return msg->str;
3762}
3763
3764
3765/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003766 * update server status based on result of name resolution
3767 * returns:
3768 * 0 if server status is updated
3769 * 1 if server status has not changed
3770 */
3771int snr_update_srv_status(struct server *s)
3772{
3773 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003774 struct dns_resolvers *resolvers;
3775
3776 resolvers = resolution->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003777
3778 switch (resolution->status) {
3779 case RSLV_STATUS_NONE:
3780 /* status when HAProxy has just (re)started */
3781 trigger_resolution(s);
3782 break;
3783
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003784 case RSLV_STATUS_VALID:
3785 /*
3786 * resume health checks
3787 * server will be turned back on if health check is safe
3788 */
3789 if (!(s->admin & SRV_ADMF_RMAINT))
3790 return 1;
3791 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3792 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3793 s->proxy->id, s->id);
3794
3795 Warning("%s.\n", trash.str);
3796 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3797 return 0;
3798
3799 case RSLV_STATUS_NX:
3800 /* stop server if resolution is NX for a long enough period */
3801 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
3802 if (s->admin & SRV_ADMF_RMAINT)
3803 return 1;
3804 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3805 return 0;
3806 }
3807 break;
3808
3809 case RSLV_STATUS_TIMEOUT:
3810 /* stop server if resolution is TIMEOUT for a long enough period */
3811 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
3812 if (s->admin & SRV_ADMF_RMAINT)
3813 return 1;
3814 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3815 return 0;
3816 }
3817 break;
3818
3819 case RSLV_STATUS_REFUSED:
3820 /* stop server if resolution is REFUSED for a long enough period */
3821 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
3822 if (s->admin & SRV_ADMF_RMAINT)
3823 return 1;
3824 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3825 return 0;
3826 }
3827 break;
3828
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003829 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003830 /* stop server if resolution is in unmatched error for a long enough period */
3831 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
3832 if (s->admin & SRV_ADMF_RMAINT)
3833 return 1;
3834 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3835 return 0;
3836 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003837 break;
3838 }
3839
3840 return 1;
3841}
3842
3843/*
3844 * Server Name Resolution valid response callback
3845 * It expects:
3846 * - <nameserver>: the name server which answered the valid response
3847 * - <response>: buffer containing a valid DNS response
3848 * - <response_len>: size of <response>
3849 * It performs the following actions:
3850 * - ignore response if current ip found and server family not met
3851 * - update with first new ip found if family is met and current IP is not found
3852 * returns:
3853 * 0 on error
3854 * 1 when no error or safe ignore
3855 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003856int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003857{
3858 struct server *s;
3859 void *serverip, *firstip;
3860 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003861 int ret;
3862 struct chunk *chk = get_trash_chunk();
3863
3864 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003865 firstip = NULL; /* pointer to the first valid response found */
3866 /* it will be used as the new IP if a change is required */
3867 firstip_sin_family = AF_UNSPEC;
3868 serverip = NULL; /* current server IP address */
3869
3870 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003871 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003872
3873 /* initializing server IP pointer */
3874 server_sin_family = s->addr.ss_family;
3875 switch (server_sin_family) {
3876 case AF_INET:
3877 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3878 break;
3879
3880 case AF_INET6:
3881 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3882 break;
3883
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003884 case AF_UNSPEC:
3885 break;
3886
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003887 default:
3888 goto invalid;
3889 }
3890
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003891 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01003892 serverip, server_sin_family, &firstip,
3893 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003894
3895 switch (ret) {
3896 case DNS_UPD_NO:
3897 if (resolution->status != RSLV_STATUS_VALID) {
3898 resolution->status = RSLV_STATUS_VALID;
3899 resolution->last_status_change = now_ms;
3900 }
3901 goto stop_resolution;
3902
3903 case DNS_UPD_SRVIP_NOT_FOUND:
3904 goto save_ip;
3905
3906 case DNS_UPD_CNAME:
3907 if (resolution->status != RSLV_STATUS_VALID) {
3908 resolution->status = RSLV_STATUS_VALID;
3909 resolution->last_status_change = now_ms;
3910 }
3911 goto invalid;
3912
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003913 case DNS_UPD_NO_IP_FOUND:
3914 if (resolution->status != RSLV_STATUS_OTHER) {
3915 resolution->status = RSLV_STATUS_OTHER;
3916 resolution->last_status_change = now_ms;
3917 }
3918 goto stop_resolution;
3919
Baptiste Assmannfad03182015-10-28 02:03:32 +01003920 case DNS_UPD_NAME_ERROR:
3921 /* if this is not the last expected response, we ignore it */
3922 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
3923 return 0;
3924 /* update resolution status to OTHER error type */
3925 if (resolution->status != RSLV_STATUS_OTHER) {
3926 resolution->status = RSLV_STATUS_OTHER;
3927 resolution->last_status_change = now_ms;
3928 }
3929 goto stop_resolution;
3930
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003931 default:
3932 goto invalid;
3933
3934 }
3935
3936 save_ip:
3937 nameserver->counters.update += 1;
3938 if (resolution->status != RSLV_STATUS_VALID) {
3939 resolution->status = RSLV_STATUS_VALID;
3940 resolution->last_status_change = now_ms;
3941 }
3942
3943 /* save the first ip we found */
3944 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3945 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3946
3947 stop_resolution:
3948 /* update last resolution date and time */
3949 resolution->last_resolution = now_ms;
3950 /* reset current status flag */
3951 resolution->step = RSLV_STEP_NONE;
3952 /* reset values */
3953 dns_reset_resolution(resolution);
3954
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003955 dns_update_resolvers_timeout(nameserver->resolvers);
3956
3957 snr_update_srv_status(s);
3958 return 0;
3959
3960 invalid:
3961 nameserver->counters.invalid += 1;
3962 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3963 goto stop_resolution;
3964
3965 snr_update_srv_status(s);
3966 return 0;
3967}
3968
3969/*
3970 * Server Name Resolution error management callback
3971 * returns:
3972 * 0 on error
3973 * 1 when no error or safe ignore
3974 */
3975int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
3976{
3977 struct server *s;
3978 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003979 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003980
3981 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003982 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003983 resolvers = resolution->resolvers;
3984
3985 /* can be ignored if this is not the last response */
3986 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3987 return 1;
3988 }
3989
3990 switch (error_code) {
3991 case DNS_RESP_INVALID:
3992 case DNS_RESP_WRONG_NAME:
3993 if (resolution->status != RSLV_STATUS_INVALID) {
3994 resolution->status = RSLV_STATUS_INVALID;
3995 resolution->last_status_change = now_ms;
3996 }
3997 break;
3998
3999 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02004000 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004001 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02004002 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02004003 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01004004 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
4005 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02004006
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00004007 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02004008 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004009 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00004010 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02004011 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004012 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02004013 }
4014 else if (res_preferred_afinet) {
4015 /* fallback from A to AAAA */
4016 resolution->query_type = DNS_RTYPE_AAAA;
4017 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02004018 else {
4019 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01004020 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00004021 resolution->query_type = DNS_RTYPE_A;
4022 } else {
4023 resolution->query_type = DNS_RTYPE_AAAA;
4024 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02004025 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004026
4027 dns_send_query(resolution);
4028
4029 /*
4030 * move the resolution to the last element of the FIFO queue
4031 * and update timeout wakeup based on the new first entry
4032 */
4033 if (dns_check_resolution_queue(resolvers) > 1) {
4034 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02004035 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004036 /* ex first resolution goes to the end of the queue */
4037 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
4038 }
4039 dns_update_resolvers_timeout(resolvers);
4040 goto leave;
4041 }
4042 else {
4043 if (resolution->status != RSLV_STATUS_OTHER) {
4044 resolution->status = RSLV_STATUS_OTHER;
4045 resolution->last_status_change = now_ms;
4046 }
4047 }
4048 break;
4049
4050 case DNS_RESP_NX_DOMAIN:
4051 if (resolution->status != RSLV_STATUS_NX) {
4052 resolution->status = RSLV_STATUS_NX;
4053 resolution->last_status_change = now_ms;
4054 }
4055 break;
4056
4057 case DNS_RESP_REFUSED:
4058 if (resolution->status != RSLV_STATUS_REFUSED) {
4059 resolution->status = RSLV_STATUS_REFUSED;
4060 resolution->last_status_change = now_ms;
4061 }
4062 break;
4063
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004064 case DNS_RESP_TIMEOUT:
4065 if (resolution->status != RSLV_STATUS_TIMEOUT) {
4066 resolution->status = RSLV_STATUS_TIMEOUT;
4067 resolution->last_status_change = now_ms;
4068 }
4069 break;
4070 }
4071
4072 /* update last resolution date and time */
4073 resolution->last_resolution = now_ms;
4074 /* reset current status flag */
4075 resolution->step = RSLV_STEP_NONE;
4076 /* reset values */
4077 dns_reset_resolution(resolution);
4078
4079 LIST_DEL(&resolution->list);
4080 dns_update_resolvers_timeout(resolvers);
4081
4082 leave:
4083 snr_update_srv_status(s);
4084 return 1;
4085}
4086
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004087/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4088 * resolver. This is suited for initial address configuration. Returns 0 on
4089 * success otherwise a non-zero error code. In case of error, *err_code, if
4090 * not NULL, is filled up.
4091 */
4092int srv_set_addr_via_libc(struct server *srv, int *err_code)
4093{
4094 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004095 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004096 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004097 return 1;
4098 }
4099 return 0;
4100}
4101
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004102/* Set the server's FDQN (->hostname) from <hostname>.
4103 * Returns -1 if failed, 0 if not.
4104 */
4105int srv_set_fqdn(struct server *srv, const char *hostname)
4106{
4107 srv_free_dns_resolution(srv);
4108
4109 return srv_alloc_dns_resolution(srv, hostname);
4110}
4111
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004112/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4113 * from the state file. This is suited for initial address configuration.
4114 * Returns 0 on success otherwise a non-zero error code. In case of error,
4115 * *err_code, if not NULL, is filled up.
4116 */
4117static int srv_apply_lastaddr(struct server *srv, int *err_code)
4118{
4119 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4120 if (err_code)
4121 *err_code |= ERR_WARN;
4122 return 1;
4123 }
4124 return 0;
4125}
4126
Willy Tarreau25e51522016-11-04 15:10:17 +01004127/* returns 0 if no error, otherwise a combination of ERR_* flags */
4128static int srv_iterate_initaddr(struct server *srv)
4129{
4130 int return_code = 0;
4131 int err_code;
4132 unsigned int methods;
4133
4134 methods = srv->init_addr_methods;
4135 if (!methods) { // default to "last,libc"
4136 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4137 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4138 }
4139
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004140 /* "-dr" : always append "none" so that server addresses resolution
4141 * failures are silently ignored, this is convenient to validate some
4142 * configs out of their environment.
4143 */
4144 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4145 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4146
Willy Tarreau25e51522016-11-04 15:10:17 +01004147 while (methods) {
4148 err_code = 0;
4149 switch (srv_get_next_initaddr(&methods)) {
4150 case SRV_IADDR_LAST:
4151 if (!srv->lastaddr)
4152 continue;
4153 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004154 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004155 return_code |= err_code;
4156 break;
4157
4158 case SRV_IADDR_LIBC:
4159 if (!srv->hostname)
4160 continue;
4161 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004162 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004163 return_code |= err_code;
4164 break;
4165
Willy Tarreau37ebe122016-11-04 15:17:58 +01004166 case SRV_IADDR_NONE:
4167 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004168 if (return_code) {
4169 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4170 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4171 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004172 return return_code;
4173
Willy Tarreau4310d362016-11-02 15:05:56 +01004174 case SRV_IADDR_IP:
4175 ipcpy(&srv->init_addr, &srv->addr);
4176 if (return_code) {
4177 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4178 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4179 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004180 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004181
Willy Tarreau25e51522016-11-04 15:10:17 +01004182 default: /* unhandled method */
4183 break;
4184 }
4185 }
4186
4187 if (!return_code) {
4188 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
4189 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4190 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004191 else {
4192 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
4193 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4194 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004195
4196 return_code |= ERR_ALERT | ERR_FATAL;
4197 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004198out:
4199 srv_set_dyncookie(srv);
4200 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004201}
4202
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004203/*
4204 * This function parses all backends and all servers within each backend
4205 * and performs servers' addr resolution based on information provided by:
4206 * - configuration file
4207 * - server-state file (states provided by an 'old' haproxy process)
4208 *
4209 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4210 */
4211int srv_init_addr(void)
4212{
4213 struct proxy *curproxy;
4214 int return_code = 0;
4215
4216 curproxy = proxy;
4217 while (curproxy) {
4218 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004219
4220 /* servers are in backend only */
4221 if (!(curproxy->cap & PR_CAP_BE))
4222 goto srv_init_addr_next;
4223
Willy Tarreau25e51522016-11-04 15:10:17 +01004224 for (srv = curproxy->srv; srv; srv = srv->next)
4225 if (srv->hostname)
4226 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004227
4228 srv_init_addr_next:
4229 curproxy = curproxy->next;
4230 }
4231
4232 return return_code;
4233}
4234
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004235const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater)
4236{
4237
4238 struct chunk *msg;
4239
4240 msg = get_trash_chunk();
4241 chunk_reset(msg);
4242
4243 if (!strcmp(fqdn, server->hostname)) {
4244 chunk_appendf(msg, "no need to change the FDQN");
4245 goto out;
4246 }
4247
4248 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4249 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4250 goto out;
4251 }
4252
4253 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4254 server->proxy->id, server->id, server->hostname, fqdn);
4255
4256 if (srv_set_fqdn(server, fqdn) < 0) {
4257 chunk_reset(msg);
4258 chunk_appendf(msg, "could not update %s/%s FQDN",
4259 server->proxy->id, server->id);
4260 goto out;
4261 }
4262
4263 /* Flag as FQDN set from stats socket. */
4264 server->admin |= SRV_ADMF_HMAINT;
4265
4266 out:
4267 if (updater)
4268 chunk_appendf(msg, " by '%s'", updater);
4269 chunk_appendf(msg, "\n");
4270
4271 return msg->str;
4272}
4273
4274
Willy Tarreau21b069d2016-11-23 17:15:08 +01004275/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4276 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004277 * 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 +01004278 * used for CLI commands requiring a server name.
4279 * Important: the <arg> is modified to remove the '/'.
4280 */
4281struct server *cli_find_server(struct appctx *appctx, char *arg)
4282{
4283 struct proxy *px;
4284 struct server *sv;
4285 char *line;
4286
4287 /* split "backend/server" and make <line> point to server */
4288 for (line = arg; *line; line++)
4289 if (*line == '/') {
4290 *line++ = '\0';
4291 break;
4292 }
4293
4294 if (!*line || !*arg) {
4295 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004296 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004297 return NULL;
4298 }
4299
4300 if (!get_backend_server(arg, line, &px, &sv)) {
4301 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004302 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004303 return NULL;
4304 }
4305
4306 if (px->state == PR_STSTOPPED) {
4307 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004308 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004309 return NULL;
4310 }
4311
4312 return sv;
4313}
4314
William Lallemand222baf22016-11-19 02:00:33 +01004315
4316static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4317{
4318 struct server *sv;
4319 const char *warning;
4320
4321 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4322 return 1;
4323
4324 sv = cli_find_server(appctx, args[2]);
4325 if (!sv)
4326 return 1;
4327
4328 if (strcmp(args[3], "weight") == 0) {
4329 warning = server_parse_weight_change_request(sv, args[4]);
4330 if (warning) {
4331 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004332 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004333 }
4334 }
4335 else if (strcmp(args[3], "state") == 0) {
4336 if (strcmp(args[4], "ready") == 0)
4337 srv_adm_set_ready(sv);
4338 else if (strcmp(args[4], "drain") == 0)
4339 srv_adm_set_drain(sv);
4340 else if (strcmp(args[4], "maint") == 0)
4341 srv_adm_set_maint(sv);
4342 else {
4343 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004344 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004345 }
4346 }
4347 else if (strcmp(args[3], "health") == 0) {
4348 if (sv->track) {
4349 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004350 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004351 }
4352 else if (strcmp(args[4], "up") == 0) {
4353 sv->check.health = sv->check.rise + sv->check.fall - 1;
4354 srv_set_running(sv, "changed from CLI");
4355 }
4356 else if (strcmp(args[4], "stopping") == 0) {
4357 sv->check.health = sv->check.rise + sv->check.fall - 1;
4358 srv_set_stopping(sv, "changed from CLI");
4359 }
4360 else if (strcmp(args[4], "down") == 0) {
4361 sv->check.health = 0;
4362 srv_set_stopped(sv, "changed from CLI");
4363 }
4364 else {
4365 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004366 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004367 }
4368 }
4369 else if (strcmp(args[3], "agent") == 0) {
4370 if (!(sv->agent.state & CHK_ST_ENABLED)) {
4371 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004372 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004373 }
4374 else if (strcmp(args[4], "up") == 0) {
4375 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4376 srv_set_running(sv, "changed from CLI");
4377 }
4378 else if (strcmp(args[4], "down") == 0) {
4379 sv->agent.health = 0;
4380 srv_set_stopped(sv, "changed from CLI");
4381 }
4382 else {
4383 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004384 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004385 }
4386 }
Misiek2da082d2017-01-09 09:40:42 +01004387 else if (strcmp(args[3], "agent-addr") == 0) {
4388 if (!(sv->agent.state & CHK_ST_ENABLED)) {
4389 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4390 appctx->st0 = CLI_ST_PRINT;
4391 } else {
4392 if (str2ip(args[4], &sv->agent.addr) == NULL) {
4393 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4394 appctx->st0 = CLI_ST_PRINT;
4395 }
4396 }
4397 }
4398 else if (strcmp(args[3], "agent-send") == 0) {
4399 if (!(sv->agent.state & CHK_ST_ENABLED)) {
4400 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4401 appctx->st0 = CLI_ST_PRINT;
4402 } else {
4403 char *nss = strdup(args[4]);
4404 if (!nss) {
4405 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4406 appctx->st0 = CLI_ST_PRINT;
4407 } else {
4408 free(sv->agent.send_string);
4409 sv->agent.send_string = nss;
4410 sv->agent.send_string_len = strlen(args[4]);
4411 }
4412 }
4413 }
William Lallemand222baf22016-11-19 02:00:33 +01004414 else if (strcmp(args[3], "check-port") == 0) {
4415 int i = 0;
4416 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
4417 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004418 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004419 }
4420 if ((i < 0) || (i > 65535)) {
4421 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004422 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004423 }
4424 /* prevent the update of port to 0 if MAPPORTS are in use */
4425 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
4426 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004427 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004428 return 1;
4429 }
4430 sv->check.port = i;
4431 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004432 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004433 }
4434 else if (strcmp(args[3], "addr") == 0) {
4435 char *addr = NULL;
4436 char *port = NULL;
4437 if (strlen(args[4]) == 0) {
4438 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004439 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004440 return 1;
4441 }
4442 else {
4443 addr = args[4];
4444 }
4445 if (strcmp(args[5], "port") == 0) {
4446 port = args[6];
4447 }
4448 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4449 if (warning) {
4450 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004451 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004452 }
4453 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4454 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004455 else if (strcmp(args[3], "fqdn") == 0) {
4456 if (!*args[4]) {
4457 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4458 appctx->st0 = CLI_ST_PRINT;
4459 return 1;
4460 }
4461 warning = update_server_fqdn(sv, args[4], "stats socket command");
4462 if (warning) {
4463 appctx->ctx.cli.msg = warning;
4464 appctx->st0 = CLI_ST_PRINT;
4465 }
4466 }
William Lallemand222baf22016-11-19 02:00:33 +01004467 else {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004468 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 +01004469 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004470 }
4471 return 1;
4472}
4473
William Lallemand6b160942016-11-22 12:34:35 +01004474static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4475{
4476 struct stream_interface *si = appctx->owner;
4477 struct proxy *px;
4478 struct server *sv;
4479 char *line;
4480
4481
4482 /* split "backend/server" and make <line> point to server */
4483 for (line = args[2]; *line; line++)
4484 if (*line == '/') {
4485 *line++ = '\0';
4486 break;
4487 }
4488
4489 if (!*line) {
4490 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004491 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004492 return 1;
4493 }
4494
4495 if (!get_backend_server(args[2], line, &px, &sv)) {
4496 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004497 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004498 return 1;
4499 }
4500
4501 /* return server's effective weight at the moment */
4502 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004503 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004504 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004505 return 0;
4506 }
William Lallemand6b160942016-11-22 12:34:35 +01004507 return 1;
4508}
4509
4510static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4511{
4512 struct server *sv;
4513 const char *warning;
4514
4515 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4516 return 1;
4517
4518 sv = cli_find_server(appctx, args[2]);
4519 if (!sv)
4520 return 1;
4521
4522 warning = server_parse_weight_change_request(sv, args[3]);
4523 if (warning) {
4524 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004525 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004526 }
4527 return 1;
4528}
4529
Willy Tarreaub8026272016-11-23 11:26:56 +01004530/* parse a "set maxconn server" command. It always returns 1. */
4531static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4532{
4533 struct server *sv;
4534 const char *warning;
4535
4536 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4537 return 1;
4538
4539 sv = cli_find_server(appctx, args[3]);
4540 if (!sv)
4541 return 1;
4542
4543 warning = server_parse_maxconn_change_request(sv, args[4]);
4544 if (warning) {
4545 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004546 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004547 }
4548 return 1;
4549}
William Lallemand6b160942016-11-22 12:34:35 +01004550
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004551/* parse a "disable agent" command. It always returns 1. */
4552static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4553{
4554 struct server *sv;
4555
4556 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4557 return 1;
4558
4559 sv = cli_find_server(appctx, args[2]);
4560 if (!sv)
4561 return 1;
4562
4563 sv->agent.state &= ~CHK_ST_ENABLED;
4564 return 1;
4565}
4566
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004567/* parse a "disable health" command. It always returns 1. */
4568static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4569{
4570 struct server *sv;
4571
4572 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4573 return 1;
4574
4575 sv = cli_find_server(appctx, args[2]);
4576 if (!sv)
4577 return 1;
4578
4579 sv->check.state &= ~CHK_ST_ENABLED;
4580 return 1;
4581}
4582
Willy Tarreauffb4d582016-11-24 12:47:00 +01004583/* parse a "disable server" command. It always returns 1. */
4584static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4585{
4586 struct server *sv;
4587
4588 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4589 return 1;
4590
4591 sv = cli_find_server(appctx, args[2]);
4592 if (!sv)
4593 return 1;
4594
4595 srv_adm_set_maint(sv);
4596 return 1;
4597}
4598
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004599/* parse a "enable agent" command. It always returns 1. */
4600static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4601{
4602 struct server *sv;
4603
4604 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4605 return 1;
4606
4607 sv = cli_find_server(appctx, args[2]);
4608 if (!sv)
4609 return 1;
4610
4611 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
4612 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004613 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004614 return 1;
4615 }
4616
4617 sv->agent.state |= CHK_ST_ENABLED;
4618 return 1;
4619}
4620
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004621/* parse a "enable health" command. It always returns 1. */
4622static int cli_parse_enable_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 "enable server" command. It always returns 1. */
4638static int cli_parse_enable_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_ready(sv);
4650 return 1;
4651}
4652
William Lallemand222baf22016-11-19 02:00:33 +01004653/* register cli keywords */
4654static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004655 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004656 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004657 { { "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 +01004658 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004659 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004660 { { "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 +01004661 { { "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 +01004662 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004663 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4664 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4665
William Lallemand222baf22016-11-19 02:00:33 +01004666 {{},}
4667}};
4668
4669__attribute__((constructor))
4670static void __server_init(void)
4671{
4672 cli_register_kw(&cli_kws);
4673}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004674
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004675/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004676 * Local variables:
4677 * c-indent-level: 8
4678 * c-basic-offset: 8
4679 * End:
4680 */