blob: 17f84f3c82e2a310f9132b6565d303c17a618071 [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);
1670 dst_dns_rslt = calloc(1, sizeof *dst_dns_rslt);
1671 hostname_dn_len = dns_str_to_dn_label_len(hostname);
1672 hostname_dn = calloc(hostname_dn_len + 1, sizeof(char));
1673
1674 if (!srv->hostname || !dst_dns_rslt || !hostname_dn)
1675 goto err;
1676
1677 srv->resolution = dst_dns_rslt;
1678 srv->resolution->hostname_dn = hostname_dn;
1679 srv->resolution->hostname_dn_len = hostname_dn_len;
1680
1681 if (!dns_str_to_dn_label(srv->hostname,
1682 srv->resolution->hostname_dn,
1683 srv->resolution->hostname_dn_len + 1))
1684 goto err;
1685
1686 srv->resolution->requester = srv;
1687 srv->resolution->requester_cb = snr_resolution_cb;
1688 srv->resolution->requester_error_cb = snr_resolution_error_cb;
1689 srv->resolution->status = RSLV_STATUS_NONE;
1690 srv->resolution->step = RSLV_STEP_NONE;
1691 /* a first resolution has been done by the configuration parser */
1692 srv->resolution->last_resolution = 0;
1693
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001694 if (srv->resolvers_id) {
1695 struct dns_resolvers *curr_resolvers;
1696 int found = 0;
1697
1698 list_for_each_entry(curr_resolvers, &dns_resolvers, list) {
1699 if (!strcmp(curr_resolvers->id, srv->resolvers_id)) {
1700 found = 1;
1701 break;
1702 }
1703 }
1704 if (!found)
1705 goto err;
1706 srv->resolution->resolvers = curr_resolvers;
1707 }
1708 else
1709 goto err;
1710
1711 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001712
1713 err:
1714 free(srv->hostname);
1715 srv->hostname = NULL;
1716 free(hostname_dn);
1717 free(dst_dns_rslt);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001718 return -1;
1719}
1720
1721static void srv_free_dns_resolution(struct server *srv)
1722{
1723 if (!srv->resolution)
1724 return;
1725
1726 free(srv->resolution->hostname_dn);
1727 free(srv->resolution);
1728 srv->resolution = NULL;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001729}
1730
1731/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001732 * Copy <src> server settings to <srv> server allocating
1733 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001734 * This function is not supposed to be called at any time, but only
1735 * during server settings parsing or during server allocations from
1736 * a server template, and just after having calloc()'ed a new server.
1737 * So, <src> may only be a default server (when parsing server settings)
1738 * or a server template (during server allocations from a server template).
1739 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1740 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001741 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001742static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001743{
1744 /* Connection source settings copy */
1745 srv_conn_src_cpy(srv, src);
1746
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001747 if (srv_tmpl) {
1748 srv->addr = src->addr;
1749 srv->svc_port = src->svc_port;
1750 }
1751
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001752 srv->pp_opts = src->pp_opts;
1753 if (src->rdr_pfx != NULL) {
1754 srv->rdr_pfx = strdup(src->rdr_pfx);
1755 srv->rdr_len = src->rdr_len;
1756 }
1757 if (src->cookie != NULL) {
1758 srv->cookie = strdup(src->cookie);
1759 srv->cklen = src->cklen;
1760 }
1761 srv->use_ssl = src->use_ssl;
1762 srv->check.addr = srv->agent.addr = src->check.addr;
1763 srv->check.use_ssl = src->check.use_ssl;
1764 srv->check.port = src->check.port;
1765 /* Note: 'flags' field has potentially been already initialized. */
1766 srv->flags |= src->flags;
1767 srv->do_check = src->do_check;
1768 srv->do_agent = src->do_agent;
1769 if (srv->check.port)
1770 srv->flags |= SRV_F_CHECKPORT;
1771 srv->check.inter = src->check.inter;
1772 srv->check.fastinter = src->check.fastinter;
1773 srv->check.downinter = src->check.downinter;
1774 srv->agent.use_ssl = src->agent.use_ssl;
1775 srv->agent.port = src->agent.port;
1776 if (src->agent.send_string != NULL)
1777 srv->agent.send_string = strdup(src->agent.send_string);
1778 srv->agent.send_string_len = src->agent.send_string_len;
1779 srv->agent.inter = src->agent.inter;
1780 srv->agent.fastinter = src->agent.fastinter;
1781 srv->agent.downinter = src->agent.downinter;
1782 srv->maxqueue = src->maxqueue;
1783 srv->minconn = src->minconn;
1784 srv->maxconn = src->maxconn;
1785 srv->slowstart = src->slowstart;
1786 srv->observe = src->observe;
1787 srv->onerror = src->onerror;
1788 srv->onmarkeddown = src->onmarkeddown;
1789 srv->onmarkedup = src->onmarkedup;
1790 if (src->trackit != NULL)
1791 srv->trackit = strdup(src->trackit);
1792 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1793 srv->uweight = srv->iweight = src->iweight;
1794
1795 srv->check.send_proxy = src->check.send_proxy;
1796 /* health: up, but will fall down at first failure */
1797 srv->check.rise = srv->check.health = src->check.rise;
1798 srv->check.fall = src->check.fall;
1799
1800 /* Here we check if 'disabled' is the default server state */
1801 if (src->admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1802 srv->admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1803 srv->state = SRV_ST_STOPPED;
1804 srv->check.state |= CHK_ST_PAUSED;
1805 srv->check.health = 0;
1806 }
1807
1808 /* health: up but will fall down at first failure */
1809 srv->agent.rise = srv->agent.health = src->agent.rise;
1810 srv->agent.fall = src->agent.fall;
1811
1812 if (src->resolvers_id != NULL)
1813 srv->resolvers_id = strdup(src->resolvers_id);
1814 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1815 if (srv->dns_opts.family_prio == AF_UNSPEC)
1816 srv->dns_opts.family_prio = AF_INET6;
1817 memcpy(srv->dns_opts.pref_net,
1818 src->dns_opts.pref_net,
1819 sizeof srv->dns_opts.pref_net);
1820 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1821
1822 srv->init_addr_methods = src->init_addr_methods;
1823 srv->init_addr = src->init_addr;
1824#if defined(USE_OPENSSL)
1825 srv_ssl_settings_cpy(srv, src);
1826#endif
1827#ifdef TCP_USER_TIMEOUT
1828 srv->tcp_ut = src->tcp_ut;
1829#endif
1830}
1831
1832static struct server *new_server(struct proxy *proxy)
1833{
1834 struct server *srv;
1835
1836 srv = calloc(1, sizeof *srv);
1837 if (!srv)
1838 return NULL;
1839
1840 srv->obj_type = OBJ_TYPE_SERVER;
1841 srv->proxy = proxy;
1842 LIST_INIT(&srv->actconns);
1843 LIST_INIT(&srv->pendconns);
1844 LIST_INIT(&srv->priv_conns);
1845 LIST_INIT(&srv->idle_conns);
1846 LIST_INIT(&srv->safe_conns);
1847
1848 srv->state = SRV_ST_RUNNING; /* early server setup */
1849 srv->last_change = now.tv_sec;
1850
1851 srv->check.status = HCHK_STATUS_INI;
1852 srv->check.server = srv;
1853 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1854
1855 srv->agent.status = HCHK_STATUS_INI;
1856 srv->agent.server = srv;
1857 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1858
1859 return srv;
1860}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001861
1862/*
1863 * Validate <srv> server health-check settings.
1864 * Returns 0 if everything is OK, -1 if not.
1865 */
1866static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1867{
1868 struct tcpcheck_rule *r = NULL;
1869 struct list *l;
1870
1871 /*
1872 * We need at least a service port, a check port or the first tcp-check rule must
1873 * be a 'connect' one when checking an IPv4/IPv6 server.
1874 */
1875 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1876 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1877 return 0;
1878
1879 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1880 if (!r) {
1881 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1882 "Check has been disabled.\n",
1883 file, linenum, srv->id);
1884 return -1;
1885 }
1886
1887 /* search the first action (connect / send / expect) in the list */
1888 l = &srv->proxy->tcpcheck_rules;
1889 list_for_each_entry(r, l, list) {
1890 if (r->action != TCPCHK_ACT_COMMENT)
1891 break;
1892 }
1893
1894 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1895 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1896 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1897 file, linenum, srv->id);
1898 return -1;
1899 }
1900
1901 /* scan the tcp-check ruleset to ensure a port has been configured */
1902 l = &srv->proxy->tcpcheck_rules;
1903 list_for_each_entry(r, l, list) {
1904 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1905 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1906 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1907 file, linenum, srv->id);
1908 return -1;
1909 }
1910 }
1911
1912 return 0;
1913}
1914
1915/*
1916 * Initialize <srv> health-check structure.
1917 * Returns the error string in case of memory allocation failure, NULL if not.
1918 */
1919static const char *do_health_check_init(struct server *srv, int check_type, int state)
1920{
1921 const char *ret;
1922
1923 if (!srv->do_check)
1924 return NULL;
1925
1926 ret = init_check(&srv->check, check_type);
1927 if (ret)
1928 return ret;
1929
1930 if (srv->resolution)
1931 srv->resolution->opts = &srv->dns_opts;
1932
1933 srv->check.state |= state;
1934 global.maxsock++;
1935
1936 return NULL;
1937}
1938
1939static int server_health_check_init(const char *file, int linenum,
1940 struct server *srv, struct proxy *curproxy)
1941{
1942 const char *ret;
1943
1944 if (!srv->do_check)
1945 return 0;
1946
1947 if (srv->trackit) {
1948 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1949 file, linenum);
1950 return ERR_ALERT | ERR_FATAL;
1951 }
1952
1953 if (server_healthcheck_validate(file, linenum, srv) < 0)
1954 return ERR_ALERT | ERR_ABORT;
1955
1956 /* note: check type will be set during the config review phase */
1957 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1958 if (ret) {
1959 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1960 return ERR_ALERT | ERR_ABORT;
1961 }
1962
1963 return 0;
1964}
1965
1966/*
1967 * Initialize <srv> agent check structure.
1968 * Returns the error string in case of memory allocation failure, NULL if not.
1969 */
1970static const char *do_server_agent_check_init(struct server *srv, int state)
1971{
1972 const char *ret;
1973
1974 if (!srv->do_agent)
1975 return NULL;
1976
1977 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1978 if (ret)
1979 return ret;
1980
1981 if (!srv->agent.inter)
1982 srv->agent.inter = srv->check.inter;
1983
1984 srv->agent.state |= state;
1985 global.maxsock++;
1986
1987 return NULL;
1988}
1989
1990static int server_agent_check_init(const char *file, int linenum,
1991 struct server *srv, struct proxy *curproxy)
1992{
1993 const char *ret;
1994
1995 if (!srv->do_agent)
1996 return 0;
1997
1998 if (!srv->agent.port) {
1999 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
2000 file, linenum, srv->id);
2001 return ERR_ALERT | ERR_FATAL;
2002 }
2003
2004 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
2005 if (ret) {
2006 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
2007 return ERR_ALERT | ERR_ABORT;
2008 }
2009
2010 return 0;
2011}
2012
2013#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2014static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
2015 struct server *srv, struct proxy *proxy)
2016{
2017 int ret;
2018 char *err = NULL;
2019
2020 if (!srv->sni_expr)
2021 return 0;
2022
2023 ret = server_parse_sni_expr(srv, proxy, &err);
2024 if (!ret)
2025 return 0;
2026
2027 display_parser_err(file, linenum, args, cur_arg, &err);
2028 free(err);
2029
2030 return ret;
2031}
2032#endif
2033
2034/*
2035 * Server initializations finalization.
2036 * Initialize health check, agent check and SNI expression if enabled.
2037 * Must not be called for a default server instance.
2038 */
2039static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2040 struct server *srv, struct proxy *px)
2041{
2042 int ret;
2043
2044 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2045 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2046 return ret;
2047 }
2048
2049#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2050 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2051 return ret;
2052#endif
2053
2054 if (srv->flags & SRV_F_BACKUP)
2055 px->srv_bck++;
2056 else
2057 px->srv_act++;
2058 srv_lb_commit_status(srv);
2059
2060 return 0;
2061}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002062
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002063/*
2064 * Parse as much as possible such a range string argument: low[-high]
2065 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2066 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2067 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2068 * Returns 0 if succeeded, -1 if not.
2069 */
2070static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2071{
2072 char *nb_high_arg;
2073
2074 *nb_high = 0;
2075 chunk_printf(&trash, "%s", arg);
2076 *nb_low = atoi(trash.str);
2077
2078 if ((nb_high_arg = strchr(trash.str, '-'))) {
2079 *nb_high_arg++ = '\0';
2080 *nb_high = atoi(nb_high_arg);
2081 }
2082 else {
2083 *nb_high += *nb_low;
2084 *nb_low = 1;
2085 }
2086
2087 if (*nb_low < 0 || *nb_high < *nb_low)
2088 return -1;
2089
2090 return 0;
2091}
2092
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002093static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2094{
2095 chunk_printf(&trash, "%s%d", prefix, nb);
2096 free(srv->id);
2097 srv->id = strdup(trash.str);
2098}
2099
2100/*
2101 * Initialize as much as possible servers from <srv> server template.
2102 * Note that a server template is a special server with
2103 * a few different parameters than a server which has
2104 * been parsed mostly the same way as a server.
2105 * Returns the number of servers succesfully allocated,
2106 * 'srv' template included.
2107 */
2108static int server_template_init(struct server *srv, struct proxy *px)
2109{
2110 int i;
2111 struct server *newsrv;
2112
2113 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2114 int check_init_state;
2115 int agent_init_state;
2116
2117 newsrv = new_server(px);
2118 if (!newsrv)
2119 goto err;
2120
2121 srv_settings_cpy(newsrv, srv, 1);
2122 srv_alloc_dns_resolution(newsrv, srv->hostname);
2123#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2124 if (newsrv->sni_expr) {
2125 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2126 if (!newsrv->ssl_ctx.sni)
2127 goto err;
2128 }
2129#endif
2130 /* Set this new server ID. */
2131 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2132
2133 /* Initial checks states. */
2134 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2135 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2136
2137 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2138 do_server_agent_check_init(newsrv, agent_init_state))
2139 goto err;
2140
2141 /* Linked backwards first. This will be restablished after parsing. */
2142 newsrv->next = px->srv;
2143 px->srv = newsrv;
2144 }
2145 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2146
2147 return i - srv->tmpl_info.nb_low;
2148
2149 err:
2150 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2151 if (newsrv) {
2152#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2153 release_sample_expr(newsrv->ssl_ctx.sni);
2154#endif
2155 free_check(&newsrv->agent);
2156 free_check(&newsrv->check);
2157 }
2158 free(newsrv);
2159 return i - srv->tmpl_info.nb_low;
2160}
2161
Willy Tarreau272adea2014-03-31 10:39:59 +02002162int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
2163{
2164 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002165 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002166 char *errmsg = NULL;
2167 int err_code = 0;
2168 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002169 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002170
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002171 if (!strcmp(args[0], "server") ||
2172 !strcmp(args[0], "default-server") ||
2173 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002174 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002175 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002176 int srv = !defsrv && !strcmp(args[0], "server");
2177 int srv_tmpl = !defsrv && !srv;
2178 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002179
2180 if (!defsrv && curproxy == defproxy) {
2181 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
2182 err_code |= ERR_ALERT | ERR_FATAL;
2183 goto out;
2184 }
2185 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
2186 err_code |= ERR_ALERT | ERR_FATAL;
2187
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002188 /* There is no mandatory first arguments for default server. */
2189 if (srv) {
2190 if (!*args[2]) {
2191 /* 'server' line number of argument check. */
2192 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
2193 file, linenum, args[0]);
2194 err_code |= ERR_ALERT | ERR_FATAL;
2195 goto out;
2196 }
2197
2198 err = invalid_char(args[1]);
2199 }
2200 else if (srv_tmpl) {
2201 if (!*args[3]) {
2202 /* 'server-template' line number of argument check. */
2203 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2204 file, linenum, args[0]);
2205 err_code |= ERR_ALERT | ERR_FATAL;
2206 goto out;
2207 }
2208
2209 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002210 }
2211
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002212 if (err) {
2213 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
2214 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002215 err_code |= ERR_ALERT | ERR_FATAL;
2216 goto out;
2217 }
2218
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002219 cur_arg = 2;
2220 if (srv_tmpl) {
2221 /* Parse server-template <nb | range> arg. */
2222 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
2223 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
2224 file, linenum, args[0], args[cur_arg]);
2225 err_code |= ERR_ALERT | ERR_FATAL;
2226 goto out;
2227 }
2228 cur_arg++;
2229 }
2230
Willy Tarreau272adea2014-03-31 10:39:59 +02002231 if (!defsrv) {
2232 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002233 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002234 struct protocol *proto;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002235 struct dns_resolution *curr_resolution;
Willy Tarreau272adea2014-03-31 10:39:59 +02002236
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002237 newsrv = new_server(curproxy);
2238 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002239 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2240 err_code |= ERR_ALERT | ERR_ABORT;
2241 goto out;
2242 }
2243
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002244 if (srv_tmpl) {
2245 newsrv->tmpl_info.nb_low = tmpl_range_low;
2246 newsrv->tmpl_info.nb_high = tmpl_range_high;
2247 }
2248
Willy Tarreau272adea2014-03-31 10:39:59 +02002249 /* the servers are linked backwards first */
2250 newsrv->next = curproxy->srv;
2251 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002252 newsrv->conf.file = strdup(file);
2253 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002254 /* Note: for a server template, its id is its prefix.
2255 * This is a temporary id which will be used for server allocations to come
2256 * after parsing.
2257 */
2258 if (srv)
2259 newsrv->id = strdup(args[1]);
2260 else
2261 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002262
2263 /* several ways to check the port component :
2264 * - IP => port=+0, relative (IPv4 only)
2265 * - IP: => port=+0, relative
2266 * - IP:N => port=N, absolute
2267 * - IP:+N => port=+N, relative
2268 * - IP:-N => port=-N, relative
2269 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002270 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002271 if (!sk) {
2272 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
2273 err_code |= ERR_ALERT | ERR_FATAL;
2274 goto out;
2275 }
2276
2277 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002278 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002279 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
2280 file, linenum, args[0], args[1]);
2281 err_code |= ERR_ALERT | ERR_FATAL;
2282 goto out;
2283 }
2284
2285 if (!port1 || !port2) {
2286 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002287 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002288 }
2289 else if (port1 != port2) {
2290 /* port range */
2291 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
2292 file, linenum, args[0], args[1], args[2]);
2293 err_code |= ERR_ALERT | ERR_FATAL;
2294 goto out;
2295 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002296
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002297 /* save hostname and create associated name resolution */
Willy Tarreau07101d52015-09-08 16:16:35 +02002298 newsrv->hostname = fqdn;
2299 if (!fqdn)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002300 goto skip_name_resolution;
2301
Willy Tarreau07101d52015-09-08 16:16:35 +02002302 fqdn = NULL;
Vincent Bernat02779b62016-04-03 13:48:43 +02002303 if ((curr_resolution = calloc(1, sizeof(*curr_resolution))) == NULL)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002304 goto skip_name_resolution;
2305
2306 curr_resolution->hostname_dn_len = dns_str_to_dn_label_len(newsrv->hostname);
2307 if ((curr_resolution->hostname_dn = calloc(curr_resolution->hostname_dn_len + 1, sizeof(char))) == NULL)
2308 goto skip_name_resolution;
2309 if ((dns_str_to_dn_label(newsrv->hostname, curr_resolution->hostname_dn, curr_resolution->hostname_dn_len + 1)) == NULL) {
2310 Alert("parsing [%s:%d] : Invalid hostname '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002311 file, linenum, args[cur_arg]);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002312 err_code |= ERR_ALERT | ERR_FATAL;
2313 goto out;
2314 }
2315
2316 curr_resolution->requester = newsrv;
2317 curr_resolution->requester_cb = snr_resolution_cb;
2318 curr_resolution->requester_error_cb = snr_resolution_error_cb;
2319 curr_resolution->status = RSLV_STATUS_NONE;
2320 curr_resolution->step = RSLV_STEP_NONE;
2321 /* a first resolution has been done by the configuration parser */
Baptiste Assmannf046f112015-08-27 22:12:46 +02002322 curr_resolution->last_resolution = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002323 newsrv->resolution = curr_resolution;
2324
2325 skip_name_resolution:
Willy Tarreau272adea2014-03-31 10:39:59 +02002326 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002327 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002328
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002329 if (!newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002330 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002331 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002332 err_code |= ERR_ALERT | ERR_FATAL;
2333 goto out;
2334 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002335
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002336 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002337 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002338 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002339 } else {
2340 newsrv = &curproxy->defsrv;
2341 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002342 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002343 }
2344
2345 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002346 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002347 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2348 if (err) {
2349 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2350 file, linenum, *err, newsrv->id);
2351 err_code |= ERR_ALERT | ERR_FATAL;
2352 goto out;
2353 }
2354 if (val <= 0) {
2355 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2356 file, linenum, val, args[cur_arg], newsrv->id);
2357 err_code |= ERR_ALERT | ERR_FATAL;
2358 goto out;
2359 }
2360 newsrv->agent.inter = val;
2361 cur_arg += 2;
2362 }
Misiekea849332017-01-09 09:39:51 +01002363 else if (!strcmp(args[cur_arg], "agent-addr")) {
2364 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2365 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2366 goto out;
2367 }
2368
2369 cur_arg += 2;
2370 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002371 else if (!strcmp(args[cur_arg], "agent-port")) {
2372 global.maxsock++;
2373 newsrv->agent.port = atol(args[cur_arg + 1]);
2374 cur_arg += 2;
2375 }
James Brown55f9ff12015-10-21 18:19:05 -07002376 else if (!strcmp(args[cur_arg], "agent-send")) {
2377 global.maxsock++;
2378 free(newsrv->agent.send_string);
2379 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2380 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2381 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2382 cur_arg += 2;
2383 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002384 else if (!strcmp(args[cur_arg], "init-addr")) {
2385 char *p, *end;
2386 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002387 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002388
2389 newsrv->init_addr_methods = 0;
2390 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2391
2392 for (p = args[cur_arg + 1]; *p; p = end) {
2393 /* cut on next comma */
2394 for (end = p; *end && *end != ','; end++);
2395 if (*end)
2396 *(end++) = 0;
2397
Willy Tarreau4310d362016-11-02 15:05:56 +01002398 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002399 if (!strcmp(p, "libc")) {
2400 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2401 }
2402 else if (!strcmp(p, "last")) {
2403 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2404 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002405 else if (!strcmp(p, "none")) {
2406 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2407 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002408 else if (str2ip2(p, &sa, 0)) {
2409 if (is_addr(&newsrv->init_addr)) {
2410 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2411 file, linenum, args[cur_arg], p);
2412 err_code |= ERR_ALERT | ERR_FATAL;
2413 goto out;
2414 }
2415 newsrv->init_addr = sa;
2416 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2417 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002418 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002419 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002420 file, linenum, args[cur_arg], p);
2421 err_code |= ERR_ALERT | ERR_FATAL;
2422 goto out;
2423 }
2424 if (!done) {
2425 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2426 file, linenum, args[cur_arg], p);
2427 err_code |= ERR_ALERT | ERR_FATAL;
2428 goto out;
2429 }
2430 }
2431 cur_arg += 2;
2432 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002433 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002434 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002435 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2436 cur_arg += 2;
2437 }
2438 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2439 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002440 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002441 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002442 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002443 else {
2444 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2445 file, linenum, args[cur_arg]);
2446 err_code |= ERR_ALERT | ERR_FATAL;
2447 goto out;
2448 }
2449 cur_arg += 2;
2450 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002451 else if (!strcmp(args[cur_arg], "resolve-net")) {
2452 char *p, *e;
2453 unsigned char mask;
2454 struct dns_options *opt;
2455
2456 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
2457 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2458 file, linenum, args[cur_arg]);
2459 err_code |= ERR_ALERT | ERR_FATAL;
2460 goto out;
2461 }
2462
2463 opt = &newsrv->dns_opts;
2464
2465 /* Split arguments by comma, and convert it from ipv4 or ipv6
2466 * string network in in_addr or in6_addr.
2467 */
2468 p = args[cur_arg + 1];
2469 e = p;
2470 while (*p != '\0') {
2471 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002472 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002473 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2474 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2475 err_code |= ERR_ALERT | ERR_FATAL;
2476 goto out;
2477 }
2478 /* look for end or comma. */
2479 while (*e != ',' && *e != '\0')
2480 e++;
2481 if (*e == ',') {
2482 *e = '\0';
2483 e++;
2484 }
2485 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2486 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2487 /* Try to convert input string from ipv4 or ipv6 network. */
2488 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2489 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2490 &mask)) {
2491 /* Try to convert input string from ipv6 network. */
2492 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2493 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2494 } else {
2495 /* All network conversions fail, retrun error. */
2496 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2497 file, linenum, args[cur_arg], p);
2498 err_code |= ERR_ALERT | ERR_FATAL;
2499 goto out;
2500 }
2501 opt->pref_net_nb++;
2502 p = e;
2503 }
2504
2505 cur_arg += 2;
2506 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002507 else if (!strcmp(args[cur_arg], "rise")) {
2508 if (!*args[cur_arg + 1]) {
2509 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2510 file, linenum, args[cur_arg]);
2511 err_code |= ERR_ALERT | ERR_FATAL;
2512 goto out;
2513 }
2514
2515 newsrv->check.rise = atol(args[cur_arg + 1]);
2516 if (newsrv->check.rise <= 0) {
2517 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2518 file, linenum, args[cur_arg]);
2519 err_code |= ERR_ALERT | ERR_FATAL;
2520 goto out;
2521 }
2522
2523 if (newsrv->check.health)
2524 newsrv->check.health = newsrv->check.rise;
2525 cur_arg += 2;
2526 }
2527 else if (!strcmp(args[cur_arg], "fall")) {
2528 newsrv->check.fall = atol(args[cur_arg + 1]);
2529
2530 if (!*args[cur_arg + 1]) {
2531 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2532 file, linenum, args[cur_arg]);
2533 err_code |= ERR_ALERT | ERR_FATAL;
2534 goto out;
2535 }
2536
2537 if (newsrv->check.fall <= 0) {
2538 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2539 file, linenum, args[cur_arg]);
2540 err_code |= ERR_ALERT | ERR_FATAL;
2541 goto out;
2542 }
2543
2544 cur_arg += 2;
2545 }
2546 else if (!strcmp(args[cur_arg], "inter")) {
2547 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2548 if (err) {
2549 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2550 file, linenum, *err, newsrv->id);
2551 err_code |= ERR_ALERT | ERR_FATAL;
2552 goto out;
2553 }
2554 if (val <= 0) {
2555 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2556 file, linenum, val, args[cur_arg], newsrv->id);
2557 err_code |= ERR_ALERT | ERR_FATAL;
2558 goto out;
2559 }
2560 newsrv->check.inter = val;
2561 cur_arg += 2;
2562 }
2563 else if (!strcmp(args[cur_arg], "fastinter")) {
2564 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2565 if (err) {
2566 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2567 file, linenum, *err, newsrv->id);
2568 err_code |= ERR_ALERT | ERR_FATAL;
2569 goto out;
2570 }
2571 if (val <= 0) {
2572 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2573 file, linenum, val, args[cur_arg], newsrv->id);
2574 err_code |= ERR_ALERT | ERR_FATAL;
2575 goto out;
2576 }
2577 newsrv->check.fastinter = val;
2578 cur_arg += 2;
2579 }
2580 else if (!strcmp(args[cur_arg], "downinter")) {
2581 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2582 if (err) {
2583 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2584 file, linenum, *err, newsrv->id);
2585 err_code |= ERR_ALERT | ERR_FATAL;
2586 goto out;
2587 }
2588 if (val <= 0) {
2589 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2590 file, linenum, val, args[cur_arg], newsrv->id);
2591 err_code |= ERR_ALERT | ERR_FATAL;
2592 goto out;
2593 }
2594 newsrv->check.downinter = val;
2595 cur_arg += 2;
2596 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002597 else if (!strcmp(args[cur_arg], "port")) {
2598 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002599 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002600 cur_arg += 2;
2601 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002602 else if (!strcmp(args[cur_arg], "weight")) {
2603 int w;
2604 w = atol(args[cur_arg + 1]);
2605 if (w < 0 || w > SRV_UWGHT_MAX) {
2606 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2607 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2608 err_code |= ERR_ALERT | ERR_FATAL;
2609 goto out;
2610 }
2611 newsrv->uweight = newsrv->iweight = w;
2612 cur_arg += 2;
2613 }
2614 else if (!strcmp(args[cur_arg], "minconn")) {
2615 newsrv->minconn = atol(args[cur_arg + 1]);
2616 cur_arg += 2;
2617 }
2618 else if (!strcmp(args[cur_arg], "maxconn")) {
2619 newsrv->maxconn = atol(args[cur_arg + 1]);
2620 cur_arg += 2;
2621 }
2622 else if (!strcmp(args[cur_arg], "maxqueue")) {
2623 newsrv->maxqueue = atol(args[cur_arg + 1]);
2624 cur_arg += 2;
2625 }
2626 else if (!strcmp(args[cur_arg], "slowstart")) {
2627 /* slowstart is stored in seconds */
2628 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2629 if (err) {
2630 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2631 file, linenum, *err, newsrv->id);
2632 err_code |= ERR_ALERT | ERR_FATAL;
2633 goto out;
2634 }
2635 newsrv->slowstart = (val + 999) / 1000;
2636 cur_arg += 2;
2637 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002638 else if (!strcmp(args[cur_arg], "on-error")) {
2639 if (!strcmp(args[cur_arg + 1], "fastinter"))
2640 newsrv->onerror = HANA_ONERR_FASTINTER;
2641 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2642 newsrv->onerror = HANA_ONERR_FAILCHK;
2643 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2644 newsrv->onerror = HANA_ONERR_SUDDTH;
2645 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2646 newsrv->onerror = HANA_ONERR_MARKDWN;
2647 else {
2648 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2649 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2650 file, linenum, args[cur_arg], args[cur_arg + 1]);
2651 err_code |= ERR_ALERT | ERR_FATAL;
2652 goto out;
2653 }
2654
2655 cur_arg += 2;
2656 }
2657 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2658 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2659 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2660 else {
2661 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2662 file, linenum, args[cur_arg], args[cur_arg + 1]);
2663 err_code |= ERR_ALERT | ERR_FATAL;
2664 goto out;
2665 }
2666
2667 cur_arg += 2;
2668 }
2669 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2670 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2671 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2672 else {
2673 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2674 file, linenum, args[cur_arg], args[cur_arg + 1]);
2675 err_code |= ERR_ALERT | ERR_FATAL;
2676 goto out;
2677 }
2678
2679 cur_arg += 2;
2680 }
2681 else if (!strcmp(args[cur_arg], "error-limit")) {
2682 if (!*args[cur_arg + 1]) {
2683 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2684 file, linenum, args[cur_arg]);
2685 err_code |= ERR_ALERT | ERR_FATAL;
2686 goto out;
2687 }
2688
2689 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2690
2691 if (newsrv->consecutive_errors_limit <= 0) {
2692 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2693 file, linenum, args[cur_arg]);
2694 err_code |= ERR_ALERT | ERR_FATAL;
2695 goto out;
2696 }
2697 cur_arg += 2;
2698 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002699 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002700 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2701 file, linenum, "usesrc", "source");
2702 err_code |= ERR_ALERT | ERR_FATAL;
2703 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002704 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002705 else {
2706 static int srv_dumped;
2707 struct srv_kw *kw;
2708 char *err;
2709
2710 kw = srv_find_kw(args[cur_arg]);
2711 if (kw) {
2712 char *err = NULL;
2713 int code;
2714
2715 if (!kw->parse) {
2716 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2717 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002718 if (kw->skip != -1)
2719 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002720 err_code |= ERR_ALERT | ERR_FATAL;
2721 goto out;
2722 }
2723
2724 if (defsrv && !kw->default_ok) {
2725 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2726 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002727 if (kw->skip != -1)
2728 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002729 err_code |= ERR_ALERT;
2730 continue;
2731 }
2732
2733 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2734 err_code |= code;
2735
2736 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002737 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002738 if (code & ERR_FATAL) {
2739 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002740 if (kw->skip != -1)
2741 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002742 goto out;
2743 }
2744 }
2745 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002746 if (kw->skip != -1)
2747 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002748 continue;
2749 }
2750
2751 err = NULL;
2752 if (!srv_dumped) {
2753 srv_dump_kws(&err);
2754 indent_msg(&err, 4);
2755 srv_dumped = 1;
2756 }
2757
2758 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2759 file, linenum, args[0], args[1], args[cur_arg],
2760 err ? " Registered keywords :" : "", err ? err : "");
2761 free(err);
2762
2763 err_code |= ERR_ALERT | ERR_FATAL;
2764 goto out;
2765 }
2766 }
2767
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002768 if (!defsrv)
2769 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2770 if (err_code & ERR_FATAL)
2771 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002772 if (srv_tmpl)
2773 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002774 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002775 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002776 return 0;
2777
2778 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002779 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002780 free(errmsg);
2781 return err_code;
2782}
2783
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002784/* Returns a pointer to the first server matching either id <id>.
2785 * NULL is returned if no match is found.
2786 * the lookup is performed in the backend <bk>
2787 */
2788struct server *server_find_by_id(struct proxy *bk, int id)
2789{
2790 struct eb32_node *eb32;
2791 struct server *curserver;
2792
2793 if (!bk || (id ==0))
2794 return NULL;
2795
2796 /* <bk> has no backend capabilities, so it can't have a server */
2797 if (!(bk->cap & PR_CAP_BE))
2798 return NULL;
2799
2800 curserver = NULL;
2801
2802 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2803 if (eb32)
2804 curserver = container_of(eb32, struct server, conf.id);
2805
2806 return curserver;
2807}
2808
2809/* Returns a pointer to the first server matching either name <name>, or id
2810 * if <name> starts with a '#'. NULL is returned if no match is found.
2811 * the lookup is performed in the backend <bk>
2812 */
2813struct server *server_find_by_name(struct proxy *bk, const char *name)
2814{
2815 struct server *curserver;
2816
2817 if (!bk || !name)
2818 return NULL;
2819
2820 /* <bk> has no backend capabilities, so it can't have a server */
2821 if (!(bk->cap & PR_CAP_BE))
2822 return NULL;
2823
2824 curserver = NULL;
2825 if (*name == '#') {
2826 curserver = server_find_by_id(bk, atoi(name + 1));
2827 if (curserver)
2828 return curserver;
2829 }
2830 else {
2831 curserver = bk->srv;
2832
2833 while (curserver && (strcmp(curserver->id, name) != 0))
2834 curserver = curserver->next;
2835
2836 if (curserver)
2837 return curserver;
2838 }
2839
2840 return NULL;
2841}
2842
2843struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2844{
2845 struct server *byname;
2846 struct server *byid;
2847
2848 if (!name && !id)
2849 return NULL;
2850
2851 if (diff)
2852 *diff = 0;
2853
2854 byname = byid = NULL;
2855
2856 if (name) {
2857 byname = server_find_by_name(bk, name);
2858 if (byname && (!id || byname->puid == id))
2859 return byname;
2860 }
2861
2862 /* remaining possibilities :
2863 * - name not set
2864 * - name set but not found
2865 * - name found but ID doesn't match
2866 */
2867 if (id) {
2868 byid = server_find_by_id(bk, id);
2869 if (byid) {
2870 if (byname) {
2871 /* use id only if forced by configuration */
2872 if (byid->flags & SRV_F_FORCED_ID) {
2873 if (diff)
2874 *diff |= 2;
2875 return byid;
2876 }
2877 else {
2878 if (diff)
2879 *diff |= 1;
2880 return byname;
2881 }
2882 }
2883
2884 /* remaining possibilities:
2885 * - name not set
2886 * - name set but not found
2887 */
2888 if (name && diff)
2889 *diff |= 2;
2890 return byid;
2891 }
2892
2893 /* id bot found */
2894 if (byname) {
2895 if (diff)
2896 *diff |= 1;
2897 return byname;
2898 }
2899 }
2900
2901 return NULL;
2902}
2903
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002904/* Update a server state using the parameters available in the params list */
2905static void srv_update_state(struct server *srv, int version, char **params)
2906{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002907 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002908 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002909
2910 /* fields since version 1
2911 * and common to all other upcoming versions
2912 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002913 enum srv_state srv_op_state;
2914 enum srv_admin srv_admin_state;
2915 unsigned srv_uweight, srv_iweight;
2916 unsigned long srv_last_time_change;
2917 short srv_check_status;
2918 enum chk_result srv_check_result;
2919 int srv_check_health;
2920 int srv_check_state, srv_agent_state;
2921 int bk_f_forced_id;
2922 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002923 int fqdn_set_by_cli;
2924 const char *fqdn;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002925
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002926 fqdn = NULL;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002927 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002928 switch (version) {
2929 case 1:
2930 /*
2931 * now we can proceed with server's state update:
2932 * srv_addr: params[0]
2933 * srv_op_state: params[1]
2934 * srv_admin_state: params[2]
2935 * srv_uweight: params[3]
2936 * srv_iweight: params[4]
2937 * srv_last_time_change: params[5]
2938 * srv_check_status: params[6]
2939 * srv_check_result: params[7]
2940 * srv_check_health: params[8]
2941 * srv_check_state: params[9]
2942 * srv_agent_state: params[10]
2943 * bk_f_forced_id: params[11]
2944 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002945 * srv_fqdn: params[13]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002946 */
2947
2948 /* validating srv_op_state */
2949 p = NULL;
2950 errno = 0;
2951 srv_op_state = strtol(params[1], &p, 10);
2952 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2953 (srv_op_state != SRV_ST_STOPPED &&
2954 srv_op_state != SRV_ST_STARTING &&
2955 srv_op_state != SRV_ST_RUNNING &&
2956 srv_op_state != SRV_ST_STOPPING)) {
2957 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2958 }
2959
2960 /* validating srv_admin_state */
2961 p = NULL;
2962 errno = 0;
2963 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002964 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002965
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002966 /* inherited statuses will be recomputed later.
2967 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2968 */
2969 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002970
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002971 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2972 (srv_admin_state != 0 &&
2973 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002974 srv_admin_state != SRV_ADMF_CMAINT &&
2975 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002976 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002977 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002978 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2979 }
2980
2981 /* validating srv_uweight */
2982 p = NULL;
2983 errno = 0;
2984 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002985 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002986 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2987
2988 /* validating srv_iweight */
2989 p = NULL;
2990 errno = 0;
2991 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002992 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002993 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2994
2995 /* validating srv_last_time_change */
2996 p = NULL;
2997 errno = 0;
2998 srv_last_time_change = strtol(params[5], &p, 10);
2999 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
3000 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
3001
3002 /* validating srv_check_status */
3003 p = NULL;
3004 errno = 0;
3005 srv_check_status = strtol(params[6], &p, 10);
3006 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3007 (srv_check_status >= HCHK_STATUS_SIZE))
3008 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3009
3010 /* validating srv_check_result */
3011 p = NULL;
3012 errno = 0;
3013 srv_check_result = strtol(params[7], &p, 10);
3014 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3015 (srv_check_result != CHK_RES_UNKNOWN &&
3016 srv_check_result != CHK_RES_NEUTRAL &&
3017 srv_check_result != CHK_RES_FAILED &&
3018 srv_check_result != CHK_RES_PASSED &&
3019 srv_check_result != CHK_RES_CONDPASS)) {
3020 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3021 }
3022
3023 /* validating srv_check_health */
3024 p = NULL;
3025 errno = 0;
3026 srv_check_health = strtol(params[8], &p, 10);
3027 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3028 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3029
3030 /* validating srv_check_state */
3031 p = NULL;
3032 errno = 0;
3033 srv_check_state = strtol(params[9], &p, 10);
3034 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3035 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3036 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3037
3038 /* validating srv_agent_state */
3039 p = NULL;
3040 errno = 0;
3041 srv_agent_state = strtol(params[10], &p, 10);
3042 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3043 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3044 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3045
3046 /* validating bk_f_forced_id */
3047 p = NULL;
3048 errno = 0;
3049 bk_f_forced_id = strtol(params[11], &p, 10);
3050 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3051 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3052
3053 /* validating srv_f_forced_id */
3054 p = NULL;
3055 errno = 0;
3056 srv_f_forced_id = strtol(params[12], &p, 10);
3057 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3058 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3059
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003060 /* validating srv_fqdn */
3061 fqdn = params[13];
3062 if (fqdn && *fqdn == '-')
3063 fqdn = NULL;
3064 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3065 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3066 fqdn = NULL;
3067 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003068
3069 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02003070 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003071 goto out;
3072
3073 /* recover operational state and apply it to this server
3074 * and all servers tracking this one */
3075 switch (srv_op_state) {
3076 case SRV_ST_STOPPED:
3077 srv->check.health = 0;
3078 srv_set_stopped(srv, "changed from server-state after a reload");
3079 break;
3080 case SRV_ST_STARTING:
3081 srv->state = srv_op_state;
3082 break;
3083 case SRV_ST_STOPPING:
3084 srv->check.health = srv->check.rise + srv->check.fall - 1;
3085 srv_set_stopping(srv, "changed from server-state after a reload");
3086 break;
3087 case SRV_ST_RUNNING:
3088 srv->check.health = srv->check.rise + srv->check.fall - 1;
3089 srv_set_running(srv, "");
3090 break;
3091 }
3092
3093 /* When applying server state, the following rules apply:
3094 * - in case of a configuration change, we apply the setting from the new
3095 * configuration, regardless of old running state
3096 * - if no configuration change, we apply old running state only if old running
3097 * state is different from new configuration state
3098 */
3099 /* configuration has changed */
3100 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->admin & SRV_ADMF_CMAINT)) {
3101 if (srv->admin & SRV_ADMF_CMAINT)
3102 srv_adm_set_maint(srv);
3103 else
3104 srv_adm_set_ready(srv);
3105 }
3106 /* configuration is the same, let's compate old running state and new conf state */
3107 else {
3108 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->admin & SRV_ADMF_CMAINT))
3109 srv_adm_set_maint(srv);
3110 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->admin & SRV_ADMF_CMAINT))
3111 srv_adm_set_ready(srv);
3112 }
3113 /* apply drain mode if server is currently enabled */
3114 if (!(srv->admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
3115 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003116 * (srv->iweight is the weight set up in configuration).
3117 * There are two possible reasons for FDRAIN to have been present :
3118 * - previous config weight was zero
3119 * - "set server b/s drain" was sent to the CLI
3120 *
3121 * In the first case, we simply want to drop this drain state
3122 * if the new weight is not zero anymore, meaning the administrator
3123 * has intentionally turned the weight back to a positive value to
3124 * enable the server again after an operation. In the second case,
3125 * the drain state was forced on the CLI regardless of the config's
3126 * weight so we don't want a change to the config weight to lose this
3127 * status. What this means is :
3128 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3129 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003130 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003131 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003132 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003133 }
3134
3135 srv->last_change = date.tv_sec - srv_last_time_change;
3136 srv->check.status = srv_check_status;
3137 srv->check.result = srv_check_result;
3138 srv->check.health = srv_check_health;
3139
3140 /* Only case we want to apply is removing ENABLED flag which could have been
3141 * done by the "disable health" command over the stats socket
3142 */
3143 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3144 (srv_check_state & CHK_ST_CONFIGURED) &&
3145 !(srv_check_state & CHK_ST_ENABLED))
3146 srv->check.state &= ~CHK_ST_ENABLED;
3147
3148 /* Only case we want to apply is removing ENABLED flag which could have been
3149 * done by the "disable agent" command over the stats socket
3150 */
3151 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3152 (srv_agent_state & CHK_ST_CONFIGURED) &&
3153 !(srv_agent_state & CHK_ST_ENABLED))
3154 srv->agent.state &= ~CHK_ST_ENABLED;
3155
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003156 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3157 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003158 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003159 * It means that a configuration file change has precedence over a unix socket change
3160 * for server's weight
3161 *
3162 * by default, HAProxy applies the following weight when parsing the configuration
3163 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003164 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003165 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003166 srv->uweight = srv_uweight;
3167 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003168 server_recalc_eweight(srv);
3169
Willy Tarreaue5a60682016-11-09 14:54:53 +01003170 /* load server IP address */
3171 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003172
3173 if (fqdn && srv->hostname) {
3174 if (!strcmp(srv->hostname, fqdn)) {
3175 /* Here we reset the 'set from stats socket FQDN' flag
3176 * to support such transitions:
3177 * Let's say initial FQDN value is foo1 (in configuration file).
3178 * - FQDN changed from stats socket, from foo1 to foo2 value,
3179 * - FQDN changed again from file configuration (with the same previous value
3180 set from stats socket, from foo1 to foo2 value),
3181 * - reload for any other reason than a FQDN modification,
3182 * the configuration file FQDN matches the fqdn server state file value.
3183 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
3184 * any futher FQDN modification.
3185 */
3186 srv->admin &= ~SRV_ADMF_HMAINT;
3187 }
3188 else {
3189 /* If the FDQN has been changed from stats socket,
3190 * apply fqdn state file value (which is the value set
3191 * from stats socket).
3192 */
3193 if (fqdn_set_by_cli) {
3194 srv_set_fqdn(srv, fqdn);
3195 srv->admin |= SRV_ADMF_HMAINT;
3196 }
3197 }
3198 }
3199
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003200 break;
3201 default:
3202 chunk_appendf(msg, ", version '%d' not supported", version);
3203 }
3204
3205 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003206 if (msg->len) {
3207 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02003208 Warning("server-state application failed for server '%s/%s'%s",
3209 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003210 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003211}
3212
3213/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3214 * For each proxy, it does the following:
3215 * - opens its server state file (either one or local one)
3216 * - read whole file, line by line
3217 * - analyse each line to check if it matches our current backend:
3218 * - backend name matches
3219 * - backend id matches if id is forced and name doesn't match
3220 * - if the server pointed by the line is found, then state is applied
3221 *
3222 * If the running backend uuid or id differs from the state file, then HAProxy reports
3223 * a warning.
3224 */
3225void apply_server_state(void)
3226{
3227 char *cur, *end;
3228 char mybuf[SRV_STATE_LINE_MAXLEN];
3229 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003230 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3231 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003232 int arg, srv_arg, version, diff;
3233 FILE *f;
3234 char *filepath;
3235 char globalfilepath[MAXPATHLEN + 1];
3236 char localfilepath[MAXPATHLEN + 1];
3237 int len, fileopenerr, globalfilepathlen, localfilepathlen;
3238 extern struct proxy *proxy;
3239 struct proxy *curproxy, *bk;
3240 struct server *srv;
3241
3242 globalfilepathlen = 0;
3243 /* create the globalfilepath variable */
3244 if (global.server_state_file) {
3245 /* absolute path or no base directory provided */
3246 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3247 len = strlen(global.server_state_file);
3248 if (len > MAXPATHLEN) {
3249 globalfilepathlen = 0;
3250 goto globalfileerror;
3251 }
3252 memcpy(globalfilepath, global.server_state_file, len);
3253 globalfilepath[len] = '\0';
3254 globalfilepathlen = len;
3255 }
3256 else if (global.server_state_base) {
3257 len = strlen(global.server_state_base);
3258 globalfilepathlen += len;
3259
3260 if (globalfilepathlen > MAXPATHLEN) {
3261 globalfilepathlen = 0;
3262 goto globalfileerror;
3263 }
3264 strncpy(globalfilepath, global.server_state_base, len);
3265 globalfilepath[globalfilepathlen] = 0;
3266
3267 /* append a slash if needed */
3268 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3269 if (globalfilepathlen + 1 > MAXPATHLEN) {
3270 globalfilepathlen = 0;
3271 goto globalfileerror;
3272 }
3273 globalfilepath[globalfilepathlen++] = '/';
3274 }
3275
3276 len = strlen(global.server_state_file);
3277 if (globalfilepathlen + len > MAXPATHLEN) {
3278 globalfilepathlen = 0;
3279 goto globalfileerror;
3280 }
3281 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3282 globalfilepathlen += len;
3283 globalfilepath[globalfilepathlen++] = 0;
3284 }
3285 }
3286 globalfileerror:
3287 if (globalfilepathlen == 0)
3288 globalfilepath[0] = '\0';
3289
3290 /* read servers state from local file */
3291 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3292 /* servers are only in backends */
3293 if (!(curproxy->cap & PR_CAP_BE))
3294 continue;
3295 fileopenerr = 0;
3296 filepath = NULL;
3297
3298 /* search server state file path and name */
3299 switch (curproxy->load_server_state_from_file) {
3300 /* read servers state from global file */
3301 case PR_SRV_STATE_FILE_GLOBAL:
3302 /* there was an error while generating global server state file path */
3303 if (globalfilepathlen == 0)
3304 continue;
3305 filepath = globalfilepath;
3306 fileopenerr = 1;
3307 break;
3308 /* this backend has its own file */
3309 case PR_SRV_STATE_FILE_LOCAL:
3310 localfilepathlen = 0;
3311 localfilepath[0] = '\0';
3312 len = 0;
3313 /* create the localfilepath variable */
3314 /* absolute path or no base directory provided */
3315 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3316 len = strlen(curproxy->server_state_file_name);
3317 if (len > MAXPATHLEN) {
3318 localfilepathlen = 0;
3319 goto localfileerror;
3320 }
3321 memcpy(localfilepath, curproxy->server_state_file_name, len);
3322 localfilepath[len] = '\0';
3323 localfilepathlen = len;
3324 }
3325 else if (global.server_state_base) {
3326 len = strlen(global.server_state_base);
3327 localfilepathlen += len;
3328
3329 if (localfilepathlen > MAXPATHLEN) {
3330 localfilepathlen = 0;
3331 goto localfileerror;
3332 }
3333 strncpy(localfilepath, global.server_state_base, len);
3334 localfilepath[localfilepathlen] = 0;
3335
3336 /* append a slash if needed */
3337 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3338 if (localfilepathlen + 1 > MAXPATHLEN) {
3339 localfilepathlen = 0;
3340 goto localfileerror;
3341 }
3342 localfilepath[localfilepathlen++] = '/';
3343 }
3344
3345 len = strlen(curproxy->server_state_file_name);
3346 if (localfilepathlen + len > MAXPATHLEN) {
3347 localfilepathlen = 0;
3348 goto localfileerror;
3349 }
3350 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3351 localfilepathlen += len;
3352 localfilepath[localfilepathlen++] = 0;
3353 }
3354 filepath = localfilepath;
3355 localfileerror:
3356 if (localfilepathlen == 0)
3357 localfilepath[0] = '\0';
3358
3359 break;
3360 case PR_SRV_STATE_FILE_NONE:
3361 default:
3362 continue;
3363 }
3364
3365 /* preload global state file */
3366 errno = 0;
3367 f = fopen(filepath, "r");
3368 if (errno && fileopenerr)
3369 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3370 if (!f)
3371 continue;
3372
3373 mybuf[0] = '\0';
3374 mybuflen = 0;
3375 version = 0;
3376
3377 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003378 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3379 Warning("Can't read first line of the server state file '%s'\n", filepath);
3380 goto fileclose;
3381 }
3382
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003383 cur = mybuf;
3384 version = atoi(cur);
3385 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3386 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003387 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003388
3389 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3390 int bk_f_forced_id = 0;
3391 int check_id = 0;
3392 int check_name = 0;
3393
3394 mybuflen = strlen(mybuf);
3395 cur = mybuf;
3396 end = cur + mybuflen;
3397
3398 bk = NULL;
3399 srv = NULL;
3400
3401 /* we need at least one character */
3402 if (mybuflen == 0)
3403 continue;
3404
3405 /* ignore blank characters at the beginning of the line */
3406 while (isspace(*cur))
3407 ++cur;
3408
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003409 /* Ignore empty or commented lines */
3410 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003411 continue;
3412
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003413 /* truncated lines */
3414 if (mybuf[mybuflen - 1] != '\n') {
3415 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003416 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003417 }
3418
3419 /* Removes trailing '\n' */
3420 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003421
3422 /* we're now ready to move the line into *srv_params[] */
3423 params[0] = cur;
3424 arg = 1;
3425 srv_arg = 0;
3426 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3427 if (isspace(*cur)) {
3428 *cur = '\0';
3429 ++cur;
3430 while (isspace(*cur))
3431 ++cur;
3432 switch (version) {
3433 case 1:
3434 /*
3435 * srv_addr: params[4] => srv_params[0]
3436 * srv_op_state: params[5] => srv_params[1]
3437 * srv_admin_state: params[6] => srv_params[2]
3438 * srv_uweight: params[7] => srv_params[3]
3439 * srv_iweight: params[8] => srv_params[4]
3440 * srv_last_time_change: params[9] => srv_params[5]
3441 * srv_check_status: params[10] => srv_params[6]
3442 * srv_check_result: params[11] => srv_params[7]
3443 * srv_check_health: params[12] => srv_params[8]
3444 * srv_check_state: params[13] => srv_params[9]
3445 * srv_agent_state: params[14] => srv_params[10]
3446 * bk_f_forced_id: params[15] => srv_params[11]
3447 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003448 * srv_fqdn: params[17] => srv_params[13]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003449 */
3450 if (arg >= 4) {
3451 srv_params[srv_arg] = cur;
3452 ++srv_arg;
3453 }
3454 break;
3455 }
3456
3457 params[arg] = cur;
3458 ++arg;
3459 }
3460 else {
3461 ++cur;
3462 }
3463 }
3464
3465 /* if line is incomplete line, then ignore it.
3466 * otherwise, update useful flags */
3467 switch (version) {
3468 case 1:
3469 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3470 continue;
3471 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3472 check_id = (atoi(params[0]) == curproxy->uuid);
3473 check_name = (strcmp(curproxy->id, params[1]) == 0);
3474 break;
3475 }
3476
3477 diff = 0;
3478 bk = curproxy;
3479
3480 /* if backend can't be found, let's continue */
3481 if (!check_id && !check_name)
3482 continue;
3483 else if (!check_id && check_name) {
3484 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3485 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3486 }
3487 else if (check_id && !check_name) {
3488 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3489 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3490 /* if name doesn't match, we still want to update curproxy if the backend id
3491 * was forced in previous the previous configuration */
3492 if (!bk_f_forced_id)
3493 continue;
3494 }
3495
3496 /* look for the server by its id: param[2] */
3497 /* else look for the server by its name: param[3] */
3498 diff = 0;
3499 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3500
3501 if (!srv) {
3502 /* if no server found, then warning and continue with next line */
3503 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3504 params[3], params[2], params[0], params[1]);
3505 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3506 params[3], params[2], params[0], params[1]);
3507 continue;
3508 }
3509 else if (diff & PR_FBM_MISMATCH_ID) {
3510 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);
3511 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);
3512 }
3513 else if (diff & PR_FBM_MISMATCH_NAME) {
3514 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);
3515 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);
3516 }
3517
3518 /* now we can proceed with server's state update */
3519 srv_update_state(srv, version, srv_params);
3520 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003521fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003522 fclose(f);
3523 }
3524}
3525
Simon Horman7d09b9a2013-02-12 10:45:51 +09003526/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003527 * update a server's current IP address.
3528 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3529 * ip is in network format.
3530 * updater is a string which contains an information about the requester of the update.
3531 * updater is used if not NULL.
3532 *
3533 * A log line and a stderr warning message is generated based on server's backend options.
3534 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003535int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003536{
3537 /* generates a log line and a warning on stderr */
3538 if (1) {
3539 /* book enough space for both IPv4 and IPv6 */
3540 char oldip[INET6_ADDRSTRLEN];
3541 char newip[INET6_ADDRSTRLEN];
3542
3543 memset(oldip, '\0', INET6_ADDRSTRLEN);
3544 memset(newip, '\0', INET6_ADDRSTRLEN);
3545
3546 /* copy old IP address in a string */
3547 switch (s->addr.ss_family) {
3548 case AF_INET:
3549 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3550 break;
3551 case AF_INET6:
3552 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3553 break;
3554 };
3555
3556 /* copy new IP address in a string */
3557 switch (ip_sin_family) {
3558 case AF_INET:
3559 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3560 break;
3561 case AF_INET6:
3562 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3563 break;
3564 };
3565
3566 /* save log line into a buffer */
3567 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3568 s->proxy->id, s->id, oldip, newip, updater);
3569
3570 /* write the buffer on stderr */
3571 Warning("%s.\n", trash.str);
3572
3573 /* send a log */
3574 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3575 }
3576
3577 /* save the new IP family */
3578 s->addr.ss_family = ip_sin_family;
3579 /* save the new IP address */
3580 switch (ip_sin_family) {
3581 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003582 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003583 break;
3584 case AF_INET6:
3585 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3586 break;
3587 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003588 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003589
3590 return 0;
3591}
3592
3593/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003594 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3595 *
3596 * Caller can pass its name through <updater> to get it integrated in the response message
3597 * returned by the function.
3598 *
3599 * The function first does the following, in that order:
3600 * - validates the new addr and/or port
3601 * - checks if an update is required (new IP or port is different than current ones)
3602 * - checks the update is allowed:
3603 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3604 * - allow all changes if no CHECKS are configured
3605 * - if CHECK is configured:
3606 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3607 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3608 * conditions are met
3609 */
3610const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3611{
3612 struct sockaddr_storage sa;
3613 int ret, port_change_required;
3614 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003615 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003616 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003617 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003618
3619 msg = get_trash_chunk();
3620 chunk_reset(msg);
3621
3622 if (addr) {
3623 memset(&sa, 0, sizeof(struct sockaddr_storage));
3624 if (str2ip2(addr, &sa, 0) == NULL) {
3625 chunk_printf(msg, "Invalid addr '%s'", addr);
3626 goto out;
3627 }
3628
3629 /* changes are allowed on AF_INET* families only */
3630 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3631 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3632 goto out;
3633 }
3634
3635 /* collecting data currently setup */
3636 memset(current_addr, '\0', sizeof(current_addr));
3637 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3638 /* changes are allowed on AF_INET* families only */
3639 if ((ret != AF_INET) && (ret != AF_INET6)) {
3640 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3641 goto out;
3642 }
3643
3644 /* applying ADDR changes if required and allowed
3645 * ipcmp returns 0 when both ADDR are the same
3646 */
3647 if (ipcmp(&s->addr, &sa) == 0) {
3648 chunk_appendf(msg, "no need to change the addr");
3649 goto port;
3650 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003651 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003652 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003653
3654 /* we also need to update check's ADDR only if it uses the server's one */
3655 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003656 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003657 }
3658
3659 /* we also need to update agent ADDR only if it use the server's one */
3660 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003661 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003662 }
3663
3664 /* update report for caller */
3665 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3666 }
3667
3668 port:
3669 if (port) {
3670 char sign = '\0';
3671 char *endptr;
3672
3673 if (addr)
3674 chunk_appendf(msg, ", ");
3675
3676 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003677 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003678
3679 /* check if PORT change is required */
3680 port_change_required = 0;
3681
3682 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003683 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003684 new_port = strtol(port, &endptr, 10);
3685 if ((errno != 0) || (port == endptr)) {
3686 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3687 goto out;
3688 }
3689
3690 /* check if caller triggers a port mapped or offset */
3691 if (sign == '-' || (sign == '+')) {
3692 /* check if server currently uses port map */
3693 if (!(s->flags & SRV_F_MAPPORTS)) {
3694 /* switch from fixed port to port map mandatorily triggers
3695 * a port change */
3696 port_change_required = 1;
3697 /* check is configured
3698 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3699 * prevent PORT change if check doesn't have it's dedicated port while switching
3700 * to port mapping */
3701 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3702 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.");
3703 goto out;
3704 }
3705 }
3706 /* we're already using port maps */
3707 else {
3708 port_change_required = current_port != new_port;
3709 }
3710 }
3711 /* fixed port */
3712 else {
3713 port_change_required = current_port != new_port;
3714 }
3715
3716 /* applying PORT changes if required and update response message */
3717 if (port_change_required) {
3718 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003719 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003720 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003721
3722 /* prepare message */
3723 chunk_appendf(msg, "port changed from '");
3724 if (s->flags & SRV_F_MAPPORTS)
3725 chunk_appendf(msg, "+");
3726 chunk_appendf(msg, "%d' to '", current_port);
3727
3728 if (sign == '-') {
3729 s->flags |= SRV_F_MAPPORTS;
3730 chunk_appendf(msg, "%c", sign);
3731 /* just use for result output */
3732 new_port = -new_port;
3733 }
3734 else if (sign == '+') {
3735 s->flags |= SRV_F_MAPPORTS;
3736 chunk_appendf(msg, "%c", sign);
3737 }
3738 else {
3739 s->flags &= ~SRV_F_MAPPORTS;
3740 }
3741
3742 chunk_appendf(msg, "%d'", new_port);
3743
3744 /* we also need to update health checks port only if it uses server's realport */
3745 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3746 s->check.port = new_port;
3747 }
3748 }
3749 else {
3750 chunk_appendf(msg, "no need to change the port");
3751 }
3752 }
3753
3754out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003755 if (changed)
3756 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003757 if (updater)
3758 chunk_appendf(msg, " by '%s'", updater);
3759 chunk_appendf(msg, "\n");
3760 return msg->str;
3761}
3762
3763
3764/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003765 * update server status based on result of name resolution
3766 * returns:
3767 * 0 if server status is updated
3768 * 1 if server status has not changed
3769 */
3770int snr_update_srv_status(struct server *s)
3771{
3772 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003773 struct dns_resolvers *resolvers;
3774
3775 resolvers = resolution->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003776
3777 switch (resolution->status) {
3778 case RSLV_STATUS_NONE:
3779 /* status when HAProxy has just (re)started */
3780 trigger_resolution(s);
3781 break;
3782
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003783 case RSLV_STATUS_VALID:
3784 /*
3785 * resume health checks
3786 * server will be turned back on if health check is safe
3787 */
3788 if (!(s->admin & SRV_ADMF_RMAINT))
3789 return 1;
3790 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3791 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3792 s->proxy->id, s->id);
3793
3794 Warning("%s.\n", trash.str);
3795 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3796 return 0;
3797
3798 case RSLV_STATUS_NX:
3799 /* stop server if resolution is NX for a long enough period */
3800 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
3801 if (s->admin & SRV_ADMF_RMAINT)
3802 return 1;
3803 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3804 return 0;
3805 }
3806 break;
3807
3808 case RSLV_STATUS_TIMEOUT:
3809 /* stop server if resolution is TIMEOUT for a long enough period */
3810 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
3811 if (s->admin & SRV_ADMF_RMAINT)
3812 return 1;
3813 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3814 return 0;
3815 }
3816 break;
3817
3818 case RSLV_STATUS_REFUSED:
3819 /* stop server if resolution is REFUSED for a long enough period */
3820 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
3821 if (s->admin & SRV_ADMF_RMAINT)
3822 return 1;
3823 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3824 return 0;
3825 }
3826 break;
3827
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003828 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003829 /* stop server if resolution is in unmatched error for a long enough period */
3830 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
3831 if (s->admin & SRV_ADMF_RMAINT)
3832 return 1;
3833 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3834 return 0;
3835 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003836 break;
3837 }
3838
3839 return 1;
3840}
3841
3842/*
3843 * Server Name Resolution valid response callback
3844 * It expects:
3845 * - <nameserver>: the name server which answered the valid response
3846 * - <response>: buffer containing a valid DNS response
3847 * - <response_len>: size of <response>
3848 * It performs the following actions:
3849 * - ignore response if current ip found and server family not met
3850 * - update with first new ip found if family is met and current IP is not found
3851 * returns:
3852 * 0 on error
3853 * 1 when no error or safe ignore
3854 */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003855int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *nameserver, struct dns_response_packet *dns_p)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003856{
3857 struct server *s;
3858 void *serverip, *firstip;
3859 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003860 int ret;
3861 struct chunk *chk = get_trash_chunk();
3862
3863 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003864 firstip = NULL; /* pointer to the first valid response found */
3865 /* it will be used as the new IP if a change is required */
3866 firstip_sin_family = AF_UNSPEC;
3867 serverip = NULL; /* current server IP address */
3868
3869 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003870 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003871
3872 /* initializing server IP pointer */
3873 server_sin_family = s->addr.ss_family;
3874 switch (server_sin_family) {
3875 case AF_INET:
3876 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3877 break;
3878
3879 case AF_INET6:
3880 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3881 break;
3882
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003883 case AF_UNSPEC:
3884 break;
3885
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003886 default:
3887 goto invalid;
3888 }
3889
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02003890 ret = dns_get_ip_from_response(dns_p, resolution,
Thierry Fournierada34842016-02-17 21:25:09 +01003891 serverip, server_sin_family, &firstip,
3892 &firstip_sin_family);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003893
3894 switch (ret) {
3895 case DNS_UPD_NO:
3896 if (resolution->status != RSLV_STATUS_VALID) {
3897 resolution->status = RSLV_STATUS_VALID;
3898 resolution->last_status_change = now_ms;
3899 }
3900 goto stop_resolution;
3901
3902 case DNS_UPD_SRVIP_NOT_FOUND:
3903 goto save_ip;
3904
3905 case DNS_UPD_CNAME:
3906 if (resolution->status != RSLV_STATUS_VALID) {
3907 resolution->status = RSLV_STATUS_VALID;
3908 resolution->last_status_change = now_ms;
3909 }
3910 goto invalid;
3911
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003912 case DNS_UPD_NO_IP_FOUND:
3913 if (resolution->status != RSLV_STATUS_OTHER) {
3914 resolution->status = RSLV_STATUS_OTHER;
3915 resolution->last_status_change = now_ms;
3916 }
3917 goto stop_resolution;
3918
Baptiste Assmannfad03182015-10-28 02:03:32 +01003919 case DNS_UPD_NAME_ERROR:
3920 /* if this is not the last expected response, we ignore it */
3921 if (resolution->nb_responses < nameserver->resolvers->count_nameservers)
3922 return 0;
3923 /* update resolution status to OTHER error type */
3924 if (resolution->status != RSLV_STATUS_OTHER) {
3925 resolution->status = RSLV_STATUS_OTHER;
3926 resolution->last_status_change = now_ms;
3927 }
3928 goto stop_resolution;
3929
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003930 default:
3931 goto invalid;
3932
3933 }
3934
3935 save_ip:
3936 nameserver->counters.update += 1;
3937 if (resolution->status != RSLV_STATUS_VALID) {
3938 resolution->status = RSLV_STATUS_VALID;
3939 resolution->last_status_change = now_ms;
3940 }
3941
3942 /* save the first ip we found */
3943 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3944 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3945
3946 stop_resolution:
3947 /* update last resolution date and time */
3948 resolution->last_resolution = now_ms;
3949 /* reset current status flag */
3950 resolution->step = RSLV_STEP_NONE;
3951 /* reset values */
3952 dns_reset_resolution(resolution);
3953
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003954 dns_update_resolvers_timeout(nameserver->resolvers);
3955
3956 snr_update_srv_status(s);
3957 return 0;
3958
3959 invalid:
3960 nameserver->counters.invalid += 1;
3961 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3962 goto stop_resolution;
3963
3964 snr_update_srv_status(s);
3965 return 0;
3966}
3967
3968/*
3969 * Server Name Resolution error management callback
3970 * returns:
3971 * 0 on error
3972 * 1 when no error or safe ignore
3973 */
3974int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
3975{
3976 struct server *s;
3977 struct dns_resolvers *resolvers;
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00003978 int res_preferred_afinet, res_preferred_afinet6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003979
3980 /* shortcut to the server whose name is being resolved */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003981 s = resolution->requester;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003982 resolvers = resolution->resolvers;
3983
3984 /* can be ignored if this is not the last response */
3985 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3986 return 1;
3987 }
3988
3989 switch (error_code) {
3990 case DNS_RESP_INVALID:
3991 case DNS_RESP_WRONG_NAME:
3992 if (resolution->status != RSLV_STATUS_INVALID) {
3993 resolution->status = RSLV_STATUS_INVALID;
3994 resolution->last_status_change = now_ms;
3995 }
3996 break;
3997
3998 case DNS_RESP_ANCOUNT_ZERO:
Baptiste Assmann0df5d962015-09-02 21:58:32 +02003999 case DNS_RESP_TRUNCATED:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004000 case DNS_RESP_ERROR:
Baptiste Assmann96972bc2015-09-09 00:46:58 +02004001 case DNS_RESP_NO_EXPECTED_RECORD:
Baptiste Assmann65ce3f52016-09-05 08:38:57 +02004002 case DNS_RESP_CNAME_ERROR:
Thierry Fournierada34842016-02-17 21:25:09 +01004003 res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
4004 res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
Baptiste Assmann90447582015-09-02 22:20:56 +02004005
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00004006 if ((res_preferred_afinet || res_preferred_afinet6)
Baptiste Assmannf778bb42015-09-09 00:54:38 +02004007 || (resolution->try > 0)) {
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004008 /* let's change the query type */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00004009 if (res_preferred_afinet6) {
Baptiste Assmann90447582015-09-02 22:20:56 +02004010 /* fallback from AAAA to A */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004011 resolution->query_type = DNS_RTYPE_A;
Baptiste Assmann90447582015-09-02 22:20:56 +02004012 }
4013 else if (res_preferred_afinet) {
4014 /* fallback from A to AAAA */
4015 resolution->query_type = DNS_RTYPE_AAAA;
4016 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02004017 else {
4018 resolution->try -= 1;
Thierry Fournierada34842016-02-17 21:25:09 +01004019 if (resolution->opts->family_prio == AF_INET) {
Andrew Hayworthe6a4a322015-10-19 22:29:51 +00004020 resolution->query_type = DNS_RTYPE_A;
4021 } else {
4022 resolution->query_type = DNS_RTYPE_AAAA;
4023 }
Baptiste Assmannf778bb42015-09-09 00:54:38 +02004024 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004025
4026 dns_send_query(resolution);
4027
4028 /*
4029 * move the resolution to the last element of the FIFO queue
4030 * and update timeout wakeup based on the new first entry
4031 */
4032 if (dns_check_resolution_queue(resolvers) > 1) {
4033 /* second resolution becomes first one */
Baptiste Assmann11c4e4e2015-09-02 22:15:58 +02004034 LIST_DEL(&resolution->list);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004035 /* ex first resolution goes to the end of the queue */
4036 LIST_ADDQ(&resolvers->curr_resolution, &resolution->list);
4037 }
4038 dns_update_resolvers_timeout(resolvers);
4039 goto leave;
4040 }
4041 else {
4042 if (resolution->status != RSLV_STATUS_OTHER) {
4043 resolution->status = RSLV_STATUS_OTHER;
4044 resolution->last_status_change = now_ms;
4045 }
4046 }
4047 break;
4048
4049 case DNS_RESP_NX_DOMAIN:
4050 if (resolution->status != RSLV_STATUS_NX) {
4051 resolution->status = RSLV_STATUS_NX;
4052 resolution->last_status_change = now_ms;
4053 }
4054 break;
4055
4056 case DNS_RESP_REFUSED:
4057 if (resolution->status != RSLV_STATUS_REFUSED) {
4058 resolution->status = RSLV_STATUS_REFUSED;
4059 resolution->last_status_change = now_ms;
4060 }
4061 break;
4062
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004063 case DNS_RESP_TIMEOUT:
4064 if (resolution->status != RSLV_STATUS_TIMEOUT) {
4065 resolution->status = RSLV_STATUS_TIMEOUT;
4066 resolution->last_status_change = now_ms;
4067 }
4068 break;
4069 }
4070
4071 /* update last resolution date and time */
4072 resolution->last_resolution = now_ms;
4073 /* reset current status flag */
4074 resolution->step = RSLV_STEP_NONE;
4075 /* reset values */
4076 dns_reset_resolution(resolution);
4077
4078 LIST_DEL(&resolution->list);
4079 dns_update_resolvers_timeout(resolvers);
4080
4081 leave:
4082 snr_update_srv_status(s);
4083 return 1;
4084}
4085
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004086/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4087 * resolver. This is suited for initial address configuration. Returns 0 on
4088 * success otherwise a non-zero error code. In case of error, *err_code, if
4089 * not NULL, is filled up.
4090 */
4091int srv_set_addr_via_libc(struct server *srv, int *err_code)
4092{
4093 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004094 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004095 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004096 return 1;
4097 }
4098 return 0;
4099}
4100
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004101/* Set the server's FDQN (->hostname) from <hostname>.
4102 * Returns -1 if failed, 0 if not.
4103 */
4104int srv_set_fqdn(struct server *srv, const char *hostname)
4105{
4106 srv_free_dns_resolution(srv);
4107
4108 return srv_alloc_dns_resolution(srv, hostname);
4109}
4110
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004111/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4112 * from the state file. This is suited for initial address configuration.
4113 * Returns 0 on success otherwise a non-zero error code. In case of error,
4114 * *err_code, if not NULL, is filled up.
4115 */
4116static int srv_apply_lastaddr(struct server *srv, int *err_code)
4117{
4118 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4119 if (err_code)
4120 *err_code |= ERR_WARN;
4121 return 1;
4122 }
4123 return 0;
4124}
4125
Willy Tarreau25e51522016-11-04 15:10:17 +01004126/* returns 0 if no error, otherwise a combination of ERR_* flags */
4127static int srv_iterate_initaddr(struct server *srv)
4128{
4129 int return_code = 0;
4130 int err_code;
4131 unsigned int methods;
4132
4133 methods = srv->init_addr_methods;
4134 if (!methods) { // default to "last,libc"
4135 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4136 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4137 }
4138
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004139 /* "-dr" : always append "none" so that server addresses resolution
4140 * failures are silently ignored, this is convenient to validate some
4141 * configs out of their environment.
4142 */
4143 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4144 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4145
Willy Tarreau25e51522016-11-04 15:10:17 +01004146 while (methods) {
4147 err_code = 0;
4148 switch (srv_get_next_initaddr(&methods)) {
4149 case SRV_IADDR_LAST:
4150 if (!srv->lastaddr)
4151 continue;
4152 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004153 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004154 return_code |= err_code;
4155 break;
4156
4157 case SRV_IADDR_LIBC:
4158 if (!srv->hostname)
4159 continue;
4160 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004161 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004162 return_code |= err_code;
4163 break;
4164
Willy Tarreau37ebe122016-11-04 15:17:58 +01004165 case SRV_IADDR_NONE:
4166 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004167 if (return_code) {
4168 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4169 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4170 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004171 return return_code;
4172
Willy Tarreau4310d362016-11-02 15:05:56 +01004173 case SRV_IADDR_IP:
4174 ipcpy(&srv->init_addr, &srv->addr);
4175 if (return_code) {
4176 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4177 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4178 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004179 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004180
Willy Tarreau25e51522016-11-04 15:10:17 +01004181 default: /* unhandled method */
4182 break;
4183 }
4184 }
4185
4186 if (!return_code) {
4187 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
4188 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4189 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004190 else {
4191 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
4192 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4193 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004194
4195 return_code |= ERR_ALERT | ERR_FATAL;
4196 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004197out:
4198 srv_set_dyncookie(srv);
4199 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004200}
4201
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004202/*
4203 * This function parses all backends and all servers within each backend
4204 * and performs servers' addr resolution based on information provided by:
4205 * - configuration file
4206 * - server-state file (states provided by an 'old' haproxy process)
4207 *
4208 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4209 */
4210int srv_init_addr(void)
4211{
4212 struct proxy *curproxy;
4213 int return_code = 0;
4214
4215 curproxy = proxy;
4216 while (curproxy) {
4217 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004218
4219 /* servers are in backend only */
4220 if (!(curproxy->cap & PR_CAP_BE))
4221 goto srv_init_addr_next;
4222
Willy Tarreau25e51522016-11-04 15:10:17 +01004223 for (srv = curproxy->srv; srv; srv = srv->next)
4224 if (srv->hostname)
4225 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004226
4227 srv_init_addr_next:
4228 curproxy = curproxy->next;
4229 }
4230
4231 return return_code;
4232}
4233
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004234const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater)
4235{
4236
4237 struct chunk *msg;
4238
4239 msg = get_trash_chunk();
4240 chunk_reset(msg);
4241
4242 if (!strcmp(fqdn, server->hostname)) {
4243 chunk_appendf(msg, "no need to change the FDQN");
4244 goto out;
4245 }
4246
4247 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4248 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4249 goto out;
4250 }
4251
4252 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4253 server->proxy->id, server->id, server->hostname, fqdn);
4254
4255 if (srv_set_fqdn(server, fqdn) < 0) {
4256 chunk_reset(msg);
4257 chunk_appendf(msg, "could not update %s/%s FQDN",
4258 server->proxy->id, server->id);
4259 goto out;
4260 }
4261
4262 /* Flag as FQDN set from stats socket. */
4263 server->admin |= SRV_ADMF_HMAINT;
4264
4265 out:
4266 if (updater)
4267 chunk_appendf(msg, " by '%s'", updater);
4268 chunk_appendf(msg, "\n");
4269
4270 return msg->str;
4271}
4272
4273
Willy Tarreau21b069d2016-11-23 17:15:08 +01004274/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4275 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004276 * 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 +01004277 * used for CLI commands requiring a server name.
4278 * Important: the <arg> is modified to remove the '/'.
4279 */
4280struct server *cli_find_server(struct appctx *appctx, char *arg)
4281{
4282 struct proxy *px;
4283 struct server *sv;
4284 char *line;
4285
4286 /* split "backend/server" and make <line> point to server */
4287 for (line = arg; *line; line++)
4288 if (*line == '/') {
4289 *line++ = '\0';
4290 break;
4291 }
4292
4293 if (!*line || !*arg) {
4294 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004295 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004296 return NULL;
4297 }
4298
4299 if (!get_backend_server(arg, line, &px, &sv)) {
4300 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004301 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004302 return NULL;
4303 }
4304
4305 if (px->state == PR_STSTOPPED) {
4306 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004307 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004308 return NULL;
4309 }
4310
4311 return sv;
4312}
4313
William Lallemand222baf22016-11-19 02:00:33 +01004314
4315static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4316{
4317 struct server *sv;
4318 const char *warning;
4319
4320 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4321 return 1;
4322
4323 sv = cli_find_server(appctx, args[2]);
4324 if (!sv)
4325 return 1;
4326
4327 if (strcmp(args[3], "weight") == 0) {
4328 warning = server_parse_weight_change_request(sv, args[4]);
4329 if (warning) {
4330 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004331 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004332 }
4333 }
4334 else if (strcmp(args[3], "state") == 0) {
4335 if (strcmp(args[4], "ready") == 0)
4336 srv_adm_set_ready(sv);
4337 else if (strcmp(args[4], "drain") == 0)
4338 srv_adm_set_drain(sv);
4339 else if (strcmp(args[4], "maint") == 0)
4340 srv_adm_set_maint(sv);
4341 else {
4342 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004343 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004344 }
4345 }
4346 else if (strcmp(args[3], "health") == 0) {
4347 if (sv->track) {
4348 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004349 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004350 }
4351 else if (strcmp(args[4], "up") == 0) {
4352 sv->check.health = sv->check.rise + sv->check.fall - 1;
4353 srv_set_running(sv, "changed from CLI");
4354 }
4355 else if (strcmp(args[4], "stopping") == 0) {
4356 sv->check.health = sv->check.rise + sv->check.fall - 1;
4357 srv_set_stopping(sv, "changed from CLI");
4358 }
4359 else if (strcmp(args[4], "down") == 0) {
4360 sv->check.health = 0;
4361 srv_set_stopped(sv, "changed from CLI");
4362 }
4363 else {
4364 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004365 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004366 }
4367 }
4368 else if (strcmp(args[3], "agent") == 0) {
4369 if (!(sv->agent.state & CHK_ST_ENABLED)) {
4370 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004371 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004372 }
4373 else if (strcmp(args[4], "up") == 0) {
4374 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4375 srv_set_running(sv, "changed from CLI");
4376 }
4377 else if (strcmp(args[4], "down") == 0) {
4378 sv->agent.health = 0;
4379 srv_set_stopped(sv, "changed from CLI");
4380 }
4381 else {
4382 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004383 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004384 }
4385 }
Misiek2da082d2017-01-09 09:40:42 +01004386 else if (strcmp(args[3], "agent-addr") == 0) {
4387 if (!(sv->agent.state & CHK_ST_ENABLED)) {
4388 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4389 appctx->st0 = CLI_ST_PRINT;
4390 } else {
4391 if (str2ip(args[4], &sv->agent.addr) == NULL) {
4392 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4393 appctx->st0 = CLI_ST_PRINT;
4394 }
4395 }
4396 }
4397 else if (strcmp(args[3], "agent-send") == 0) {
4398 if (!(sv->agent.state & CHK_ST_ENABLED)) {
4399 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4400 appctx->st0 = CLI_ST_PRINT;
4401 } else {
4402 char *nss = strdup(args[4]);
4403 if (!nss) {
4404 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4405 appctx->st0 = CLI_ST_PRINT;
4406 } else {
4407 free(sv->agent.send_string);
4408 sv->agent.send_string = nss;
4409 sv->agent.send_string_len = strlen(args[4]);
4410 }
4411 }
4412 }
William Lallemand222baf22016-11-19 02:00:33 +01004413 else if (strcmp(args[3], "check-port") == 0) {
4414 int i = 0;
4415 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
4416 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004417 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004418 }
4419 if ((i < 0) || (i > 65535)) {
4420 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004421 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004422 }
4423 /* prevent the update of port to 0 if MAPPORTS are in use */
4424 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
4425 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004426 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004427 return 1;
4428 }
4429 sv->check.port = i;
4430 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004431 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004432 }
4433 else if (strcmp(args[3], "addr") == 0) {
4434 char *addr = NULL;
4435 char *port = NULL;
4436 if (strlen(args[4]) == 0) {
4437 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004438 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004439 return 1;
4440 }
4441 else {
4442 addr = args[4];
4443 }
4444 if (strcmp(args[5], "port") == 0) {
4445 port = args[6];
4446 }
4447 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4448 if (warning) {
4449 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004450 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004451 }
4452 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4453 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004454 else if (strcmp(args[3], "fqdn") == 0) {
4455 if (!*args[4]) {
4456 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4457 appctx->st0 = CLI_ST_PRINT;
4458 return 1;
4459 }
4460 warning = update_server_fqdn(sv, args[4], "stats socket command");
4461 if (warning) {
4462 appctx->ctx.cli.msg = warning;
4463 appctx->st0 = CLI_ST_PRINT;
4464 }
4465 }
William Lallemand222baf22016-11-19 02:00:33 +01004466 else {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004467 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 +01004468 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004469 }
4470 return 1;
4471}
4472
William Lallemand6b160942016-11-22 12:34:35 +01004473static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4474{
4475 struct stream_interface *si = appctx->owner;
4476 struct proxy *px;
4477 struct server *sv;
4478 char *line;
4479
4480
4481 /* split "backend/server" and make <line> point to server */
4482 for (line = args[2]; *line; line++)
4483 if (*line == '/') {
4484 *line++ = '\0';
4485 break;
4486 }
4487
4488 if (!*line) {
4489 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004490 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004491 return 1;
4492 }
4493
4494 if (!get_backend_server(args[2], line, &px, &sv)) {
4495 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004496 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004497 return 1;
4498 }
4499
4500 /* return server's effective weight at the moment */
4501 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004502 if (bi_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004503 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004504 return 0;
4505 }
William Lallemand6b160942016-11-22 12:34:35 +01004506 return 1;
4507}
4508
4509static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4510{
4511 struct server *sv;
4512 const char *warning;
4513
4514 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4515 return 1;
4516
4517 sv = cli_find_server(appctx, args[2]);
4518 if (!sv)
4519 return 1;
4520
4521 warning = server_parse_weight_change_request(sv, args[3]);
4522 if (warning) {
4523 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004524 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004525 }
4526 return 1;
4527}
4528
Willy Tarreaub8026272016-11-23 11:26:56 +01004529/* parse a "set maxconn server" command. It always returns 1. */
4530static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4531{
4532 struct server *sv;
4533 const char *warning;
4534
4535 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4536 return 1;
4537
4538 sv = cli_find_server(appctx, args[3]);
4539 if (!sv)
4540 return 1;
4541
4542 warning = server_parse_maxconn_change_request(sv, args[4]);
4543 if (warning) {
4544 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004545 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004546 }
4547 return 1;
4548}
William Lallemand6b160942016-11-22 12:34:35 +01004549
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004550/* parse a "disable agent" command. It always returns 1. */
4551static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4552{
4553 struct server *sv;
4554
4555 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4556 return 1;
4557
4558 sv = cli_find_server(appctx, args[2]);
4559 if (!sv)
4560 return 1;
4561
4562 sv->agent.state &= ~CHK_ST_ENABLED;
4563 return 1;
4564}
4565
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004566/* parse a "disable health" command. It always returns 1. */
4567static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4568{
4569 struct server *sv;
4570
4571 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4572 return 1;
4573
4574 sv = cli_find_server(appctx, args[2]);
4575 if (!sv)
4576 return 1;
4577
4578 sv->check.state &= ~CHK_ST_ENABLED;
4579 return 1;
4580}
4581
Willy Tarreauffb4d582016-11-24 12:47:00 +01004582/* parse a "disable server" command. It always returns 1. */
4583static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4584{
4585 struct server *sv;
4586
4587 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4588 return 1;
4589
4590 sv = cli_find_server(appctx, args[2]);
4591 if (!sv)
4592 return 1;
4593
4594 srv_adm_set_maint(sv);
4595 return 1;
4596}
4597
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004598/* parse a "enable agent" command. It always returns 1. */
4599static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4600{
4601 struct server *sv;
4602
4603 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4604 return 1;
4605
4606 sv = cli_find_server(appctx, args[2]);
4607 if (!sv)
4608 return 1;
4609
4610 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
4611 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004612 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004613 return 1;
4614 }
4615
4616 sv->agent.state |= CHK_ST_ENABLED;
4617 return 1;
4618}
4619
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004620/* parse a "enable health" command. It always returns 1. */
4621static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4622{
4623 struct server *sv;
4624
4625 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4626 return 1;
4627
4628 sv = cli_find_server(appctx, args[2]);
4629 if (!sv)
4630 return 1;
4631
4632 sv->check.state |= CHK_ST_ENABLED;
4633 return 1;
4634}
4635
Willy Tarreauffb4d582016-11-24 12:47:00 +01004636/* parse a "enable server" command. It always returns 1. */
4637static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4638{
4639 struct server *sv;
4640
4641 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4642 return 1;
4643
4644 sv = cli_find_server(appctx, args[2]);
4645 if (!sv)
4646 return 1;
4647
4648 srv_adm_set_ready(sv);
4649 return 1;
4650}
4651
William Lallemand222baf22016-11-19 02:00:33 +01004652/* register cli keywords */
4653static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004654 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004655 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004656 { { "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 +01004657 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004658 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004659 { { "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 +01004660 { { "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 +01004661 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004662 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4663 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4664
William Lallemand222baf22016-11-19 02:00:33 +01004665 {{},}
4666}};
4667
4668__attribute__((constructor))
4669static void __server_init(void)
4670{
4671 cli_register_kw(&cli_kws);
4672}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004673
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004674/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004675 * Local variables:
4676 * c-indent-level: 8
4677 * c-basic-offset: 8
4678 * End:
4679 */