blob: bae9a3b6694f64b3c44d71fece4bb1bc2244fc3b [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);
Olivier Houchard0c18a6f2018-12-02 14:11:41 +010053static struct task *cleanup_idle_connections(struct task *task, void *ctx, unsigned short state);
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
Willy Tarreau21faa912012-10-10 08:27:36 +020055/* List head of all known server keywords */
56static struct srv_kw_list srv_keywords = {
57 .list = LIST_HEAD_INIT(srv_keywords.list)
58};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020059
Simon Hormana3608442013-11-01 16:46:15 +090060int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020061{
Emeric Brun52a91d32017-08-31 14:41:55 +020062 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020063 return s->down_time;
64
65 return now.tv_sec - s->last_change + s->down_time;
66}
Willy Tarreaubaaee002006-06-26 02:48:02 +020067
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050068int srv_lastsession(const struct server *s)
69{
70 if (s->counters.last_sess)
71 return now.tv_sec - s->counters.last_sess;
72
73 return -1;
74}
75
Simon Horman4a741432013-02-23 15:35:38 +090076int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020077{
Simon Horman4a741432013-02-23 15:35:38 +090078 const struct server *s = check->server;
79
Willy Tarreauff5ae352013-12-11 20:36:34 +010080 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090081 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010082
Emeric Brun52a91d32017-08-31 14:41:55 +020083 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090084 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010085
Simon Horman4a741432013-02-23 15:35:38 +090086 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010087}
88
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010089/*
90 * Check that we did not get a hash collision.
91 * Unlikely, but it can happen.
92 */
93static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +010094{
95 struct proxy *p = s->proxy;
96 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010097
98 for (tmpserv = p->srv; tmpserv != NULL;
99 tmpserv = tmpserv->next) {
100 if (tmpserv == s)
101 continue;
102 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
103 continue;
104 if (tmpserv->cookie &&
105 strcmp(tmpserv->cookie, s->cookie) == 0) {
106 ha_warning("We generated two equal cookies for two different servers.\n"
107 "Please change the secret key for '%s'.\n",
108 s->proxy->id);
109 }
110 }
111
112}
113
Willy Tarreau46b7f532018-08-21 11:54:26 +0200114/*
115 * Must be called with the server lock held.
116 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100117void srv_set_dyncookie(struct server *s)
118{
119 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100120 char *tmpbuf;
121 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100122 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100123 size_t buffer_len;
124 int addr_len;
125 int port;
126
127 if ((s->flags & SRV_F_COOKIESET) ||
128 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
129 s->proxy->dyncookie_key == NULL)
130 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100131 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100132
133 if (s->addr.ss_family != AF_INET &&
134 s->addr.ss_family != AF_INET6)
135 return;
136 /*
137 * Buffer to calculate the cookie value.
138 * The buffer contains the secret key + the server IP address
139 * + the TCP port.
140 */
141 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
142 /*
143 * The TCP port should use only 2 bytes, but is stored in
144 * an unsigned int in struct server, so let's use 4, to be
145 * on the safe side.
146 */
147 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200148 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100149 memcpy(tmpbuf, p->dyncookie_key, key_len);
150 memcpy(&(tmpbuf[key_len]),
151 s->addr.ss_family == AF_INET ?
152 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
153 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
154 addr_len);
155 /*
156 * Make sure it's the same across all the load balancers,
157 * no matter their endianness.
158 */
159 port = htonl(s->svc_port);
160 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
161 hash_value = XXH64(tmpbuf, buffer_len, 0);
162 memprintf(&s->cookie, "%016llx", hash_value);
163 if (!s->cookie)
164 return;
165 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100166
167 /* Don't bother checking if the dyncookie is duplicated if
168 * the server is marked as "disabled", maybe it doesn't have
169 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100170 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100171 if (!(s->next_admin & SRV_ADMF_FMAINT))
172 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100173}
174
Willy Tarreau21faa912012-10-10 08:27:36 +0200175/*
176 * Registers the server keyword list <kwl> as a list of valid keywords for next
177 * parsing sessions.
178 */
179void srv_register_keywords(struct srv_kw_list *kwl)
180{
181 LIST_ADDQ(&srv_keywords.list, &kwl->list);
182}
183
184/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
185 * keyword is found with a NULL ->parse() function, then an attempt is made to
186 * find one with a valid ->parse() function. This way it is possible to declare
187 * platform-dependant, known keywords as NULL, then only declare them as valid
188 * if some options are met. Note that if the requested keyword contains an
189 * opening parenthesis, everything from this point is ignored.
190 */
191struct srv_kw *srv_find_kw(const char *kw)
192{
193 int index;
194 const char *kwend;
195 struct srv_kw_list *kwl;
196 struct srv_kw *ret = NULL;
197
198 kwend = strchr(kw, '(');
199 if (!kwend)
200 kwend = kw + strlen(kw);
201
202 list_for_each_entry(kwl, &srv_keywords.list, list) {
203 for (index = 0; kwl->kw[index].kw != NULL; index++) {
204 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
205 kwl->kw[index].kw[kwend-kw] == 0) {
206 if (kwl->kw[index].parse)
207 return &kwl->kw[index]; /* found it !*/
208 else
209 ret = &kwl->kw[index]; /* may be OK */
210 }
211 }
212 }
213 return ret;
214}
215
216/* Dumps all registered "server" keywords to the <out> string pointer. The
217 * unsupported keywords are only dumped if their supported form was not
218 * found.
219 */
220void srv_dump_kws(char **out)
221{
222 struct srv_kw_list *kwl;
223 int index;
224
225 *out = NULL;
226 list_for_each_entry(kwl, &srv_keywords.list, list) {
227 for (index = 0; kwl->kw[index].kw != NULL; index++) {
228 if (kwl->kw[index].parse ||
229 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
230 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
231 kwl->scope,
232 kwl->kw[index].kw,
233 kwl->kw[index].skip ? " <arg>" : "",
234 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
235 kwl->kw[index].parse ? "" : " (not supported)");
236 }
237 }
238 }
239}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100240
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100241/* Parse the "addr" server keyword */
242static int srv_parse_addr(char **args, int *cur_arg,
243 struct proxy *curproxy, struct server *newsrv, char **err)
244{
245 char *errmsg, *arg;
246 struct sockaddr_storage *sk;
247 int port1, port2;
248 struct protocol *proto;
249
250 errmsg = NULL;
251 arg = args[*cur_arg + 1];
252
253 if (!*arg) {
254 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
255 goto err;
256 }
257
258 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
259 if (!sk) {
260 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
261 goto err;
262 }
263
264 proto = protocol_by_family(sk->ss_family);
265 if (!proto || !proto->connect) {
266 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
267 args[*cur_arg], arg);
268 goto err;
269 }
270
271 if (port1 != port2) {
272 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
273 args[*cur_arg], arg);
274 goto err;
275 }
276
277 newsrv->check.addr = newsrv->agent.addr = *sk;
278 newsrv->flags |= SRV_F_CHECKADDR;
279 newsrv->flags |= SRV_F_AGENTADDR;
280
281 return 0;
282
283 err:
284 free(errmsg);
285 return ERR_ALERT | ERR_FATAL;
286}
287
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100288/* Parse the "agent-check" server keyword */
289static int srv_parse_agent_check(char **args, int *cur_arg,
290 struct proxy *curproxy, struct server *newsrv, char **err)
291{
292 newsrv->do_agent = 1;
293 return 0;
294}
295
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100296/* Parse the "backup" server keyword */
297static int srv_parse_backup(char **args, int *cur_arg,
298 struct proxy *curproxy, struct server *newsrv, char **err)
299{
300 newsrv->flags |= SRV_F_BACKUP;
301 return 0;
302}
303
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100304/* Parse the "check" server keyword */
305static int srv_parse_check(char **args, int *cur_arg,
306 struct proxy *curproxy, struct server *newsrv, char **err)
307{
308 newsrv->do_check = 1;
309 return 0;
310}
311
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100312/* Parse the "check-send-proxy" server keyword */
313static int srv_parse_check_send_proxy(char **args, int *cur_arg,
314 struct proxy *curproxy, struct server *newsrv, char **err)
315{
316 newsrv->check.send_proxy = 1;
317 return 0;
318}
319
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100320/* Parse the "cookie" server keyword */
321static int srv_parse_cookie(char **args, int *cur_arg,
322 struct proxy *curproxy, struct server *newsrv, char **err)
323{
324 char *arg;
325
326 arg = args[*cur_arg + 1];
327 if (!*arg) {
328 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
329 return ERR_ALERT | ERR_FATAL;
330 }
331
332 free(newsrv->cookie);
333 newsrv->cookie = strdup(arg);
334 newsrv->cklen = strlen(arg);
335 newsrv->flags |= SRV_F_COOKIESET;
336 return 0;
337}
338
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100339/* Parse the "disabled" server keyword */
340static int srv_parse_disabled(char **args, int *cur_arg,
341 struct proxy *curproxy, struct server *newsrv, char **err)
342{
Emeric Brun52a91d32017-08-31 14:41:55 +0200343 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
344 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100345 newsrv->check.state |= CHK_ST_PAUSED;
346 newsrv->check.health = 0;
347 return 0;
348}
349
350/* Parse the "enabled" server keyword */
351static int srv_parse_enabled(char **args, int *cur_arg,
352 struct proxy *curproxy, struct server *newsrv, char **err)
353{
Emeric Brun52a91d32017-08-31 14:41:55 +0200354 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
355 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100356 newsrv->check.state &= ~CHK_ST_PAUSED;
357 newsrv->check.health = newsrv->check.rise;
358 return 0;
359}
360
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100361static int srv_parse_idle_timeout(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
362{
363 const char *res;
364 char *arg;
365 unsigned int time;
366
367 arg = args[*cur_arg + 1];
368 if (!*arg) {
369 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
370 return ERR_ALERT | ERR_FATAL;
371 }
372 res = parse_time_err(arg, &time, TIME_UNIT_MS);
373 if (res) {
374 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
375 *res, args[*cur_arg]);
376 return ERR_ALERT | ERR_FATAL;
377 }
378 newsrv->idle_timeout = time;
379
380 return 0;
381}
382
Willy Tarreaudff55432012-10-10 17:51:05 +0200383/* parse the "id" server keyword */
384static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
385{
386 struct eb32_node *node;
387
388 if (!*args[*cur_arg + 1]) {
389 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
390 return ERR_ALERT | ERR_FATAL;
391 }
392
393 newsrv->puid = atol(args[*cur_arg + 1]);
394 newsrv->conf.id.key = newsrv->puid;
395
396 if (newsrv->puid <= 0) {
397 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
398 return ERR_ALERT | ERR_FATAL;
399 }
400
401 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
402 if (node) {
403 struct server *target = container_of(node, struct server, conf.id);
404 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
405 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
406 target->id);
407 return ERR_ALERT | ERR_FATAL;
408 }
409
410 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200411 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200412 return 0;
413}
414
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100415/* Parse the "namespace" server keyword */
416static int srv_parse_namespace(char **args, int *cur_arg,
417 struct proxy *curproxy, struct server *newsrv, char **err)
418{
419#ifdef CONFIG_HAP_NS
420 char *arg;
421
422 arg = args[*cur_arg + 1];
423 if (!*arg) {
424 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
425 return ERR_ALERT | ERR_FATAL;
426 }
427
428 if (!strcmp(arg, "*")) {
429 /* Use the namespace associated with the connection (if present). */
430 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
431 return 0;
432 }
433
434 /*
435 * As this parser may be called several times for the same 'default-server'
436 * object, or for a new 'server' instance deriving from a 'default-server'
437 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
438 */
439 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
440
441 newsrv->netns = netns_store_lookup(arg, strlen(arg));
442 if (!newsrv->netns)
443 newsrv->netns = netns_store_insert(arg);
444
445 if (!newsrv->netns) {
446 memprintf(err, "Cannot open namespace '%s'", arg);
447 return ERR_ALERT | ERR_FATAL;
448 }
449
450 return 0;
451#else
452 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
453 return ERR_ALERT | ERR_FATAL;
454#endif
455}
456
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100457/* Parse the "no-agent-check" server keyword */
458static int srv_parse_no_agent_check(char **args, int *cur_arg,
459 struct proxy *curproxy, struct server *newsrv, char **err)
460{
461 free_check(&newsrv->agent);
462 newsrv->agent.inter = 0;
463 newsrv->agent.port = 0;
464 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
465 newsrv->do_agent = 0;
466 return 0;
467}
468
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100469/* Parse the "no-backup" server keyword */
470static int srv_parse_no_backup(char **args, int *cur_arg,
471 struct proxy *curproxy, struct server *newsrv, char **err)
472{
473 newsrv->flags &= ~SRV_F_BACKUP;
474 return 0;
475}
476
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100477/* Parse the "no-check" server keyword */
478static int srv_parse_no_check(char **args, int *cur_arg,
479 struct proxy *curproxy, struct server *newsrv, char **err)
480{
481 free_check(&newsrv->check);
482 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
483 newsrv->do_check = 0;
484 return 0;
485}
486
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100487/* Parse the "no-check-send-proxy" server keyword */
488static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
489 struct proxy *curproxy, struct server *newsrv, char **err)
490{
491 newsrv->check.send_proxy = 0;
492 return 0;
493}
494
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100495/* Disable server PROXY protocol flags. */
496static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
497{
498 srv->pp_opts &= ~flags;
499 return 0;
500}
501
502/* Parse the "no-send-proxy" server keyword */
503static int srv_parse_no_send_proxy(char **args, int *cur_arg,
504 struct proxy *curproxy, struct server *newsrv, char **err)
505{
506 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
507}
508
509/* Parse the "no-send-proxy-v2" server keyword */
510static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
511 struct proxy *curproxy, struct server *newsrv, char **err)
512{
513 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
514}
515
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100516/* Parse the "non-stick" server keyword */
517static int srv_parse_non_stick(char **args, int *cur_arg,
518 struct proxy *curproxy, struct server *newsrv, char **err)
519{
520 newsrv->flags |= SRV_F_NON_STICK;
521 return 0;
522}
523
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100524/* Enable server PROXY protocol flags. */
525static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
526{
527 srv->pp_opts |= flags;
528 return 0;
529}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200530/* parse the "proto" server keyword */
531static int srv_parse_proto(char **args, int *cur_arg,
532 struct proxy *px, struct server *newsrv, char **err)
533{
534 struct ist proto;
535
536 if (!*args[*cur_arg + 1]) {
537 memprintf(err, "'%s' : missing value", args[*cur_arg]);
538 return ERR_ALERT | ERR_FATAL;
539 }
540 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
541 newsrv->mux_proto = get_mux_proto(proto);
542 if (!newsrv->mux_proto) {
543 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
544 return ERR_ALERT | ERR_FATAL;
545 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200546 return 0;
547}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100548
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100549/* parse the "proxy-v2-options" */
550static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
551 struct proxy *px, struct server *newsrv, char **err)
552{
553 char *p, *n;
554 for (p = args[*cur_arg+1]; p; p = n) {
555 n = strchr(p, ',');
556 if (n)
557 *n++ = '\0';
558 if (!strcmp(p, "ssl")) {
559 newsrv->pp_opts |= SRV_PP_V2_SSL;
560 } else if (!strcmp(p, "cert-cn")) {
561 newsrv->pp_opts |= SRV_PP_V2_SSL;
562 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100563 } else if (!strcmp(p, "cert-key")) {
564 newsrv->pp_opts |= SRV_PP_V2_SSL;
565 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
566 } else if (!strcmp(p, "cert-sig")) {
567 newsrv->pp_opts |= SRV_PP_V2_SSL;
568 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
569 } else if (!strcmp(p, "ssl-cipher")) {
570 newsrv->pp_opts |= SRV_PP_V2_SSL;
571 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100572 } else if (!strcmp(p, "authority")) {
573 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100574 } else if (!strcmp(p, "crc32c")) {
575 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100576 } else
577 goto fail;
578 }
579 return 0;
580 fail:
581 if (err)
582 memprintf(err, "'%s' : proxy v2 option not implemented", p);
583 return ERR_ALERT | ERR_FATAL;
584}
585
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100586/* Parse the "observe" server keyword */
587static int srv_parse_observe(char **args, int *cur_arg,
588 struct proxy *curproxy, struct server *newsrv, char **err)
589{
590 char *arg;
591
592 arg = args[*cur_arg + 1];
593 if (!*arg) {
594 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
595 return ERR_ALERT | ERR_FATAL;
596 }
597
598 if (!strcmp(arg, "none")) {
599 newsrv->observe = HANA_OBS_NONE;
600 }
601 else if (!strcmp(arg, "layer4")) {
602 newsrv->observe = HANA_OBS_LAYER4;
603 }
604 else if (!strcmp(arg, "layer7")) {
605 if (curproxy->mode != PR_MODE_HTTP) {
606 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
607 return ERR_ALERT;
608 }
609 newsrv->observe = HANA_OBS_LAYER7;
610 }
611 else {
612 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
613 "but got '%s'\n", args[*cur_arg], arg);
614 return ERR_ALERT | ERR_FATAL;
615 }
616
617 return 0;
618}
619
Frédéric Lécaille16186232017-03-14 16:42:49 +0100620/* Parse the "redir" server keyword */
621static int srv_parse_redir(char **args, int *cur_arg,
622 struct proxy *curproxy, struct server *newsrv, char **err)
623{
624 char *arg;
625
626 arg = args[*cur_arg + 1];
627 if (!*arg) {
628 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
629 return ERR_ALERT | ERR_FATAL;
630 }
631
632 free(newsrv->rdr_pfx);
633 newsrv->rdr_pfx = strdup(arg);
634 newsrv->rdr_len = strlen(arg);
635
636 return 0;
637}
638
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100639/* Parse the "send-proxy" server keyword */
640static int srv_parse_send_proxy(char **args, int *cur_arg,
641 struct proxy *curproxy, struct server *newsrv, char **err)
642{
643 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
644}
645
646/* Parse the "send-proxy-v2" server keyword */
647static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
648 struct proxy *curproxy, struct server *newsrv, char **err)
649{
650 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
651}
652
Frédéric Lécailledba97072017-03-17 15:33:50 +0100653
654/* Parse the "source" server keyword */
655static int srv_parse_source(char **args, int *cur_arg,
656 struct proxy *curproxy, struct server *newsrv, char **err)
657{
658 char *errmsg;
659 int port_low, port_high;
660 struct sockaddr_storage *sk;
661 struct protocol *proto;
662
663 errmsg = NULL;
664
665 if (!*args[*cur_arg + 1]) {
666 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
667 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
668 goto err;
669 }
670
671 /* 'sk' is statically allocated (no need to be freed). */
672 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
673 if (!sk) {
674 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
675 goto err;
676 }
677
678 proto = protocol_by_family(sk->ss_family);
679 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100680 ha_alert("'%s %s' : connect() not supported for this address family.\n",
681 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100682 goto err;
683 }
684
685 newsrv->conn_src.opts |= CO_SRC_BIND;
686 newsrv->conn_src.source_addr = *sk;
687
688 if (port_low != port_high) {
689 int i;
690
691 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100692 ha_alert("'%s' does not support port offsets (found '%s').\n",
693 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100694 goto err;
695 }
696
697 if (port_low <= 0 || port_low > 65535 ||
698 port_high <= 0 || port_high > 65535 ||
699 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100700 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 +0100701 goto err;
702 }
703 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
704 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
705 newsrv->conn_src.sport_range->ports[i] = port_low + i;
706 }
707
708 *cur_arg += 2;
709 while (*(args[*cur_arg])) {
710 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
711#if defined(CONFIG_HAP_TRANSPARENT)
712 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100713 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
714 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100715 goto err;
716 }
717 if (!strcmp(args[*cur_arg + 1], "client")) {
718 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
719 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
720 }
721 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
722 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
723 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
724 }
725 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
726 char *name, *end;
727
728 name = args[*cur_arg + 1] + 7;
729 while (isspace(*name))
730 name++;
731
732 end = name;
733 while (*end && !isspace(*end) && *end != ',' && *end != ')')
734 end++;
735
736 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
737 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
738 free(newsrv->conn_src.bind_hdr_name);
739 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
740 newsrv->conn_src.bind_hdr_len = end - name;
741 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
742 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
743 newsrv->conn_src.bind_hdr_occ = -1;
744
745 /* now look for an occurrence number */
746 while (isspace(*end))
747 end++;
748 if (*end == ',') {
749 end++;
750 name = end;
751 if (*end == '-')
752 end++;
753 while (isdigit((int)*end))
754 end++;
755 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
756 }
757
758 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100759 ha_alert("usesrc hdr_ip(name,num) does not support negative"
760 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100761 goto err;
762 }
763 }
764 else {
765 struct sockaddr_storage *sk;
766 int port1, port2;
767
768 /* 'sk' is statically allocated (no need to be freed). */
769 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
770 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100771 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100772 goto err;
773 }
774
775 proto = protocol_by_family(sk->ss_family);
776 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100777 ha_alert("'%s %s' : connect() not supported for this address family.\n",
778 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100779 goto err;
780 }
781
782 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100783 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
784 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100785 goto err;
786 }
787 newsrv->conn_src.tproxy_addr = *sk;
788 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
789 }
790 global.last_checks |= LSTCHK_NETADM;
791 *cur_arg += 2;
792 continue;
793#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100794 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 +0100795 goto err;
796#endif /* defined(CONFIG_HAP_TRANSPARENT) */
797 } /* "usesrc" */
798
799 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
800#ifdef SO_BINDTODEVICE
801 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100802 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100803 goto err;
804 }
805 free(newsrv->conn_src.iface_name);
806 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
807 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
808 global.last_checks |= LSTCHK_NETADM;
809#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100810 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100811 goto err;
812#endif
813 *cur_arg += 2;
814 continue;
815 }
816 /* this keyword in not an option of "source" */
817 break;
818 } /* while */
819
820 return 0;
821
822 err:
823 free(errmsg);
824 return ERR_ALERT | ERR_FATAL;
825}
826
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100827/* Parse the "stick" server keyword */
828static int srv_parse_stick(char **args, int *cur_arg,
829 struct proxy *curproxy, struct server *newsrv, char **err)
830{
831 newsrv->flags &= ~SRV_F_NON_STICK;
832 return 0;
833}
834
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100835/* Parse the "track" server keyword */
836static int srv_parse_track(char **args, int *cur_arg,
837 struct proxy *curproxy, struct server *newsrv, char **err)
838{
839 char *arg;
840
841 arg = args[*cur_arg + 1];
842 if (!*arg) {
843 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
844 return ERR_ALERT | ERR_FATAL;
845 }
846
847 free(newsrv->trackit);
848 newsrv->trackit = strdup(arg);
849
850 return 0;
851}
852
Frédéric Lécailledba97072017-03-17 15:33:50 +0100853
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200854/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200855 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200856 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200857 *
858 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200859 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200860void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200861{
Willy Tarreau87b09662015-04-03 00:22:06 +0200862 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200863
Willy Tarreau87b09662015-04-03 00:22:06 +0200864 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
865 if (stream->srv_conn == srv)
866 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200867}
868
869/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200870 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200871 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200872 *
873 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200874 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200875void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200876{
877 struct server *srv;
878
879 for (srv = px->srv; srv != NULL; srv = srv->next)
880 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200881 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200882}
883
Willy Tarreaubda92272014-05-20 21:55:30 +0200884/* Appends some information to a message string related to a server going UP or
885 * DOWN. If both <forced> and <reason> are null and the server tracks another
886 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200887 * If <check> is non-null, an entire string describing the check result will be
888 * appended after a comma and a space (eg: to report some information from the
889 * check that changed the state). In the other case, the string will be built
890 * using the check results stored into the struct server if present.
891 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200892 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200893 *
894 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200895 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200896void srv_append_status(struct buffer *msg, struct server *s,
897 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200898{
Emeric Brun5a133512017-10-19 14:42:30 +0200899 short status = s->op_st_chg.status;
900 short code = s->op_st_chg.code;
901 long duration = s->op_st_chg.duration;
902 char *desc = s->op_st_chg.reason;
903
904 if (check) {
905 status = check->status;
906 code = check->code;
907 duration = check->duration;
908 desc = check->desc;
909 }
910
911 if (status != -1) {
912 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
913
914 if (status >= HCHK_STATUS_L57DATA)
915 chunk_appendf(msg, ", code: %d", code);
916
917 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200918 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200919
920 chunk_appendf(msg, ", info: \"");
921
922 chunk_initlen(&src, desc, 0, strlen(desc));
923 chunk_asciiencode(msg, &src, '"');
924
925 chunk_appendf(msg, "\"");
926 }
927
928 if (duration >= 0)
929 chunk_appendf(msg, ", check duration: %ldms", duration);
930 }
931 else if (desc && *desc) {
932 chunk_appendf(msg, ", %s", desc);
933 }
934 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200935 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200936 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200937
938 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200939 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200940 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
941 " %d sessions active, %d requeued, %d remaining in queue",
942 s->proxy->srv_act, s->proxy->srv_bck,
943 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
944 s->cur_sess, xferred, s->nbpend);
945 else
946 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
947 " %d sessions requeued, %d total in queue",
948 s->proxy->srv_act, s->proxy->srv_bck,
949 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
950 xferred, s->nbpend);
951 }
952}
953
Emeric Brun5a133512017-10-19 14:42:30 +0200954/* Marks server <s> down, regardless of its checks' statuses. The server is
955 * registered in a list to postpone the counting of the remaining servers on
956 * the proxy and transfers queued streams whenever possible to other servers at
957 * a sync point. Maintenance servers are ignored. It stores the <reason> if
958 * non-null as the reason for going down or the available data from the check
959 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200960 *
961 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200962 */
Emeric Brun5a133512017-10-19 14:42:30 +0200963void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200964{
965 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200966
Emeric Brun64cc49c2017-10-03 14:46:45 +0200967 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200968 return;
969
Emeric Brun52a91d32017-08-31 14:41:55 +0200970 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200971 *s->op_st_chg.reason = 0;
972 s->op_st_chg.status = -1;
973 if (reason) {
974 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
975 }
976 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100977 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200978 s->op_st_chg.code = check->code;
979 s->op_st_chg.status = check->status;
980 s->op_st_chg.duration = check->duration;
981 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200982
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200983 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200984 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200985
Emeric Brun9f0b4582017-10-23 14:39:51 +0200986 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100987 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200988 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100989 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200990 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200991}
992
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200993/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200994 * in maintenance. The server is registered in a list to postpone the counting
995 * of the remaining servers on the proxy and tries to grab requests from the
996 * proxy at a sync point. Maintenance servers are ignored. It stores the
997 * <reason> if non-null as the reason for going down or the available data
998 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200999 *
1000 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001001 */
Emeric Brun5a133512017-10-19 14:42:30 +02001002void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001003{
1004 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001005
Emeric Brun64cc49c2017-10-03 14:46:45 +02001006 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001007 return;
1008
Emeric Brun52a91d32017-08-31 14:41:55 +02001009 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001010 return;
1011
Emeric Brun52a91d32017-08-31 14:41:55 +02001012 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001013 *s->op_st_chg.reason = 0;
1014 s->op_st_chg.status = -1;
1015 if (reason) {
1016 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1017 }
1018 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001019 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001020 s->op_st_chg.code = check->code;
1021 s->op_st_chg.status = check->status;
1022 s->op_st_chg.duration = check->duration;
1023 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001024
Emeric Brun64cc49c2017-10-03 14:46:45 +02001025 if (s->slowstart <= 0)
1026 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001027
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001028 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001029 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001030
Emeric Brun9f0b4582017-10-23 14:39:51 +02001031 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001032 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001033 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001034 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001035 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001036}
1037
Willy Tarreau8eb77842014-05-21 13:54:57 +02001038/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001039 * isn't in maintenance. The server is registered in a list to postpone the
1040 * counting of the remaining servers on the proxy and tries to grab requests
1041 * from the proxy. Maintenance servers are ignored. It stores the
1042 * <reason> if non-null as the reason for going down or the available data
1043 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001044 * up. Note that it makes use of the trash to build the log strings, so <reason>
1045 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001046 *
1047 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001048 */
Emeric Brun5a133512017-10-19 14:42:30 +02001049void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001050{
1051 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001052
Emeric Brun64cc49c2017-10-03 14:46:45 +02001053 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001054 return;
1055
Emeric Brun52a91d32017-08-31 14:41:55 +02001056 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001057 return;
1058
Emeric Brun52a91d32017-08-31 14:41:55 +02001059 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001060 *s->op_st_chg.reason = 0;
1061 s->op_st_chg.status = -1;
1062 if (reason) {
1063 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1064 }
1065 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001066 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001067 s->op_st_chg.code = check->code;
1068 s->op_st_chg.status = check->status;
1069 s->op_st_chg.duration = check->duration;
1070 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001071
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001072 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001073 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001074
Emeric Brun9f0b4582017-10-23 14:39:51 +02001075 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001076 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001077 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001078 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001079 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001080}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001081
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001082/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1083 * enforce either maint mode or drain mode. It is not allowed to set more than
1084 * one flag at once. The equivalent "inherited" flag is propagated to all
1085 * tracking servers. Maintenance mode disables health checks (but not agent
1086 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001087 * is done. If <cause> is non-null, it will be displayed at the end of the log
1088 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001089 *
1090 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001091 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001092void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001093{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001094 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001095
1096 if (!mode)
1097 return;
1098
1099 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001100 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001101 return;
1102
Emeric Brun52a91d32017-08-31 14:41:55 +02001103 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001104 if (cause)
1105 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1106
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001107 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001108 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001109
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001110 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001111 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1112 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001113 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001114
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001115 /* compute the inherited flag to propagate */
1116 if (mode & SRV_ADMF_MAINT)
1117 mode = SRV_ADMF_IMAINT;
1118 else if (mode & SRV_ADMF_DRAIN)
1119 mode = SRV_ADMF_IDRAIN;
1120
Emeric Brun9f0b4582017-10-23 14:39:51 +02001121 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001122 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001123 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001124 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001125 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001126}
1127
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001128/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1129 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1130 * than one flag at once. The equivalent "inherited" flag is propagated to all
1131 * tracking servers. Leaving maintenance mode re-enables health checks. When
1132 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001133 *
1134 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001135 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001136void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001137{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001138 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001139
1140 if (!mode)
1141 return;
1142
1143 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001144 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001145 return;
1146
Emeric Brun52a91d32017-08-31 14:41:55 +02001147 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001148
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001149 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001150 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001151
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001152 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001153 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1154 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001155 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001156
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001157 if (mode & SRV_ADMF_MAINT)
1158 mode = SRV_ADMF_IMAINT;
1159 else if (mode & SRV_ADMF_DRAIN)
1160 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001161
Emeric Brun9f0b4582017-10-23 14:39:51 +02001162 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001163 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001164 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001165 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001166 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001167}
1168
Willy Tarreau757478e2016-11-03 19:22:19 +01001169/* principle: propagate maint and drain to tracking servers. This is useful
1170 * upon startup so that inherited states are correct.
1171 */
1172static void srv_propagate_admin_state(struct server *srv)
1173{
1174 struct server *srv2;
1175
1176 if (!srv->trackers)
1177 return;
1178
1179 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001180 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001181 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001182 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001183
Emeric Brun52a91d32017-08-31 14:41:55 +02001184 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001185 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001186 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001187 }
1188}
1189
1190/* Compute and propagate the admin states for all servers in proxy <px>.
1191 * Only servers *not* tracking another one are considered, because other
1192 * ones will be handled when the server they track is visited.
1193 */
1194void srv_compute_all_admin_states(struct proxy *px)
1195{
1196 struct server *srv;
1197
1198 for (srv = px->srv; srv; srv = srv->next) {
1199 if (srv->track)
1200 continue;
1201 srv_propagate_admin_state(srv);
1202 }
1203}
1204
Willy Tarreaudff55432012-10-10 17:51:05 +02001205/* Note: must not be declared <const> as its list will be overwritten.
1206 * Please take care of keeping this list alphabetically sorted, doing so helps
1207 * all code contributors.
1208 * Optional keywords are also declared with a NULL ->parse() function so that
1209 * the config parser can report an appropriate error when a known keyword was
1210 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001211 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001212 */
1213static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001214 { "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 +01001215 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001216 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001217 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001218 { "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 +01001219 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001220 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1221 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001222 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001223 { "idle-timeout", srv_parse_idle_timeout, 1, 1 }, /* Set the time before we destroy orphan idle connections, defaults to 0 */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001224 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001225 { "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 +01001226 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001227 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001228 { "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 +01001229 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1230 { "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 +01001231 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001232 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001233 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001234 { "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 +01001235 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001236 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1237 { "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 +02001238 { "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 +01001239 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001240 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001241 { NULL, NULL, 0 },
1242}};
1243
Willy Tarreau0108d902018-11-25 19:14:37 +01001244INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001245
Willy Tarreau004e0452013-11-21 11:22:01 +01001246/* Recomputes the server's eweight based on its state, uweight, the current time,
1247 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001248 * state is automatically disabled if the time is elapsed. If <must_update> is
1249 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001250 *
1251 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001252 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001253void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001254{
1255 struct proxy *px = sv->proxy;
1256 unsigned w;
1257
1258 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1259 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001260 if (sv->next_state == SRV_ST_STARTING)
1261 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001262 }
1263
1264 /* We must take care of not pushing the server to full throttle during slow starts.
1265 * It must also start immediately, at least at the minimal step when leaving maintenance.
1266 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001267 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001268 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1269 else
1270 w = px->lbprm.wdiv;
1271
Emeric Brun52a91d32017-08-31 14:41:55 +02001272 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001273
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001274 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001275 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001276 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001277}
1278
Willy Tarreaubaaee002006-06-26 02:48:02 +02001279/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001280 * Parses weight_str and configures sv accordingly.
1281 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001282 *
1283 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001284 */
1285const char *server_parse_weight_change_request(struct server *sv,
1286 const char *weight_str)
1287{
1288 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001289 long int w;
1290 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001291
1292 px = sv->proxy;
1293
1294 /* if the weight is terminated with '%', it is set relative to
1295 * the initial weight, otherwise it is absolute.
1296 */
1297 if (!*weight_str)
1298 return "Require <weight> or <weight%>.\n";
1299
Simon Hormanb796afa2013-02-12 10:45:53 +09001300 w = strtol(weight_str, &end, 10);
1301 if (end == weight_str)
1302 return "Empty weight string empty or preceded by garbage";
1303 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001304 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001305 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001306 /* Avoid integer overflow */
1307 if (w > 25600)
1308 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001309 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001310 if (w > 256)
1311 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001312 }
1313 else if (w < 0 || w > 256)
1314 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001315 else if (end[0] != '\0')
1316 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001317
1318 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1319 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1320
1321 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001322 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001323
1324 return NULL;
1325}
1326
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001327/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001328 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1329 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001330 * Returns:
1331 * - error string on error
1332 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001333 *
1334 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001335 */
1336const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001337 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001338{
1339 unsigned char ip[INET6_ADDRSTRLEN];
1340
1341 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001342 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001343 return NULL;
1344 }
1345 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001346 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001347 return NULL;
1348 }
1349
1350 return "Could not understand IP address format.\n";
1351}
1352
Willy Tarreau46b7f532018-08-21 11:54:26 +02001353/*
1354 * Must be called with the server lock held.
1355 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001356const char *server_parse_maxconn_change_request(struct server *sv,
1357 const char *maxconn_str)
1358{
1359 long int v;
1360 char *end;
1361
1362 if (!*maxconn_str)
1363 return "Require <maxconn>.\n";
1364
1365 v = strtol(maxconn_str, &end, 10);
1366 if (end == maxconn_str)
1367 return "maxconn string empty or preceded by garbage";
1368 else if (end[0] != '\0')
1369 return "Trailing garbage in maxconn string";
1370
1371 if (sv->maxconn == sv->minconn) { // static maxconn
1372 sv->maxconn = sv->minconn = v;
1373 } else { // dynamic maxconn
1374 sv->maxconn = v;
1375 }
1376
1377 if (may_dequeue_tasks(sv, sv->proxy))
1378 process_srv_queue(sv);
1379
1380 return NULL;
1381}
1382
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001383#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001384static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1385 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001386{
1387 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001388 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001389 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001390 NULL,
1391 };
1392
1393 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001394 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001395
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001396 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1397}
1398
1399static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1400{
1401 struct sample_expr *expr;
1402
1403 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 +01001404 if (!expr) {
1405 memprintf(err, "error detected while parsing sni expression : %s", *err);
1406 return ERR_ALERT | ERR_FATAL;
1407 }
1408
1409 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1410 memprintf(err, "error detected while parsing sni expression : "
1411 " fetch method '%s' extracts information from '%s', "
1412 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001413 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001414 return ERR_ALERT | ERR_FATAL;
1415 }
1416
1417 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1418 release_sample_expr(newsrv->ssl_ctx.sni);
1419 newsrv->ssl_ctx.sni = expr;
1420
1421 return 0;
1422}
1423#endif
1424
1425static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1426{
1427 if (err && *err) {
1428 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001429 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 +01001430 }
1431 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001432 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1433 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001434}
1435
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001436static void srv_conn_src_sport_range_cpy(struct server *srv,
1437 struct server *src)
1438{
1439 int range_sz;
1440
1441 range_sz = src->conn_src.sport_range->size;
1442 if (range_sz > 0) {
1443 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1444 if (srv->conn_src.sport_range != NULL) {
1445 int i;
1446
1447 for (i = 0; i < range_sz; i++) {
1448 srv->conn_src.sport_range->ports[i] =
1449 src->conn_src.sport_range->ports[i];
1450 }
1451 }
1452 }
1453}
1454
1455/*
1456 * Copy <src> server connection source settings to <srv> server everything needed.
1457 */
1458static void srv_conn_src_cpy(struct server *srv, struct server *src)
1459{
1460 srv->conn_src.opts = src->conn_src.opts;
1461 srv->conn_src.source_addr = src->conn_src.source_addr;
1462
1463 /* Source port range copy. */
1464 if (src->conn_src.sport_range != NULL)
1465 srv_conn_src_sport_range_cpy(srv, src);
1466
1467#ifdef CONFIG_HAP_TRANSPARENT
1468 if (src->conn_src.bind_hdr_name != NULL) {
1469 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1470 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1471 }
1472 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1473 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1474#endif
1475 if (src->conn_src.iface_name != NULL)
1476 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1477}
1478
1479/*
1480 * Copy <src> server SSL settings to <srv> server allocating
1481 * everything needed.
1482 */
1483#if defined(USE_OPENSSL)
1484static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1485{
1486 if (src->ssl_ctx.ca_file != NULL)
1487 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1488 if (src->ssl_ctx.crl_file != NULL)
1489 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1490 if (src->ssl_ctx.client_crt != NULL)
1491 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1492
1493 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1494
1495 if (src->ssl_ctx.verify_host != NULL)
1496 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1497 if (src->ssl_ctx.ciphers != NULL)
1498 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001499#if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
1500 if (src->ssl_ctx.ciphersuites != NULL)
1501 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1502#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001503 if (src->sni_expr != NULL)
1504 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001505
1506#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1507 if (src->ssl_ctx.alpn_str) {
1508 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1509 if (srv->ssl_ctx.alpn_str) {
1510 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1511 src->ssl_ctx.alpn_len);
1512 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1513 }
1514 }
1515#endif
1516#ifdef OPENSSL_NPN_NEGOTIATED
1517 if (src->ssl_ctx.npn_str) {
1518 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1519 if (srv->ssl_ctx.npn_str) {
1520 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1521 src->ssl_ctx.npn_len);
1522 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1523 }
1524 }
1525#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001526}
1527#endif
1528
1529/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001530 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001531 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001532 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001533 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001534static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001535{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001536 char *hostname_dn;
1537 int hostname_len, hostname_dn_len;
1538
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001539 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001540 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001541
Christopher Faulet67957bd2017-09-27 11:00:59 +02001542 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001543 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001544 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1545 hostname_dn, trash.size);
1546 if (hostname_dn_len == -1)
1547 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001548
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001549
Christopher Faulet67957bd2017-09-27 11:00:59 +02001550 free(srv->hostname);
1551 free(srv->hostname_dn);
1552 srv->hostname = strdup(hostname);
1553 srv->hostname_dn = strdup(hostname_dn);
1554 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001555 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001556 goto err;
1557
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001558 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001559
1560 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001561 free(srv->hostname); srv->hostname = NULL;
1562 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001563 return -1;
1564}
1565
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001566/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001567 * Copy <src> server settings to <srv> server allocating
1568 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001569 * This function is not supposed to be called at any time, but only
1570 * during server settings parsing or during server allocations from
1571 * a server template, and just after having calloc()'ed a new server.
1572 * So, <src> may only be a default server (when parsing server settings)
1573 * or a server template (during server allocations from a server template).
1574 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1575 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001576 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001577static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001578{
1579 /* Connection source settings copy */
1580 srv_conn_src_cpy(srv, src);
1581
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001582 if (srv_tmpl) {
1583 srv->addr = src->addr;
1584 srv->svc_port = src->svc_port;
1585 }
1586
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001587 srv->pp_opts = src->pp_opts;
1588 if (src->rdr_pfx != NULL) {
1589 srv->rdr_pfx = strdup(src->rdr_pfx);
1590 srv->rdr_len = src->rdr_len;
1591 }
1592 if (src->cookie != NULL) {
1593 srv->cookie = strdup(src->cookie);
1594 srv->cklen = src->cklen;
1595 }
1596 srv->use_ssl = src->use_ssl;
1597 srv->check.addr = srv->agent.addr = src->check.addr;
1598 srv->check.use_ssl = src->check.use_ssl;
1599 srv->check.port = src->check.port;
1600 /* Note: 'flags' field has potentially been already initialized. */
1601 srv->flags |= src->flags;
1602 srv->do_check = src->do_check;
1603 srv->do_agent = src->do_agent;
1604 if (srv->check.port)
1605 srv->flags |= SRV_F_CHECKPORT;
1606 srv->check.inter = src->check.inter;
1607 srv->check.fastinter = src->check.fastinter;
1608 srv->check.downinter = src->check.downinter;
1609 srv->agent.use_ssl = src->agent.use_ssl;
1610 srv->agent.port = src->agent.port;
1611 if (src->agent.send_string != NULL)
1612 srv->agent.send_string = strdup(src->agent.send_string);
1613 srv->agent.send_string_len = src->agent.send_string_len;
1614 srv->agent.inter = src->agent.inter;
1615 srv->agent.fastinter = src->agent.fastinter;
1616 srv->agent.downinter = src->agent.downinter;
1617 srv->maxqueue = src->maxqueue;
1618 srv->minconn = src->minconn;
1619 srv->maxconn = src->maxconn;
1620 srv->slowstart = src->slowstart;
1621 srv->observe = src->observe;
1622 srv->onerror = src->onerror;
1623 srv->onmarkeddown = src->onmarkeddown;
1624 srv->onmarkedup = src->onmarkedup;
1625 if (src->trackit != NULL)
1626 srv->trackit = strdup(src->trackit);
1627 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1628 srv->uweight = srv->iweight = src->iweight;
1629
1630 srv->check.send_proxy = src->check.send_proxy;
1631 /* health: up, but will fall down at first failure */
1632 srv->check.rise = srv->check.health = src->check.rise;
1633 srv->check.fall = src->check.fall;
1634
1635 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001636 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1637 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1638 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001639 srv->check.state |= CHK_ST_PAUSED;
1640 srv->check.health = 0;
1641 }
1642
1643 /* health: up but will fall down at first failure */
1644 srv->agent.rise = srv->agent.health = src->agent.rise;
1645 srv->agent.fall = src->agent.fall;
1646
1647 if (src->resolvers_id != NULL)
1648 srv->resolvers_id = strdup(src->resolvers_id);
1649 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001650 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001651 if (srv->dns_opts.family_prio == AF_UNSPEC)
1652 srv->dns_opts.family_prio = AF_INET6;
1653 memcpy(srv->dns_opts.pref_net,
1654 src->dns_opts.pref_net,
1655 sizeof srv->dns_opts.pref_net);
1656 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1657
1658 srv->init_addr_methods = src->init_addr_methods;
1659 srv->init_addr = src->init_addr;
1660#if defined(USE_OPENSSL)
1661 srv_ssl_settings_cpy(srv, src);
1662#endif
1663#ifdef TCP_USER_TIMEOUT
1664 srv->tcp_ut = src->tcp_ut;
1665#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001666 srv->mux_proto = src->mux_proto;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001667 srv->idle_timeout = src->idle_timeout;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001668
Olivier Houchard8da5f982017-08-04 18:35:36 +02001669 if (srv_tmpl)
1670 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001671}
1672
William Lallemand313bfd12018-10-26 14:47:32 +02001673struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001674{
1675 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001676 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001677
1678 srv = calloc(1, sizeof *srv);
1679 if (!srv)
1680 return NULL;
1681
1682 srv->obj_type = OBJ_TYPE_SERVER;
1683 srv->proxy = proxy;
1684 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001685 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001686
1687 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1688 goto free_srv;
1689 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1690 goto free_priv_conns;
1691 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1692 goto free_idle_conns;
1693
1694 for (i = 0; i < global.nbthread; i++) {
1695 LIST_INIT(&srv->priv_conns[i]);
1696 LIST_INIT(&srv->idle_conns[i]);
1697 LIST_INIT(&srv->safe_conns[i]);
1698 }
1699
Emeric Brun52a91d32017-08-31 14:41:55 +02001700 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001701 srv->last_change = now.tv_sec;
1702
1703 srv->check.status = HCHK_STATUS_INI;
1704 srv->check.server = srv;
1705 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1706
1707 srv->agent.status = HCHK_STATUS_INI;
1708 srv->agent.server = srv;
1709 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1710
1711 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001712
1713 free_idle_conns:
1714 free(srv->idle_conns);
1715 free_priv_conns:
1716 free(srv->priv_conns);
1717 free_srv:
1718 free(srv);
1719 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001720}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001721
1722/*
1723 * Validate <srv> server health-check settings.
1724 * Returns 0 if everything is OK, -1 if not.
1725 */
1726static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1727{
1728 struct tcpcheck_rule *r = NULL;
1729 struct list *l;
1730
1731 /*
1732 * We need at least a service port, a check port or the first tcp-check rule must
1733 * be a 'connect' one when checking an IPv4/IPv6 server.
1734 */
1735 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1736 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1737 return 0;
1738
1739 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1740 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001741 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1742 "Check has been disabled.\n",
1743 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001744 return -1;
1745 }
1746
1747 /* search the first action (connect / send / expect) in the list */
1748 l = &srv->proxy->tcpcheck_rules;
1749 list_for_each_entry(r, l, list) {
1750 if (r->action != TCPCHK_ACT_COMMENT)
1751 break;
1752 }
1753
1754 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001755 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1756 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1757 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001758 return -1;
1759 }
1760
1761 /* scan the tcp-check ruleset to ensure a port has been configured */
1762 l = &srv->proxy->tcpcheck_rules;
1763 list_for_each_entry(r, l, list) {
1764 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001765 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1766 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1767 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001768 return -1;
1769 }
1770 }
1771
1772 return 0;
1773}
1774
1775/*
1776 * Initialize <srv> health-check structure.
1777 * Returns the error string in case of memory allocation failure, NULL if not.
1778 */
1779static const char *do_health_check_init(struct server *srv, int check_type, int state)
1780{
1781 const char *ret;
1782
1783 if (!srv->do_check)
1784 return NULL;
1785
1786 ret = init_check(&srv->check, check_type);
1787 if (ret)
1788 return ret;
1789
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001790 srv->check.state |= state;
1791 global.maxsock++;
1792
1793 return NULL;
1794}
1795
1796static int server_health_check_init(const char *file, int linenum,
1797 struct server *srv, struct proxy *curproxy)
1798{
1799 const char *ret;
1800
1801 if (!srv->do_check)
1802 return 0;
1803
1804 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001805 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1806 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001807 return ERR_ALERT | ERR_FATAL;
1808 }
1809
1810 if (server_healthcheck_validate(file, linenum, srv) < 0)
1811 return ERR_ALERT | ERR_ABORT;
1812
1813 /* note: check type will be set during the config review phase */
1814 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1815 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001816 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001817 return ERR_ALERT | ERR_ABORT;
1818 }
1819
1820 return 0;
1821}
1822
1823/*
1824 * Initialize <srv> agent check structure.
1825 * Returns the error string in case of memory allocation failure, NULL if not.
1826 */
1827static const char *do_server_agent_check_init(struct server *srv, int state)
1828{
1829 const char *ret;
1830
1831 if (!srv->do_agent)
1832 return NULL;
1833
1834 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1835 if (ret)
1836 return ret;
1837
1838 if (!srv->agent.inter)
1839 srv->agent.inter = srv->check.inter;
1840
1841 srv->agent.state |= state;
1842 global.maxsock++;
1843
1844 return NULL;
1845}
1846
1847static int server_agent_check_init(const char *file, int linenum,
1848 struct server *srv, struct proxy *curproxy)
1849{
1850 const char *ret;
1851
1852 if (!srv->do_agent)
1853 return 0;
1854
1855 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001856 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 +02001857 file, linenum, srv->id);
1858 return ERR_ALERT | ERR_FATAL;
1859 }
1860
1861 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1862 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001863 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001864 return ERR_ALERT | ERR_ABORT;
1865 }
1866
1867 return 0;
1868}
1869
1870#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1871static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1872 struct server *srv, struct proxy *proxy)
1873{
1874 int ret;
1875 char *err = NULL;
1876
1877 if (!srv->sni_expr)
1878 return 0;
1879
1880 ret = server_parse_sni_expr(srv, proxy, &err);
1881 if (!ret)
1882 return 0;
1883
1884 display_parser_err(file, linenum, args, cur_arg, &err);
1885 free(err);
1886
1887 return ret;
1888}
1889#endif
1890
1891/*
1892 * Server initializations finalization.
1893 * Initialize health check, agent check and SNI expression if enabled.
1894 * Must not be called for a default server instance.
1895 */
1896static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1897 struct server *srv, struct proxy *px)
1898{
1899 int ret;
1900
1901 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1902 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1903 return ret;
1904 }
1905
1906#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1907 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1908 return ret;
1909#endif
1910
1911 if (srv->flags & SRV_F_BACKUP)
1912 px->srv_bck++;
1913 else
1914 px->srv_act++;
1915 srv_lb_commit_status(srv);
1916
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001917 if (!srv->tmpl_info.prefix && srv->idle_timeout != 0) {
1918 int i;
1919
1920 srv->idle_orphan_conns = calloc(global.nbthread, sizeof(*srv->idle_orphan_conns));
1921 if (!srv->idle_orphan_conns)
1922 goto err;
1923 srv->idle_task = calloc(global.nbthread, sizeof(*srv->idle_task));
1924 if (!srv->idle_task)
1925 goto err;
1926 for (i = 0; i < global.nbthread; i++) {
1927 LIST_INIT(&srv->idle_orphan_conns[i]);
1928 srv->idle_task[i] = task_new(1 << i);
1929 if (!srv->idle_task[i])
1930 goto err;
1931 srv->idle_task[i]->process = cleanup_idle_connections;
1932 srv->idle_task[i]->context = srv;
1933 }
1934 }
1935
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001936 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001937err:
1938 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001939}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001940
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001941/*
1942 * Parse as much as possible such a range string argument: low[-high]
1943 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1944 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1945 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1946 * Returns 0 if succeeded, -1 if not.
1947 */
1948static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1949{
1950 char *nb_high_arg;
1951
1952 *nb_high = 0;
1953 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001954 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001955
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001956 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001957 *nb_high_arg++ = '\0';
1958 *nb_high = atoi(nb_high_arg);
1959 }
1960 else {
1961 *nb_high += *nb_low;
1962 *nb_low = 1;
1963 }
1964
1965 if (*nb_low < 0 || *nb_high < *nb_low)
1966 return -1;
1967
1968 return 0;
1969}
1970
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001971static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1972{
1973 chunk_printf(&trash, "%s%d", prefix, nb);
1974 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001975 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001976}
1977
1978/*
1979 * Initialize as much as possible servers from <srv> server template.
1980 * Note that a server template is a special server with
1981 * a few different parameters than a server which has
1982 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001983 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001984 * 'srv' template included.
1985 */
1986static int server_template_init(struct server *srv, struct proxy *px)
1987{
1988 int i;
1989 struct server *newsrv;
1990
1991 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1992 int check_init_state;
1993 int agent_init_state;
1994
1995 newsrv = new_server(px);
1996 if (!newsrv)
1997 goto err;
1998
1999 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002000 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002001#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2002 if (newsrv->sni_expr) {
2003 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2004 if (!newsrv->ssl_ctx.sni)
2005 goto err;
2006 }
2007#endif
2008 /* Set this new server ID. */
2009 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2010
2011 /* Initial checks states. */
2012 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2013 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2014
2015 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2016 do_server_agent_check_init(newsrv, agent_init_state))
2017 goto err;
2018
2019 /* Linked backwards first. This will be restablished after parsing. */
2020 newsrv->next = px->srv;
2021 px->srv = newsrv;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002022 if (newsrv->idle_timeout != 0) {
2023 int i;
2024
2025 newsrv->idle_orphan_conns = calloc(global.nbthread, sizeof(*newsrv->idle_orphan_conns));
2026 if (!newsrv->idle_orphan_conns)
2027 goto err;
2028 newsrv->idle_task = calloc(global.nbthread, sizeof(*newsrv->idle_task));
2029 if (!newsrv->idle_task)
2030 goto err;
2031 for (i = 0; i < global.nbthread; i++) {
2032 LIST_INIT(&newsrv->idle_orphan_conns[i]);
2033 newsrv->idle_task[i] = task_new(1 << i);
2034 if (!newsrv->idle_task[i])
2035 goto err;
2036 newsrv->idle_task[i]->process = cleanup_idle_connections;
2037 newsrv->idle_task[i]->context = newsrv;
2038 }
2039 }
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002040 }
2041 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2042
2043 return i - srv->tmpl_info.nb_low;
2044
2045 err:
2046 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2047 if (newsrv) {
2048#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2049 release_sample_expr(newsrv->ssl_ctx.sni);
2050#endif
2051 free_check(&newsrv->agent);
2052 free_check(&newsrv->check);
2053 }
2054 free(newsrv);
2055 return i - srv->tmpl_info.nb_low;
2056}
2057
Willy Tarreau272adea2014-03-31 10:39:59 +02002058int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
2059{
2060 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002061 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002062 char *errmsg = NULL;
2063 int err_code = 0;
2064 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002065 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002066
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002067 if (!strcmp(args[0], "server") ||
2068 !strcmp(args[0], "default-server") ||
2069 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002070 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002071 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002072 int srv = !defsrv && !strcmp(args[0], "server");
2073 int srv_tmpl = !defsrv && !srv;
2074 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002075
2076 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002077 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002078 err_code |= ERR_ALERT | ERR_FATAL;
2079 goto out;
2080 }
2081 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002082 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002083
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002084 /* There is no mandatory first arguments for default server. */
2085 if (srv) {
2086 if (!*args[2]) {
2087 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002088 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002089 file, linenum, args[0]);
2090 err_code |= ERR_ALERT | ERR_FATAL;
2091 goto out;
2092 }
2093
2094 err = invalid_char(args[1]);
2095 }
2096 else if (srv_tmpl) {
2097 if (!*args[3]) {
2098 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002099 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 +02002100 file, linenum, args[0]);
2101 err_code |= ERR_ALERT | ERR_FATAL;
2102 goto out;
2103 }
2104
2105 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002106 }
2107
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002108 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002109 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 +02002110 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002111 err_code |= ERR_ALERT | ERR_FATAL;
2112 goto out;
2113 }
2114
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002115 cur_arg = 2;
2116 if (srv_tmpl) {
2117 /* Parse server-template <nb | range> arg. */
2118 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002119 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002120 file, linenum, args[0], args[cur_arg]);
2121 err_code |= ERR_ALERT | ERR_FATAL;
2122 goto out;
2123 }
2124 cur_arg++;
2125 }
2126
Willy Tarreau272adea2014-03-31 10:39:59 +02002127 if (!defsrv) {
2128 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002129 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002130 struct protocol *proto;
2131
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002132 newsrv = new_server(curproxy);
2133 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002134 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002135 err_code |= ERR_ALERT | ERR_ABORT;
2136 goto out;
2137 }
2138
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002139 if (srv_tmpl) {
2140 newsrv->tmpl_info.nb_low = tmpl_range_low;
2141 newsrv->tmpl_info.nb_high = tmpl_range_high;
2142 }
2143
Willy Tarreau272adea2014-03-31 10:39:59 +02002144 /* the servers are linked backwards first */
2145 newsrv->next = curproxy->srv;
2146 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002147 newsrv->conf.file = strdup(file);
2148 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002149 /* Note: for a server template, its id is its prefix.
2150 * This is a temporary id which will be used for server allocations to come
2151 * after parsing.
2152 */
2153 if (srv)
2154 newsrv->id = strdup(args[1]);
2155 else
2156 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002157
2158 /* several ways to check the port component :
2159 * - IP => port=+0, relative (IPv4 only)
2160 * - IP: => port=+0, relative
2161 * - IP:N => port=N, absolute
2162 * - IP:+N => port=+N, relative
2163 * - IP:-N => port=-N, relative
2164 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002165 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002166 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002167 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002168 err_code |= ERR_ALERT | ERR_FATAL;
2169 goto out;
2170 }
2171
2172 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002173 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002174 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002175 file, linenum, args[0], args[1]);
2176 err_code |= ERR_ALERT | ERR_FATAL;
2177 goto out;
2178 }
2179
2180 if (!port1 || !port2) {
2181 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002182 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002183 }
2184 else if (port1 != port2) {
2185 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002186 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002187 file, linenum, args[0], args[1], args[2]);
2188 err_code |= ERR_ALERT | ERR_FATAL;
2189 goto out;
2190 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002191
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002192 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002193 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002194 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002195 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002196 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2197 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2198 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002199 err_code |= ERR_ALERT | ERR_FATAL;
2200 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002201 }
2202 }
2203 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002204 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002205 file, linenum, newsrv->id);
2206 err_code |= ERR_ALERT | ERR_FATAL;
2207 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002208 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002209 }
2210
Willy Tarreau272adea2014-03-31 10:39:59 +02002211 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002212 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002213
Olivier Houchard8da5f982017-08-04 18:35:36 +02002214 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002215 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002216 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002217 err_code |= ERR_ALERT | ERR_FATAL;
2218 goto out;
2219 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002220
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002221 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002222 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002223 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002224 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002225 } else {
2226 newsrv = &curproxy->defsrv;
2227 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002228 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002229 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002230 }
2231
2232 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002233 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002234 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2235 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002236 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002237 file, linenum, *err, newsrv->id);
2238 err_code |= ERR_ALERT | ERR_FATAL;
2239 goto out;
2240 }
2241 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002242 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002243 file, linenum, val, args[cur_arg], newsrv->id);
2244 err_code |= ERR_ALERT | ERR_FATAL;
2245 goto out;
2246 }
2247 newsrv->agent.inter = val;
2248 cur_arg += 2;
2249 }
Misiekea849332017-01-09 09:39:51 +01002250 else if (!strcmp(args[cur_arg], "agent-addr")) {
2251 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002252 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002253 goto out;
2254 }
2255
2256 cur_arg += 2;
2257 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002258 else if (!strcmp(args[cur_arg], "agent-port")) {
2259 global.maxsock++;
2260 newsrv->agent.port = atol(args[cur_arg + 1]);
2261 cur_arg += 2;
2262 }
James Brown55f9ff12015-10-21 18:19:05 -07002263 else if (!strcmp(args[cur_arg], "agent-send")) {
2264 global.maxsock++;
2265 free(newsrv->agent.send_string);
2266 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2267 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2268 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2269 cur_arg += 2;
2270 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002271 else if (!strcmp(args[cur_arg], "init-addr")) {
2272 char *p, *end;
2273 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002274 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002275
2276 newsrv->init_addr_methods = 0;
2277 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2278
2279 for (p = args[cur_arg + 1]; *p; p = end) {
2280 /* cut on next comma */
2281 for (end = p; *end && *end != ','; end++);
2282 if (*end)
2283 *(end++) = 0;
2284
Willy Tarreau4310d362016-11-02 15:05:56 +01002285 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002286 if (!strcmp(p, "libc")) {
2287 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2288 }
2289 else if (!strcmp(p, "last")) {
2290 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2291 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002292 else if (!strcmp(p, "none")) {
2293 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2294 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002295 else if (str2ip2(p, &sa, 0)) {
2296 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002297 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002298 file, linenum, args[cur_arg], p);
2299 err_code |= ERR_ALERT | ERR_FATAL;
2300 goto out;
2301 }
2302 newsrv->init_addr = sa;
2303 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2304 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002305 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002306 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 +02002307 file, linenum, args[cur_arg], p);
2308 err_code |= ERR_ALERT | ERR_FATAL;
2309 goto out;
2310 }
2311 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002312 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002313 file, linenum, args[cur_arg], p);
2314 err_code |= ERR_ALERT | ERR_FATAL;
2315 goto out;
2316 }
2317 }
2318 cur_arg += 2;
2319 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002320 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002321 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002322 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2323 cur_arg += 2;
2324 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002325 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2326 char *p, *end;
2327
2328 for (p = args[cur_arg + 1]; *p; p = end) {
2329 /* cut on next comma */
2330 for (end = p; *end && *end != ','; end++);
2331 if (*end)
2332 *(end++) = 0;
2333
2334 if (!strcmp(p, "allow-dup-ip")) {
2335 newsrv->dns_opts.accept_duplicate_ip = 1;
2336 }
2337 else if (!strcmp(p, "prevent-dup-ip")) {
2338 newsrv->dns_opts.accept_duplicate_ip = 0;
2339 }
2340 else {
2341 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2342 file, linenum, args[cur_arg], p);
2343 err_code |= ERR_ALERT | ERR_FATAL;
2344 goto out;
2345 }
2346 }
2347
2348 cur_arg += 2;
2349 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002350 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2351 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002352 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002353 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002354 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002355 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002356 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002357 file, linenum, args[cur_arg]);
2358 err_code |= ERR_ALERT | ERR_FATAL;
2359 goto out;
2360 }
2361 cur_arg += 2;
2362 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002363 else if (!strcmp(args[cur_arg], "resolve-net")) {
2364 char *p, *e;
2365 unsigned char mask;
2366 struct dns_options *opt;
2367
2368 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002369 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002370 file, linenum, args[cur_arg]);
2371 err_code |= ERR_ALERT | ERR_FATAL;
2372 goto out;
2373 }
2374
2375 opt = &newsrv->dns_opts;
2376
2377 /* Split arguments by comma, and convert it from ipv4 or ipv6
2378 * string network in in_addr or in6_addr.
2379 */
2380 p = args[cur_arg + 1];
2381 e = p;
2382 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002383 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002384 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002385 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002386 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2387 err_code |= ERR_ALERT | ERR_FATAL;
2388 goto out;
2389 }
2390 /* look for end or comma. */
2391 while (*e != ',' && *e != '\0')
2392 e++;
2393 if (*e == ',') {
2394 *e = '\0';
2395 e++;
2396 }
2397 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2398 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2399 /* Try to convert input string from ipv4 or ipv6 network. */
2400 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2401 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2402 &mask)) {
2403 /* Try to convert input string from ipv6 network. */
2404 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2405 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2406 } else {
2407 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002408 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002409 file, linenum, args[cur_arg], p);
2410 err_code |= ERR_ALERT | ERR_FATAL;
2411 goto out;
2412 }
2413 opt->pref_net_nb++;
2414 p = e;
2415 }
2416
2417 cur_arg += 2;
2418 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002419 else if (!strcmp(args[cur_arg], "rise")) {
2420 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002421 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002422 file, linenum, args[cur_arg]);
2423 err_code |= ERR_ALERT | ERR_FATAL;
2424 goto out;
2425 }
2426
2427 newsrv->check.rise = atol(args[cur_arg + 1]);
2428 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002429 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002430 file, linenum, args[cur_arg]);
2431 err_code |= ERR_ALERT | ERR_FATAL;
2432 goto out;
2433 }
2434
2435 if (newsrv->check.health)
2436 newsrv->check.health = newsrv->check.rise;
2437 cur_arg += 2;
2438 }
2439 else if (!strcmp(args[cur_arg], "fall")) {
2440 newsrv->check.fall = atol(args[cur_arg + 1]);
2441
2442 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002443 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002444 file, linenum, args[cur_arg]);
2445 err_code |= ERR_ALERT | ERR_FATAL;
2446 goto out;
2447 }
2448
2449 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002450 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002451 file, linenum, args[cur_arg]);
2452 err_code |= ERR_ALERT | ERR_FATAL;
2453 goto out;
2454 }
2455
2456 cur_arg += 2;
2457 }
2458 else if (!strcmp(args[cur_arg], "inter")) {
2459 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2460 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002461 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002462 file, linenum, *err, newsrv->id);
2463 err_code |= ERR_ALERT | ERR_FATAL;
2464 goto out;
2465 }
2466 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002467 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002468 file, linenum, val, args[cur_arg], newsrv->id);
2469 err_code |= ERR_ALERT | ERR_FATAL;
2470 goto out;
2471 }
2472 newsrv->check.inter = val;
2473 cur_arg += 2;
2474 }
2475 else if (!strcmp(args[cur_arg], "fastinter")) {
2476 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2477 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002478 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002479 file, linenum, *err, newsrv->id);
2480 err_code |= ERR_ALERT | ERR_FATAL;
2481 goto out;
2482 }
2483 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002484 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002485 file, linenum, val, args[cur_arg], newsrv->id);
2486 err_code |= ERR_ALERT | ERR_FATAL;
2487 goto out;
2488 }
2489 newsrv->check.fastinter = val;
2490 cur_arg += 2;
2491 }
2492 else if (!strcmp(args[cur_arg], "downinter")) {
2493 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2494 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002495 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002496 file, linenum, *err, newsrv->id);
2497 err_code |= ERR_ALERT | ERR_FATAL;
2498 goto out;
2499 }
2500 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002501 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002502 file, linenum, val, args[cur_arg], newsrv->id);
2503 err_code |= ERR_ALERT | ERR_FATAL;
2504 goto out;
2505 }
2506 newsrv->check.downinter = val;
2507 cur_arg += 2;
2508 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002509 else if (!strcmp(args[cur_arg], "port")) {
2510 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002511 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002512 cur_arg += 2;
2513 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002514 else if (!strcmp(args[cur_arg], "weight")) {
2515 int w;
2516 w = atol(args[cur_arg + 1]);
2517 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002518 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002519 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2520 err_code |= ERR_ALERT | ERR_FATAL;
2521 goto out;
2522 }
2523 newsrv->uweight = newsrv->iweight = w;
2524 cur_arg += 2;
2525 }
2526 else if (!strcmp(args[cur_arg], "minconn")) {
2527 newsrv->minconn = atol(args[cur_arg + 1]);
2528 cur_arg += 2;
2529 }
2530 else if (!strcmp(args[cur_arg], "maxconn")) {
2531 newsrv->maxconn = atol(args[cur_arg + 1]);
2532 cur_arg += 2;
2533 }
2534 else if (!strcmp(args[cur_arg], "maxqueue")) {
2535 newsrv->maxqueue = atol(args[cur_arg + 1]);
2536 cur_arg += 2;
2537 }
2538 else if (!strcmp(args[cur_arg], "slowstart")) {
2539 /* slowstart is stored in seconds */
2540 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2541 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002542 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002543 file, linenum, *err, newsrv->id);
2544 err_code |= ERR_ALERT | ERR_FATAL;
2545 goto out;
2546 }
2547 newsrv->slowstart = (val + 999) / 1000;
2548 cur_arg += 2;
2549 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002550 else if (!strcmp(args[cur_arg], "on-error")) {
2551 if (!strcmp(args[cur_arg + 1], "fastinter"))
2552 newsrv->onerror = HANA_ONERR_FASTINTER;
2553 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2554 newsrv->onerror = HANA_ONERR_FAILCHK;
2555 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2556 newsrv->onerror = HANA_ONERR_SUDDTH;
2557 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2558 newsrv->onerror = HANA_ONERR_MARKDWN;
2559 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002560 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002561 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2562 file, linenum, args[cur_arg], args[cur_arg + 1]);
2563 err_code |= ERR_ALERT | ERR_FATAL;
2564 goto out;
2565 }
2566
2567 cur_arg += 2;
2568 }
2569 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2570 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2571 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2572 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002573 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002574 file, linenum, args[cur_arg], args[cur_arg + 1]);
2575 err_code |= ERR_ALERT | ERR_FATAL;
2576 goto out;
2577 }
2578
2579 cur_arg += 2;
2580 }
2581 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2582 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2583 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2584 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002585 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002586 file, linenum, args[cur_arg], args[cur_arg + 1]);
2587 err_code |= ERR_ALERT | ERR_FATAL;
2588 goto out;
2589 }
2590
2591 cur_arg += 2;
2592 }
2593 else if (!strcmp(args[cur_arg], "error-limit")) {
2594 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002595 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002596 file, linenum, args[cur_arg]);
2597 err_code |= ERR_ALERT | ERR_FATAL;
2598 goto out;
2599 }
2600
2601 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2602
2603 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002604 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002605 file, linenum, args[cur_arg]);
2606 err_code |= ERR_ALERT | ERR_FATAL;
2607 goto out;
2608 }
2609 cur_arg += 2;
2610 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002611 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002612 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002613 file, linenum, "usesrc", "source");
2614 err_code |= ERR_ALERT | ERR_FATAL;
2615 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002616 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002617 else {
2618 static int srv_dumped;
2619 struct srv_kw *kw;
2620 char *err;
2621
2622 kw = srv_find_kw(args[cur_arg]);
2623 if (kw) {
2624 char *err = NULL;
2625 int code;
2626
2627 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002628 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 +02002629 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002630 if (kw->skip != -1)
2631 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002632 err_code |= ERR_ALERT | ERR_FATAL;
2633 goto out;
2634 }
2635
2636 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002637 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002638 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002639 if (kw->skip != -1)
2640 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002641 err_code |= ERR_ALERT;
2642 continue;
2643 }
2644
2645 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2646 err_code |= code;
2647
2648 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002649 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002650 if (code & ERR_FATAL) {
2651 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002652 if (kw->skip != -1)
2653 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002654 goto out;
2655 }
2656 }
2657 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002658 if (kw->skip != -1)
2659 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002660 continue;
2661 }
2662
2663 err = NULL;
2664 if (!srv_dumped) {
2665 srv_dump_kws(&err);
2666 indent_msg(&err, 4);
2667 srv_dumped = 1;
2668 }
2669
Christopher Faulet767a84b2017-11-24 16:50:31 +01002670 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002671 file, linenum, args[0], args[1], args[cur_arg],
2672 err ? " Registered keywords :" : "", err ? err : "");
2673 free(err);
2674
2675 err_code |= ERR_ALERT | ERR_FATAL;
2676 goto out;
2677 }
2678 }
2679
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002680 if (!defsrv)
2681 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2682 if (err_code & ERR_FATAL)
2683 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002684 if (srv_tmpl)
2685 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002686 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002687 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002688 return 0;
2689
2690 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002691 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002692 free(errmsg);
2693 return err_code;
2694}
2695
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002696/* Returns a pointer to the first server matching either id <id>.
2697 * NULL is returned if no match is found.
2698 * the lookup is performed in the backend <bk>
2699 */
2700struct server *server_find_by_id(struct proxy *bk, int id)
2701{
2702 struct eb32_node *eb32;
2703 struct server *curserver;
2704
2705 if (!bk || (id ==0))
2706 return NULL;
2707
2708 /* <bk> has no backend capabilities, so it can't have a server */
2709 if (!(bk->cap & PR_CAP_BE))
2710 return NULL;
2711
2712 curserver = NULL;
2713
2714 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2715 if (eb32)
2716 curserver = container_of(eb32, struct server, conf.id);
2717
2718 return curserver;
2719}
2720
2721/* Returns a pointer to the first server matching either name <name>, or id
2722 * if <name> starts with a '#'. NULL is returned if no match is found.
2723 * the lookup is performed in the backend <bk>
2724 */
2725struct server *server_find_by_name(struct proxy *bk, const char *name)
2726{
2727 struct server *curserver;
2728
2729 if (!bk || !name)
2730 return NULL;
2731
2732 /* <bk> has no backend capabilities, so it can't have a server */
2733 if (!(bk->cap & PR_CAP_BE))
2734 return NULL;
2735
2736 curserver = NULL;
2737 if (*name == '#') {
2738 curserver = server_find_by_id(bk, atoi(name + 1));
2739 if (curserver)
2740 return curserver;
2741 }
2742 else {
2743 curserver = bk->srv;
2744
2745 while (curserver && (strcmp(curserver->id, name) != 0))
2746 curserver = curserver->next;
2747
2748 if (curserver)
2749 return curserver;
2750 }
2751
2752 return NULL;
2753}
2754
2755struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2756{
2757 struct server *byname;
2758 struct server *byid;
2759
2760 if (!name && !id)
2761 return NULL;
2762
2763 if (diff)
2764 *diff = 0;
2765
2766 byname = byid = NULL;
2767
2768 if (name) {
2769 byname = server_find_by_name(bk, name);
2770 if (byname && (!id || byname->puid == id))
2771 return byname;
2772 }
2773
2774 /* remaining possibilities :
2775 * - name not set
2776 * - name set but not found
2777 * - name found but ID doesn't match
2778 */
2779 if (id) {
2780 byid = server_find_by_id(bk, id);
2781 if (byid) {
2782 if (byname) {
2783 /* use id only if forced by configuration */
2784 if (byid->flags & SRV_F_FORCED_ID) {
2785 if (diff)
2786 *diff |= 2;
2787 return byid;
2788 }
2789 else {
2790 if (diff)
2791 *diff |= 1;
2792 return byname;
2793 }
2794 }
2795
2796 /* remaining possibilities:
2797 * - name not set
2798 * - name set but not found
2799 */
2800 if (name && diff)
2801 *diff |= 2;
2802 return byid;
2803 }
2804
2805 /* id bot found */
2806 if (byname) {
2807 if (diff)
2808 *diff |= 1;
2809 return byname;
2810 }
2811 }
2812
2813 return NULL;
2814}
2815
Willy Tarreau46b7f532018-08-21 11:54:26 +02002816/* Update a server state using the parameters available in the params list.
2817 *
2818 * Grabs the server lock during operation.
2819 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002820static void srv_update_state(struct server *srv, int version, char **params)
2821{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002822 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002823 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002824
2825 /* fields since version 1
2826 * and common to all other upcoming versions
2827 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002828 enum srv_state srv_op_state;
2829 enum srv_admin srv_admin_state;
2830 unsigned srv_uweight, srv_iweight;
2831 unsigned long srv_last_time_change;
2832 short srv_check_status;
2833 enum chk_result srv_check_result;
2834 int srv_check_health;
2835 int srv_check_state, srv_agent_state;
2836 int bk_f_forced_id;
2837 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002838 int fqdn_set_by_cli;
2839 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002840 const char *port_str;
2841 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002842 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002843
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002844 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002845 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002846 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002847 switch (version) {
2848 case 1:
2849 /*
2850 * now we can proceed with server's state update:
2851 * srv_addr: params[0]
2852 * srv_op_state: params[1]
2853 * srv_admin_state: params[2]
2854 * srv_uweight: params[3]
2855 * srv_iweight: params[4]
2856 * srv_last_time_change: params[5]
2857 * srv_check_status: params[6]
2858 * srv_check_result: params[7]
2859 * srv_check_health: params[8]
2860 * srv_check_state: params[9]
2861 * srv_agent_state: params[10]
2862 * bk_f_forced_id: params[11]
2863 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002864 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002865 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002866 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002867 */
2868
2869 /* validating srv_op_state */
2870 p = NULL;
2871 errno = 0;
2872 srv_op_state = strtol(params[1], &p, 10);
2873 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2874 (srv_op_state != SRV_ST_STOPPED &&
2875 srv_op_state != SRV_ST_STARTING &&
2876 srv_op_state != SRV_ST_RUNNING &&
2877 srv_op_state != SRV_ST_STOPPING)) {
2878 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2879 }
2880
2881 /* validating srv_admin_state */
2882 p = NULL;
2883 errno = 0;
2884 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002885 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002886
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002887 /* inherited statuses will be recomputed later.
2888 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2889 */
2890 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002891
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002892 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2893 (srv_admin_state != 0 &&
2894 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002895 srv_admin_state != SRV_ADMF_CMAINT &&
2896 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002897 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002898 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002899 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2900 }
2901
2902 /* validating srv_uweight */
2903 p = NULL;
2904 errno = 0;
2905 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002906 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002907 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2908
2909 /* validating srv_iweight */
2910 p = NULL;
2911 errno = 0;
2912 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002913 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002914 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2915
2916 /* validating srv_last_time_change */
2917 p = NULL;
2918 errno = 0;
2919 srv_last_time_change = strtol(params[5], &p, 10);
2920 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2921 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2922
2923 /* validating srv_check_status */
2924 p = NULL;
2925 errno = 0;
2926 srv_check_status = strtol(params[6], &p, 10);
2927 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2928 (srv_check_status >= HCHK_STATUS_SIZE))
2929 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2930
2931 /* validating srv_check_result */
2932 p = NULL;
2933 errno = 0;
2934 srv_check_result = strtol(params[7], &p, 10);
2935 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2936 (srv_check_result != CHK_RES_UNKNOWN &&
2937 srv_check_result != CHK_RES_NEUTRAL &&
2938 srv_check_result != CHK_RES_FAILED &&
2939 srv_check_result != CHK_RES_PASSED &&
2940 srv_check_result != CHK_RES_CONDPASS)) {
2941 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2942 }
2943
2944 /* validating srv_check_health */
2945 p = NULL;
2946 errno = 0;
2947 srv_check_health = strtol(params[8], &p, 10);
2948 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2949 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2950
2951 /* validating srv_check_state */
2952 p = NULL;
2953 errno = 0;
2954 srv_check_state = strtol(params[9], &p, 10);
2955 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2956 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2957 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2958
2959 /* validating srv_agent_state */
2960 p = NULL;
2961 errno = 0;
2962 srv_agent_state = strtol(params[10], &p, 10);
2963 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2964 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2965 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2966
2967 /* validating bk_f_forced_id */
2968 p = NULL;
2969 errno = 0;
2970 bk_f_forced_id = strtol(params[11], &p, 10);
2971 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2972 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2973
2974 /* validating srv_f_forced_id */
2975 p = NULL;
2976 errno = 0;
2977 srv_f_forced_id = strtol(params[12], &p, 10);
2978 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2979 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2980
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002981 /* validating srv_fqdn */
2982 fqdn = params[13];
2983 if (fqdn && *fqdn == '-')
2984 fqdn = NULL;
2985 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2986 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2987 fqdn = NULL;
2988 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002989
Frédéric Lécaille31694712017-08-01 08:47:19 +02002990 port_str = params[14];
2991 if (port_str) {
2992 port = strl2uic(port_str, strlen(port_str));
2993 if (port > USHRT_MAX) {
2994 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2995 port_str = NULL;
2996 }
2997 }
2998
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002999 /* SRV record
3000 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3001 */
3002 srvrecord = params[15];
3003 if (srvrecord && *srvrecord != '_')
3004 srvrecord = NULL;
3005
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003006 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003007 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003008 goto out;
3009
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003010 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003011 /* recover operational state and apply it to this server
3012 * and all servers tracking this one */
3013 switch (srv_op_state) {
3014 case SRV_ST_STOPPED:
3015 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003016 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003017 break;
3018 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02003019 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003020 break;
3021 case SRV_ST_STOPPING:
3022 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003023 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003024 break;
3025 case SRV_ST_RUNNING:
3026 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003027 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003028 break;
3029 }
3030
3031 /* When applying server state, the following rules apply:
3032 * - in case of a configuration change, we apply the setting from the new
3033 * configuration, regardless of old running state
3034 * - if no configuration change, we apply old running state only if old running
3035 * state is different from new configuration state
3036 */
3037 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003038 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3039 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003040 srv_adm_set_maint(srv);
3041 else
3042 srv_adm_set_ready(srv);
3043 }
3044 /* configuration is the same, let's compate old running state and new conf state */
3045 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003046 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003047 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003048 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003049 srv_adm_set_ready(srv);
3050 }
3051 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003052 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003053 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003054 * (srv->iweight is the weight set up in configuration).
3055 * There are two possible reasons for FDRAIN to have been present :
3056 * - previous config weight was zero
3057 * - "set server b/s drain" was sent to the CLI
3058 *
3059 * In the first case, we simply want to drop this drain state
3060 * if the new weight is not zero anymore, meaning the administrator
3061 * has intentionally turned the weight back to a positive value to
3062 * enable the server again after an operation. In the second case,
3063 * the drain state was forced on the CLI regardless of the config's
3064 * weight so we don't want a change to the config weight to lose this
3065 * status. What this means is :
3066 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3067 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003068 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003069 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003070 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003071 }
3072
3073 srv->last_change = date.tv_sec - srv_last_time_change;
3074 srv->check.status = srv_check_status;
3075 srv->check.result = srv_check_result;
3076 srv->check.health = srv_check_health;
3077
3078 /* Only case we want to apply is removing ENABLED flag which could have been
3079 * done by the "disable health" command over the stats socket
3080 */
3081 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3082 (srv_check_state & CHK_ST_CONFIGURED) &&
3083 !(srv_check_state & CHK_ST_ENABLED))
3084 srv->check.state &= ~CHK_ST_ENABLED;
3085
3086 /* Only case we want to apply is removing ENABLED flag which could have been
3087 * done by the "disable agent" command over the stats socket
3088 */
3089 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3090 (srv_agent_state & CHK_ST_CONFIGURED) &&
3091 !(srv_agent_state & CHK_ST_ENABLED))
3092 srv->agent.state &= ~CHK_ST_ENABLED;
3093
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003094 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3095 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003096 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003097 * It means that a configuration file change has precedence over a unix socket change
3098 * for server's weight
3099 *
3100 * by default, HAProxy applies the following weight when parsing the configuration
3101 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003102 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003103 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003104 srv->uweight = srv_uweight;
3105 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003106 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003107
Willy Tarreaue5a60682016-11-09 14:54:53 +01003108 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003109 if (strcmp(params[0], "-"))
3110 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003111
3112 if (fqdn && srv->hostname) {
3113 if (!strcmp(srv->hostname, fqdn)) {
3114 /* Here we reset the 'set from stats socket FQDN' flag
3115 * to support such transitions:
3116 * Let's say initial FQDN value is foo1 (in configuration file).
3117 * - FQDN changed from stats socket, from foo1 to foo2 value,
3118 * - FQDN changed again from file configuration (with the same previous value
3119 set from stats socket, from foo1 to foo2 value),
3120 * - reload for any other reason than a FQDN modification,
3121 * the configuration file FQDN matches the fqdn server state file value.
3122 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003123 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003124 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003125 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003126 }
3127 else {
3128 /* If the FDQN has been changed from stats socket,
3129 * apply fqdn state file value (which is the value set
3130 * from stats socket).
3131 */
3132 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003133 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003134 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003135 }
3136 }
3137 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003138 /* If all the conditions below are validated, this means
3139 * we're evaluating a server managed by SRV resolution
3140 */
3141 else if (fqdn && !srv->hostname && srvrecord) {
3142 int res;
3143
3144 /* we can't apply previous state if SRV record has changed */
3145 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3146 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);
3147 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3148 goto out;
3149 }
3150
3151 /* create or find a SRV resolution for this srv record */
3152 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3153 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3154 if (srv->srvrq == NULL) {
3155 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3156 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3157 goto out;
3158 }
3159
3160 /* prepare DNS resolution for this server */
3161 res = srv_prepare_for_resolution(srv, fqdn);
3162 if (res == -1) {
3163 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3164 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3165 goto out;
3166 }
3167
3168 /* configure check.port accordingly */
3169 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3170 !(srv->flags & SRV_F_CHECKPORT))
3171 srv->check.port = port;
3172
3173 /* Unset SRV_F_MAPPORTS for SRV records.
3174 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3175 * because no ports are provided in the configuration file.
3176 * This is because HAProxy will use the port found into the SRV record.
3177 */
3178 srv->flags &= ~SRV_F_MAPPORTS;
3179 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003180
Frédéric Lécaille31694712017-08-01 08:47:19 +02003181 if (port_str)
3182 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003183 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003184
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003185 break;
3186 default:
3187 chunk_appendf(msg, ", version '%d' not supported", version);
3188 }
3189
3190 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003191 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003192 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003193 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003194 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003195 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003196}
3197
3198/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3199 * For each proxy, it does the following:
3200 * - opens its server state file (either one or local one)
3201 * - read whole file, line by line
3202 * - analyse each line to check if it matches our current backend:
3203 * - backend name matches
3204 * - backend id matches if id is forced and name doesn't match
3205 * - if the server pointed by the line is found, then state is applied
3206 *
3207 * If the running backend uuid or id differs from the state file, then HAProxy reports
3208 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003209 *
3210 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003211 */
3212void apply_server_state(void)
3213{
3214 char *cur, *end;
3215 char mybuf[SRV_STATE_LINE_MAXLEN];
3216 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003217 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3218 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003219 int arg, srv_arg, version, diff;
3220 FILE *f;
3221 char *filepath;
3222 char globalfilepath[MAXPATHLEN + 1];
3223 char localfilepath[MAXPATHLEN + 1];
3224 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003225 struct proxy *curproxy, *bk;
3226 struct server *srv;
3227
3228 globalfilepathlen = 0;
3229 /* create the globalfilepath variable */
3230 if (global.server_state_file) {
3231 /* absolute path or no base directory provided */
3232 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3233 len = strlen(global.server_state_file);
3234 if (len > MAXPATHLEN) {
3235 globalfilepathlen = 0;
3236 goto globalfileerror;
3237 }
3238 memcpy(globalfilepath, global.server_state_file, len);
3239 globalfilepath[len] = '\0';
3240 globalfilepathlen = len;
3241 }
3242 else if (global.server_state_base) {
3243 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003244 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003245 globalfilepathlen = 0;
3246 goto globalfileerror;
3247 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003248 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003249 globalfilepath[len] = 0;
3250 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003251
3252 /* append a slash if needed */
3253 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3254 if (globalfilepathlen + 1 > MAXPATHLEN) {
3255 globalfilepathlen = 0;
3256 goto globalfileerror;
3257 }
3258 globalfilepath[globalfilepathlen++] = '/';
3259 }
3260
3261 len = strlen(global.server_state_file);
3262 if (globalfilepathlen + len > MAXPATHLEN) {
3263 globalfilepathlen = 0;
3264 goto globalfileerror;
3265 }
3266 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3267 globalfilepathlen += len;
3268 globalfilepath[globalfilepathlen++] = 0;
3269 }
3270 }
3271 globalfileerror:
3272 if (globalfilepathlen == 0)
3273 globalfilepath[0] = '\0';
3274
3275 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003276 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003277 /* servers are only in backends */
3278 if (!(curproxy->cap & PR_CAP_BE))
3279 continue;
3280 fileopenerr = 0;
3281 filepath = NULL;
3282
3283 /* search server state file path and name */
3284 switch (curproxy->load_server_state_from_file) {
3285 /* read servers state from global file */
3286 case PR_SRV_STATE_FILE_GLOBAL:
3287 /* there was an error while generating global server state file path */
3288 if (globalfilepathlen == 0)
3289 continue;
3290 filepath = globalfilepath;
3291 fileopenerr = 1;
3292 break;
3293 /* this backend has its own file */
3294 case PR_SRV_STATE_FILE_LOCAL:
3295 localfilepathlen = 0;
3296 localfilepath[0] = '\0';
3297 len = 0;
3298 /* create the localfilepath variable */
3299 /* absolute path or no base directory provided */
3300 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3301 len = strlen(curproxy->server_state_file_name);
3302 if (len > MAXPATHLEN) {
3303 localfilepathlen = 0;
3304 goto localfileerror;
3305 }
3306 memcpy(localfilepath, curproxy->server_state_file_name, len);
3307 localfilepath[len] = '\0';
3308 localfilepathlen = len;
3309 }
3310 else if (global.server_state_base) {
3311 len = strlen(global.server_state_base);
3312 localfilepathlen += len;
3313
3314 if (localfilepathlen > MAXPATHLEN) {
3315 localfilepathlen = 0;
3316 goto localfileerror;
3317 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003318 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003319 localfilepath[localfilepathlen] = 0;
3320
3321 /* append a slash if needed */
3322 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3323 if (localfilepathlen + 1 > MAXPATHLEN) {
3324 localfilepathlen = 0;
3325 goto localfileerror;
3326 }
3327 localfilepath[localfilepathlen++] = '/';
3328 }
3329
3330 len = strlen(curproxy->server_state_file_name);
3331 if (localfilepathlen + len > MAXPATHLEN) {
3332 localfilepathlen = 0;
3333 goto localfileerror;
3334 }
3335 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3336 localfilepathlen += len;
3337 localfilepath[localfilepathlen++] = 0;
3338 }
3339 filepath = localfilepath;
3340 localfileerror:
3341 if (localfilepathlen == 0)
3342 localfilepath[0] = '\0';
3343
3344 break;
3345 case PR_SRV_STATE_FILE_NONE:
3346 default:
3347 continue;
3348 }
3349
3350 /* preload global state file */
3351 errno = 0;
3352 f = fopen(filepath, "r");
3353 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003354 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003355 if (!f)
3356 continue;
3357
3358 mybuf[0] = '\0';
3359 mybuflen = 0;
3360 version = 0;
3361
3362 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003363 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003364 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003365 goto fileclose;
3366 }
3367
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003368 cur = mybuf;
3369 version = atoi(cur);
3370 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3371 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003372 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003373
3374 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3375 int bk_f_forced_id = 0;
3376 int check_id = 0;
3377 int check_name = 0;
3378
3379 mybuflen = strlen(mybuf);
3380 cur = mybuf;
3381 end = cur + mybuflen;
3382
3383 bk = NULL;
3384 srv = NULL;
3385
3386 /* we need at least one character */
3387 if (mybuflen == 0)
3388 continue;
3389
3390 /* ignore blank characters at the beginning of the line */
3391 while (isspace(*cur))
3392 ++cur;
3393
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003394 /* Ignore empty or commented lines */
3395 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003396 continue;
3397
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003398 /* truncated lines */
3399 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003400 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003401 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003402 }
3403
3404 /* Removes trailing '\n' */
3405 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003406
3407 /* we're now ready to move the line into *srv_params[] */
3408 params[0] = cur;
3409 arg = 1;
3410 srv_arg = 0;
3411 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3412 if (isspace(*cur)) {
3413 *cur = '\0';
3414 ++cur;
3415 while (isspace(*cur))
3416 ++cur;
3417 switch (version) {
3418 case 1:
3419 /*
3420 * srv_addr: params[4] => srv_params[0]
3421 * srv_op_state: params[5] => srv_params[1]
3422 * srv_admin_state: params[6] => srv_params[2]
3423 * srv_uweight: params[7] => srv_params[3]
3424 * srv_iweight: params[8] => srv_params[4]
3425 * srv_last_time_change: params[9] => srv_params[5]
3426 * srv_check_status: params[10] => srv_params[6]
3427 * srv_check_result: params[11] => srv_params[7]
3428 * srv_check_health: params[12] => srv_params[8]
3429 * srv_check_state: params[13] => srv_params[9]
3430 * srv_agent_state: params[14] => srv_params[10]
3431 * bk_f_forced_id: params[15] => srv_params[11]
3432 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003433 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003434 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003435 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003436 */
3437 if (arg >= 4) {
3438 srv_params[srv_arg] = cur;
3439 ++srv_arg;
3440 }
3441 break;
3442 }
3443
3444 params[arg] = cur;
3445 ++arg;
3446 }
3447 else {
3448 ++cur;
3449 }
3450 }
3451
3452 /* if line is incomplete line, then ignore it.
3453 * otherwise, update useful flags */
3454 switch (version) {
3455 case 1:
3456 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3457 continue;
3458 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3459 check_id = (atoi(params[0]) == curproxy->uuid);
3460 check_name = (strcmp(curproxy->id, params[1]) == 0);
3461 break;
3462 }
3463
3464 diff = 0;
3465 bk = curproxy;
3466
3467 /* if backend can't be found, let's continue */
3468 if (!check_id && !check_name)
3469 continue;
3470 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003471 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 +02003472 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3473 }
3474 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003475 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 +02003476 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3477 /* if name doesn't match, we still want to update curproxy if the backend id
3478 * was forced in previous the previous configuration */
3479 if (!bk_f_forced_id)
3480 continue;
3481 }
3482
3483 /* look for the server by its id: param[2] */
3484 /* else look for the server by its name: param[3] */
3485 diff = 0;
3486 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3487
3488 if (!srv) {
3489 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003490 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3491 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003492 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3493 params[3], params[2], params[0], params[1]);
3494 continue;
3495 }
3496 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003497 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 +02003498 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 +02003499 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003500 }
3501 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003502 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 +02003503 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 +02003504 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003505 }
3506
3507 /* now we can proceed with server's state update */
3508 srv_update_state(srv, version, srv_params);
3509 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003510fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003511 fclose(f);
3512 }
3513}
3514
Simon Horman7d09b9a2013-02-12 10:45:51 +09003515/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003516 * update a server's current IP address.
3517 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3518 * ip is in network format.
3519 * updater is a string which contains an information about the requester of the update.
3520 * updater is used if not NULL.
3521 *
3522 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003523 *
3524 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003525 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003526int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003527{
3528 /* generates a log line and a warning on stderr */
3529 if (1) {
3530 /* book enough space for both IPv4 and IPv6 */
3531 char oldip[INET6_ADDRSTRLEN];
3532 char newip[INET6_ADDRSTRLEN];
3533
3534 memset(oldip, '\0', INET6_ADDRSTRLEN);
3535 memset(newip, '\0', INET6_ADDRSTRLEN);
3536
3537 /* copy old IP address in a string */
3538 switch (s->addr.ss_family) {
3539 case AF_INET:
3540 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3541 break;
3542 case AF_INET6:
3543 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3544 break;
3545 };
3546
3547 /* copy new IP address in a string */
3548 switch (ip_sin_family) {
3549 case AF_INET:
3550 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3551 break;
3552 case AF_INET6:
3553 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3554 break;
3555 };
3556
3557 /* save log line into a buffer */
3558 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3559 s->proxy->id, s->id, oldip, newip, updater);
3560
3561 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003562 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003563
3564 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003565 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003566 }
3567
3568 /* save the new IP family */
3569 s->addr.ss_family = ip_sin_family;
3570 /* save the new IP address */
3571 switch (ip_sin_family) {
3572 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003573 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003574 break;
3575 case AF_INET6:
3576 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3577 break;
3578 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003579 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003580
3581 return 0;
3582}
3583
3584/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003585 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3586 *
3587 * Caller can pass its name through <updater> to get it integrated in the response message
3588 * returned by the function.
3589 *
3590 * The function first does the following, in that order:
3591 * - validates the new addr and/or port
3592 * - checks if an update is required (new IP or port is different than current ones)
3593 * - checks the update is allowed:
3594 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3595 * - allow all changes if no CHECKS are configured
3596 * - if CHECK is configured:
3597 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3598 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3599 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003600 *
3601 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003602 */
3603const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3604{
3605 struct sockaddr_storage sa;
3606 int ret, port_change_required;
3607 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003608 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003609 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003610 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003611
3612 msg = get_trash_chunk();
3613 chunk_reset(msg);
3614
3615 if (addr) {
3616 memset(&sa, 0, sizeof(struct sockaddr_storage));
3617 if (str2ip2(addr, &sa, 0) == NULL) {
3618 chunk_printf(msg, "Invalid addr '%s'", addr);
3619 goto out;
3620 }
3621
3622 /* changes are allowed on AF_INET* families only */
3623 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3624 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3625 goto out;
3626 }
3627
3628 /* collecting data currently setup */
3629 memset(current_addr, '\0', sizeof(current_addr));
3630 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3631 /* changes are allowed on AF_INET* families only */
3632 if ((ret != AF_INET) && (ret != AF_INET6)) {
3633 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3634 goto out;
3635 }
3636
3637 /* applying ADDR changes if required and allowed
3638 * ipcmp returns 0 when both ADDR are the same
3639 */
3640 if (ipcmp(&s->addr, &sa) == 0) {
3641 chunk_appendf(msg, "no need to change the addr");
3642 goto port;
3643 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003644 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003645 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003646
3647 /* we also need to update check's ADDR only if it uses the server's one */
3648 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003649 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003650 }
3651
3652 /* we also need to update agent ADDR only if it use the server's one */
3653 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003654 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003655 }
3656
3657 /* update report for caller */
3658 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3659 }
3660
3661 port:
3662 if (port) {
3663 char sign = '\0';
3664 char *endptr;
3665
3666 if (addr)
3667 chunk_appendf(msg, ", ");
3668
3669 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003670 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003671
3672 /* check if PORT change is required */
3673 port_change_required = 0;
3674
3675 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003676 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003677 new_port = strtol(port, &endptr, 10);
3678 if ((errno != 0) || (port == endptr)) {
3679 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3680 goto out;
3681 }
3682
3683 /* check if caller triggers a port mapped or offset */
3684 if (sign == '-' || (sign == '+')) {
3685 /* check if server currently uses port map */
3686 if (!(s->flags & SRV_F_MAPPORTS)) {
3687 /* switch from fixed port to port map mandatorily triggers
3688 * a port change */
3689 port_change_required = 1;
3690 /* check is configured
3691 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3692 * prevent PORT change if check doesn't have it's dedicated port while switching
3693 * to port mapping */
3694 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3695 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.");
3696 goto out;
3697 }
3698 }
3699 /* we're already using port maps */
3700 else {
3701 port_change_required = current_port != new_port;
3702 }
3703 }
3704 /* fixed port */
3705 else {
3706 port_change_required = current_port != new_port;
3707 }
3708
3709 /* applying PORT changes if required and update response message */
3710 if (port_change_required) {
3711 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003712 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003713 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003714
3715 /* prepare message */
3716 chunk_appendf(msg, "port changed from '");
3717 if (s->flags & SRV_F_MAPPORTS)
3718 chunk_appendf(msg, "+");
3719 chunk_appendf(msg, "%d' to '", current_port);
3720
3721 if (sign == '-') {
3722 s->flags |= SRV_F_MAPPORTS;
3723 chunk_appendf(msg, "%c", sign);
3724 /* just use for result output */
3725 new_port = -new_port;
3726 }
3727 else if (sign == '+') {
3728 s->flags |= SRV_F_MAPPORTS;
3729 chunk_appendf(msg, "%c", sign);
3730 }
3731 else {
3732 s->flags &= ~SRV_F_MAPPORTS;
3733 }
3734
3735 chunk_appendf(msg, "%d'", new_port);
3736
3737 /* we also need to update health checks port only if it uses server's realport */
3738 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3739 s->check.port = new_port;
3740 }
3741 }
3742 else {
3743 chunk_appendf(msg, "no need to change the port");
3744 }
3745 }
3746
3747out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003748 if (changed)
3749 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003750 if (updater)
3751 chunk_appendf(msg, " by '%s'", updater);
3752 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003753 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003754}
3755
3756
3757/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003758 * update server status based on result of name resolution
3759 * returns:
3760 * 0 if server status is updated
3761 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003762 *
3763 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003764 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003765int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003766{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003767 struct dns_resolvers *resolvers = s->resolvers;
3768 struct dns_resolution *resolution = s->dns_requester->resolution;
3769 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003770
3771 switch (resolution->status) {
3772 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003773 /* status when HAProxy has just (re)started.
3774 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003775 break;
3776
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003777 case RSLV_STATUS_VALID:
3778 /*
3779 * resume health checks
3780 * server will be turned back on if health check is safe
3781 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003782 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003783 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003784 return 1;
3785 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3786 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003787 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003788 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003789
Emeric Brun52a91d32017-08-31 14:41:55 +02003790 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003791 return 1;
3792 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3793 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3794 s->proxy->id, s->id);
3795
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003796 ha_warning("%s.\n", trash.area);
3797 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003798 return 0;
3799
3800 case RSLV_STATUS_NX:
3801 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003802 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3803 if (!tick_is_expired(exp, now_ms))
3804 break;
3805
3806 if (s->next_admin & SRV_ADMF_RMAINT)
3807 return 1;
3808 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3809 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003810
3811 case RSLV_STATUS_TIMEOUT:
3812 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003813 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3814 if (!tick_is_expired(exp, now_ms))
3815 break;
3816
3817 if (s->next_admin & SRV_ADMF_RMAINT)
3818 return 1;
3819 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3820 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003821
3822 case RSLV_STATUS_REFUSED:
3823 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003824 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3825 if (!tick_is_expired(exp, now_ms))
3826 break;
3827
3828 if (s->next_admin & SRV_ADMF_RMAINT)
3829 return 1;
3830 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3831 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003832
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003833 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003834 /* stop server if resolution failed for a long enough period */
3835 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3836 if (!tick_is_expired(exp, now_ms))
3837 break;
3838
3839 if (s->next_admin & SRV_ADMF_RMAINT)
3840 return 1;
3841 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3842 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003843 }
3844
3845 return 1;
3846}
3847
3848/*
3849 * Server Name Resolution valid response callback
3850 * It expects:
3851 * - <nameserver>: the name server which answered the valid response
3852 * - <response>: buffer containing a valid DNS response
3853 * - <response_len>: size of <response>
3854 * It performs the following actions:
3855 * - ignore response if current ip found and server family not met
3856 * - update with first new ip found if family is met and current IP is not found
3857 * returns:
3858 * 0 on error
3859 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003860 *
3861 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003862 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003863int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003864{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003865 struct server *s = NULL;
3866 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003867 void *serverip, *firstip;
3868 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003869 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003870 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003871 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003872
Christopher Faulet67957bd2017-09-27 11:00:59 +02003873 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003874 if (!s)
3875 return 1;
3876
Christopher Faulet67957bd2017-09-27 11:00:59 +02003877 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003878
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003879 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003880 firstip = NULL; /* pointer to the first valid response found */
3881 /* it will be used as the new IP if a change is required */
3882 firstip_sin_family = AF_UNSPEC;
3883 serverip = NULL; /* current server IP address */
3884
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003885 /* initializing server IP pointer */
3886 server_sin_family = s->addr.ss_family;
3887 switch (server_sin_family) {
3888 case AF_INET:
3889 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3890 break;
3891
3892 case AF_INET6:
3893 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3894 break;
3895
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003896 case AF_UNSPEC:
3897 break;
3898
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003899 default:
3900 goto invalid;
3901 }
3902
Baptiste Assmann729c9012017-05-22 15:13:10 +02003903 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003904 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003905 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003906
3907 switch (ret) {
3908 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003909 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003910
3911 case DNS_UPD_SRVIP_NOT_FOUND:
3912 goto save_ip;
3913
3914 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003915 goto invalid;
3916
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003917 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003918 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003919 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003920
Baptiste Assmannfad03182015-10-28 02:03:32 +01003921 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003922 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003923 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003924 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003925
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003926 default:
3927 goto invalid;
3928
3929 }
3930
3931 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003932 if (nameserver) {
3933 nameserver->counters.update++;
3934 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003935 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003936 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003937 else
3938 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003939 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003940
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003941 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003942 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003943 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003944
3945 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003946 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003947 nameserver->counters.invalid++;
3948 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003949 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003950 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003951 return 0;
3952}
3953
3954/*
3955 * Server Name Resolution error management callback
3956 * returns:
3957 * 0 on error
3958 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003959 *
3960 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003961 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003962int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003963{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003964 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003965
Christopher Faulet67957bd2017-09-27 11:00:59 +02003966 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003967 if (!s)
3968 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003969 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003970 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003971 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003972 return 1;
3973}
3974
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003975/*
3976 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003977 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003978 * It returns a pointer to the first server found or NULL if <ip> is not yet
3979 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003980 *
3981 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003982 */
3983struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3984{
3985 struct server *tmpsrv;
3986 struct proxy *be;
3987
3988 if (!srv)
3989 return NULL;
3990
3991 be = srv->proxy;
3992 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003993 /* we found the current server is the same, ignore it */
3994 if (srv == tmpsrv)
3995 continue;
3996
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003997 /* We want to compare the IP in the record with the IP of the servers in the
3998 * same backend, only if:
3999 * * DNS resolution is enabled on the server
4000 * * the hostname used for the resolution by our server is the same than the
4001 * one used for the server found in the backend
4002 * * the server found in the backend is not our current server
4003 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004004 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004005 if ((tmpsrv->hostname_dn == NULL) ||
4006 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4007 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004008 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004009 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004010 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004011 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004012
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004013 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004014 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004015 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004016 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004017 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004018
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004019 /* At this point, we have 2 different servers using the same DNS hostname
4020 * for their respective resolution.
4021 */
4022 if (*ip_family == tmpsrv->addr.ss_family &&
4023 ((tmpsrv->addr.ss_family == AF_INET &&
4024 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4025 (tmpsrv->addr.ss_family == AF_INET6 &&
4026 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004027 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004028 return tmpsrv;
4029 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004030 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004031 }
4032
Emeric Brune9fd6b52017-11-02 17:20:39 +01004033
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004034 return NULL;
4035}
4036
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004037/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4038 * resolver. This is suited for initial address configuration. Returns 0 on
4039 * success otherwise a non-zero error code. In case of error, *err_code, if
4040 * not NULL, is filled up.
4041 */
4042int srv_set_addr_via_libc(struct server *srv, int *err_code)
4043{
4044 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004045 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004046 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004047 return 1;
4048 }
4049 return 0;
4050}
4051
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004052/* Set the server's FDQN (->hostname) from <hostname>.
4053 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004054 *
4055 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004056 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004057int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004058{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004059 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004060 char *hostname_dn;
4061 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004062
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004063 /* Note that the server lock is already held. */
4064 if (!srv->resolvers)
4065 return -1;
4066
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004067 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004068 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004069 /* run time DNS resolution was not active for this server
4070 * and we can't enable it at run time for now.
4071 */
4072 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004073 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004074
4075 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004076 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004077 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004078 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4079 hostname_dn, trash.size);
4080 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004081 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004082
Christopher Faulet67957bd2017-09-27 11:00:59 +02004083 resolution = srv->dns_requester->resolution;
4084 if (resolution &&
4085 resolution->hostname_dn &&
4086 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004087 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004088
Christopher Faulet67957bd2017-09-27 11:00:59 +02004089 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004090
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004091 free(srv->hostname);
4092 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004093 srv->hostname = strdup(hostname);
4094 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004095 srv->hostname_dn_len = hostname_dn_len;
4096 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004097 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004098
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004099 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004100 goto err;
4101
4102 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004103 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004104 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004105 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004106
4107 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004108 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004109 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004110 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004111}
4112
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004113/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4114 * from the state file. This is suited for initial address configuration.
4115 * Returns 0 on success otherwise a non-zero error code. In case of error,
4116 * *err_code, if not NULL, is filled up.
4117 */
4118static int srv_apply_lastaddr(struct server *srv, int *err_code)
4119{
4120 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4121 if (err_code)
4122 *err_code |= ERR_WARN;
4123 return 1;
4124 }
4125 return 0;
4126}
4127
Willy Tarreau25e51522016-11-04 15:10:17 +01004128/* returns 0 if no error, otherwise a combination of ERR_* flags */
4129static int srv_iterate_initaddr(struct server *srv)
4130{
4131 int return_code = 0;
4132 int err_code;
4133 unsigned int methods;
4134
4135 methods = srv->init_addr_methods;
4136 if (!methods) { // default to "last,libc"
4137 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4138 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4139 }
4140
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004141 /* "-dr" : always append "none" so that server addresses resolution
4142 * failures are silently ignored, this is convenient to validate some
4143 * configs out of their environment.
4144 */
4145 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4146 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4147
Willy Tarreau25e51522016-11-04 15:10:17 +01004148 while (methods) {
4149 err_code = 0;
4150 switch (srv_get_next_initaddr(&methods)) {
4151 case SRV_IADDR_LAST:
4152 if (!srv->lastaddr)
4153 continue;
4154 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004155 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004156 return_code |= err_code;
4157 break;
4158
4159 case SRV_IADDR_LIBC:
4160 if (!srv->hostname)
4161 continue;
4162 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004163 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004164 return_code |= err_code;
4165 break;
4166
Willy Tarreau37ebe122016-11-04 15:17:58 +01004167 case SRV_IADDR_NONE:
4168 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004169 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004170 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4171 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004172 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004173 return return_code;
4174
Willy Tarreau4310d362016-11-02 15:05:56 +01004175 case SRV_IADDR_IP:
4176 ipcpy(&srv->init_addr, &srv->addr);
4177 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004178 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4179 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004180 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004181 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004182
Willy Tarreau25e51522016-11-04 15:10:17 +01004183 default: /* unhandled method */
4184 break;
4185 }
4186 }
4187
4188 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004189 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004190 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4191 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004192 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004193 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004194 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4195 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004196
4197 return_code |= ERR_ALERT | ERR_FATAL;
4198 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004199out:
4200 srv_set_dyncookie(srv);
4201 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004202}
4203
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004204/*
4205 * This function parses all backends and all servers within each backend
4206 * and performs servers' addr resolution based on information provided by:
4207 * - configuration file
4208 * - server-state file (states provided by an 'old' haproxy process)
4209 *
4210 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4211 */
4212int srv_init_addr(void)
4213{
4214 struct proxy *curproxy;
4215 int return_code = 0;
4216
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004217 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004218 while (curproxy) {
4219 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004220
4221 /* servers are in backend only */
4222 if (!(curproxy->cap & PR_CAP_BE))
4223 goto srv_init_addr_next;
4224
Willy Tarreau25e51522016-11-04 15:10:17 +01004225 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004226 if (srv->hostname)
4227 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004228
4229 srv_init_addr_next:
4230 curproxy = curproxy->next;
4231 }
4232
4233 return return_code;
4234}
4235
Willy Tarreau46b7f532018-08-21 11:54:26 +02004236/*
4237 * Must be called with the server lock held.
4238 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004239const 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 +02004240{
4241
Willy Tarreau83061a82018-07-13 11:56:34 +02004242 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004243
4244 msg = get_trash_chunk();
4245 chunk_reset(msg);
4246
Olivier Houchard8da5f982017-08-04 18:35:36 +02004247 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004248 chunk_appendf(msg, "no need to change the FDQN");
4249 goto out;
4250 }
4251
4252 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4253 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4254 goto out;
4255 }
4256
4257 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4258 server->proxy->id, server->id, server->hostname, fqdn);
4259
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004260 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004261 chunk_reset(msg);
4262 chunk_appendf(msg, "could not update %s/%s FQDN",
4263 server->proxy->id, server->id);
4264 goto out;
4265 }
4266
4267 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004268 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004269
4270 out:
4271 if (updater)
4272 chunk_appendf(msg, " by '%s'", updater);
4273 chunk_appendf(msg, "\n");
4274
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004275 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004276}
4277
4278
Willy Tarreau21b069d2016-11-23 17:15:08 +01004279/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4280 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004281 * 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 +01004282 * used for CLI commands requiring a server name.
4283 * Important: the <arg> is modified to remove the '/'.
4284 */
4285struct server *cli_find_server(struct appctx *appctx, char *arg)
4286{
4287 struct proxy *px;
4288 struct server *sv;
4289 char *line;
4290
4291 /* split "backend/server" and make <line> point to server */
4292 for (line = arg; *line; line++)
4293 if (*line == '/') {
4294 *line++ = '\0';
4295 break;
4296 }
4297
4298 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004299 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004300 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004301 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004302 return NULL;
4303 }
4304
4305 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004306 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004307 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004308 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004309 return NULL;
4310 }
4311
4312 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004313 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004314 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004315 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004316 return NULL;
4317 }
4318
4319 return sv;
4320}
4321
William Lallemand222baf22016-11-19 02:00:33 +01004322
Willy Tarreau46b7f532018-08-21 11:54:26 +02004323/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004324static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004325{
4326 struct server *sv;
4327 const char *warning;
4328
4329 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4330 return 1;
4331
4332 sv = cli_find_server(appctx, args[2]);
4333 if (!sv)
4334 return 1;
4335
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004336 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004337
William Lallemand222baf22016-11-19 02:00:33 +01004338 if (strcmp(args[3], "weight") == 0) {
4339 warning = server_parse_weight_change_request(sv, args[4]);
4340 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004341 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004342 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004343 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004344 }
4345 }
4346 else if (strcmp(args[3], "state") == 0) {
4347 if (strcmp(args[4], "ready") == 0)
4348 srv_adm_set_ready(sv);
4349 else if (strcmp(args[4], "drain") == 0)
4350 srv_adm_set_drain(sv);
4351 else if (strcmp(args[4], "maint") == 0)
4352 srv_adm_set_maint(sv);
4353 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004354 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004355 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004356 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004357 }
4358 }
4359 else if (strcmp(args[3], "health") == 0) {
4360 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004361 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004362 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004363 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004364 }
4365 else if (strcmp(args[4], "up") == 0) {
4366 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004367 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004368 }
4369 else if (strcmp(args[4], "stopping") == 0) {
4370 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004371 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004372 }
4373 else if (strcmp(args[4], "down") == 0) {
4374 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004375 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004376 }
4377 else {
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> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004380 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004381 }
4382 }
4383 else if (strcmp(args[3], "agent") == 0) {
4384 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004385 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004386 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004387 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004388 }
4389 else if (strcmp(args[4], "up") == 0) {
4390 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004391 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004392 }
4393 else if (strcmp(args[4], "down") == 0) {
4394 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004395 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004396 }
4397 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004398 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004399 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004400 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004401 }
4402 }
Misiek2da082d2017-01-09 09:40:42 +01004403 else if (strcmp(args[3], "agent-addr") == 0) {
4404 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004405 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004406 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4407 appctx->st0 = CLI_ST_PRINT;
4408 } else {
4409 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004410 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004411 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4412 appctx->st0 = CLI_ST_PRINT;
4413 }
4414 }
4415 }
4416 else if (strcmp(args[3], "agent-send") == 0) {
4417 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004418 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004419 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4420 appctx->st0 = CLI_ST_PRINT;
4421 } else {
4422 char *nss = strdup(args[4]);
4423 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004424 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004425 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4426 appctx->st0 = CLI_ST_PRINT;
4427 } else {
4428 free(sv->agent.send_string);
4429 sv->agent.send_string = nss;
4430 sv->agent.send_string_len = strlen(args[4]);
4431 }
4432 }
4433 }
William Lallemand222baf22016-11-19 02:00:33 +01004434 else if (strcmp(args[3], "check-port") == 0) {
4435 int i = 0;
4436 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004437 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004438 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004439 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004440 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004441 }
4442 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004443 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004444 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004445 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004446 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004447 }
4448 /* prevent the update of port to 0 if MAPPORTS are in use */
4449 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004450 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004451 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004452 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004453 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004454 }
4455 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004456 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004457 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004458 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004459 }
4460 else if (strcmp(args[3], "addr") == 0) {
4461 char *addr = NULL;
4462 char *port = NULL;
4463 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004464 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004465 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004466 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004467 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004468 }
4469 else {
4470 addr = args[4];
4471 }
4472 if (strcmp(args[5], "port") == 0) {
4473 port = args[6];
4474 }
4475 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4476 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004477 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004478 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004479 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004480 }
4481 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4482 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004483 else if (strcmp(args[3], "fqdn") == 0) {
4484 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004485 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004486 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4487 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004488 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004489 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004490 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004491 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004492 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004493 appctx->ctx.cli.msg = warning;
4494 appctx->st0 = CLI_ST_PRINT;
4495 }
4496 }
William Lallemand222baf22016-11-19 02:00:33 +01004497 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004498 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004499 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 +01004500 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004501 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004502 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004503 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004504 return 1;
4505}
4506
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004507static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004508{
4509 struct stream_interface *si = appctx->owner;
4510 struct proxy *px;
4511 struct server *sv;
4512 char *line;
4513
4514
4515 /* split "backend/server" and make <line> point to server */
4516 for (line = args[2]; *line; line++)
4517 if (*line == '/') {
4518 *line++ = '\0';
4519 break;
4520 }
4521
4522 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004523 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004524 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004525 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004526 return 1;
4527 }
4528
4529 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004530 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004531 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004532 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004533 return 1;
4534 }
4535
4536 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004537 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4538 sv->iweight);
4539 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004540 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004541 return 0;
4542 }
William Lallemand6b160942016-11-22 12:34:35 +01004543 return 1;
4544}
4545
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004546/* Parse a "set weight" command.
4547 *
4548 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004549 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004550static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004551{
4552 struct server *sv;
4553 const char *warning;
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);
4563
William Lallemand6b160942016-11-22 12:34:35 +01004564 warning = server_parse_weight_change_request(sv, args[3]);
4565 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004566 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004567 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004568 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004569 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004570
4571 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4572
William Lallemand6b160942016-11-22 12:34:35 +01004573 return 1;
4574}
4575
Willy Tarreau46b7f532018-08-21 11:54:26 +02004576/* parse a "set maxconn server" command. It always returns 1.
4577 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004578 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004579 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004580static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004581{
4582 struct server *sv;
4583 const char *warning;
4584
4585 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4586 return 1;
4587
4588 sv = cli_find_server(appctx, args[3]);
4589 if (!sv)
4590 return 1;
4591
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004592 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4593
Willy Tarreaub8026272016-11-23 11:26:56 +01004594 warning = server_parse_maxconn_change_request(sv, args[4]);
4595 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004596 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004597 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004598 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004599 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004600
4601 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4602
Willy Tarreaub8026272016-11-23 11:26:56 +01004603 return 1;
4604}
William Lallemand6b160942016-11-22 12:34:35 +01004605
Willy Tarreau46b7f532018-08-21 11:54:26 +02004606/* parse a "disable agent" command. It always returns 1.
4607 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004608 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004609 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004610static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004611{
4612 struct server *sv;
4613
4614 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4615 return 1;
4616
4617 sv = cli_find_server(appctx, args[2]);
4618 if (!sv)
4619 return 1;
4620
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004621 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004622 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004623 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004624 return 1;
4625}
4626
Willy Tarreau46b7f532018-08-21 11:54:26 +02004627/* parse a "disable health" command. It always returns 1.
4628 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004629 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004630 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004631static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004632{
4633 struct server *sv;
4634
4635 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4636 return 1;
4637
4638 sv = cli_find_server(appctx, args[2]);
4639 if (!sv)
4640 return 1;
4641
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004642 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004643 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004644 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004645 return 1;
4646}
4647
Willy Tarreau46b7f532018-08-21 11:54:26 +02004648/* parse a "disable server" command. It always returns 1.
4649 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004650 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004651 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004652static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004653{
4654 struct server *sv;
4655
4656 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4657 return 1;
4658
4659 sv = cli_find_server(appctx, args[2]);
4660 if (!sv)
4661 return 1;
4662
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004663 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004664 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004665 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004666 return 1;
4667}
4668
Willy Tarreau46b7f532018-08-21 11:54:26 +02004669/* parse a "enable agent" command. It always returns 1.
4670 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004671 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004672 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004673static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004674{
4675 struct server *sv;
4676
4677 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4678 return 1;
4679
4680 sv = cli_find_server(appctx, args[2]);
4681 if (!sv)
4682 return 1;
4683
4684 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004685 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004686 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004687 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004688 return 1;
4689 }
4690
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004691 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004692 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004693 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004694 return 1;
4695}
4696
Willy Tarreau46b7f532018-08-21 11:54:26 +02004697/* parse a "enable health" command. It always returns 1.
4698 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004699 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004700 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004701static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004702{
4703 struct server *sv;
4704
4705 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4706 return 1;
4707
4708 sv = cli_find_server(appctx, args[2]);
4709 if (!sv)
4710 return 1;
4711
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004712 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004713 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004714 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004715 return 1;
4716}
4717
Willy Tarreau46b7f532018-08-21 11:54:26 +02004718/* parse a "enable server" command. It always returns 1.
4719 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004720 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004721 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004722static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004723{
4724 struct server *sv;
4725
4726 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4727 return 1;
4728
4729 sv = cli_find_server(appctx, args[2]);
4730 if (!sv)
4731 return 1;
4732
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004733 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004734 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004735 if (!(sv->flags & SRV_F_COOKIESET)
4736 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4737 sv->cookie)
4738 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004739 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004740 return 1;
4741}
4742
William Lallemand222baf22016-11-19 02:00:33 +01004743/* register cli keywords */
4744static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004745 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004746 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004747 { { "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 +01004748 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004749 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004750 { { "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 +01004751 { { "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 +01004752 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004753 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4754 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4755
William Lallemand222baf22016-11-19 02:00:33 +01004756 {{},}
4757}};
4758
Willy Tarreau0108d902018-11-25 19:14:37 +01004759INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004760
Emeric Brun64cc49c2017-10-03 14:46:45 +02004761/*
4762 * This function applies server's status changes, it is
4763 * is designed to be called asynchronously.
4764 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004765 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004766 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004767static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004768{
4769 struct check *check = &s->check;
4770 int xferred;
4771 struct proxy *px = s->proxy;
4772 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4773 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4774 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004775 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004776
Emeric Brun64cc49c2017-10-03 14:46:45 +02004777 /* If currently main is not set we try to apply pending state changes */
4778 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4779 int next_admin;
4780
4781 /* Backup next admin */
4782 next_admin = s->next_admin;
4783
4784 /* restore current admin state */
4785 s->next_admin = s->cur_admin;
4786
4787 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4788 s->last_change = now.tv_sec;
4789 if (s->proxy->lbprm.set_server_status_down)
4790 s->proxy->lbprm.set_server_status_down(s);
4791
4792 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4793 srv_shutdown_streams(s, SF_ERR_DOWN);
4794
4795 /* we might have streams queued on this server and waiting for
4796 * a connection. Those which are redispatchable will be queued
4797 * to another server or to the proxy itself.
4798 */
4799 xferred = pendconn_redistribute(s);
4800
4801 tmptrash = alloc_trash_chunk();
4802 if (tmptrash) {
4803 chunk_printf(tmptrash,
4804 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4805 s->proxy->id, s->id);
4806
Emeric Brun5a133512017-10-19 14:42:30 +02004807 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004808 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004809
4810 /* we don't send an alert if the server was previously paused */
4811 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004812 send_log(s->proxy, log_level, "%s.\n",
4813 tmptrash->area);
4814 send_email_alert(s, log_level, "%s",
4815 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004816 free_trash_chunk(tmptrash);
4817 tmptrash = NULL;
4818 }
4819 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4820 set_backend_down(s->proxy);
4821
4822 s->counters.down_trans++;
4823 }
4824 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4825 s->last_change = now.tv_sec;
4826 if (s->proxy->lbprm.set_server_status_down)
4827 s->proxy->lbprm.set_server_status_down(s);
4828
4829 /* we might have streams queued on this server and waiting for
4830 * a connection. Those which are redispatchable will be queued
4831 * to another server or to the proxy itself.
4832 */
4833 xferred = pendconn_redistribute(s);
4834
4835 tmptrash = alloc_trash_chunk();
4836 if (tmptrash) {
4837 chunk_printf(tmptrash,
4838 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4839 s->proxy->id, s->id);
4840
Emeric Brun5a133512017-10-19 14:42:30 +02004841 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004842
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);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004846 free_trash_chunk(tmptrash);
4847 tmptrash = NULL;
4848 }
4849
4850 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4851 set_backend_down(s->proxy);
4852 }
4853 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4854 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4855 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4856 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4857 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4858 s->proxy->last_change = now.tv_sec;
4859 }
4860
4861 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4862 s->down_time += now.tv_sec - s->last_change;
4863
4864 s->last_change = now.tv_sec;
4865 if (s->next_state == SRV_ST_STARTING)
4866 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4867
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004868 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004869 /* now propagate the status change to any LB algorithms */
4870 if (px->lbprm.update_server_eweight)
4871 px->lbprm.update_server_eweight(s);
4872 else if (srv_willbe_usable(s)) {
4873 if (px->lbprm.set_server_status_up)
4874 px->lbprm.set_server_status_up(s);
4875 }
4876 else {
4877 if (px->lbprm.set_server_status_down)
4878 px->lbprm.set_server_status_down(s);
4879 }
4880
4881 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4882 * and it's not a backup server and its effective weight is > 0,
4883 * then it can accept new connections, so we shut down all streams
4884 * on all backup servers.
4885 */
4886 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4887 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4888 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4889
4890 /* check if we can handle some connections queued at the proxy. We
4891 * will take as many as we can handle.
4892 */
4893 xferred = pendconn_grab_from_px(s);
4894
4895 tmptrash = alloc_trash_chunk();
4896 if (tmptrash) {
4897 chunk_printf(tmptrash,
4898 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4899 s->proxy->id, s->id);
4900
Emeric Brun5a133512017-10-19 14:42:30 +02004901 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004902 ha_warning("%s.\n", tmptrash->area);
4903 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4904 tmptrash->area);
4905 send_email_alert(s, LOG_NOTICE, "%s",
4906 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004907 free_trash_chunk(tmptrash);
4908 tmptrash = NULL;
4909 }
4910
4911 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4912 set_backend_down(s->proxy);
4913 }
4914 else if (s->cur_eweight != s->next_eweight) {
4915 /* now propagate the status change to any LB algorithms */
4916 if (px->lbprm.update_server_eweight)
4917 px->lbprm.update_server_eweight(s);
4918 else if (srv_willbe_usable(s)) {
4919 if (px->lbprm.set_server_status_up)
4920 px->lbprm.set_server_status_up(s);
4921 }
4922 else {
4923 if (px->lbprm.set_server_status_down)
4924 px->lbprm.set_server_status_down(s);
4925 }
4926
4927 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4928 set_backend_down(s->proxy);
4929 }
4930
4931 s->next_admin = next_admin;
4932 }
4933
Emeric Brun5a133512017-10-19 14:42:30 +02004934 /* reset operational state change */
4935 *s->op_st_chg.reason = 0;
4936 s->op_st_chg.status = s->op_st_chg.code = -1;
4937 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004938
4939 /* Now we try to apply pending admin changes */
4940
4941 /* Maintenance must also disable health checks */
4942 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4943 if (s->check.state & CHK_ST_ENABLED) {
4944 s->check.state |= CHK_ST_PAUSED;
4945 check->health = 0;
4946 }
4947
4948 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004949 tmptrash = alloc_trash_chunk();
4950 if (tmptrash) {
4951 chunk_printf(tmptrash,
4952 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4953 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4954 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004955
Olivier Houchard796a2b32017-10-24 17:42:47 +02004956 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004957
Olivier Houchard796a2b32017-10-24 17:42:47 +02004958 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004959 ha_warning("%s.\n", tmptrash->area);
4960 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4961 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004962 }
4963 free_trash_chunk(tmptrash);
4964 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004965 }
Emeric Brun8f298292017-12-06 16:47:17 +01004966 /* commit new admin status */
4967
4968 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004969 }
4970 else { /* server was still running */
4971 check->health = 0; /* failure */
4972 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004973
4974 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004975 if (s->proxy->lbprm.set_server_status_down)
4976 s->proxy->lbprm.set_server_status_down(s);
4977
Emeric Brun64cc49c2017-10-03 14:46:45 +02004978 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4979 srv_shutdown_streams(s, SF_ERR_DOWN);
4980
4981 /* we might have streams queued on this server and waiting for
4982 * a connection. Those which are redispatchable will be queued
4983 * to another server or to the proxy itself.
4984 */
4985 xferred = pendconn_redistribute(s);
4986
4987 tmptrash = alloc_trash_chunk();
4988 if (tmptrash) {
4989 chunk_printf(tmptrash,
4990 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4991 s->flags & SRV_F_BACKUP ? "Backup " : "",
4992 s->proxy->id, s->id,
4993 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4994
4995 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4996
4997 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004998 ha_warning("%s.\n", tmptrash->area);
4999 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5000 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005001 }
5002 free_trash_chunk(tmptrash);
5003 tmptrash = NULL;
5004 }
5005 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5006 set_backend_down(s->proxy);
5007
5008 s->counters.down_trans++;
5009 }
5010 }
5011 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5012 /* OK here we're leaving maintenance, we have many things to check,
5013 * because the server might possibly be coming back up depending on
5014 * its state. In practice, leaving maintenance means that we should
5015 * immediately turn to UP (more or less the slowstart) under the
5016 * following conditions :
5017 * - server is neither checked nor tracked
5018 * - server tracks another server which is not checked
5019 * - server tracks another server which is already up
5020 * Which sums up as something simpler :
5021 * "either the tracking server is up or the server's checks are disabled
5022 * or up". Otherwise we only re-enable health checks. There's a special
5023 * case associated to the stopping state which can be inherited. Note
5024 * that the server might still be in drain mode, which is naturally dealt
5025 * with by the lower level functions.
5026 */
5027
5028 if (s->check.state & CHK_ST_ENABLED) {
5029 s->check.state &= ~CHK_ST_PAUSED;
5030 check->health = check->rise; /* start OK but check immediately */
5031 }
5032
5033 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5034 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5035 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5036 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5037 s->next_state = SRV_ST_STOPPING;
5038 }
5039 else {
5040 s->next_state = SRV_ST_STARTING;
5041 if (s->slowstart > 0)
5042 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5043 else
5044 s->next_state = SRV_ST_RUNNING;
5045 }
5046
5047 }
5048
5049 tmptrash = alloc_trash_chunk();
5050 if (tmptrash) {
5051 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5052 chunk_printf(tmptrash,
5053 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5054 s->flags & SRV_F_BACKUP ? "Backup " : "",
5055 s->proxy->id, s->id,
5056 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5057 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5058 }
5059 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5060 chunk_printf(tmptrash,
5061 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5062 s->flags & SRV_F_BACKUP ? "Backup " : "",
5063 s->proxy->id, s->id, s->hostname,
5064 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5065 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5066 }
5067 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5068 chunk_printf(tmptrash,
5069 "%sServer %s/%s is %s/%s (leaving maintenance)",
5070 s->flags & SRV_F_BACKUP ? "Backup " : "",
5071 s->proxy->id, s->id,
5072 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5073 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5074 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005075 ha_warning("%s.\n", tmptrash->area);
5076 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5077 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005078 free_trash_chunk(tmptrash);
5079 tmptrash = NULL;
5080 }
5081
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005082 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005083 /* now propagate the status change to any LB algorithms */
5084 if (px->lbprm.update_server_eweight)
5085 px->lbprm.update_server_eweight(s);
5086 else if (srv_willbe_usable(s)) {
5087 if (px->lbprm.set_server_status_up)
5088 px->lbprm.set_server_status_up(s);
5089 }
5090 else {
5091 if (px->lbprm.set_server_status_down)
5092 px->lbprm.set_server_status_down(s);
5093 }
5094
5095 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5096 set_backend_down(s->proxy);
5097
Willy Tarreau6a78e612018-08-07 10:14:53 +02005098 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5099 * and it's not a backup server and its effective weight is > 0,
5100 * then it can accept new connections, so we shut down all streams
5101 * on all backup servers.
5102 */
5103 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5104 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5105 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5106
5107 /* check if we can handle some connections queued at the proxy. We
5108 * will take as many as we can handle.
5109 */
5110 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005111 }
5112 else if (s->next_admin & SRV_ADMF_MAINT) {
5113 /* remaining in maintenance mode, let's inform precisely about the
5114 * situation.
5115 */
5116 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5117 tmptrash = alloc_trash_chunk();
5118 if (tmptrash) {
5119 chunk_printf(tmptrash,
5120 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5121 s->flags & SRV_F_BACKUP ? "Backup " : "",
5122 s->proxy->id, s->id);
5123
5124 if (s->track) /* normally it's mandatory here */
5125 chunk_appendf(tmptrash, " via %s/%s",
5126 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005127 ha_warning("%s.\n", tmptrash->area);
5128 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5129 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005130 free_trash_chunk(tmptrash);
5131 tmptrash = NULL;
5132 }
5133 }
5134 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5135 tmptrash = alloc_trash_chunk();
5136 if (tmptrash) {
5137 chunk_printf(tmptrash,
5138 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5139 s->flags & SRV_F_BACKUP ? "Backup " : "",
5140 s->proxy->id, s->id, s->hostname);
5141
5142 if (s->track) /* normally it's mandatory here */
5143 chunk_appendf(tmptrash, " via %s/%s",
5144 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005145 ha_warning("%s.\n", tmptrash->area);
5146 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5147 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005148 free_trash_chunk(tmptrash);
5149 tmptrash = NULL;
5150 }
5151 }
5152 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5153 tmptrash = alloc_trash_chunk();
5154 if (tmptrash) {
5155 chunk_printf(tmptrash,
5156 "%sServer %s/%s remains in forced maintenance",
5157 s->flags & SRV_F_BACKUP ? "Backup " : "",
5158 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005159 ha_warning("%s.\n", tmptrash->area);
5160 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5161 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005162 free_trash_chunk(tmptrash);
5163 tmptrash = NULL;
5164 }
5165 }
5166 /* don't report anything when leaving drain mode and remaining in maintenance */
5167
5168 s->cur_admin = s->next_admin;
5169 }
5170
5171 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5172 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5173 /* drain state is applied only if not yet in maint */
5174
5175 s->last_change = now.tv_sec;
5176 if (px->lbprm.set_server_status_down)
5177 px->lbprm.set_server_status_down(s);
5178
5179 /* we might have streams queued on this server and waiting for
5180 * a connection. Those which are redispatchable will be queued
5181 * to another server or to the proxy itself.
5182 */
5183 xferred = pendconn_redistribute(s);
5184
5185 tmptrash = alloc_trash_chunk();
5186 if (tmptrash) {
5187 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5188 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5189 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5190
5191 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5192
5193 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005194 ha_warning("%s.\n", tmptrash->area);
5195 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5196 tmptrash->area);
5197 send_email_alert(s, LOG_NOTICE, "%s",
5198 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005199 }
5200 free_trash_chunk(tmptrash);
5201 tmptrash = NULL;
5202 }
5203
5204 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5205 set_backend_down(s->proxy);
5206 }
5207 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5208 /* OK completely leaving drain mode */
5209 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5210 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5211 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5212 s->proxy->last_change = now.tv_sec;
5213 }
5214
5215 if (s->last_change < now.tv_sec) // ignore negative times
5216 s->down_time += now.tv_sec - s->last_change;
5217 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005218 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005219
5220 tmptrash = alloc_trash_chunk();
5221 if (tmptrash) {
5222 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5223 chunk_printf(tmptrash,
5224 "%sServer %s/%s is %s (leaving forced drain)",
5225 s->flags & SRV_F_BACKUP ? "Backup " : "",
5226 s->proxy->id, s->id,
5227 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5228 }
5229 else {
5230 chunk_printf(tmptrash,
5231 "%sServer %s/%s is %s (leaving drain)",
5232 s->flags & SRV_F_BACKUP ? "Backup " : "",
5233 s->proxy->id, s->id,
5234 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5235 if (s->track) /* normally it's mandatory here */
5236 chunk_appendf(tmptrash, " via %s/%s",
5237 s->track->proxy->id, s->track->id);
5238 }
5239
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005240 ha_warning("%s.\n", tmptrash->area);
5241 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5242 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005243 free_trash_chunk(tmptrash);
5244 tmptrash = NULL;
5245 }
5246
5247 /* now propagate the status change to any LB algorithms */
5248 if (px->lbprm.update_server_eweight)
5249 px->lbprm.update_server_eweight(s);
5250 else if (srv_willbe_usable(s)) {
5251 if (px->lbprm.set_server_status_up)
5252 px->lbprm.set_server_status_up(s);
5253 }
5254 else {
5255 if (px->lbprm.set_server_status_down)
5256 px->lbprm.set_server_status_down(s);
5257 }
5258 }
5259 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5260 /* remaining in drain mode after removing one of its flags */
5261
5262 tmptrash = alloc_trash_chunk();
5263 if (tmptrash) {
5264 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5265 chunk_printf(tmptrash,
5266 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5267 s->flags & SRV_F_BACKUP ? "Backup " : "",
5268 s->proxy->id, s->id);
5269
5270 if (s->track) /* normally it's mandatory here */
5271 chunk_appendf(tmptrash, " via %s/%s",
5272 s->track->proxy->id, s->track->id);
5273 }
5274 else {
5275 chunk_printf(tmptrash,
5276 "%sServer %s/%s remains in forced drain mode",
5277 s->flags & SRV_F_BACKUP ? "Backup " : "",
5278 s->proxy->id, s->id);
5279 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005280 ha_warning("%s.\n", tmptrash->area);
5281 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5282 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005283 free_trash_chunk(tmptrash);
5284 tmptrash = NULL;
5285 }
5286
5287 /* commit new admin status */
5288
5289 s->cur_admin = s->next_admin;
5290 }
5291 }
5292
5293 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005294 *s->adm_st_chg_cause = 0;
5295}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005296
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005297static struct task *cleanup_idle_connections(struct task *task, void *context, unsigned short state)
5298{
5299 struct server *srv = context;
5300 struct connection *conn, *conn_back;
5301 unsigned int next_wakeup = 0;
5302
5303 list_for_each_entry_safe(conn, conn_back, &srv->idle_orphan_conns[tid], list) {
5304 if (conn->idle_time + srv->idle_timeout > now_ms) {
5305 next_wakeup = conn->idle_time + srv->idle_timeout;
5306 break;
5307 }
5308 conn->mux->destroy(conn);
5309 }
5310 if (next_wakeup > 0)
5311 task_schedule(task, next_wakeup);
5312 return task;
5313}
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005314/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005315 * Local variables:
5316 * c-indent-level: 8
5317 * c-basic-offset: 8
5318 * End:
5319 */