blob: eb2bc6e9cd2bfadda5bbc6e5b4d10be6caa3a208 [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>
Willy Tarreau0108d902018-11-25 19:14:37 +010022#include <common/initcall.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010023#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020024#include <common/time.h>
25
William Lallemand222baf22016-11-19 02:00:33 +010026#include <types/applet.h>
27#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020028#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010029#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020030#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010031#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020032
William Lallemand222baf22016-11-19 02:00:33 +010033#include <proto/applet.h>
34#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090035#include <proto/checks.h>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020036#include <proto/connection.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020037#include <proto/port_range.h>
38#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020039#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010040#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020041#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020042#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010043#include <proto/stream_interface.h>
44#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020045#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020046#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010047#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020048
Willy Tarreau3ff577e2018-08-02 11:48:52 +020049static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020050static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010051static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010052static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Willy Tarreaubaaee002006-06-26 02:48:02 +020053
Willy Tarreau21faa912012-10-10 08:27:36 +020054/* List head of all known server keywords */
55static struct srv_kw_list srv_keywords = {
56 .list = LIST_HEAD_INIT(srv_keywords.list)
57};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020058
Simon Hormana3608442013-11-01 16:46:15 +090059int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020060{
Emeric Brun52a91d32017-08-31 14:41:55 +020061 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020062 return s->down_time;
63
64 return now.tv_sec - s->last_change + s->down_time;
65}
Willy Tarreaubaaee002006-06-26 02:48:02 +020066
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050067int srv_lastsession(const struct server *s)
68{
69 if (s->counters.last_sess)
70 return now.tv_sec - s->counters.last_sess;
71
72 return -1;
73}
74
Simon Horman4a741432013-02-23 15:35:38 +090075int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020076{
Simon Horman4a741432013-02-23 15:35:38 +090077 const struct server *s = check->server;
78
Willy Tarreauff5ae352013-12-11 20:36:34 +010079 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090080 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010081
Emeric Brun52a91d32017-08-31 14:41:55 +020082 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090083 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010084
Simon Horman4a741432013-02-23 15:35:38 +090085 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010086}
87
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010088/*
89 * Check that we did not get a hash collision.
90 * Unlikely, but it can happen.
91 */
92static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +010093{
94 struct proxy *p = s->proxy;
95 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010096
97 for (tmpserv = p->srv; tmpserv != NULL;
98 tmpserv = tmpserv->next) {
99 if (tmpserv == s)
100 continue;
101 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
102 continue;
103 if (tmpserv->cookie &&
104 strcmp(tmpserv->cookie, s->cookie) == 0) {
105 ha_warning("We generated two equal cookies for two different servers.\n"
106 "Please change the secret key for '%s'.\n",
107 s->proxy->id);
108 }
109 }
110
111}
112
Willy Tarreau46b7f532018-08-21 11:54:26 +0200113/*
114 * Must be called with the server lock held.
115 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100116void srv_set_dyncookie(struct server *s)
117{
118 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100119 char *tmpbuf;
120 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100121 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100122 size_t buffer_len;
123 int addr_len;
124 int port;
125
126 if ((s->flags & SRV_F_COOKIESET) ||
127 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
128 s->proxy->dyncookie_key == NULL)
129 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100130 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100131
132 if (s->addr.ss_family != AF_INET &&
133 s->addr.ss_family != AF_INET6)
134 return;
135 /*
136 * Buffer to calculate the cookie value.
137 * The buffer contains the secret key + the server IP address
138 * + the TCP port.
139 */
140 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
141 /*
142 * The TCP port should use only 2 bytes, but is stored in
143 * an unsigned int in struct server, so let's use 4, to be
144 * on the safe side.
145 */
146 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200147 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100148 memcpy(tmpbuf, p->dyncookie_key, key_len);
149 memcpy(&(tmpbuf[key_len]),
150 s->addr.ss_family == AF_INET ?
151 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
152 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
153 addr_len);
154 /*
155 * Make sure it's the same across all the load balancers,
156 * no matter their endianness.
157 */
158 port = htonl(s->svc_port);
159 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
160 hash_value = XXH64(tmpbuf, buffer_len, 0);
161 memprintf(&s->cookie, "%016llx", hash_value);
162 if (!s->cookie)
163 return;
164 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100165
166 /* Don't bother checking if the dyncookie is duplicated if
167 * the server is marked as "disabled", maybe it doesn't have
168 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100169 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100170 if (!(s->next_admin & SRV_ADMF_FMAINT))
171 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100172}
173
Willy Tarreau21faa912012-10-10 08:27:36 +0200174/*
175 * Registers the server keyword list <kwl> as a list of valid keywords for next
176 * parsing sessions.
177 */
178void srv_register_keywords(struct srv_kw_list *kwl)
179{
180 LIST_ADDQ(&srv_keywords.list, &kwl->list);
181}
182
183/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
184 * keyword is found with a NULL ->parse() function, then an attempt is made to
185 * find one with a valid ->parse() function. This way it is possible to declare
186 * platform-dependant, known keywords as NULL, then only declare them as valid
187 * if some options are met. Note that if the requested keyword contains an
188 * opening parenthesis, everything from this point is ignored.
189 */
190struct srv_kw *srv_find_kw(const char *kw)
191{
192 int index;
193 const char *kwend;
194 struct srv_kw_list *kwl;
195 struct srv_kw *ret = NULL;
196
197 kwend = strchr(kw, '(');
198 if (!kwend)
199 kwend = kw + strlen(kw);
200
201 list_for_each_entry(kwl, &srv_keywords.list, list) {
202 for (index = 0; kwl->kw[index].kw != NULL; index++) {
203 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
204 kwl->kw[index].kw[kwend-kw] == 0) {
205 if (kwl->kw[index].parse)
206 return &kwl->kw[index]; /* found it !*/
207 else
208 ret = &kwl->kw[index]; /* may be OK */
209 }
210 }
211 }
212 return ret;
213}
214
215/* Dumps all registered "server" keywords to the <out> string pointer. The
216 * unsupported keywords are only dumped if their supported form was not
217 * found.
218 */
219void srv_dump_kws(char **out)
220{
221 struct srv_kw_list *kwl;
222 int index;
223
224 *out = NULL;
225 list_for_each_entry(kwl, &srv_keywords.list, list) {
226 for (index = 0; kwl->kw[index].kw != NULL; index++) {
227 if (kwl->kw[index].parse ||
228 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
229 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
230 kwl->scope,
231 kwl->kw[index].kw,
232 kwl->kw[index].skip ? " <arg>" : "",
233 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
234 kwl->kw[index].parse ? "" : " (not supported)");
235 }
236 }
237 }
238}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100239
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100240/* Parse the "addr" server keyword */
241static int srv_parse_addr(char **args, int *cur_arg,
242 struct proxy *curproxy, struct server *newsrv, char **err)
243{
244 char *errmsg, *arg;
245 struct sockaddr_storage *sk;
246 int port1, port2;
247 struct protocol *proto;
248
249 errmsg = NULL;
250 arg = args[*cur_arg + 1];
251
252 if (!*arg) {
253 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
254 goto err;
255 }
256
257 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
258 if (!sk) {
259 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
260 goto err;
261 }
262
263 proto = protocol_by_family(sk->ss_family);
264 if (!proto || !proto->connect) {
265 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
266 args[*cur_arg], arg);
267 goto err;
268 }
269
270 if (port1 != port2) {
271 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
272 args[*cur_arg], arg);
273 goto err;
274 }
275
276 newsrv->check.addr = newsrv->agent.addr = *sk;
277 newsrv->flags |= SRV_F_CHECKADDR;
278 newsrv->flags |= SRV_F_AGENTADDR;
279
280 return 0;
281
282 err:
283 free(errmsg);
284 return ERR_ALERT | ERR_FATAL;
285}
286
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100287/* Parse the "agent-check" server keyword */
288static int srv_parse_agent_check(char **args, int *cur_arg,
289 struct proxy *curproxy, struct server *newsrv, char **err)
290{
291 newsrv->do_agent = 1;
292 return 0;
293}
294
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100295/* Parse the "backup" server keyword */
296static int srv_parse_backup(char **args, int *cur_arg,
297 struct proxy *curproxy, struct server *newsrv, char **err)
298{
299 newsrv->flags |= SRV_F_BACKUP;
300 return 0;
301}
302
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100303/* Parse the "check" server keyword */
304static int srv_parse_check(char **args, int *cur_arg,
305 struct proxy *curproxy, struct server *newsrv, char **err)
306{
307 newsrv->do_check = 1;
308 return 0;
309}
310
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100311/* Parse the "check-send-proxy" server keyword */
312static int srv_parse_check_send_proxy(char **args, int *cur_arg,
313 struct proxy *curproxy, struct server *newsrv, char **err)
314{
315 newsrv->check.send_proxy = 1;
316 return 0;
317}
318
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100319/* Parse the "cookie" server keyword */
320static int srv_parse_cookie(char **args, int *cur_arg,
321 struct proxy *curproxy, struct server *newsrv, char **err)
322{
323 char *arg;
324
325 arg = args[*cur_arg + 1];
326 if (!*arg) {
327 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
328 return ERR_ALERT | ERR_FATAL;
329 }
330
331 free(newsrv->cookie);
332 newsrv->cookie = strdup(arg);
333 newsrv->cklen = strlen(arg);
334 newsrv->flags |= SRV_F_COOKIESET;
335 return 0;
336}
337
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100338/* Parse the "disabled" server keyword */
339static int srv_parse_disabled(char **args, int *cur_arg,
340 struct proxy *curproxy, struct server *newsrv, char **err)
341{
Emeric Brun52a91d32017-08-31 14:41:55 +0200342 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
343 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100344 newsrv->check.state |= CHK_ST_PAUSED;
345 newsrv->check.health = 0;
346 return 0;
347}
348
349/* Parse the "enabled" server keyword */
350static int srv_parse_enabled(char **args, int *cur_arg,
351 struct proxy *curproxy, struct server *newsrv, char **err)
352{
Emeric Brun52a91d32017-08-31 14:41:55 +0200353 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
354 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100355 newsrv->check.state &= ~CHK_ST_PAUSED;
356 newsrv->check.health = newsrv->check.rise;
357 return 0;
358}
359
Willy Tarreaudff55432012-10-10 17:51:05 +0200360/* parse the "id" server keyword */
361static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
362{
363 struct eb32_node *node;
364
365 if (!*args[*cur_arg + 1]) {
366 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
367 return ERR_ALERT | ERR_FATAL;
368 }
369
370 newsrv->puid = atol(args[*cur_arg + 1]);
371 newsrv->conf.id.key = newsrv->puid;
372
373 if (newsrv->puid <= 0) {
374 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
375 return ERR_ALERT | ERR_FATAL;
376 }
377
378 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
379 if (node) {
380 struct server *target = container_of(node, struct server, conf.id);
381 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
382 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
383 target->id);
384 return ERR_ALERT | ERR_FATAL;
385 }
386
387 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200388 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200389 return 0;
390}
391
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100392/* Parse the "namespace" server keyword */
393static int srv_parse_namespace(char **args, int *cur_arg,
394 struct proxy *curproxy, struct server *newsrv, char **err)
395{
396#ifdef CONFIG_HAP_NS
397 char *arg;
398
399 arg = args[*cur_arg + 1];
400 if (!*arg) {
401 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
402 return ERR_ALERT | ERR_FATAL;
403 }
404
405 if (!strcmp(arg, "*")) {
406 /* Use the namespace associated with the connection (if present). */
407 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
408 return 0;
409 }
410
411 /*
412 * As this parser may be called several times for the same 'default-server'
413 * object, or for a new 'server' instance deriving from a 'default-server'
414 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
415 */
416 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
417
418 newsrv->netns = netns_store_lookup(arg, strlen(arg));
419 if (!newsrv->netns)
420 newsrv->netns = netns_store_insert(arg);
421
422 if (!newsrv->netns) {
423 memprintf(err, "Cannot open namespace '%s'", arg);
424 return ERR_ALERT | ERR_FATAL;
425 }
426
427 return 0;
428#else
429 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
430 return ERR_ALERT | ERR_FATAL;
431#endif
432}
433
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100434/* Parse the "no-agent-check" server keyword */
435static int srv_parse_no_agent_check(char **args, int *cur_arg,
436 struct proxy *curproxy, struct server *newsrv, char **err)
437{
438 free_check(&newsrv->agent);
439 newsrv->agent.inter = 0;
440 newsrv->agent.port = 0;
441 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
442 newsrv->do_agent = 0;
443 return 0;
444}
445
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100446/* Parse the "no-backup" server keyword */
447static int srv_parse_no_backup(char **args, int *cur_arg,
448 struct proxy *curproxy, struct server *newsrv, char **err)
449{
450 newsrv->flags &= ~SRV_F_BACKUP;
451 return 0;
452}
453
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100454/* Parse the "no-check" server keyword */
455static int srv_parse_no_check(char **args, int *cur_arg,
456 struct proxy *curproxy, struct server *newsrv, char **err)
457{
458 free_check(&newsrv->check);
459 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
460 newsrv->do_check = 0;
461 return 0;
462}
463
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100464/* Parse the "no-check-send-proxy" server keyword */
465static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
466 struct proxy *curproxy, struct server *newsrv, char **err)
467{
468 newsrv->check.send_proxy = 0;
469 return 0;
470}
471
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100472/* Disable server PROXY protocol flags. */
473static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
474{
475 srv->pp_opts &= ~flags;
476 return 0;
477}
478
479/* Parse the "no-send-proxy" server keyword */
480static int srv_parse_no_send_proxy(char **args, int *cur_arg,
481 struct proxy *curproxy, struct server *newsrv, char **err)
482{
483 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
484}
485
486/* Parse the "no-send-proxy-v2" server keyword */
487static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
488 struct proxy *curproxy, struct server *newsrv, char **err)
489{
490 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
491}
492
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100493/* Parse the "non-stick" server keyword */
494static int srv_parse_non_stick(char **args, int *cur_arg,
495 struct proxy *curproxy, struct server *newsrv, char **err)
496{
497 newsrv->flags |= SRV_F_NON_STICK;
498 return 0;
499}
500
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100501/* Enable server PROXY protocol flags. */
502static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
503{
504 srv->pp_opts |= flags;
505 return 0;
506}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200507/* parse the "proto" server keyword */
508static int srv_parse_proto(char **args, int *cur_arg,
509 struct proxy *px, struct server *newsrv, char **err)
510{
511 struct ist proto;
512
513 if (!*args[*cur_arg + 1]) {
514 memprintf(err, "'%s' : missing value", args[*cur_arg]);
515 return ERR_ALERT | ERR_FATAL;
516 }
517 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
518 newsrv->mux_proto = get_mux_proto(proto);
519 if (!newsrv->mux_proto) {
520 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
521 return ERR_ALERT | ERR_FATAL;
522 }
523 else if (!(newsrv->mux_proto->side & PROTO_SIDE_BE)) {
524 memprintf(err, "'%s' : MUX protocol '%s' cannot be used for outgoing connections",
525 args[*cur_arg], args[*cur_arg+1]);
526 return ERR_ALERT | ERR_FATAL;
527 }
528 return 0;
529}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100530
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100531/* parse the "proxy-v2-options" */
532static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
533 struct proxy *px, struct server *newsrv, char **err)
534{
535 char *p, *n;
536 for (p = args[*cur_arg+1]; p; p = n) {
537 n = strchr(p, ',');
538 if (n)
539 *n++ = '\0';
540 if (!strcmp(p, "ssl")) {
541 newsrv->pp_opts |= SRV_PP_V2_SSL;
542 } else if (!strcmp(p, "cert-cn")) {
543 newsrv->pp_opts |= SRV_PP_V2_SSL;
544 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100545 } else if (!strcmp(p, "cert-key")) {
546 newsrv->pp_opts |= SRV_PP_V2_SSL;
547 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
548 } else if (!strcmp(p, "cert-sig")) {
549 newsrv->pp_opts |= SRV_PP_V2_SSL;
550 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
551 } else if (!strcmp(p, "ssl-cipher")) {
552 newsrv->pp_opts |= SRV_PP_V2_SSL;
553 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100554 } else if (!strcmp(p, "authority")) {
555 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100556 } else if (!strcmp(p, "crc32c")) {
557 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100558 } else
559 goto fail;
560 }
561 return 0;
562 fail:
563 if (err)
564 memprintf(err, "'%s' : proxy v2 option not implemented", p);
565 return ERR_ALERT | ERR_FATAL;
566}
567
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100568/* Parse the "observe" server keyword */
569static int srv_parse_observe(char **args, int *cur_arg,
570 struct proxy *curproxy, struct server *newsrv, char **err)
571{
572 char *arg;
573
574 arg = args[*cur_arg + 1];
575 if (!*arg) {
576 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
577 return ERR_ALERT | ERR_FATAL;
578 }
579
580 if (!strcmp(arg, "none")) {
581 newsrv->observe = HANA_OBS_NONE;
582 }
583 else if (!strcmp(arg, "layer4")) {
584 newsrv->observe = HANA_OBS_LAYER4;
585 }
586 else if (!strcmp(arg, "layer7")) {
587 if (curproxy->mode != PR_MODE_HTTP) {
588 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
589 return ERR_ALERT;
590 }
591 newsrv->observe = HANA_OBS_LAYER7;
592 }
593 else {
594 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
595 "but got '%s'\n", args[*cur_arg], arg);
596 return ERR_ALERT | ERR_FATAL;
597 }
598
599 return 0;
600}
601
Frédéric Lécaille16186232017-03-14 16:42:49 +0100602/* Parse the "redir" server keyword */
603static int srv_parse_redir(char **args, int *cur_arg,
604 struct proxy *curproxy, struct server *newsrv, char **err)
605{
606 char *arg;
607
608 arg = args[*cur_arg + 1];
609 if (!*arg) {
610 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
611 return ERR_ALERT | ERR_FATAL;
612 }
613
614 free(newsrv->rdr_pfx);
615 newsrv->rdr_pfx = strdup(arg);
616 newsrv->rdr_len = strlen(arg);
617
618 return 0;
619}
620
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100621/* Parse the "send-proxy" server keyword */
622static int srv_parse_send_proxy(char **args, int *cur_arg,
623 struct proxy *curproxy, struct server *newsrv, char **err)
624{
625 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
626}
627
628/* Parse the "send-proxy-v2" server keyword */
629static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
630 struct proxy *curproxy, struct server *newsrv, char **err)
631{
632 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
633}
634
Frédéric Lécailledba97072017-03-17 15:33:50 +0100635
636/* Parse the "source" server keyword */
637static int srv_parse_source(char **args, int *cur_arg,
638 struct proxy *curproxy, struct server *newsrv, char **err)
639{
640 char *errmsg;
641 int port_low, port_high;
642 struct sockaddr_storage *sk;
643 struct protocol *proto;
644
645 errmsg = NULL;
646
647 if (!*args[*cur_arg + 1]) {
648 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
649 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
650 goto err;
651 }
652
653 /* 'sk' is statically allocated (no need to be freed). */
654 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
655 if (!sk) {
656 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
657 goto err;
658 }
659
660 proto = protocol_by_family(sk->ss_family);
661 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100662 ha_alert("'%s %s' : connect() not supported for this address family.\n",
663 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100664 goto err;
665 }
666
667 newsrv->conn_src.opts |= CO_SRC_BIND;
668 newsrv->conn_src.source_addr = *sk;
669
670 if (port_low != port_high) {
671 int i;
672
673 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100674 ha_alert("'%s' does not support port offsets (found '%s').\n",
675 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100676 goto err;
677 }
678
679 if (port_low <= 0 || port_low > 65535 ||
680 port_high <= 0 || port_high > 65535 ||
681 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100682 ha_alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100683 goto err;
684 }
685 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
686 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
687 newsrv->conn_src.sport_range->ports[i] = port_low + i;
688 }
689
690 *cur_arg += 2;
691 while (*(args[*cur_arg])) {
692 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
693#if defined(CONFIG_HAP_TRANSPARENT)
694 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100695 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
696 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100697 goto err;
698 }
699 if (!strcmp(args[*cur_arg + 1], "client")) {
700 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
701 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
702 }
703 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
704 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
705 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
706 }
707 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
708 char *name, *end;
709
710 name = args[*cur_arg + 1] + 7;
711 while (isspace(*name))
712 name++;
713
714 end = name;
715 while (*end && !isspace(*end) && *end != ',' && *end != ')')
716 end++;
717
718 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
719 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
720 free(newsrv->conn_src.bind_hdr_name);
721 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
722 newsrv->conn_src.bind_hdr_len = end - name;
723 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
724 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
725 newsrv->conn_src.bind_hdr_occ = -1;
726
727 /* now look for an occurrence number */
728 while (isspace(*end))
729 end++;
730 if (*end == ',') {
731 end++;
732 name = end;
733 if (*end == '-')
734 end++;
735 while (isdigit((int)*end))
736 end++;
737 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
738 }
739
740 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100741 ha_alert("usesrc hdr_ip(name,num) does not support negative"
742 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100743 goto err;
744 }
745 }
746 else {
747 struct sockaddr_storage *sk;
748 int port1, port2;
749
750 /* 'sk' is statically allocated (no need to be freed). */
751 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
752 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100753 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100754 goto err;
755 }
756
757 proto = protocol_by_family(sk->ss_family);
758 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100759 ha_alert("'%s %s' : connect() not supported for this address family.\n",
760 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100761 goto err;
762 }
763
764 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100765 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
766 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100767 goto err;
768 }
769 newsrv->conn_src.tproxy_addr = *sk;
770 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
771 }
772 global.last_checks |= LSTCHK_NETADM;
773 *cur_arg += 2;
774 continue;
775#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100776 ha_alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100777 goto err;
778#endif /* defined(CONFIG_HAP_TRANSPARENT) */
779 } /* "usesrc" */
780
781 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
782#ifdef SO_BINDTODEVICE
783 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100784 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100785 goto err;
786 }
787 free(newsrv->conn_src.iface_name);
788 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
789 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
790 global.last_checks |= LSTCHK_NETADM;
791#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100792 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100793 goto err;
794#endif
795 *cur_arg += 2;
796 continue;
797 }
798 /* this keyword in not an option of "source" */
799 break;
800 } /* while */
801
802 return 0;
803
804 err:
805 free(errmsg);
806 return ERR_ALERT | ERR_FATAL;
807}
808
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100809/* Parse the "stick" server keyword */
810static int srv_parse_stick(char **args, int *cur_arg,
811 struct proxy *curproxy, struct server *newsrv, char **err)
812{
813 newsrv->flags &= ~SRV_F_NON_STICK;
814 return 0;
815}
816
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100817/* Parse the "track" server keyword */
818static int srv_parse_track(char **args, int *cur_arg,
819 struct proxy *curproxy, struct server *newsrv, char **err)
820{
821 char *arg;
822
823 arg = args[*cur_arg + 1];
824 if (!*arg) {
825 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
826 return ERR_ALERT | ERR_FATAL;
827 }
828
829 free(newsrv->trackit);
830 newsrv->trackit = strdup(arg);
831
832 return 0;
833}
834
Frédéric Lécailledba97072017-03-17 15:33:50 +0100835
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200836/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200837 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200838 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200839 *
840 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200841 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200842void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200843{
Willy Tarreau87b09662015-04-03 00:22:06 +0200844 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200845
Willy Tarreau87b09662015-04-03 00:22:06 +0200846 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
847 if (stream->srv_conn == srv)
848 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200849}
850
851/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200852 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200853 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200854 *
855 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200856 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200857void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200858{
859 struct server *srv;
860
861 for (srv = px->srv; srv != NULL; srv = srv->next)
862 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200863 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200864}
865
Willy Tarreaubda92272014-05-20 21:55:30 +0200866/* Appends some information to a message string related to a server going UP or
867 * DOWN. If both <forced> and <reason> are null and the server tracks another
868 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200869 * If <check> is non-null, an entire string describing the check result will be
870 * appended after a comma and a space (eg: to report some information from the
871 * check that changed the state). In the other case, the string will be built
872 * using the check results stored into the struct server if present.
873 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200874 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200875 *
876 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200877 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200878void srv_append_status(struct buffer *msg, struct server *s,
879 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200880{
Emeric Brun5a133512017-10-19 14:42:30 +0200881 short status = s->op_st_chg.status;
882 short code = s->op_st_chg.code;
883 long duration = s->op_st_chg.duration;
884 char *desc = s->op_st_chg.reason;
885
886 if (check) {
887 status = check->status;
888 code = check->code;
889 duration = check->duration;
890 desc = check->desc;
891 }
892
893 if (status != -1) {
894 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
895
896 if (status >= HCHK_STATUS_L57DATA)
897 chunk_appendf(msg, ", code: %d", code);
898
899 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200900 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200901
902 chunk_appendf(msg, ", info: \"");
903
904 chunk_initlen(&src, desc, 0, strlen(desc));
905 chunk_asciiencode(msg, &src, '"');
906
907 chunk_appendf(msg, "\"");
908 }
909
910 if (duration >= 0)
911 chunk_appendf(msg, ", check duration: %ldms", duration);
912 }
913 else if (desc && *desc) {
914 chunk_appendf(msg, ", %s", desc);
915 }
916 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200917 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200918 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200919
920 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200921 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200922 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
923 " %d sessions active, %d requeued, %d remaining in queue",
924 s->proxy->srv_act, s->proxy->srv_bck,
925 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
926 s->cur_sess, xferred, s->nbpend);
927 else
928 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
929 " %d sessions requeued, %d total in queue",
930 s->proxy->srv_act, s->proxy->srv_bck,
931 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
932 xferred, s->nbpend);
933 }
934}
935
Emeric Brun5a133512017-10-19 14:42:30 +0200936/* Marks server <s> down, regardless of its checks' statuses. The server is
937 * registered in a list to postpone the counting of the remaining servers on
938 * the proxy and transfers queued streams whenever possible to other servers at
939 * a sync point. Maintenance servers are ignored. It stores the <reason> if
940 * non-null as the reason for going down or the available data from the check
941 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200942 *
943 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200944 */
Emeric Brun5a133512017-10-19 14:42:30 +0200945void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200946{
947 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200948
Emeric Brun64cc49c2017-10-03 14:46:45 +0200949 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200950 return;
951
Emeric Brun52a91d32017-08-31 14:41:55 +0200952 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200953 *s->op_st_chg.reason = 0;
954 s->op_st_chg.status = -1;
955 if (reason) {
956 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
957 }
958 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100959 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200960 s->op_st_chg.code = check->code;
961 s->op_st_chg.status = check->status;
962 s->op_st_chg.duration = check->duration;
963 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200964
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200965 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200966 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200967
Emeric Brun9f0b4582017-10-23 14:39:51 +0200968 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100969 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200970 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100971 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200972 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200973}
974
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200975/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200976 * in maintenance. The server is registered in a list to postpone the counting
977 * of the remaining servers on the proxy and tries to grab requests from the
978 * proxy at a sync point. Maintenance servers are ignored. It stores the
979 * <reason> if non-null as the reason for going down or the available data
980 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200981 *
982 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200983 */
Emeric Brun5a133512017-10-19 14:42:30 +0200984void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200985{
986 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200987
Emeric Brun64cc49c2017-10-03 14:46:45 +0200988 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200989 return;
990
Emeric Brun52a91d32017-08-31 14:41:55 +0200991 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200992 return;
993
Emeric Brun52a91d32017-08-31 14:41:55 +0200994 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200995 *s->op_st_chg.reason = 0;
996 s->op_st_chg.status = -1;
997 if (reason) {
998 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
999 }
1000 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001001 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001002 s->op_st_chg.code = check->code;
1003 s->op_st_chg.status = check->status;
1004 s->op_st_chg.duration = check->duration;
1005 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001006
Emeric Brun64cc49c2017-10-03 14:46:45 +02001007 if (s->slowstart <= 0)
1008 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001009
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001010 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001011 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001012
Emeric Brun9f0b4582017-10-23 14:39:51 +02001013 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001014 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001015 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001016 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001017 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001018}
1019
Willy Tarreau8eb77842014-05-21 13:54:57 +02001020/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001021 * isn't in maintenance. The server is registered in a list to postpone the
1022 * counting of the remaining servers on the proxy and tries to grab requests
1023 * from the proxy. Maintenance servers are ignored. It stores the
1024 * <reason> if non-null as the reason for going down or the available data
1025 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001026 * up. Note that it makes use of the trash to build the log strings, so <reason>
1027 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001028 *
1029 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001030 */
Emeric Brun5a133512017-10-19 14:42:30 +02001031void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001032{
1033 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001034
Emeric Brun64cc49c2017-10-03 14:46:45 +02001035 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001036 return;
1037
Emeric Brun52a91d32017-08-31 14:41:55 +02001038 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001039 return;
1040
Emeric Brun52a91d32017-08-31 14:41:55 +02001041 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001042 *s->op_st_chg.reason = 0;
1043 s->op_st_chg.status = -1;
1044 if (reason) {
1045 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1046 }
1047 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001048 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001049 s->op_st_chg.code = check->code;
1050 s->op_st_chg.status = check->status;
1051 s->op_st_chg.duration = check->duration;
1052 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001053
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001054 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001055 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001056
Emeric Brun9f0b4582017-10-23 14:39:51 +02001057 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001058 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001059 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001060 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001061 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001062}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001063
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001064/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1065 * enforce either maint mode or drain mode. It is not allowed to set more than
1066 * one flag at once. The equivalent "inherited" flag is propagated to all
1067 * tracking servers. Maintenance mode disables health checks (but not agent
1068 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001069 * is done. If <cause> is non-null, it will be displayed at the end of the log
1070 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001071 *
1072 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001073 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001074void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001075{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001076 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001077
1078 if (!mode)
1079 return;
1080
1081 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001082 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001083 return;
1084
Emeric Brun52a91d32017-08-31 14:41:55 +02001085 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001086 if (cause)
1087 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1088
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001089 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001090 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001091
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001092 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001093 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1094 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001095 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001096
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001097 /* compute the inherited flag to propagate */
1098 if (mode & SRV_ADMF_MAINT)
1099 mode = SRV_ADMF_IMAINT;
1100 else if (mode & SRV_ADMF_DRAIN)
1101 mode = SRV_ADMF_IDRAIN;
1102
Emeric Brun9f0b4582017-10-23 14:39:51 +02001103 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001104 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001105 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001106 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001107 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001108}
1109
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001110/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1111 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1112 * than one flag at once. The equivalent "inherited" flag is propagated to all
1113 * tracking servers. Leaving maintenance mode re-enables health checks. When
1114 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001115 *
1116 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001117 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001118void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001119{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001120 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001121
1122 if (!mode)
1123 return;
1124
1125 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001126 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001127 return;
1128
Emeric Brun52a91d32017-08-31 14:41:55 +02001129 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001130
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001131 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001132 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001133
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001134 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001135 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1136 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001137 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001138
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001139 if (mode & SRV_ADMF_MAINT)
1140 mode = SRV_ADMF_IMAINT;
1141 else if (mode & SRV_ADMF_DRAIN)
1142 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001143
Emeric Brun9f0b4582017-10-23 14:39:51 +02001144 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001145 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001146 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001147 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001148 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001149}
1150
Willy Tarreau757478e2016-11-03 19:22:19 +01001151/* principle: propagate maint and drain to tracking servers. This is useful
1152 * upon startup so that inherited states are correct.
1153 */
1154static void srv_propagate_admin_state(struct server *srv)
1155{
1156 struct server *srv2;
1157
1158 if (!srv->trackers)
1159 return;
1160
1161 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001162 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001163 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001164 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001165
Emeric Brun52a91d32017-08-31 14:41:55 +02001166 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001167 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001168 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001169 }
1170}
1171
1172/* Compute and propagate the admin states for all servers in proxy <px>.
1173 * Only servers *not* tracking another one are considered, because other
1174 * ones will be handled when the server they track is visited.
1175 */
1176void srv_compute_all_admin_states(struct proxy *px)
1177{
1178 struct server *srv;
1179
1180 for (srv = px->srv; srv; srv = srv->next) {
1181 if (srv->track)
1182 continue;
1183 srv_propagate_admin_state(srv);
1184 }
1185}
1186
Willy Tarreaudff55432012-10-10 17:51:05 +02001187/* Note: must not be declared <const> as its list will be overwritten.
1188 * Please take care of keeping this list alphabetically sorted, doing so helps
1189 * all code contributors.
1190 * Optional keywords are also declared with a NULL ->parse() function so that
1191 * the config parser can report an appropriate error when a known keyword was
1192 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001193 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001194 */
1195static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001196 { "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 +01001197 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001198 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001199 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001200 { "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 +01001201 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001202 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1203 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001204 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001205 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001206 { "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 +01001207 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001208 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001209 { "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 +01001210 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1211 { "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 +01001212 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001213 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001214 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001215 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1 }, /* options for send-proxy-v2 */
Frédéric Lécaille16186232017-03-14 16:42:49 +01001216 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001217 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1218 { "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 +02001219 { "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 +01001220 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001221 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001222 { NULL, NULL, 0 },
1223}};
1224
Willy Tarreau0108d902018-11-25 19:14:37 +01001225INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001226
Willy Tarreau004e0452013-11-21 11:22:01 +01001227/* Recomputes the server's eweight based on its state, uweight, the current time,
1228 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001229 * state is automatically disabled if the time is elapsed. If <must_update> is
1230 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001231 *
1232 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001233 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001234void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001235{
1236 struct proxy *px = sv->proxy;
1237 unsigned w;
1238
1239 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1240 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001241 if (sv->next_state == SRV_ST_STARTING)
1242 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001243 }
1244
1245 /* We must take care of not pushing the server to full throttle during slow starts.
1246 * It must also start immediately, at least at the minimal step when leaving maintenance.
1247 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001248 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001249 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1250 else
1251 w = px->lbprm.wdiv;
1252
Emeric Brun52a91d32017-08-31 14:41:55 +02001253 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001254
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001255 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001256 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001257 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001258}
1259
Willy Tarreaubaaee002006-06-26 02:48:02 +02001260/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001261 * Parses weight_str and configures sv accordingly.
1262 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001263 *
1264 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001265 */
1266const char *server_parse_weight_change_request(struct server *sv,
1267 const char *weight_str)
1268{
1269 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001270 long int w;
1271 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001272
1273 px = sv->proxy;
1274
1275 /* if the weight is terminated with '%', it is set relative to
1276 * the initial weight, otherwise it is absolute.
1277 */
1278 if (!*weight_str)
1279 return "Require <weight> or <weight%>.\n";
1280
Simon Hormanb796afa2013-02-12 10:45:53 +09001281 w = strtol(weight_str, &end, 10);
1282 if (end == weight_str)
1283 return "Empty weight string empty or preceded by garbage";
1284 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001285 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001286 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001287 /* Avoid integer overflow */
1288 if (w > 25600)
1289 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001290 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001291 if (w > 256)
1292 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001293 }
1294 else if (w < 0 || w > 256)
1295 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001296 else if (end[0] != '\0')
1297 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001298
1299 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1300 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1301
1302 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001303 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001304
1305 return NULL;
1306}
1307
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001308/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001309 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1310 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001311 * Returns:
1312 * - error string on error
1313 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001314 *
1315 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001316 */
1317const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001318 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001319{
1320 unsigned char ip[INET6_ADDRSTRLEN];
1321
1322 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001323 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001324 return NULL;
1325 }
1326 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001327 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001328 return NULL;
1329 }
1330
1331 return "Could not understand IP address format.\n";
1332}
1333
Willy Tarreau46b7f532018-08-21 11:54:26 +02001334/*
1335 * Must be called with the server lock held.
1336 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001337const char *server_parse_maxconn_change_request(struct server *sv,
1338 const char *maxconn_str)
1339{
1340 long int v;
1341 char *end;
1342
1343 if (!*maxconn_str)
1344 return "Require <maxconn>.\n";
1345
1346 v = strtol(maxconn_str, &end, 10);
1347 if (end == maxconn_str)
1348 return "maxconn string empty or preceded by garbage";
1349 else if (end[0] != '\0')
1350 return "Trailing garbage in maxconn string";
1351
1352 if (sv->maxconn == sv->minconn) { // static maxconn
1353 sv->maxconn = sv->minconn = v;
1354 } else { // dynamic maxconn
1355 sv->maxconn = v;
1356 }
1357
1358 if (may_dequeue_tasks(sv, sv->proxy))
1359 process_srv_queue(sv);
1360
1361 return NULL;
1362}
1363
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001364#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001365static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1366 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001367{
1368 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001369 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001370 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001371 NULL,
1372 };
1373
1374 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001375 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001376
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001377 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1378}
1379
1380static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1381{
1382 struct sample_expr *expr;
1383
1384 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 +01001385 if (!expr) {
1386 memprintf(err, "error detected while parsing sni expression : %s", *err);
1387 return ERR_ALERT | ERR_FATAL;
1388 }
1389
1390 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1391 memprintf(err, "error detected while parsing sni expression : "
1392 " fetch method '%s' extracts information from '%s', "
1393 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001394 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001395 return ERR_ALERT | ERR_FATAL;
1396 }
1397
1398 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1399 release_sample_expr(newsrv->ssl_ctx.sni);
1400 newsrv->ssl_ctx.sni = expr;
1401
1402 return 0;
1403}
1404#endif
1405
1406static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1407{
1408 if (err && *err) {
1409 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001410 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001411 }
1412 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001413 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1414 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001415}
1416
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001417static void srv_conn_src_sport_range_cpy(struct server *srv,
1418 struct server *src)
1419{
1420 int range_sz;
1421
1422 range_sz = src->conn_src.sport_range->size;
1423 if (range_sz > 0) {
1424 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1425 if (srv->conn_src.sport_range != NULL) {
1426 int i;
1427
1428 for (i = 0; i < range_sz; i++) {
1429 srv->conn_src.sport_range->ports[i] =
1430 src->conn_src.sport_range->ports[i];
1431 }
1432 }
1433 }
1434}
1435
1436/*
1437 * Copy <src> server connection source settings to <srv> server everything needed.
1438 */
1439static void srv_conn_src_cpy(struct server *srv, struct server *src)
1440{
1441 srv->conn_src.opts = src->conn_src.opts;
1442 srv->conn_src.source_addr = src->conn_src.source_addr;
1443
1444 /* Source port range copy. */
1445 if (src->conn_src.sport_range != NULL)
1446 srv_conn_src_sport_range_cpy(srv, src);
1447
1448#ifdef CONFIG_HAP_TRANSPARENT
1449 if (src->conn_src.bind_hdr_name != NULL) {
1450 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1451 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1452 }
1453 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1454 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1455#endif
1456 if (src->conn_src.iface_name != NULL)
1457 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1458}
1459
1460/*
1461 * Copy <src> server SSL settings to <srv> server allocating
1462 * everything needed.
1463 */
1464#if defined(USE_OPENSSL)
1465static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1466{
1467 if (src->ssl_ctx.ca_file != NULL)
1468 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1469 if (src->ssl_ctx.crl_file != NULL)
1470 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1471 if (src->ssl_ctx.client_crt != NULL)
1472 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1473
1474 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1475
1476 if (src->ssl_ctx.verify_host != NULL)
1477 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1478 if (src->ssl_ctx.ciphers != NULL)
1479 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001480#if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
1481 if (src->ssl_ctx.ciphersuites != NULL)
1482 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1483#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001484 if (src->sni_expr != NULL)
1485 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001486
1487#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1488 if (src->ssl_ctx.alpn_str) {
1489 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1490 if (srv->ssl_ctx.alpn_str) {
1491 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1492 src->ssl_ctx.alpn_len);
1493 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1494 }
1495 }
1496#endif
1497#ifdef OPENSSL_NPN_NEGOTIATED
1498 if (src->ssl_ctx.npn_str) {
1499 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1500 if (srv->ssl_ctx.npn_str) {
1501 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1502 src->ssl_ctx.npn_len);
1503 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1504 }
1505 }
1506#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001507}
1508#endif
1509
1510/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001511 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001512 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001513 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001514 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001515static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001516{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001517 char *hostname_dn;
1518 int hostname_len, hostname_dn_len;
1519
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001520 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001521 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001522
Christopher Faulet67957bd2017-09-27 11:00:59 +02001523 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001524 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001525 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1526 hostname_dn, trash.size);
1527 if (hostname_dn_len == -1)
1528 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001529
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001530
Christopher Faulet67957bd2017-09-27 11:00:59 +02001531 free(srv->hostname);
1532 free(srv->hostname_dn);
1533 srv->hostname = strdup(hostname);
1534 srv->hostname_dn = strdup(hostname_dn);
1535 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001536 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001537 goto err;
1538
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001539 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001540
1541 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001542 free(srv->hostname); srv->hostname = NULL;
1543 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001544 return -1;
1545}
1546
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001547/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001548 * Copy <src> server settings to <srv> server allocating
1549 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001550 * This function is not supposed to be called at any time, but only
1551 * during server settings parsing or during server allocations from
1552 * a server template, and just after having calloc()'ed a new server.
1553 * So, <src> may only be a default server (when parsing server settings)
1554 * or a server template (during server allocations from a server template).
1555 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1556 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001557 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001558static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001559{
1560 /* Connection source settings copy */
1561 srv_conn_src_cpy(srv, src);
1562
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001563 if (srv_tmpl) {
1564 srv->addr = src->addr;
1565 srv->svc_port = src->svc_port;
1566 }
1567
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001568 srv->pp_opts = src->pp_opts;
1569 if (src->rdr_pfx != NULL) {
1570 srv->rdr_pfx = strdup(src->rdr_pfx);
1571 srv->rdr_len = src->rdr_len;
1572 }
1573 if (src->cookie != NULL) {
1574 srv->cookie = strdup(src->cookie);
1575 srv->cklen = src->cklen;
1576 }
1577 srv->use_ssl = src->use_ssl;
1578 srv->check.addr = srv->agent.addr = src->check.addr;
1579 srv->check.use_ssl = src->check.use_ssl;
1580 srv->check.port = src->check.port;
1581 /* Note: 'flags' field has potentially been already initialized. */
1582 srv->flags |= src->flags;
1583 srv->do_check = src->do_check;
1584 srv->do_agent = src->do_agent;
1585 if (srv->check.port)
1586 srv->flags |= SRV_F_CHECKPORT;
1587 srv->check.inter = src->check.inter;
1588 srv->check.fastinter = src->check.fastinter;
1589 srv->check.downinter = src->check.downinter;
1590 srv->agent.use_ssl = src->agent.use_ssl;
1591 srv->agent.port = src->agent.port;
1592 if (src->agent.send_string != NULL)
1593 srv->agent.send_string = strdup(src->agent.send_string);
1594 srv->agent.send_string_len = src->agent.send_string_len;
1595 srv->agent.inter = src->agent.inter;
1596 srv->agent.fastinter = src->agent.fastinter;
1597 srv->agent.downinter = src->agent.downinter;
1598 srv->maxqueue = src->maxqueue;
1599 srv->minconn = src->minconn;
1600 srv->maxconn = src->maxconn;
1601 srv->slowstart = src->slowstart;
1602 srv->observe = src->observe;
1603 srv->onerror = src->onerror;
1604 srv->onmarkeddown = src->onmarkeddown;
1605 srv->onmarkedup = src->onmarkedup;
1606 if (src->trackit != NULL)
1607 srv->trackit = strdup(src->trackit);
1608 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1609 srv->uweight = srv->iweight = src->iweight;
1610
1611 srv->check.send_proxy = src->check.send_proxy;
1612 /* health: up, but will fall down at first failure */
1613 srv->check.rise = srv->check.health = src->check.rise;
1614 srv->check.fall = src->check.fall;
1615
1616 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001617 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1618 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1619 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001620 srv->check.state |= CHK_ST_PAUSED;
1621 srv->check.health = 0;
1622 }
1623
1624 /* health: up but will fall down at first failure */
1625 srv->agent.rise = srv->agent.health = src->agent.rise;
1626 srv->agent.fall = src->agent.fall;
1627
1628 if (src->resolvers_id != NULL)
1629 srv->resolvers_id = strdup(src->resolvers_id);
1630 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001631 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001632 if (srv->dns_opts.family_prio == AF_UNSPEC)
1633 srv->dns_opts.family_prio = AF_INET6;
1634 memcpy(srv->dns_opts.pref_net,
1635 src->dns_opts.pref_net,
1636 sizeof srv->dns_opts.pref_net);
1637 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1638
1639 srv->init_addr_methods = src->init_addr_methods;
1640 srv->init_addr = src->init_addr;
1641#if defined(USE_OPENSSL)
1642 srv_ssl_settings_cpy(srv, src);
1643#endif
1644#ifdef TCP_USER_TIMEOUT
1645 srv->tcp_ut = src->tcp_ut;
1646#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001647 srv->mux_proto = src->mux_proto;
1648
Olivier Houchard8da5f982017-08-04 18:35:36 +02001649 if (srv_tmpl)
1650 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001651}
1652
William Lallemand313bfd12018-10-26 14:47:32 +02001653struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001654{
1655 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001656 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001657
1658 srv = calloc(1, sizeof *srv);
1659 if (!srv)
1660 return NULL;
1661
1662 srv->obj_type = OBJ_TYPE_SERVER;
1663 srv->proxy = proxy;
1664 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001665 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001666
1667 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1668 goto free_srv;
1669 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1670 goto free_priv_conns;
1671 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1672 goto free_idle_conns;
1673
1674 for (i = 0; i < global.nbthread; i++) {
1675 LIST_INIT(&srv->priv_conns[i]);
1676 LIST_INIT(&srv->idle_conns[i]);
1677 LIST_INIT(&srv->safe_conns[i]);
1678 }
1679
Emeric Brun52a91d32017-08-31 14:41:55 +02001680 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001681 srv->last_change = now.tv_sec;
1682
1683 srv->check.status = HCHK_STATUS_INI;
1684 srv->check.server = srv;
1685 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1686
1687 srv->agent.status = HCHK_STATUS_INI;
1688 srv->agent.server = srv;
1689 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1690
1691 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001692
1693 free_idle_conns:
1694 free(srv->idle_conns);
1695 free_priv_conns:
1696 free(srv->priv_conns);
1697 free_srv:
1698 free(srv);
1699 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001700}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001701
1702/*
1703 * Validate <srv> server health-check settings.
1704 * Returns 0 if everything is OK, -1 if not.
1705 */
1706static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1707{
1708 struct tcpcheck_rule *r = NULL;
1709 struct list *l;
1710
1711 /*
1712 * We need at least a service port, a check port or the first tcp-check rule must
1713 * be a 'connect' one when checking an IPv4/IPv6 server.
1714 */
1715 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1716 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1717 return 0;
1718
1719 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1720 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001721 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1722 "Check has been disabled.\n",
1723 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001724 return -1;
1725 }
1726
1727 /* search the first action (connect / send / expect) in the list */
1728 l = &srv->proxy->tcpcheck_rules;
1729 list_for_each_entry(r, l, list) {
1730 if (r->action != TCPCHK_ACT_COMMENT)
1731 break;
1732 }
1733
1734 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001735 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1736 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1737 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001738 return -1;
1739 }
1740
1741 /* scan the tcp-check ruleset to ensure a port has been configured */
1742 l = &srv->proxy->tcpcheck_rules;
1743 list_for_each_entry(r, l, list) {
1744 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001745 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1746 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1747 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001748 return -1;
1749 }
1750 }
1751
1752 return 0;
1753}
1754
1755/*
1756 * Initialize <srv> health-check structure.
1757 * Returns the error string in case of memory allocation failure, NULL if not.
1758 */
1759static const char *do_health_check_init(struct server *srv, int check_type, int state)
1760{
1761 const char *ret;
1762
1763 if (!srv->do_check)
1764 return NULL;
1765
1766 ret = init_check(&srv->check, check_type);
1767 if (ret)
1768 return ret;
1769
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001770 srv->check.state |= state;
1771 global.maxsock++;
1772
1773 return NULL;
1774}
1775
1776static int server_health_check_init(const char *file, int linenum,
1777 struct server *srv, struct proxy *curproxy)
1778{
1779 const char *ret;
1780
1781 if (!srv->do_check)
1782 return 0;
1783
1784 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001785 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1786 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001787 return ERR_ALERT | ERR_FATAL;
1788 }
1789
1790 if (server_healthcheck_validate(file, linenum, srv) < 0)
1791 return ERR_ALERT | ERR_ABORT;
1792
1793 /* note: check type will be set during the config review phase */
1794 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1795 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001796 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001797 return ERR_ALERT | ERR_ABORT;
1798 }
1799
1800 return 0;
1801}
1802
1803/*
1804 * Initialize <srv> agent check structure.
1805 * Returns the error string in case of memory allocation failure, NULL if not.
1806 */
1807static const char *do_server_agent_check_init(struct server *srv, int state)
1808{
1809 const char *ret;
1810
1811 if (!srv->do_agent)
1812 return NULL;
1813
1814 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1815 if (ret)
1816 return ret;
1817
1818 if (!srv->agent.inter)
1819 srv->agent.inter = srv->check.inter;
1820
1821 srv->agent.state |= state;
1822 global.maxsock++;
1823
1824 return NULL;
1825}
1826
1827static int server_agent_check_init(const char *file, int linenum,
1828 struct server *srv, struct proxy *curproxy)
1829{
1830 const char *ret;
1831
1832 if (!srv->do_agent)
1833 return 0;
1834
1835 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001836 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001837 file, linenum, srv->id);
1838 return ERR_ALERT | ERR_FATAL;
1839 }
1840
1841 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1842 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001843 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001844 return ERR_ALERT | ERR_ABORT;
1845 }
1846
1847 return 0;
1848}
1849
1850#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1851static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1852 struct server *srv, struct proxy *proxy)
1853{
1854 int ret;
1855 char *err = NULL;
1856
1857 if (!srv->sni_expr)
1858 return 0;
1859
1860 ret = server_parse_sni_expr(srv, proxy, &err);
1861 if (!ret)
1862 return 0;
1863
1864 display_parser_err(file, linenum, args, cur_arg, &err);
1865 free(err);
1866
1867 return ret;
1868}
1869#endif
1870
1871/*
1872 * Server initializations finalization.
1873 * Initialize health check, agent check and SNI expression if enabled.
1874 * Must not be called for a default server instance.
1875 */
1876static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1877 struct server *srv, struct proxy *px)
1878{
1879 int ret;
1880
1881 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1882 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1883 return ret;
1884 }
1885
1886#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1887 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1888 return ret;
1889#endif
1890
1891 if (srv->flags & SRV_F_BACKUP)
1892 px->srv_bck++;
1893 else
1894 px->srv_act++;
1895 srv_lb_commit_status(srv);
1896
1897 return 0;
1898}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001899
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001900/*
1901 * Parse as much as possible such a range string argument: low[-high]
1902 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1903 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1904 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1905 * Returns 0 if succeeded, -1 if not.
1906 */
1907static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1908{
1909 char *nb_high_arg;
1910
1911 *nb_high = 0;
1912 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001913 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001914
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001915 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001916 *nb_high_arg++ = '\0';
1917 *nb_high = atoi(nb_high_arg);
1918 }
1919 else {
1920 *nb_high += *nb_low;
1921 *nb_low = 1;
1922 }
1923
1924 if (*nb_low < 0 || *nb_high < *nb_low)
1925 return -1;
1926
1927 return 0;
1928}
1929
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001930static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1931{
1932 chunk_printf(&trash, "%s%d", prefix, nb);
1933 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001934 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001935}
1936
1937/*
1938 * Initialize as much as possible servers from <srv> server template.
1939 * Note that a server template is a special server with
1940 * a few different parameters than a server which has
1941 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001942 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001943 * 'srv' template included.
1944 */
1945static int server_template_init(struct server *srv, struct proxy *px)
1946{
1947 int i;
1948 struct server *newsrv;
1949
1950 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1951 int check_init_state;
1952 int agent_init_state;
1953
1954 newsrv = new_server(px);
1955 if (!newsrv)
1956 goto err;
1957
1958 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001959 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001960#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1961 if (newsrv->sni_expr) {
1962 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1963 if (!newsrv->ssl_ctx.sni)
1964 goto err;
1965 }
1966#endif
1967 /* Set this new server ID. */
1968 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1969
1970 /* Initial checks states. */
1971 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1972 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1973
1974 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1975 do_server_agent_check_init(newsrv, agent_init_state))
1976 goto err;
1977
1978 /* Linked backwards first. This will be restablished after parsing. */
1979 newsrv->next = px->srv;
1980 px->srv = newsrv;
1981 }
1982 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1983
1984 return i - srv->tmpl_info.nb_low;
1985
1986 err:
1987 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1988 if (newsrv) {
1989#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1990 release_sample_expr(newsrv->ssl_ctx.sni);
1991#endif
1992 free_check(&newsrv->agent);
1993 free_check(&newsrv->check);
1994 }
1995 free(newsrv);
1996 return i - srv->tmpl_info.nb_low;
1997}
1998
Willy Tarreau272adea2014-03-31 10:39:59 +02001999int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
2000{
2001 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002002 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002003 char *errmsg = NULL;
2004 int err_code = 0;
2005 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002006 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002007
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002008 if (!strcmp(args[0], "server") ||
2009 !strcmp(args[0], "default-server") ||
2010 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002011 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002012 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002013 int srv = !defsrv && !strcmp(args[0], "server");
2014 int srv_tmpl = !defsrv && !srv;
2015 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002016
2017 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002018 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002019 err_code |= ERR_ALERT | ERR_FATAL;
2020 goto out;
2021 }
2022 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002023 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002024
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002025 /* There is no mandatory first arguments for default server. */
2026 if (srv) {
2027 if (!*args[2]) {
2028 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002029 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002030 file, linenum, args[0]);
2031 err_code |= ERR_ALERT | ERR_FATAL;
2032 goto out;
2033 }
2034
2035 err = invalid_char(args[1]);
2036 }
2037 else if (srv_tmpl) {
2038 if (!*args[3]) {
2039 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002040 ha_alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002041 file, linenum, args[0]);
2042 err_code |= ERR_ALERT | ERR_FATAL;
2043 goto out;
2044 }
2045
2046 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002047 }
2048
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002049 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002050 ha_alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002051 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002052 err_code |= ERR_ALERT | ERR_FATAL;
2053 goto out;
2054 }
2055
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002056 cur_arg = 2;
2057 if (srv_tmpl) {
2058 /* Parse server-template <nb | range> arg. */
2059 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002060 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002061 file, linenum, args[0], args[cur_arg]);
2062 err_code |= ERR_ALERT | ERR_FATAL;
2063 goto out;
2064 }
2065 cur_arg++;
2066 }
2067
Willy Tarreau272adea2014-03-31 10:39:59 +02002068 if (!defsrv) {
2069 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002070 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002071 struct protocol *proto;
2072
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002073 newsrv = new_server(curproxy);
2074 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002075 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002076 err_code |= ERR_ALERT | ERR_ABORT;
2077 goto out;
2078 }
2079
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002080 if (srv_tmpl) {
2081 newsrv->tmpl_info.nb_low = tmpl_range_low;
2082 newsrv->tmpl_info.nb_high = tmpl_range_high;
2083 }
2084
Willy Tarreau272adea2014-03-31 10:39:59 +02002085 /* the servers are linked backwards first */
2086 newsrv->next = curproxy->srv;
2087 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002088 newsrv->conf.file = strdup(file);
2089 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002090 /* Note: for a server template, its id is its prefix.
2091 * This is a temporary id which will be used for server allocations to come
2092 * after parsing.
2093 */
2094 if (srv)
2095 newsrv->id = strdup(args[1]);
2096 else
2097 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002098
2099 /* several ways to check the port component :
2100 * - IP => port=+0, relative (IPv4 only)
2101 * - IP: => port=+0, relative
2102 * - IP:N => port=N, absolute
2103 * - IP:+N => port=+N, relative
2104 * - IP:-N => port=-N, relative
2105 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002106 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002107 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002108 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002109 err_code |= ERR_ALERT | ERR_FATAL;
2110 goto out;
2111 }
2112
2113 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002114 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002115 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002116 file, linenum, args[0], args[1]);
2117 err_code |= ERR_ALERT | ERR_FATAL;
2118 goto out;
2119 }
2120
2121 if (!port1 || !port2) {
2122 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002123 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002124 }
2125 else if (port1 != port2) {
2126 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002127 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002128 file, linenum, args[0], args[1], args[2]);
2129 err_code |= ERR_ALERT | ERR_FATAL;
2130 goto out;
2131 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002132
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002133 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002134 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002135 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002136 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002137 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2138 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2139 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002140 err_code |= ERR_ALERT | ERR_FATAL;
2141 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002142 }
2143 }
2144 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002145 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002146 file, linenum, newsrv->id);
2147 err_code |= ERR_ALERT | ERR_FATAL;
2148 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002149 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002150 }
2151
Willy Tarreau272adea2014-03-31 10:39:59 +02002152 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002153 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002154
Olivier Houchard8da5f982017-08-04 18:35:36 +02002155 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002156 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002157 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002158 err_code |= ERR_ALERT | ERR_FATAL;
2159 goto out;
2160 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002161
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002162 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002163 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002164 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002165 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002166 } else {
2167 newsrv = &curproxy->defsrv;
2168 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002169 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002170 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002171 }
2172
2173 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002174 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002175 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2176 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002177 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002178 file, linenum, *err, newsrv->id);
2179 err_code |= ERR_ALERT | ERR_FATAL;
2180 goto out;
2181 }
2182 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002183 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002184 file, linenum, val, args[cur_arg], newsrv->id);
2185 err_code |= ERR_ALERT | ERR_FATAL;
2186 goto out;
2187 }
2188 newsrv->agent.inter = val;
2189 cur_arg += 2;
2190 }
Misiekea849332017-01-09 09:39:51 +01002191 else if (!strcmp(args[cur_arg], "agent-addr")) {
2192 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002193 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002194 goto out;
2195 }
2196
2197 cur_arg += 2;
2198 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002199 else if (!strcmp(args[cur_arg], "agent-port")) {
2200 global.maxsock++;
2201 newsrv->agent.port = atol(args[cur_arg + 1]);
2202 cur_arg += 2;
2203 }
James Brown55f9ff12015-10-21 18:19:05 -07002204 else if (!strcmp(args[cur_arg], "agent-send")) {
2205 global.maxsock++;
2206 free(newsrv->agent.send_string);
2207 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2208 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2209 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2210 cur_arg += 2;
2211 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002212 else if (!strcmp(args[cur_arg], "init-addr")) {
2213 char *p, *end;
2214 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002215 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002216
2217 newsrv->init_addr_methods = 0;
2218 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2219
2220 for (p = args[cur_arg + 1]; *p; p = end) {
2221 /* cut on next comma */
2222 for (end = p; *end && *end != ','; end++);
2223 if (*end)
2224 *(end++) = 0;
2225
Willy Tarreau4310d362016-11-02 15:05:56 +01002226 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002227 if (!strcmp(p, "libc")) {
2228 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2229 }
2230 else if (!strcmp(p, "last")) {
2231 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2232 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002233 else if (!strcmp(p, "none")) {
2234 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2235 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002236 else if (str2ip2(p, &sa, 0)) {
2237 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002238 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002239 file, linenum, args[cur_arg], p);
2240 err_code |= ERR_ALERT | ERR_FATAL;
2241 goto out;
2242 }
2243 newsrv->init_addr = sa;
2244 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2245 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002246 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002247 ha_alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002248 file, linenum, args[cur_arg], p);
2249 err_code |= ERR_ALERT | ERR_FATAL;
2250 goto out;
2251 }
2252 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002253 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002254 file, linenum, args[cur_arg], p);
2255 err_code |= ERR_ALERT | ERR_FATAL;
2256 goto out;
2257 }
2258 }
2259 cur_arg += 2;
2260 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002261 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002262 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002263 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2264 cur_arg += 2;
2265 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002266 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2267 char *p, *end;
2268
2269 for (p = args[cur_arg + 1]; *p; p = end) {
2270 /* cut on next comma */
2271 for (end = p; *end && *end != ','; end++);
2272 if (*end)
2273 *(end++) = 0;
2274
2275 if (!strcmp(p, "allow-dup-ip")) {
2276 newsrv->dns_opts.accept_duplicate_ip = 1;
2277 }
2278 else if (!strcmp(p, "prevent-dup-ip")) {
2279 newsrv->dns_opts.accept_duplicate_ip = 0;
2280 }
2281 else {
2282 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2283 file, linenum, args[cur_arg], p);
2284 err_code |= ERR_ALERT | ERR_FATAL;
2285 goto out;
2286 }
2287 }
2288
2289 cur_arg += 2;
2290 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002291 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2292 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002293 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002294 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002295 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002296 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002297 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002298 file, linenum, args[cur_arg]);
2299 err_code |= ERR_ALERT | ERR_FATAL;
2300 goto out;
2301 }
2302 cur_arg += 2;
2303 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002304 else if (!strcmp(args[cur_arg], "resolve-net")) {
2305 char *p, *e;
2306 unsigned char mask;
2307 struct dns_options *opt;
2308
2309 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002310 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002311 file, linenum, args[cur_arg]);
2312 err_code |= ERR_ALERT | ERR_FATAL;
2313 goto out;
2314 }
2315
2316 opt = &newsrv->dns_opts;
2317
2318 /* Split arguments by comma, and convert it from ipv4 or ipv6
2319 * string network in in_addr or in6_addr.
2320 */
2321 p = args[cur_arg + 1];
2322 e = p;
2323 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002324 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002325 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002326 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002327 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2328 err_code |= ERR_ALERT | ERR_FATAL;
2329 goto out;
2330 }
2331 /* look for end or comma. */
2332 while (*e != ',' && *e != '\0')
2333 e++;
2334 if (*e == ',') {
2335 *e = '\0';
2336 e++;
2337 }
2338 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2339 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2340 /* Try to convert input string from ipv4 or ipv6 network. */
2341 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2342 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2343 &mask)) {
2344 /* Try to convert input string from ipv6 network. */
2345 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2346 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2347 } else {
2348 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002349 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002350 file, linenum, args[cur_arg], p);
2351 err_code |= ERR_ALERT | ERR_FATAL;
2352 goto out;
2353 }
2354 opt->pref_net_nb++;
2355 p = e;
2356 }
2357
2358 cur_arg += 2;
2359 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002360 else if (!strcmp(args[cur_arg], "rise")) {
2361 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002362 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002363 file, linenum, args[cur_arg]);
2364 err_code |= ERR_ALERT | ERR_FATAL;
2365 goto out;
2366 }
2367
2368 newsrv->check.rise = atol(args[cur_arg + 1]);
2369 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002370 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002371 file, linenum, args[cur_arg]);
2372 err_code |= ERR_ALERT | ERR_FATAL;
2373 goto out;
2374 }
2375
2376 if (newsrv->check.health)
2377 newsrv->check.health = newsrv->check.rise;
2378 cur_arg += 2;
2379 }
2380 else if (!strcmp(args[cur_arg], "fall")) {
2381 newsrv->check.fall = atol(args[cur_arg + 1]);
2382
2383 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002384 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002385 file, linenum, args[cur_arg]);
2386 err_code |= ERR_ALERT | ERR_FATAL;
2387 goto out;
2388 }
2389
2390 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002391 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002392 file, linenum, args[cur_arg]);
2393 err_code |= ERR_ALERT | ERR_FATAL;
2394 goto out;
2395 }
2396
2397 cur_arg += 2;
2398 }
2399 else if (!strcmp(args[cur_arg], "inter")) {
2400 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2401 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002402 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002403 file, linenum, *err, newsrv->id);
2404 err_code |= ERR_ALERT | ERR_FATAL;
2405 goto out;
2406 }
2407 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002408 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002409 file, linenum, val, args[cur_arg], newsrv->id);
2410 err_code |= ERR_ALERT | ERR_FATAL;
2411 goto out;
2412 }
2413 newsrv->check.inter = val;
2414 cur_arg += 2;
2415 }
2416 else if (!strcmp(args[cur_arg], "fastinter")) {
2417 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2418 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002419 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002420 file, linenum, *err, newsrv->id);
2421 err_code |= ERR_ALERT | ERR_FATAL;
2422 goto out;
2423 }
2424 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002425 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002426 file, linenum, val, args[cur_arg], newsrv->id);
2427 err_code |= ERR_ALERT | ERR_FATAL;
2428 goto out;
2429 }
2430 newsrv->check.fastinter = val;
2431 cur_arg += 2;
2432 }
2433 else if (!strcmp(args[cur_arg], "downinter")) {
2434 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2435 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002436 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002437 file, linenum, *err, newsrv->id);
2438 err_code |= ERR_ALERT | ERR_FATAL;
2439 goto out;
2440 }
2441 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002442 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002443 file, linenum, val, args[cur_arg], newsrv->id);
2444 err_code |= ERR_ALERT | ERR_FATAL;
2445 goto out;
2446 }
2447 newsrv->check.downinter = val;
2448 cur_arg += 2;
2449 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002450 else if (!strcmp(args[cur_arg], "port")) {
2451 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002452 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002453 cur_arg += 2;
2454 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002455 else if (!strcmp(args[cur_arg], "weight")) {
2456 int w;
2457 w = atol(args[cur_arg + 1]);
2458 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002459 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002460 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2461 err_code |= ERR_ALERT | ERR_FATAL;
2462 goto out;
2463 }
2464 newsrv->uweight = newsrv->iweight = w;
2465 cur_arg += 2;
2466 }
2467 else if (!strcmp(args[cur_arg], "minconn")) {
2468 newsrv->minconn = atol(args[cur_arg + 1]);
2469 cur_arg += 2;
2470 }
2471 else if (!strcmp(args[cur_arg], "maxconn")) {
2472 newsrv->maxconn = atol(args[cur_arg + 1]);
2473 cur_arg += 2;
2474 }
2475 else if (!strcmp(args[cur_arg], "maxqueue")) {
2476 newsrv->maxqueue = atol(args[cur_arg + 1]);
2477 cur_arg += 2;
2478 }
2479 else if (!strcmp(args[cur_arg], "slowstart")) {
2480 /* slowstart is stored in seconds */
2481 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2482 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002483 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002484 file, linenum, *err, newsrv->id);
2485 err_code |= ERR_ALERT | ERR_FATAL;
2486 goto out;
2487 }
2488 newsrv->slowstart = (val + 999) / 1000;
2489 cur_arg += 2;
2490 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002491 else if (!strcmp(args[cur_arg], "on-error")) {
2492 if (!strcmp(args[cur_arg + 1], "fastinter"))
2493 newsrv->onerror = HANA_ONERR_FASTINTER;
2494 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2495 newsrv->onerror = HANA_ONERR_FAILCHK;
2496 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2497 newsrv->onerror = HANA_ONERR_SUDDTH;
2498 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2499 newsrv->onerror = HANA_ONERR_MARKDWN;
2500 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002501 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002502 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2503 file, linenum, args[cur_arg], args[cur_arg + 1]);
2504 err_code |= ERR_ALERT | ERR_FATAL;
2505 goto out;
2506 }
2507
2508 cur_arg += 2;
2509 }
2510 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2511 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2512 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2513 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002514 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002515 file, linenum, args[cur_arg], args[cur_arg + 1]);
2516 err_code |= ERR_ALERT | ERR_FATAL;
2517 goto out;
2518 }
2519
2520 cur_arg += 2;
2521 }
2522 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2523 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2524 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2525 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002526 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002527 file, linenum, args[cur_arg], args[cur_arg + 1]);
2528 err_code |= ERR_ALERT | ERR_FATAL;
2529 goto out;
2530 }
2531
2532 cur_arg += 2;
2533 }
2534 else if (!strcmp(args[cur_arg], "error-limit")) {
2535 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002536 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002537 file, linenum, args[cur_arg]);
2538 err_code |= ERR_ALERT | ERR_FATAL;
2539 goto out;
2540 }
2541
2542 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2543
2544 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002545 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002546 file, linenum, args[cur_arg]);
2547 err_code |= ERR_ALERT | ERR_FATAL;
2548 goto out;
2549 }
2550 cur_arg += 2;
2551 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002552 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002553 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002554 file, linenum, "usesrc", "source");
2555 err_code |= ERR_ALERT | ERR_FATAL;
2556 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002557 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002558 else {
2559 static int srv_dumped;
2560 struct srv_kw *kw;
2561 char *err;
2562
2563 kw = srv_find_kw(args[cur_arg]);
2564 if (kw) {
2565 char *err = NULL;
2566 int code;
2567
2568 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002569 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002570 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002571 if (kw->skip != -1)
2572 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002573 err_code |= ERR_ALERT | ERR_FATAL;
2574 goto out;
2575 }
2576
2577 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002578 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002579 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002580 if (kw->skip != -1)
2581 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002582 err_code |= ERR_ALERT;
2583 continue;
2584 }
2585
2586 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2587 err_code |= code;
2588
2589 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002590 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002591 if (code & ERR_FATAL) {
2592 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002593 if (kw->skip != -1)
2594 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002595 goto out;
2596 }
2597 }
2598 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002599 if (kw->skip != -1)
2600 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002601 continue;
2602 }
2603
2604 err = NULL;
2605 if (!srv_dumped) {
2606 srv_dump_kws(&err);
2607 indent_msg(&err, 4);
2608 srv_dumped = 1;
2609 }
2610
Christopher Faulet767a84b2017-11-24 16:50:31 +01002611 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002612 file, linenum, args[0], args[1], args[cur_arg],
2613 err ? " Registered keywords :" : "", err ? err : "");
2614 free(err);
2615
2616 err_code |= ERR_ALERT | ERR_FATAL;
2617 goto out;
2618 }
2619 }
2620
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002621 if (!defsrv)
2622 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2623 if (err_code & ERR_FATAL)
2624 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002625 if (srv_tmpl)
2626 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002627 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002628 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002629 return 0;
2630
2631 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002632 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002633 free(errmsg);
2634 return err_code;
2635}
2636
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002637/* Returns a pointer to the first server matching either id <id>.
2638 * NULL is returned if no match is found.
2639 * the lookup is performed in the backend <bk>
2640 */
2641struct server *server_find_by_id(struct proxy *bk, int id)
2642{
2643 struct eb32_node *eb32;
2644 struct server *curserver;
2645
2646 if (!bk || (id ==0))
2647 return NULL;
2648
2649 /* <bk> has no backend capabilities, so it can't have a server */
2650 if (!(bk->cap & PR_CAP_BE))
2651 return NULL;
2652
2653 curserver = NULL;
2654
2655 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2656 if (eb32)
2657 curserver = container_of(eb32, struct server, conf.id);
2658
2659 return curserver;
2660}
2661
2662/* Returns a pointer to the first server matching either name <name>, or id
2663 * if <name> starts with a '#'. NULL is returned if no match is found.
2664 * the lookup is performed in the backend <bk>
2665 */
2666struct server *server_find_by_name(struct proxy *bk, const char *name)
2667{
2668 struct server *curserver;
2669
2670 if (!bk || !name)
2671 return NULL;
2672
2673 /* <bk> has no backend capabilities, so it can't have a server */
2674 if (!(bk->cap & PR_CAP_BE))
2675 return NULL;
2676
2677 curserver = NULL;
2678 if (*name == '#') {
2679 curserver = server_find_by_id(bk, atoi(name + 1));
2680 if (curserver)
2681 return curserver;
2682 }
2683 else {
2684 curserver = bk->srv;
2685
2686 while (curserver && (strcmp(curserver->id, name) != 0))
2687 curserver = curserver->next;
2688
2689 if (curserver)
2690 return curserver;
2691 }
2692
2693 return NULL;
2694}
2695
2696struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2697{
2698 struct server *byname;
2699 struct server *byid;
2700
2701 if (!name && !id)
2702 return NULL;
2703
2704 if (diff)
2705 *diff = 0;
2706
2707 byname = byid = NULL;
2708
2709 if (name) {
2710 byname = server_find_by_name(bk, name);
2711 if (byname && (!id || byname->puid == id))
2712 return byname;
2713 }
2714
2715 /* remaining possibilities :
2716 * - name not set
2717 * - name set but not found
2718 * - name found but ID doesn't match
2719 */
2720 if (id) {
2721 byid = server_find_by_id(bk, id);
2722 if (byid) {
2723 if (byname) {
2724 /* use id only if forced by configuration */
2725 if (byid->flags & SRV_F_FORCED_ID) {
2726 if (diff)
2727 *diff |= 2;
2728 return byid;
2729 }
2730 else {
2731 if (diff)
2732 *diff |= 1;
2733 return byname;
2734 }
2735 }
2736
2737 /* remaining possibilities:
2738 * - name not set
2739 * - name set but not found
2740 */
2741 if (name && diff)
2742 *diff |= 2;
2743 return byid;
2744 }
2745
2746 /* id bot found */
2747 if (byname) {
2748 if (diff)
2749 *diff |= 1;
2750 return byname;
2751 }
2752 }
2753
2754 return NULL;
2755}
2756
Willy Tarreau46b7f532018-08-21 11:54:26 +02002757/* Update a server state using the parameters available in the params list.
2758 *
2759 * Grabs the server lock during operation.
2760 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002761static void srv_update_state(struct server *srv, int version, char **params)
2762{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002763 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002764 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002765
2766 /* fields since version 1
2767 * and common to all other upcoming versions
2768 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002769 enum srv_state srv_op_state;
2770 enum srv_admin srv_admin_state;
2771 unsigned srv_uweight, srv_iweight;
2772 unsigned long srv_last_time_change;
2773 short srv_check_status;
2774 enum chk_result srv_check_result;
2775 int srv_check_health;
2776 int srv_check_state, srv_agent_state;
2777 int bk_f_forced_id;
2778 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002779 int fqdn_set_by_cli;
2780 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002781 const char *port_str;
2782 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002783 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002784
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002785 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002786 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002787 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002788 switch (version) {
2789 case 1:
2790 /*
2791 * now we can proceed with server's state update:
2792 * srv_addr: params[0]
2793 * srv_op_state: params[1]
2794 * srv_admin_state: params[2]
2795 * srv_uweight: params[3]
2796 * srv_iweight: params[4]
2797 * srv_last_time_change: params[5]
2798 * srv_check_status: params[6]
2799 * srv_check_result: params[7]
2800 * srv_check_health: params[8]
2801 * srv_check_state: params[9]
2802 * srv_agent_state: params[10]
2803 * bk_f_forced_id: params[11]
2804 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002805 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002806 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002807 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002808 */
2809
2810 /* validating srv_op_state */
2811 p = NULL;
2812 errno = 0;
2813 srv_op_state = strtol(params[1], &p, 10);
2814 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2815 (srv_op_state != SRV_ST_STOPPED &&
2816 srv_op_state != SRV_ST_STARTING &&
2817 srv_op_state != SRV_ST_RUNNING &&
2818 srv_op_state != SRV_ST_STOPPING)) {
2819 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2820 }
2821
2822 /* validating srv_admin_state */
2823 p = NULL;
2824 errno = 0;
2825 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002826 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002827
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002828 /* inherited statuses will be recomputed later.
2829 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2830 */
2831 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002832
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002833 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2834 (srv_admin_state != 0 &&
2835 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002836 srv_admin_state != SRV_ADMF_CMAINT &&
2837 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002838 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002839 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002840 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2841 }
2842
2843 /* validating srv_uweight */
2844 p = NULL;
2845 errno = 0;
2846 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002847 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002848 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2849
2850 /* validating srv_iweight */
2851 p = NULL;
2852 errno = 0;
2853 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002854 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002855 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2856
2857 /* validating srv_last_time_change */
2858 p = NULL;
2859 errno = 0;
2860 srv_last_time_change = strtol(params[5], &p, 10);
2861 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2862 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2863
2864 /* validating srv_check_status */
2865 p = NULL;
2866 errno = 0;
2867 srv_check_status = strtol(params[6], &p, 10);
2868 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2869 (srv_check_status >= HCHK_STATUS_SIZE))
2870 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2871
2872 /* validating srv_check_result */
2873 p = NULL;
2874 errno = 0;
2875 srv_check_result = strtol(params[7], &p, 10);
2876 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2877 (srv_check_result != CHK_RES_UNKNOWN &&
2878 srv_check_result != CHK_RES_NEUTRAL &&
2879 srv_check_result != CHK_RES_FAILED &&
2880 srv_check_result != CHK_RES_PASSED &&
2881 srv_check_result != CHK_RES_CONDPASS)) {
2882 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2883 }
2884
2885 /* validating srv_check_health */
2886 p = NULL;
2887 errno = 0;
2888 srv_check_health = strtol(params[8], &p, 10);
2889 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2890 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2891
2892 /* validating srv_check_state */
2893 p = NULL;
2894 errno = 0;
2895 srv_check_state = strtol(params[9], &p, 10);
2896 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2897 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2898 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2899
2900 /* validating srv_agent_state */
2901 p = NULL;
2902 errno = 0;
2903 srv_agent_state = strtol(params[10], &p, 10);
2904 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2905 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2906 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2907
2908 /* validating bk_f_forced_id */
2909 p = NULL;
2910 errno = 0;
2911 bk_f_forced_id = strtol(params[11], &p, 10);
2912 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2913 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2914
2915 /* validating srv_f_forced_id */
2916 p = NULL;
2917 errno = 0;
2918 srv_f_forced_id = strtol(params[12], &p, 10);
2919 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2920 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2921
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002922 /* validating srv_fqdn */
2923 fqdn = params[13];
2924 if (fqdn && *fqdn == '-')
2925 fqdn = NULL;
2926 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2927 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2928 fqdn = NULL;
2929 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002930
Frédéric Lécaille31694712017-08-01 08:47:19 +02002931 port_str = params[14];
2932 if (port_str) {
2933 port = strl2uic(port_str, strlen(port_str));
2934 if (port > USHRT_MAX) {
2935 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2936 port_str = NULL;
2937 }
2938 }
2939
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002940 /* SRV record
2941 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2942 */
2943 srvrecord = params[15];
2944 if (srvrecord && *srvrecord != '_')
2945 srvrecord = NULL;
2946
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002947 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002948 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002949 goto out;
2950
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002951 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002952 /* recover operational state and apply it to this server
2953 * and all servers tracking this one */
2954 switch (srv_op_state) {
2955 case SRV_ST_STOPPED:
2956 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002957 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002958 break;
2959 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002960 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002961 break;
2962 case SRV_ST_STOPPING:
2963 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002964 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002965 break;
2966 case SRV_ST_RUNNING:
2967 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002968 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002969 break;
2970 }
2971
2972 /* When applying server state, the following rules apply:
2973 * - in case of a configuration change, we apply the setting from the new
2974 * configuration, regardless of old running state
2975 * - if no configuration change, we apply old running state only if old running
2976 * state is different from new configuration state
2977 */
2978 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002979 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2980 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002981 srv_adm_set_maint(srv);
2982 else
2983 srv_adm_set_ready(srv);
2984 }
2985 /* configuration is the same, let's compate old running state and new conf state */
2986 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002987 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002988 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002989 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002990 srv_adm_set_ready(srv);
2991 }
2992 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002993 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002994 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002995 * (srv->iweight is the weight set up in configuration).
2996 * There are two possible reasons for FDRAIN to have been present :
2997 * - previous config weight was zero
2998 * - "set server b/s drain" was sent to the CLI
2999 *
3000 * In the first case, we simply want to drop this drain state
3001 * if the new weight is not zero anymore, meaning the administrator
3002 * has intentionally turned the weight back to a positive value to
3003 * enable the server again after an operation. In the second case,
3004 * the drain state was forced on the CLI regardless of the config's
3005 * weight so we don't want a change to the config weight to lose this
3006 * status. What this means is :
3007 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3008 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003009 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003010 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003011 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003012 }
3013
3014 srv->last_change = date.tv_sec - srv_last_time_change;
3015 srv->check.status = srv_check_status;
3016 srv->check.result = srv_check_result;
3017 srv->check.health = srv_check_health;
3018
3019 /* Only case we want to apply is removing ENABLED flag which could have been
3020 * done by the "disable health" command over the stats socket
3021 */
3022 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3023 (srv_check_state & CHK_ST_CONFIGURED) &&
3024 !(srv_check_state & CHK_ST_ENABLED))
3025 srv->check.state &= ~CHK_ST_ENABLED;
3026
3027 /* Only case we want to apply is removing ENABLED flag which could have been
3028 * done by the "disable agent" command over the stats socket
3029 */
3030 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3031 (srv_agent_state & CHK_ST_CONFIGURED) &&
3032 !(srv_agent_state & CHK_ST_ENABLED))
3033 srv->agent.state &= ~CHK_ST_ENABLED;
3034
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003035 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3036 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003037 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003038 * It means that a configuration file change has precedence over a unix socket change
3039 * for server's weight
3040 *
3041 * by default, HAProxy applies the following weight when parsing the configuration
3042 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003043 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003044 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003045 srv->uweight = srv_uweight;
3046 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003047 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003048
Willy Tarreaue5a60682016-11-09 14:54:53 +01003049 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003050 if (strcmp(params[0], "-"))
3051 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003052
3053 if (fqdn && srv->hostname) {
3054 if (!strcmp(srv->hostname, fqdn)) {
3055 /* Here we reset the 'set from stats socket FQDN' flag
3056 * to support such transitions:
3057 * Let's say initial FQDN value is foo1 (in configuration file).
3058 * - FQDN changed from stats socket, from foo1 to foo2 value,
3059 * - FQDN changed again from file configuration (with the same previous value
3060 set from stats socket, from foo1 to foo2 value),
3061 * - reload for any other reason than a FQDN modification,
3062 * the configuration file FQDN matches the fqdn server state file value.
3063 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003064 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003065 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003066 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003067 }
3068 else {
3069 /* If the FDQN has been changed from stats socket,
3070 * apply fqdn state file value (which is the value set
3071 * from stats socket).
3072 */
3073 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003074 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003075 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003076 }
3077 }
3078 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003079 /* If all the conditions below are validated, this means
3080 * we're evaluating a server managed by SRV resolution
3081 */
3082 else if (fqdn && !srv->hostname && srvrecord) {
3083 int res;
3084
3085 /* we can't apply previous state if SRV record has changed */
3086 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3087 chunk_appendf(msg, ", SRV record mismatch between configuration ('%s') and state file ('%s) for server '%s'. Previous state not applied", srv->srvrq->name, srvrecord, srv->id);
3088 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3089 goto out;
3090 }
3091
3092 /* create or find a SRV resolution for this srv record */
3093 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3094 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3095 if (srv->srvrq == NULL) {
3096 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3097 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3098 goto out;
3099 }
3100
3101 /* prepare DNS resolution for this server */
3102 res = srv_prepare_for_resolution(srv, fqdn);
3103 if (res == -1) {
3104 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3105 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3106 goto out;
3107 }
3108
3109 /* configure check.port accordingly */
3110 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3111 !(srv->flags & SRV_F_CHECKPORT))
3112 srv->check.port = port;
3113
3114 /* Unset SRV_F_MAPPORTS for SRV records.
3115 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3116 * because no ports are provided in the configuration file.
3117 * This is because HAProxy will use the port found into the SRV record.
3118 */
3119 srv->flags &= ~SRV_F_MAPPORTS;
3120 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003121
Frédéric Lécaille31694712017-08-01 08:47:19 +02003122 if (port_str)
3123 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003124 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003125
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003126 break;
3127 default:
3128 chunk_appendf(msg, ", version '%d' not supported", version);
3129 }
3130
3131 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003132 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003133 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003134 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003135 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003136 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003137}
3138
3139/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3140 * For each proxy, it does the following:
3141 * - opens its server state file (either one or local one)
3142 * - read whole file, line by line
3143 * - analyse each line to check if it matches our current backend:
3144 * - backend name matches
3145 * - backend id matches if id is forced and name doesn't match
3146 * - if the server pointed by the line is found, then state is applied
3147 *
3148 * If the running backend uuid or id differs from the state file, then HAProxy reports
3149 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003150 *
3151 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003152 */
3153void apply_server_state(void)
3154{
3155 char *cur, *end;
3156 char mybuf[SRV_STATE_LINE_MAXLEN];
3157 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003158 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3159 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003160 int arg, srv_arg, version, diff;
3161 FILE *f;
3162 char *filepath;
3163 char globalfilepath[MAXPATHLEN + 1];
3164 char localfilepath[MAXPATHLEN + 1];
3165 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003166 struct proxy *curproxy, *bk;
3167 struct server *srv;
3168
3169 globalfilepathlen = 0;
3170 /* create the globalfilepath variable */
3171 if (global.server_state_file) {
3172 /* absolute path or no base directory provided */
3173 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3174 len = strlen(global.server_state_file);
3175 if (len > MAXPATHLEN) {
3176 globalfilepathlen = 0;
3177 goto globalfileerror;
3178 }
3179 memcpy(globalfilepath, global.server_state_file, len);
3180 globalfilepath[len] = '\0';
3181 globalfilepathlen = len;
3182 }
3183 else if (global.server_state_base) {
3184 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003185 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003186 globalfilepathlen = 0;
3187 goto globalfileerror;
3188 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003189 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003190 globalfilepath[len] = 0;
3191 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003192
3193 /* append a slash if needed */
3194 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3195 if (globalfilepathlen + 1 > MAXPATHLEN) {
3196 globalfilepathlen = 0;
3197 goto globalfileerror;
3198 }
3199 globalfilepath[globalfilepathlen++] = '/';
3200 }
3201
3202 len = strlen(global.server_state_file);
3203 if (globalfilepathlen + len > MAXPATHLEN) {
3204 globalfilepathlen = 0;
3205 goto globalfileerror;
3206 }
3207 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3208 globalfilepathlen += len;
3209 globalfilepath[globalfilepathlen++] = 0;
3210 }
3211 }
3212 globalfileerror:
3213 if (globalfilepathlen == 0)
3214 globalfilepath[0] = '\0';
3215
3216 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003217 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003218 /* servers are only in backends */
3219 if (!(curproxy->cap & PR_CAP_BE))
3220 continue;
3221 fileopenerr = 0;
3222 filepath = NULL;
3223
3224 /* search server state file path and name */
3225 switch (curproxy->load_server_state_from_file) {
3226 /* read servers state from global file */
3227 case PR_SRV_STATE_FILE_GLOBAL:
3228 /* there was an error while generating global server state file path */
3229 if (globalfilepathlen == 0)
3230 continue;
3231 filepath = globalfilepath;
3232 fileopenerr = 1;
3233 break;
3234 /* this backend has its own file */
3235 case PR_SRV_STATE_FILE_LOCAL:
3236 localfilepathlen = 0;
3237 localfilepath[0] = '\0';
3238 len = 0;
3239 /* create the localfilepath variable */
3240 /* absolute path or no base directory provided */
3241 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3242 len = strlen(curproxy->server_state_file_name);
3243 if (len > MAXPATHLEN) {
3244 localfilepathlen = 0;
3245 goto localfileerror;
3246 }
3247 memcpy(localfilepath, curproxy->server_state_file_name, len);
3248 localfilepath[len] = '\0';
3249 localfilepathlen = len;
3250 }
3251 else if (global.server_state_base) {
3252 len = strlen(global.server_state_base);
3253 localfilepathlen += len;
3254
3255 if (localfilepathlen > MAXPATHLEN) {
3256 localfilepathlen = 0;
3257 goto localfileerror;
3258 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003259 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003260 localfilepath[localfilepathlen] = 0;
3261
3262 /* append a slash if needed */
3263 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3264 if (localfilepathlen + 1 > MAXPATHLEN) {
3265 localfilepathlen = 0;
3266 goto localfileerror;
3267 }
3268 localfilepath[localfilepathlen++] = '/';
3269 }
3270
3271 len = strlen(curproxy->server_state_file_name);
3272 if (localfilepathlen + len > MAXPATHLEN) {
3273 localfilepathlen = 0;
3274 goto localfileerror;
3275 }
3276 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3277 localfilepathlen += len;
3278 localfilepath[localfilepathlen++] = 0;
3279 }
3280 filepath = localfilepath;
3281 localfileerror:
3282 if (localfilepathlen == 0)
3283 localfilepath[0] = '\0';
3284
3285 break;
3286 case PR_SRV_STATE_FILE_NONE:
3287 default:
3288 continue;
3289 }
3290
3291 /* preload global state file */
3292 errno = 0;
3293 f = fopen(filepath, "r");
3294 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003295 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003296 if (!f)
3297 continue;
3298
3299 mybuf[0] = '\0';
3300 mybuflen = 0;
3301 version = 0;
3302
3303 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003304 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003305 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003306 goto fileclose;
3307 }
3308
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003309 cur = mybuf;
3310 version = atoi(cur);
3311 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3312 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003313 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003314
3315 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3316 int bk_f_forced_id = 0;
3317 int check_id = 0;
3318 int check_name = 0;
3319
3320 mybuflen = strlen(mybuf);
3321 cur = mybuf;
3322 end = cur + mybuflen;
3323
3324 bk = NULL;
3325 srv = NULL;
3326
3327 /* we need at least one character */
3328 if (mybuflen == 0)
3329 continue;
3330
3331 /* ignore blank characters at the beginning of the line */
3332 while (isspace(*cur))
3333 ++cur;
3334
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003335 /* Ignore empty or commented lines */
3336 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003337 continue;
3338
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003339 /* truncated lines */
3340 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003341 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003342 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003343 }
3344
3345 /* Removes trailing '\n' */
3346 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003347
3348 /* we're now ready to move the line into *srv_params[] */
3349 params[0] = cur;
3350 arg = 1;
3351 srv_arg = 0;
3352 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3353 if (isspace(*cur)) {
3354 *cur = '\0';
3355 ++cur;
3356 while (isspace(*cur))
3357 ++cur;
3358 switch (version) {
3359 case 1:
3360 /*
3361 * srv_addr: params[4] => srv_params[0]
3362 * srv_op_state: params[5] => srv_params[1]
3363 * srv_admin_state: params[6] => srv_params[2]
3364 * srv_uweight: params[7] => srv_params[3]
3365 * srv_iweight: params[8] => srv_params[4]
3366 * srv_last_time_change: params[9] => srv_params[5]
3367 * srv_check_status: params[10] => srv_params[6]
3368 * srv_check_result: params[11] => srv_params[7]
3369 * srv_check_health: params[12] => srv_params[8]
3370 * srv_check_state: params[13] => srv_params[9]
3371 * srv_agent_state: params[14] => srv_params[10]
3372 * bk_f_forced_id: params[15] => srv_params[11]
3373 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003374 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003375 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003376 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003377 */
3378 if (arg >= 4) {
3379 srv_params[srv_arg] = cur;
3380 ++srv_arg;
3381 }
3382 break;
3383 }
3384
3385 params[arg] = cur;
3386 ++arg;
3387 }
3388 else {
3389 ++cur;
3390 }
3391 }
3392
3393 /* if line is incomplete line, then ignore it.
3394 * otherwise, update useful flags */
3395 switch (version) {
3396 case 1:
3397 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3398 continue;
3399 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3400 check_id = (atoi(params[0]) == curproxy->uuid);
3401 check_name = (strcmp(curproxy->id, params[1]) == 0);
3402 break;
3403 }
3404
3405 diff = 0;
3406 bk = curproxy;
3407
3408 /* if backend can't be found, let's continue */
3409 if (!check_id && !check_name)
3410 continue;
3411 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003412 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003413 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3414 }
3415 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003416 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003417 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3418 /* if name doesn't match, we still want to update curproxy if the backend id
3419 * was forced in previous the previous configuration */
3420 if (!bk_f_forced_id)
3421 continue;
3422 }
3423
3424 /* look for the server by its id: param[2] */
3425 /* else look for the server by its name: param[3] */
3426 diff = 0;
3427 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3428
3429 if (!srv) {
3430 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003431 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3432 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003433 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3434 params[3], params[2], params[0], params[1]);
3435 continue;
3436 }
3437 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003438 ha_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);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003439 send_log(bk, LOG_NOTICE, "In backend '%s' (id: %d): server ID mismatch: from server state file: '%s', from running config %d\n", bk->id, bk->uuid, params[2], srv->puid);
Frédéric Lécaille0bedb8a2017-06-15 14:09:10 +02003440 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003441 }
3442 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003443 ha_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);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003444 send_log(bk, LOG_NOTICE, "In backend '%s' (id: %d): server name mismatch: from server state file: '%s', from running config '%s'\n", bk->id, bk->uuid, params[3], srv->id);
Frédéric Lécaille0bedb8a2017-06-15 14:09:10 +02003445 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003446 }
3447
3448 /* now we can proceed with server's state update */
3449 srv_update_state(srv, version, srv_params);
3450 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003451fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003452 fclose(f);
3453 }
3454}
3455
Simon Horman7d09b9a2013-02-12 10:45:51 +09003456/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003457 * update a server's current IP address.
3458 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3459 * ip is in network format.
3460 * updater is a string which contains an information about the requester of the update.
3461 * updater is used if not NULL.
3462 *
3463 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003464 *
3465 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003466 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003467int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003468{
3469 /* generates a log line and a warning on stderr */
3470 if (1) {
3471 /* book enough space for both IPv4 and IPv6 */
3472 char oldip[INET6_ADDRSTRLEN];
3473 char newip[INET6_ADDRSTRLEN];
3474
3475 memset(oldip, '\0', INET6_ADDRSTRLEN);
3476 memset(newip, '\0', INET6_ADDRSTRLEN);
3477
3478 /* copy old IP address in a string */
3479 switch (s->addr.ss_family) {
3480 case AF_INET:
3481 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3482 break;
3483 case AF_INET6:
3484 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3485 break;
3486 };
3487
3488 /* copy new IP address in a string */
3489 switch (ip_sin_family) {
3490 case AF_INET:
3491 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3492 break;
3493 case AF_INET6:
3494 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3495 break;
3496 };
3497
3498 /* save log line into a buffer */
3499 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3500 s->proxy->id, s->id, oldip, newip, updater);
3501
3502 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003503 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003504
3505 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003506 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003507 }
3508
3509 /* save the new IP family */
3510 s->addr.ss_family = ip_sin_family;
3511 /* save the new IP address */
3512 switch (ip_sin_family) {
3513 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003514 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003515 break;
3516 case AF_INET6:
3517 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3518 break;
3519 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003520 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003521
3522 return 0;
3523}
3524
3525/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003526 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3527 *
3528 * Caller can pass its name through <updater> to get it integrated in the response message
3529 * returned by the function.
3530 *
3531 * The function first does the following, in that order:
3532 * - validates the new addr and/or port
3533 * - checks if an update is required (new IP or port is different than current ones)
3534 * - checks the update is allowed:
3535 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3536 * - allow all changes if no CHECKS are configured
3537 * - if CHECK is configured:
3538 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3539 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3540 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003541 *
3542 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003543 */
3544const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3545{
3546 struct sockaddr_storage sa;
3547 int ret, port_change_required;
3548 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003549 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003550 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003551 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003552
3553 msg = get_trash_chunk();
3554 chunk_reset(msg);
3555
3556 if (addr) {
3557 memset(&sa, 0, sizeof(struct sockaddr_storage));
3558 if (str2ip2(addr, &sa, 0) == NULL) {
3559 chunk_printf(msg, "Invalid addr '%s'", addr);
3560 goto out;
3561 }
3562
3563 /* changes are allowed on AF_INET* families only */
3564 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3565 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3566 goto out;
3567 }
3568
3569 /* collecting data currently setup */
3570 memset(current_addr, '\0', sizeof(current_addr));
3571 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3572 /* changes are allowed on AF_INET* families only */
3573 if ((ret != AF_INET) && (ret != AF_INET6)) {
3574 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3575 goto out;
3576 }
3577
3578 /* applying ADDR changes if required and allowed
3579 * ipcmp returns 0 when both ADDR are the same
3580 */
3581 if (ipcmp(&s->addr, &sa) == 0) {
3582 chunk_appendf(msg, "no need to change the addr");
3583 goto port;
3584 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003585 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003586 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003587
3588 /* we also need to update check's ADDR only if it uses the server's one */
3589 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003590 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003591 }
3592
3593 /* we also need to update agent ADDR only if it use the server's one */
3594 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003595 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003596 }
3597
3598 /* update report for caller */
3599 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3600 }
3601
3602 port:
3603 if (port) {
3604 char sign = '\0';
3605 char *endptr;
3606
3607 if (addr)
3608 chunk_appendf(msg, ", ");
3609
3610 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003611 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003612
3613 /* check if PORT change is required */
3614 port_change_required = 0;
3615
3616 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003617 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003618 new_port = strtol(port, &endptr, 10);
3619 if ((errno != 0) || (port == endptr)) {
3620 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3621 goto out;
3622 }
3623
3624 /* check if caller triggers a port mapped or offset */
3625 if (sign == '-' || (sign == '+')) {
3626 /* check if server currently uses port map */
3627 if (!(s->flags & SRV_F_MAPPORTS)) {
3628 /* switch from fixed port to port map mandatorily triggers
3629 * a port change */
3630 port_change_required = 1;
3631 /* check is configured
3632 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3633 * prevent PORT change if check doesn't have it's dedicated port while switching
3634 * to port mapping */
3635 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3636 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.");
3637 goto out;
3638 }
3639 }
3640 /* we're already using port maps */
3641 else {
3642 port_change_required = current_port != new_port;
3643 }
3644 }
3645 /* fixed port */
3646 else {
3647 port_change_required = current_port != new_port;
3648 }
3649
3650 /* applying PORT changes if required and update response message */
3651 if (port_change_required) {
3652 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003653 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003654 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003655
3656 /* prepare message */
3657 chunk_appendf(msg, "port changed from '");
3658 if (s->flags & SRV_F_MAPPORTS)
3659 chunk_appendf(msg, "+");
3660 chunk_appendf(msg, "%d' to '", current_port);
3661
3662 if (sign == '-') {
3663 s->flags |= SRV_F_MAPPORTS;
3664 chunk_appendf(msg, "%c", sign);
3665 /* just use for result output */
3666 new_port = -new_port;
3667 }
3668 else if (sign == '+') {
3669 s->flags |= SRV_F_MAPPORTS;
3670 chunk_appendf(msg, "%c", sign);
3671 }
3672 else {
3673 s->flags &= ~SRV_F_MAPPORTS;
3674 }
3675
3676 chunk_appendf(msg, "%d'", new_port);
3677
3678 /* we also need to update health checks port only if it uses server's realport */
3679 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3680 s->check.port = new_port;
3681 }
3682 }
3683 else {
3684 chunk_appendf(msg, "no need to change the port");
3685 }
3686 }
3687
3688out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003689 if (changed)
3690 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003691 if (updater)
3692 chunk_appendf(msg, " by '%s'", updater);
3693 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003694 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003695}
3696
3697
3698/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003699 * update server status based on result of name resolution
3700 * returns:
3701 * 0 if server status is updated
3702 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003703 *
3704 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003705 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003706int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003707{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003708 struct dns_resolvers *resolvers = s->resolvers;
3709 struct dns_resolution *resolution = s->dns_requester->resolution;
3710 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003711
3712 switch (resolution->status) {
3713 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003714 /* status when HAProxy has just (re)started.
3715 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003716 break;
3717
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003718 case RSLV_STATUS_VALID:
3719 /*
3720 * resume health checks
3721 * server will be turned back on if health check is safe
3722 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003723 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003724 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003725 return 1;
3726 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3727 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003728 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003729 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003730
Emeric Brun52a91d32017-08-31 14:41:55 +02003731 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003732 return 1;
3733 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3734 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3735 s->proxy->id, s->id);
3736
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003737 ha_warning("%s.\n", trash.area);
3738 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003739 return 0;
3740
3741 case RSLV_STATUS_NX:
3742 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003743 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3744 if (!tick_is_expired(exp, now_ms))
3745 break;
3746
3747 if (s->next_admin & SRV_ADMF_RMAINT)
3748 return 1;
3749 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3750 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003751
3752 case RSLV_STATUS_TIMEOUT:
3753 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003754 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3755 if (!tick_is_expired(exp, now_ms))
3756 break;
3757
3758 if (s->next_admin & SRV_ADMF_RMAINT)
3759 return 1;
3760 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3761 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003762
3763 case RSLV_STATUS_REFUSED:
3764 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003765 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3766 if (!tick_is_expired(exp, now_ms))
3767 break;
3768
3769 if (s->next_admin & SRV_ADMF_RMAINT)
3770 return 1;
3771 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3772 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003773
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003774 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003775 /* stop server if resolution failed for a long enough period */
3776 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3777 if (!tick_is_expired(exp, now_ms))
3778 break;
3779
3780 if (s->next_admin & SRV_ADMF_RMAINT)
3781 return 1;
3782 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3783 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003784 }
3785
3786 return 1;
3787}
3788
3789/*
3790 * Server Name Resolution valid response callback
3791 * It expects:
3792 * - <nameserver>: the name server which answered the valid response
3793 * - <response>: buffer containing a valid DNS response
3794 * - <response_len>: size of <response>
3795 * It performs the following actions:
3796 * - ignore response if current ip found and server family not met
3797 * - update with first new ip found if family is met and current IP is not found
3798 * returns:
3799 * 0 on error
3800 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003801 *
3802 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003803 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003804int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003805{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003806 struct server *s = NULL;
3807 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003808 void *serverip, *firstip;
3809 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003810 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003811 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003812 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003813
Christopher Faulet67957bd2017-09-27 11:00:59 +02003814 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003815 if (!s)
3816 return 1;
3817
Christopher Faulet67957bd2017-09-27 11:00:59 +02003818 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003819
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003820 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003821 firstip = NULL; /* pointer to the first valid response found */
3822 /* it will be used as the new IP if a change is required */
3823 firstip_sin_family = AF_UNSPEC;
3824 serverip = NULL; /* current server IP address */
3825
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003826 /* initializing server IP pointer */
3827 server_sin_family = s->addr.ss_family;
3828 switch (server_sin_family) {
3829 case AF_INET:
3830 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3831 break;
3832
3833 case AF_INET6:
3834 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3835 break;
3836
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003837 case AF_UNSPEC:
3838 break;
3839
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003840 default:
3841 goto invalid;
3842 }
3843
Baptiste Assmann729c9012017-05-22 15:13:10 +02003844 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003845 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003846 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003847
3848 switch (ret) {
3849 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003850 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003851
3852 case DNS_UPD_SRVIP_NOT_FOUND:
3853 goto save_ip;
3854
3855 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003856 goto invalid;
3857
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003858 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003859 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003860 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003861
Baptiste Assmannfad03182015-10-28 02:03:32 +01003862 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003863 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003864 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003865 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003866
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003867 default:
3868 goto invalid;
3869
3870 }
3871
3872 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003873 if (nameserver) {
3874 nameserver->counters.update++;
3875 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003876 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003877 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003878 else
3879 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003880 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003881
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003882 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003883 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003884 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003885
3886 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003887 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003888 nameserver->counters.invalid++;
3889 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003890 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003891 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003892 return 0;
3893}
3894
3895/*
3896 * Server Name Resolution error management callback
3897 * returns:
3898 * 0 on error
3899 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003900 *
3901 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003902 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003903int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003904{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003905 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003906
Christopher Faulet67957bd2017-09-27 11:00:59 +02003907 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003908 if (!s)
3909 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003910 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003911 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003912 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003913 return 1;
3914}
3915
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003916/*
3917 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003918 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003919 * It returns a pointer to the first server found or NULL if <ip> is not yet
3920 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003921 *
3922 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003923 */
3924struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3925{
3926 struct server *tmpsrv;
3927 struct proxy *be;
3928
3929 if (!srv)
3930 return NULL;
3931
3932 be = srv->proxy;
3933 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003934 /* we found the current server is the same, ignore it */
3935 if (srv == tmpsrv)
3936 continue;
3937
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003938 /* We want to compare the IP in the record with the IP of the servers in the
3939 * same backend, only if:
3940 * * DNS resolution is enabled on the server
3941 * * the hostname used for the resolution by our server is the same than the
3942 * one used for the server found in the backend
3943 * * the server found in the backend is not our current server
3944 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003945 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003946 if ((tmpsrv->hostname_dn == NULL) ||
3947 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3948 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003949 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003950 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003951 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003952 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003953
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003954 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003955 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003956 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003957 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003958 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003959
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003960 /* At this point, we have 2 different servers using the same DNS hostname
3961 * for their respective resolution.
3962 */
3963 if (*ip_family == tmpsrv->addr.ss_family &&
3964 ((tmpsrv->addr.ss_family == AF_INET &&
3965 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3966 (tmpsrv->addr.ss_family == AF_INET6 &&
3967 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003968 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003969 return tmpsrv;
3970 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003971 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003972 }
3973
Emeric Brune9fd6b52017-11-02 17:20:39 +01003974
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003975 return NULL;
3976}
3977
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003978/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3979 * resolver. This is suited for initial address configuration. Returns 0 on
3980 * success otherwise a non-zero error code. In case of error, *err_code, if
3981 * not NULL, is filled up.
3982 */
3983int srv_set_addr_via_libc(struct server *srv, int *err_code)
3984{
3985 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003986 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003987 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003988 return 1;
3989 }
3990 return 0;
3991}
3992
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003993/* Set the server's FDQN (->hostname) from <hostname>.
3994 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003995 *
3996 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003997 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003998int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003999{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004000 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004001 char *hostname_dn;
4002 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004003
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004004 /* Note that the server lock is already held. */
4005 if (!srv->resolvers)
4006 return -1;
4007
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004008 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004009 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004010 /* run time DNS resolution was not active for this server
4011 * and we can't enable it at run time for now.
4012 */
4013 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004014 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004015
4016 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004017 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004018 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004019 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4020 hostname_dn, trash.size);
4021 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004022 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004023
Christopher Faulet67957bd2017-09-27 11:00:59 +02004024 resolution = srv->dns_requester->resolution;
4025 if (resolution &&
4026 resolution->hostname_dn &&
4027 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004028 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004029
Christopher Faulet67957bd2017-09-27 11:00:59 +02004030 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004031
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004032 free(srv->hostname);
4033 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004034 srv->hostname = strdup(hostname);
4035 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004036 srv->hostname_dn_len = hostname_dn_len;
4037 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004038 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004039
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004040 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004041 goto err;
4042
4043 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004044 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004045 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004046 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004047
4048 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004049 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004050 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004051 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004052}
4053
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004054/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4055 * from the state file. This is suited for initial address configuration.
4056 * Returns 0 on success otherwise a non-zero error code. In case of error,
4057 * *err_code, if not NULL, is filled up.
4058 */
4059static int srv_apply_lastaddr(struct server *srv, int *err_code)
4060{
4061 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4062 if (err_code)
4063 *err_code |= ERR_WARN;
4064 return 1;
4065 }
4066 return 0;
4067}
4068
Willy Tarreau25e51522016-11-04 15:10:17 +01004069/* returns 0 if no error, otherwise a combination of ERR_* flags */
4070static int srv_iterate_initaddr(struct server *srv)
4071{
4072 int return_code = 0;
4073 int err_code;
4074 unsigned int methods;
4075
4076 methods = srv->init_addr_methods;
4077 if (!methods) { // default to "last,libc"
4078 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4079 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4080 }
4081
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004082 /* "-dr" : always append "none" so that server addresses resolution
4083 * failures are silently ignored, this is convenient to validate some
4084 * configs out of their environment.
4085 */
4086 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4087 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4088
Willy Tarreau25e51522016-11-04 15:10:17 +01004089 while (methods) {
4090 err_code = 0;
4091 switch (srv_get_next_initaddr(&methods)) {
4092 case SRV_IADDR_LAST:
4093 if (!srv->lastaddr)
4094 continue;
4095 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004096 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004097 return_code |= err_code;
4098 break;
4099
4100 case SRV_IADDR_LIBC:
4101 if (!srv->hostname)
4102 continue;
4103 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004104 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004105 return_code |= err_code;
4106 break;
4107
Willy Tarreau37ebe122016-11-04 15:17:58 +01004108 case SRV_IADDR_NONE:
4109 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004110 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004111 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4112 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004113 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004114 return return_code;
4115
Willy Tarreau4310d362016-11-02 15:05:56 +01004116 case SRV_IADDR_IP:
4117 ipcpy(&srv->init_addr, &srv->addr);
4118 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004119 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4120 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004121 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004122 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004123
Willy Tarreau25e51522016-11-04 15:10:17 +01004124 default: /* unhandled method */
4125 break;
4126 }
4127 }
4128
4129 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004130 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004131 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4132 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004133 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004134 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004135 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4136 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004137
4138 return_code |= ERR_ALERT | ERR_FATAL;
4139 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004140out:
4141 srv_set_dyncookie(srv);
4142 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004143}
4144
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004145/*
4146 * This function parses all backends and all servers within each backend
4147 * and performs servers' addr resolution based on information provided by:
4148 * - configuration file
4149 * - server-state file (states provided by an 'old' haproxy process)
4150 *
4151 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4152 */
4153int srv_init_addr(void)
4154{
4155 struct proxy *curproxy;
4156 int return_code = 0;
4157
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004158 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004159 while (curproxy) {
4160 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004161
4162 /* servers are in backend only */
4163 if (!(curproxy->cap & PR_CAP_BE))
4164 goto srv_init_addr_next;
4165
Willy Tarreau25e51522016-11-04 15:10:17 +01004166 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004167 if (srv->hostname)
4168 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004169
4170 srv_init_addr_next:
4171 curproxy = curproxy->next;
4172 }
4173
4174 return return_code;
4175}
4176
Willy Tarreau46b7f532018-08-21 11:54:26 +02004177/*
4178 * Must be called with the server lock held.
4179 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004180const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004181{
4182
Willy Tarreau83061a82018-07-13 11:56:34 +02004183 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004184
4185 msg = get_trash_chunk();
4186 chunk_reset(msg);
4187
Olivier Houchard8da5f982017-08-04 18:35:36 +02004188 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004189 chunk_appendf(msg, "no need to change the FDQN");
4190 goto out;
4191 }
4192
4193 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4194 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4195 goto out;
4196 }
4197
4198 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4199 server->proxy->id, server->id, server->hostname, fqdn);
4200
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004201 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004202 chunk_reset(msg);
4203 chunk_appendf(msg, "could not update %s/%s FQDN",
4204 server->proxy->id, server->id);
4205 goto out;
4206 }
4207
4208 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004209 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004210
4211 out:
4212 if (updater)
4213 chunk_appendf(msg, " by '%s'", updater);
4214 chunk_appendf(msg, "\n");
4215
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004216 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004217}
4218
4219
Willy Tarreau21b069d2016-11-23 17:15:08 +01004220/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4221 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004222 * 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 +01004223 * used for CLI commands requiring a server name.
4224 * Important: the <arg> is modified to remove the '/'.
4225 */
4226struct server *cli_find_server(struct appctx *appctx, char *arg)
4227{
4228 struct proxy *px;
4229 struct server *sv;
4230 char *line;
4231
4232 /* split "backend/server" and make <line> point to server */
4233 for (line = arg; *line; line++)
4234 if (*line == '/') {
4235 *line++ = '\0';
4236 break;
4237 }
4238
4239 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004240 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004241 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004242 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004243 return NULL;
4244 }
4245
4246 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004247 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004248 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004249 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004250 return NULL;
4251 }
4252
4253 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004254 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004255 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004256 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004257 return NULL;
4258 }
4259
4260 return sv;
4261}
4262
William Lallemand222baf22016-11-19 02:00:33 +01004263
Willy Tarreau46b7f532018-08-21 11:54:26 +02004264/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004265static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004266{
4267 struct server *sv;
4268 const char *warning;
4269
4270 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4271 return 1;
4272
4273 sv = cli_find_server(appctx, args[2]);
4274 if (!sv)
4275 return 1;
4276
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004277 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004278
William Lallemand222baf22016-11-19 02:00:33 +01004279 if (strcmp(args[3], "weight") == 0) {
4280 warning = server_parse_weight_change_request(sv, args[4]);
4281 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004282 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004283 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004284 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004285 }
4286 }
4287 else if (strcmp(args[3], "state") == 0) {
4288 if (strcmp(args[4], "ready") == 0)
4289 srv_adm_set_ready(sv);
4290 else if (strcmp(args[4], "drain") == 0)
4291 srv_adm_set_drain(sv);
4292 else if (strcmp(args[4], "maint") == 0)
4293 srv_adm_set_maint(sv);
4294 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004295 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004296 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004297 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004298 }
4299 }
4300 else if (strcmp(args[3], "health") == 0) {
4301 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004302 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004303 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004304 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004305 }
4306 else if (strcmp(args[4], "up") == 0) {
4307 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004308 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004309 }
4310 else if (strcmp(args[4], "stopping") == 0) {
4311 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004312 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004313 }
4314 else if (strcmp(args[4], "down") == 0) {
4315 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004316 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004317 }
4318 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004319 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004320 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004321 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004322 }
4323 }
4324 else if (strcmp(args[3], "agent") == 0) {
4325 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004326 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004327 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004328 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004329 }
4330 else if (strcmp(args[4], "up") == 0) {
4331 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004332 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004333 }
4334 else if (strcmp(args[4], "down") == 0) {
4335 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004336 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004337 }
4338 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004339 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004340 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004341 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004342 }
4343 }
Misiek2da082d2017-01-09 09:40:42 +01004344 else if (strcmp(args[3], "agent-addr") == 0) {
4345 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004346 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004347 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4348 appctx->st0 = CLI_ST_PRINT;
4349 } else {
4350 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004351 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004352 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4353 appctx->st0 = CLI_ST_PRINT;
4354 }
4355 }
4356 }
4357 else if (strcmp(args[3], "agent-send") == 0) {
4358 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004359 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004360 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4361 appctx->st0 = CLI_ST_PRINT;
4362 } else {
4363 char *nss = strdup(args[4]);
4364 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004365 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004366 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4367 appctx->st0 = CLI_ST_PRINT;
4368 } else {
4369 free(sv->agent.send_string);
4370 sv->agent.send_string = nss;
4371 sv->agent.send_string_len = strlen(args[4]);
4372 }
4373 }
4374 }
William Lallemand222baf22016-11-19 02:00:33 +01004375 else if (strcmp(args[3], "check-port") == 0) {
4376 int i = 0;
4377 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004378 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004379 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004380 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004381 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004382 }
4383 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004384 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004385 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004386 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004387 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004388 }
4389 /* prevent the update of port to 0 if MAPPORTS are in use */
4390 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004391 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004392 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004393 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004394 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004395 }
4396 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004397 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004398 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004399 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004400 }
4401 else if (strcmp(args[3], "addr") == 0) {
4402 char *addr = NULL;
4403 char *port = NULL;
4404 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004405 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004406 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004407 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004408 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004409 }
4410 else {
4411 addr = args[4];
4412 }
4413 if (strcmp(args[5], "port") == 0) {
4414 port = args[6];
4415 }
4416 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4417 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004418 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004419 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004420 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004421 }
4422 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4423 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004424 else if (strcmp(args[3], "fqdn") == 0) {
4425 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004426 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004427 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4428 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004429 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004430 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004431 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004432 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004433 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004434 appctx->ctx.cli.msg = warning;
4435 appctx->st0 = CLI_ST_PRINT;
4436 }
4437 }
William Lallemand222baf22016-11-19 02:00:33 +01004438 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004439 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004440 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 +01004441 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004442 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004443 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004444 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004445 return 1;
4446}
4447
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004448static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004449{
4450 struct stream_interface *si = appctx->owner;
4451 struct proxy *px;
4452 struct server *sv;
4453 char *line;
4454
4455
4456 /* split "backend/server" and make <line> point to server */
4457 for (line = args[2]; *line; line++)
4458 if (*line == '/') {
4459 *line++ = '\0';
4460 break;
4461 }
4462
4463 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004464 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004465 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004466 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004467 return 1;
4468 }
4469
4470 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004471 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004472 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004473 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004474 return 1;
4475 }
4476
4477 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004478 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4479 sv->iweight);
4480 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004481 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004482 return 0;
4483 }
William Lallemand6b160942016-11-22 12:34:35 +01004484 return 1;
4485}
4486
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004487/* Parse a "set weight" command.
4488 *
4489 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004490 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004491static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004492{
4493 struct server *sv;
4494 const char *warning;
4495
4496 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4497 return 1;
4498
4499 sv = cli_find_server(appctx, args[2]);
4500 if (!sv)
4501 return 1;
4502
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004503 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4504
William Lallemand6b160942016-11-22 12:34:35 +01004505 warning = server_parse_weight_change_request(sv, args[3]);
4506 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004507 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004508 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004509 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004510 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004511
4512 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4513
William Lallemand6b160942016-11-22 12:34:35 +01004514 return 1;
4515}
4516
Willy Tarreau46b7f532018-08-21 11:54:26 +02004517/* parse a "set maxconn server" command. It always returns 1.
4518 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004519 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004520 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004521static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004522{
4523 struct server *sv;
4524 const char *warning;
4525
4526 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4527 return 1;
4528
4529 sv = cli_find_server(appctx, args[3]);
4530 if (!sv)
4531 return 1;
4532
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004533 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4534
Willy Tarreaub8026272016-11-23 11:26:56 +01004535 warning = server_parse_maxconn_change_request(sv, args[4]);
4536 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004537 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004538 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004539 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004540 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004541
4542 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4543
Willy Tarreaub8026272016-11-23 11:26:56 +01004544 return 1;
4545}
William Lallemand6b160942016-11-22 12:34:35 +01004546
Willy Tarreau46b7f532018-08-21 11:54:26 +02004547/* parse a "disable agent" command. It always returns 1.
4548 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004549 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004550 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004551static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004552{
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
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004562 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004563 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004564 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004565 return 1;
4566}
4567
Willy Tarreau46b7f532018-08-21 11:54:26 +02004568/* parse a "disable health" command. It always returns 1.
4569 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004570 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004571 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004572static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004573{
4574 struct server *sv;
4575
4576 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4577 return 1;
4578
4579 sv = cli_find_server(appctx, args[2]);
4580 if (!sv)
4581 return 1;
4582
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004583 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004584 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004585 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004586 return 1;
4587}
4588
Willy Tarreau46b7f532018-08-21 11:54:26 +02004589/* parse a "disable server" command. It always returns 1.
4590 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004591 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004592 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004593static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004594{
4595 struct server *sv;
4596
4597 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4598 return 1;
4599
4600 sv = cli_find_server(appctx, args[2]);
4601 if (!sv)
4602 return 1;
4603
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004604 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004605 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004606 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004607 return 1;
4608}
4609
Willy Tarreau46b7f532018-08-21 11:54:26 +02004610/* parse a "enable agent" command. It always returns 1.
4611 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004612 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004613 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004614static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004615{
4616 struct server *sv;
4617
4618 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4619 return 1;
4620
4621 sv = cli_find_server(appctx, args[2]);
4622 if (!sv)
4623 return 1;
4624
4625 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004626 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004627 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004628 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004629 return 1;
4630 }
4631
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004632 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004633 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004634 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004635 return 1;
4636}
4637
Willy Tarreau46b7f532018-08-21 11:54:26 +02004638/* parse a "enable health" command. It always returns 1.
4639 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004640 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004641 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004642static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004643{
4644 struct server *sv;
4645
4646 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4647 return 1;
4648
4649 sv = cli_find_server(appctx, args[2]);
4650 if (!sv)
4651 return 1;
4652
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004653 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004654 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004655 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004656 return 1;
4657}
4658
Willy Tarreau46b7f532018-08-21 11:54:26 +02004659/* parse a "enable server" command. It always returns 1.
4660 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004661 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004662 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004663static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004664{
4665 struct server *sv;
4666
4667 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4668 return 1;
4669
4670 sv = cli_find_server(appctx, args[2]);
4671 if (!sv)
4672 return 1;
4673
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004674 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004675 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004676 if (!(sv->flags & SRV_F_COOKIESET)
4677 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4678 sv->cookie)
4679 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004680 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004681 return 1;
4682}
4683
William Lallemand222baf22016-11-19 02:00:33 +01004684/* register cli keywords */
4685static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004686 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004687 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004688 { { "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 +01004689 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004690 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004691 { { "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 +01004692 { { "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 +01004693 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004694 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4695 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4696
William Lallemand222baf22016-11-19 02:00:33 +01004697 {{},}
4698}};
4699
Willy Tarreau0108d902018-11-25 19:14:37 +01004700INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004701
Emeric Brun64cc49c2017-10-03 14:46:45 +02004702/*
4703 * This function applies server's status changes, it is
4704 * is designed to be called asynchronously.
4705 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004706 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004707 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004708static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004709{
4710 struct check *check = &s->check;
4711 int xferred;
4712 struct proxy *px = s->proxy;
4713 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4714 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4715 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004716 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004717
Emeric Brun64cc49c2017-10-03 14:46:45 +02004718 /* If currently main is not set we try to apply pending state changes */
4719 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4720 int next_admin;
4721
4722 /* Backup next admin */
4723 next_admin = s->next_admin;
4724
4725 /* restore current admin state */
4726 s->next_admin = s->cur_admin;
4727
4728 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4729 s->last_change = now.tv_sec;
4730 if (s->proxy->lbprm.set_server_status_down)
4731 s->proxy->lbprm.set_server_status_down(s);
4732
4733 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4734 srv_shutdown_streams(s, SF_ERR_DOWN);
4735
4736 /* we might have streams queued on this server and waiting for
4737 * a connection. Those which are redispatchable will be queued
4738 * to another server or to the proxy itself.
4739 */
4740 xferred = pendconn_redistribute(s);
4741
4742 tmptrash = alloc_trash_chunk();
4743 if (tmptrash) {
4744 chunk_printf(tmptrash,
4745 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4746 s->proxy->id, s->id);
4747
Emeric Brun5a133512017-10-19 14:42:30 +02004748 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004749 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004750
4751 /* we don't send an alert if the server was previously paused */
4752 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004753 send_log(s->proxy, log_level, "%s.\n",
4754 tmptrash->area);
4755 send_email_alert(s, log_level, "%s",
4756 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004757 free_trash_chunk(tmptrash);
4758 tmptrash = NULL;
4759 }
4760 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4761 set_backend_down(s->proxy);
4762
4763 s->counters.down_trans++;
4764 }
4765 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4766 s->last_change = now.tv_sec;
4767 if (s->proxy->lbprm.set_server_status_down)
4768 s->proxy->lbprm.set_server_status_down(s);
4769
4770 /* we might have streams queued on this server and waiting for
4771 * a connection. Those which are redispatchable will be queued
4772 * to another server or to the proxy itself.
4773 */
4774 xferred = pendconn_redistribute(s);
4775
4776 tmptrash = alloc_trash_chunk();
4777 if (tmptrash) {
4778 chunk_printf(tmptrash,
4779 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4780 s->proxy->id, s->id);
4781
Emeric Brun5a133512017-10-19 14:42:30 +02004782 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004783
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004784 ha_warning("%s.\n", tmptrash->area);
4785 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4786 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004787 free_trash_chunk(tmptrash);
4788 tmptrash = NULL;
4789 }
4790
4791 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4792 set_backend_down(s->proxy);
4793 }
4794 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4795 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4796 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4797 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4798 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4799 s->proxy->last_change = now.tv_sec;
4800 }
4801
4802 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4803 s->down_time += now.tv_sec - s->last_change;
4804
4805 s->last_change = now.tv_sec;
4806 if (s->next_state == SRV_ST_STARTING)
4807 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4808
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004809 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004810 /* now propagate the status change to any LB algorithms */
4811 if (px->lbprm.update_server_eweight)
4812 px->lbprm.update_server_eweight(s);
4813 else if (srv_willbe_usable(s)) {
4814 if (px->lbprm.set_server_status_up)
4815 px->lbprm.set_server_status_up(s);
4816 }
4817 else {
4818 if (px->lbprm.set_server_status_down)
4819 px->lbprm.set_server_status_down(s);
4820 }
4821
4822 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4823 * and it's not a backup server and its effective weight is > 0,
4824 * then it can accept new connections, so we shut down all streams
4825 * on all backup servers.
4826 */
4827 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4828 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4829 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4830
4831 /* check if we can handle some connections queued at the proxy. We
4832 * will take as many as we can handle.
4833 */
4834 xferred = pendconn_grab_from_px(s);
4835
4836 tmptrash = alloc_trash_chunk();
4837 if (tmptrash) {
4838 chunk_printf(tmptrash,
4839 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4840 s->proxy->id, s->id);
4841
Emeric Brun5a133512017-10-19 14:42:30 +02004842 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004843 ha_warning("%s.\n", tmptrash->area);
4844 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4845 tmptrash->area);
4846 send_email_alert(s, LOG_NOTICE, "%s",
4847 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004848 free_trash_chunk(tmptrash);
4849 tmptrash = NULL;
4850 }
4851
4852 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4853 set_backend_down(s->proxy);
4854 }
4855 else if (s->cur_eweight != s->next_eweight) {
4856 /* now propagate the status change to any LB algorithms */
4857 if (px->lbprm.update_server_eweight)
4858 px->lbprm.update_server_eweight(s);
4859 else if (srv_willbe_usable(s)) {
4860 if (px->lbprm.set_server_status_up)
4861 px->lbprm.set_server_status_up(s);
4862 }
4863 else {
4864 if (px->lbprm.set_server_status_down)
4865 px->lbprm.set_server_status_down(s);
4866 }
4867
4868 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4869 set_backend_down(s->proxy);
4870 }
4871
4872 s->next_admin = next_admin;
4873 }
4874
Emeric Brun5a133512017-10-19 14:42:30 +02004875 /* reset operational state change */
4876 *s->op_st_chg.reason = 0;
4877 s->op_st_chg.status = s->op_st_chg.code = -1;
4878 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004879
4880 /* Now we try to apply pending admin changes */
4881
4882 /* Maintenance must also disable health checks */
4883 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4884 if (s->check.state & CHK_ST_ENABLED) {
4885 s->check.state |= CHK_ST_PAUSED;
4886 check->health = 0;
4887 }
4888
4889 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004890 tmptrash = alloc_trash_chunk();
4891 if (tmptrash) {
4892 chunk_printf(tmptrash,
4893 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4894 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4895 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004896
Olivier Houchard796a2b32017-10-24 17:42:47 +02004897 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004898
Olivier Houchard796a2b32017-10-24 17:42:47 +02004899 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004900 ha_warning("%s.\n", tmptrash->area);
4901 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4902 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004903 }
4904 free_trash_chunk(tmptrash);
4905 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004906 }
Emeric Brun8f298292017-12-06 16:47:17 +01004907 /* commit new admin status */
4908
4909 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004910 }
4911 else { /* server was still running */
4912 check->health = 0; /* failure */
4913 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004914
4915 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004916 if (s->proxy->lbprm.set_server_status_down)
4917 s->proxy->lbprm.set_server_status_down(s);
4918
Emeric Brun64cc49c2017-10-03 14:46:45 +02004919 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4920 srv_shutdown_streams(s, SF_ERR_DOWN);
4921
4922 /* we might have streams queued on this server and waiting for
4923 * a connection. Those which are redispatchable will be queued
4924 * to another server or to the proxy itself.
4925 */
4926 xferred = pendconn_redistribute(s);
4927
4928 tmptrash = alloc_trash_chunk();
4929 if (tmptrash) {
4930 chunk_printf(tmptrash,
4931 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4932 s->flags & SRV_F_BACKUP ? "Backup " : "",
4933 s->proxy->id, s->id,
4934 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4935
4936 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4937
4938 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004939 ha_warning("%s.\n", tmptrash->area);
4940 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4941 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004942 }
4943 free_trash_chunk(tmptrash);
4944 tmptrash = NULL;
4945 }
4946 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4947 set_backend_down(s->proxy);
4948
4949 s->counters.down_trans++;
4950 }
4951 }
4952 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4953 /* OK here we're leaving maintenance, we have many things to check,
4954 * because the server might possibly be coming back up depending on
4955 * its state. In practice, leaving maintenance means that we should
4956 * immediately turn to UP (more or less the slowstart) under the
4957 * following conditions :
4958 * - server is neither checked nor tracked
4959 * - server tracks another server which is not checked
4960 * - server tracks another server which is already up
4961 * Which sums up as something simpler :
4962 * "either the tracking server is up or the server's checks are disabled
4963 * or up". Otherwise we only re-enable health checks. There's a special
4964 * case associated to the stopping state which can be inherited. Note
4965 * that the server might still be in drain mode, which is naturally dealt
4966 * with by the lower level functions.
4967 */
4968
4969 if (s->check.state & CHK_ST_ENABLED) {
4970 s->check.state &= ~CHK_ST_PAUSED;
4971 check->health = check->rise; /* start OK but check immediately */
4972 }
4973
4974 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4975 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4976 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4977 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4978 s->next_state = SRV_ST_STOPPING;
4979 }
4980 else {
4981 s->next_state = SRV_ST_STARTING;
4982 if (s->slowstart > 0)
4983 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4984 else
4985 s->next_state = SRV_ST_RUNNING;
4986 }
4987
4988 }
4989
4990 tmptrash = alloc_trash_chunk();
4991 if (tmptrash) {
4992 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4993 chunk_printf(tmptrash,
4994 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4995 s->flags & SRV_F_BACKUP ? "Backup " : "",
4996 s->proxy->id, s->id,
4997 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4998 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4999 }
5000 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5001 chunk_printf(tmptrash,
5002 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5003 s->flags & SRV_F_BACKUP ? "Backup " : "",
5004 s->proxy->id, s->id, s->hostname,
5005 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5006 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5007 }
5008 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5009 chunk_printf(tmptrash,
5010 "%sServer %s/%s is %s/%s (leaving maintenance)",
5011 s->flags & SRV_F_BACKUP ? "Backup " : "",
5012 s->proxy->id, s->id,
5013 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5014 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5015 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005016 ha_warning("%s.\n", tmptrash->area);
5017 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5018 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005019 free_trash_chunk(tmptrash);
5020 tmptrash = NULL;
5021 }
5022
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005023 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005024 /* now propagate the status change to any LB algorithms */
5025 if (px->lbprm.update_server_eweight)
5026 px->lbprm.update_server_eweight(s);
5027 else if (srv_willbe_usable(s)) {
5028 if (px->lbprm.set_server_status_up)
5029 px->lbprm.set_server_status_up(s);
5030 }
5031 else {
5032 if (px->lbprm.set_server_status_down)
5033 px->lbprm.set_server_status_down(s);
5034 }
5035
5036 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5037 set_backend_down(s->proxy);
5038
Willy Tarreau6a78e612018-08-07 10:14:53 +02005039 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5040 * and it's not a backup server and its effective weight is > 0,
5041 * then it can accept new connections, so we shut down all streams
5042 * on all backup servers.
5043 */
5044 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5045 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5046 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5047
5048 /* check if we can handle some connections queued at the proxy. We
5049 * will take as many as we can handle.
5050 */
5051 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005052 }
5053 else if (s->next_admin & SRV_ADMF_MAINT) {
5054 /* remaining in maintenance mode, let's inform precisely about the
5055 * situation.
5056 */
5057 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5058 tmptrash = alloc_trash_chunk();
5059 if (tmptrash) {
5060 chunk_printf(tmptrash,
5061 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5062 s->flags & SRV_F_BACKUP ? "Backup " : "",
5063 s->proxy->id, s->id);
5064
5065 if (s->track) /* normally it's mandatory here */
5066 chunk_appendf(tmptrash, " via %s/%s",
5067 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005068 ha_warning("%s.\n", tmptrash->area);
5069 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5070 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005071 free_trash_chunk(tmptrash);
5072 tmptrash = NULL;
5073 }
5074 }
5075 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5076 tmptrash = alloc_trash_chunk();
5077 if (tmptrash) {
5078 chunk_printf(tmptrash,
5079 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5080 s->flags & SRV_F_BACKUP ? "Backup " : "",
5081 s->proxy->id, s->id, s->hostname);
5082
5083 if (s->track) /* normally it's mandatory here */
5084 chunk_appendf(tmptrash, " via %s/%s",
5085 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005086 ha_warning("%s.\n", tmptrash->area);
5087 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5088 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005089 free_trash_chunk(tmptrash);
5090 tmptrash = NULL;
5091 }
5092 }
5093 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5094 tmptrash = alloc_trash_chunk();
5095 if (tmptrash) {
5096 chunk_printf(tmptrash,
5097 "%sServer %s/%s remains in forced maintenance",
5098 s->flags & SRV_F_BACKUP ? "Backup " : "",
5099 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005100 ha_warning("%s.\n", tmptrash->area);
5101 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5102 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005103 free_trash_chunk(tmptrash);
5104 tmptrash = NULL;
5105 }
5106 }
5107 /* don't report anything when leaving drain mode and remaining in maintenance */
5108
5109 s->cur_admin = s->next_admin;
5110 }
5111
5112 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5113 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5114 /* drain state is applied only if not yet in maint */
5115
5116 s->last_change = now.tv_sec;
5117 if (px->lbprm.set_server_status_down)
5118 px->lbprm.set_server_status_down(s);
5119
5120 /* we might have streams queued on this server and waiting for
5121 * a connection. Those which are redispatchable will be queued
5122 * to another server or to the proxy itself.
5123 */
5124 xferred = pendconn_redistribute(s);
5125
5126 tmptrash = alloc_trash_chunk();
5127 if (tmptrash) {
5128 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5129 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5130 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5131
5132 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5133
5134 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005135 ha_warning("%s.\n", tmptrash->area);
5136 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5137 tmptrash->area);
5138 send_email_alert(s, LOG_NOTICE, "%s",
5139 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005140 }
5141 free_trash_chunk(tmptrash);
5142 tmptrash = NULL;
5143 }
5144
5145 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5146 set_backend_down(s->proxy);
5147 }
5148 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5149 /* OK completely leaving drain mode */
5150 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5151 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5152 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5153 s->proxy->last_change = now.tv_sec;
5154 }
5155
5156 if (s->last_change < now.tv_sec) // ignore negative times
5157 s->down_time += now.tv_sec - s->last_change;
5158 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005159 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005160
5161 tmptrash = alloc_trash_chunk();
5162 if (tmptrash) {
5163 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5164 chunk_printf(tmptrash,
5165 "%sServer %s/%s is %s (leaving forced drain)",
5166 s->flags & SRV_F_BACKUP ? "Backup " : "",
5167 s->proxy->id, s->id,
5168 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5169 }
5170 else {
5171 chunk_printf(tmptrash,
5172 "%sServer %s/%s is %s (leaving drain)",
5173 s->flags & SRV_F_BACKUP ? "Backup " : "",
5174 s->proxy->id, s->id,
5175 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5176 if (s->track) /* normally it's mandatory here */
5177 chunk_appendf(tmptrash, " via %s/%s",
5178 s->track->proxy->id, s->track->id);
5179 }
5180
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005181 ha_warning("%s.\n", tmptrash->area);
5182 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5183 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005184 free_trash_chunk(tmptrash);
5185 tmptrash = NULL;
5186 }
5187
5188 /* now propagate the status change to any LB algorithms */
5189 if (px->lbprm.update_server_eweight)
5190 px->lbprm.update_server_eweight(s);
5191 else if (srv_willbe_usable(s)) {
5192 if (px->lbprm.set_server_status_up)
5193 px->lbprm.set_server_status_up(s);
5194 }
5195 else {
5196 if (px->lbprm.set_server_status_down)
5197 px->lbprm.set_server_status_down(s);
5198 }
5199 }
5200 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5201 /* remaining in drain mode after removing one of its flags */
5202
5203 tmptrash = alloc_trash_chunk();
5204 if (tmptrash) {
5205 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5206 chunk_printf(tmptrash,
5207 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5208 s->flags & SRV_F_BACKUP ? "Backup " : "",
5209 s->proxy->id, s->id);
5210
5211 if (s->track) /* normally it's mandatory here */
5212 chunk_appendf(tmptrash, " via %s/%s",
5213 s->track->proxy->id, s->track->id);
5214 }
5215 else {
5216 chunk_printf(tmptrash,
5217 "%sServer %s/%s remains in forced drain mode",
5218 s->flags & SRV_F_BACKUP ? "Backup " : "",
5219 s->proxy->id, s->id);
5220 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005221 ha_warning("%s.\n", tmptrash->area);
5222 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5223 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005224 free_trash_chunk(tmptrash);
5225 tmptrash = NULL;
5226 }
5227
5228 /* commit new admin status */
5229
5230 s->cur_admin = s->next_admin;
5231 }
5232 }
5233
5234 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005235 *s->adm_st_chg_cause = 0;
5236}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005237
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005238/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005239 * Local variables:
5240 * c-indent-level: 8
5241 * c-basic-offset: 8
5242 * End:
5243 */