blob: c5e8945374b75b38b99507cb2c395b72d3378256 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
Olivier Houchard4e694042017-03-14 20:01:29 +010017#include <import/xxhash.h>
18
Willy Tarreau272adea2014-03-31 10:39:59 +020019#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020020#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020021#include <common/errors.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010022#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020023#include <common/time.h>
24
William Lallemand222baf22016-11-19 02:00:33 +010025#include <types/applet.h>
26#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020027#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010028#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020029#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010030#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020031
William Lallemand222baf22016-11-19 02:00:33 +010032#include <proto/applet.h>
33#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090034#include <proto/checks.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020035#include <proto/port_range.h>
36#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020037#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010038#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020039#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020040#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010041#include <proto/stream_interface.h>
42#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020043#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020044#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010045#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020046
Willy Tarreau3ff577e2018-08-02 11:48:52 +020047static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020048static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010049static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010050static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Willy Tarreaubaaee002006-06-26 02:48:02 +020051
Willy Tarreau21faa912012-10-10 08:27:36 +020052/* List head of all known server keywords */
53static struct srv_kw_list srv_keywords = {
54 .list = LIST_HEAD_INIT(srv_keywords.list)
55};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020056
Simon Hormana3608442013-11-01 16:46:15 +090057int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020058{
Emeric Brun52a91d32017-08-31 14:41:55 +020059 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020060 return s->down_time;
61
62 return now.tv_sec - s->last_change + s->down_time;
63}
Willy Tarreaubaaee002006-06-26 02:48:02 +020064
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050065int srv_lastsession(const struct server *s)
66{
67 if (s->counters.last_sess)
68 return now.tv_sec - s->counters.last_sess;
69
70 return -1;
71}
72
Simon Horman4a741432013-02-23 15:35:38 +090073int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020074{
Simon Horman4a741432013-02-23 15:35:38 +090075 const struct server *s = check->server;
76
Willy Tarreauff5ae352013-12-11 20:36:34 +010077 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090078 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010079
Emeric Brun52a91d32017-08-31 14:41:55 +020080 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090081 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010082
Simon Horman4a741432013-02-23 15:35:38 +090083 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010084}
85
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010086/*
87 * Check that we did not get a hash collision.
88 * Unlikely, but it can happen.
89 */
90static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +010091{
92 struct proxy *p = s->proxy;
93 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010094
95 for (tmpserv = p->srv; tmpserv != NULL;
96 tmpserv = tmpserv->next) {
97 if (tmpserv == s)
98 continue;
99 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
100 continue;
101 if (tmpserv->cookie &&
102 strcmp(tmpserv->cookie, s->cookie) == 0) {
103 ha_warning("We generated two equal cookies for two different servers.\n"
104 "Please change the secret key for '%s'.\n",
105 s->proxy->id);
106 }
107 }
108
109}
110
111void srv_set_dyncookie(struct server *s)
112{
113 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100114 char *tmpbuf;
115 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100116 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100117 size_t buffer_len;
118 int addr_len;
119 int port;
120
121 if ((s->flags & SRV_F_COOKIESET) ||
122 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
123 s->proxy->dyncookie_key == NULL)
124 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100125 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100126
127 if (s->addr.ss_family != AF_INET &&
128 s->addr.ss_family != AF_INET6)
129 return;
130 /*
131 * Buffer to calculate the cookie value.
132 * The buffer contains the secret key + the server IP address
133 * + the TCP port.
134 */
135 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
136 /*
137 * The TCP port should use only 2 bytes, but is stored in
138 * an unsigned int in struct server, so let's use 4, to be
139 * on the safe side.
140 */
141 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200142 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100143 memcpy(tmpbuf, p->dyncookie_key, key_len);
144 memcpy(&(tmpbuf[key_len]),
145 s->addr.ss_family == AF_INET ?
146 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
147 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
148 addr_len);
149 /*
150 * Make sure it's the same across all the load balancers,
151 * no matter their endianness.
152 */
153 port = htonl(s->svc_port);
154 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
155 hash_value = XXH64(tmpbuf, buffer_len, 0);
156 memprintf(&s->cookie, "%016llx", hash_value);
157 if (!s->cookie)
158 return;
159 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100160
161 /* Don't bother checking if the dyncookie is duplicated if
162 * the server is marked as "disabled", maybe it doesn't have
163 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100164 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100165 if (!(s->next_admin & SRV_ADMF_FMAINT))
166 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100167}
168
Willy Tarreau21faa912012-10-10 08:27:36 +0200169/*
170 * Registers the server keyword list <kwl> as a list of valid keywords for next
171 * parsing sessions.
172 */
173void srv_register_keywords(struct srv_kw_list *kwl)
174{
175 LIST_ADDQ(&srv_keywords.list, &kwl->list);
176}
177
178/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
179 * keyword is found with a NULL ->parse() function, then an attempt is made to
180 * find one with a valid ->parse() function. This way it is possible to declare
181 * platform-dependant, known keywords as NULL, then only declare them as valid
182 * if some options are met. Note that if the requested keyword contains an
183 * opening parenthesis, everything from this point is ignored.
184 */
185struct srv_kw *srv_find_kw(const char *kw)
186{
187 int index;
188 const char *kwend;
189 struct srv_kw_list *kwl;
190 struct srv_kw *ret = NULL;
191
192 kwend = strchr(kw, '(');
193 if (!kwend)
194 kwend = kw + strlen(kw);
195
196 list_for_each_entry(kwl, &srv_keywords.list, list) {
197 for (index = 0; kwl->kw[index].kw != NULL; index++) {
198 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
199 kwl->kw[index].kw[kwend-kw] == 0) {
200 if (kwl->kw[index].parse)
201 return &kwl->kw[index]; /* found it !*/
202 else
203 ret = &kwl->kw[index]; /* may be OK */
204 }
205 }
206 }
207 return ret;
208}
209
210/* Dumps all registered "server" keywords to the <out> string pointer. The
211 * unsupported keywords are only dumped if their supported form was not
212 * found.
213 */
214void srv_dump_kws(char **out)
215{
216 struct srv_kw_list *kwl;
217 int index;
218
219 *out = NULL;
220 list_for_each_entry(kwl, &srv_keywords.list, list) {
221 for (index = 0; kwl->kw[index].kw != NULL; index++) {
222 if (kwl->kw[index].parse ||
223 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
224 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
225 kwl->scope,
226 kwl->kw[index].kw,
227 kwl->kw[index].skip ? " <arg>" : "",
228 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
229 kwl->kw[index].parse ? "" : " (not supported)");
230 }
231 }
232 }
233}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100234
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100235/* Parse the "addr" server keyword */
236static int srv_parse_addr(char **args, int *cur_arg,
237 struct proxy *curproxy, struct server *newsrv, char **err)
238{
239 char *errmsg, *arg;
240 struct sockaddr_storage *sk;
241 int port1, port2;
242 struct protocol *proto;
243
244 errmsg = NULL;
245 arg = args[*cur_arg + 1];
246
247 if (!*arg) {
248 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
249 goto err;
250 }
251
252 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
253 if (!sk) {
254 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
255 goto err;
256 }
257
258 proto = protocol_by_family(sk->ss_family);
259 if (!proto || !proto->connect) {
260 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
261 args[*cur_arg], arg);
262 goto err;
263 }
264
265 if (port1 != port2) {
266 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
267 args[*cur_arg], arg);
268 goto err;
269 }
270
271 newsrv->check.addr = newsrv->agent.addr = *sk;
272 newsrv->flags |= SRV_F_CHECKADDR;
273 newsrv->flags |= SRV_F_AGENTADDR;
274
275 return 0;
276
277 err:
278 free(errmsg);
279 return ERR_ALERT | ERR_FATAL;
280}
281
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100282/* Parse the "agent-check" server keyword */
283static int srv_parse_agent_check(char **args, int *cur_arg,
284 struct proxy *curproxy, struct server *newsrv, char **err)
285{
286 newsrv->do_agent = 1;
287 return 0;
288}
289
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100290/* Parse the "backup" server keyword */
291static int srv_parse_backup(char **args, int *cur_arg,
292 struct proxy *curproxy, struct server *newsrv, char **err)
293{
294 newsrv->flags |= SRV_F_BACKUP;
295 return 0;
296}
297
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100298/* Parse the "check" server keyword */
299static int srv_parse_check(char **args, int *cur_arg,
300 struct proxy *curproxy, struct server *newsrv, char **err)
301{
302 newsrv->do_check = 1;
303 return 0;
304}
305
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100306/* Parse the "check-send-proxy" server keyword */
307static int srv_parse_check_send_proxy(char **args, int *cur_arg,
308 struct proxy *curproxy, struct server *newsrv, char **err)
309{
310 newsrv->check.send_proxy = 1;
311 return 0;
312}
313
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100314/* Parse the "cookie" server keyword */
315static int srv_parse_cookie(char **args, int *cur_arg,
316 struct proxy *curproxy, struct server *newsrv, char **err)
317{
318 char *arg;
319
320 arg = args[*cur_arg + 1];
321 if (!*arg) {
322 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
323 return ERR_ALERT | ERR_FATAL;
324 }
325
326 free(newsrv->cookie);
327 newsrv->cookie = strdup(arg);
328 newsrv->cklen = strlen(arg);
329 newsrv->flags |= SRV_F_COOKIESET;
330 return 0;
331}
332
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100333/* Parse the "disabled" server keyword */
334static int srv_parse_disabled(char **args, int *cur_arg,
335 struct proxy *curproxy, struct server *newsrv, char **err)
336{
Emeric Brun52a91d32017-08-31 14:41:55 +0200337 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
338 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100339 newsrv->check.state |= CHK_ST_PAUSED;
340 newsrv->check.health = 0;
341 return 0;
342}
343
344/* Parse the "enabled" server keyword */
345static int srv_parse_enabled(char **args, int *cur_arg,
346 struct proxy *curproxy, struct server *newsrv, char **err)
347{
Emeric Brun52a91d32017-08-31 14:41:55 +0200348 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
349 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100350 newsrv->check.state &= ~CHK_ST_PAUSED;
351 newsrv->check.health = newsrv->check.rise;
352 return 0;
353}
354
Willy Tarreaudff55432012-10-10 17:51:05 +0200355/* parse the "id" server keyword */
356static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
357{
358 struct eb32_node *node;
359
360 if (!*args[*cur_arg + 1]) {
361 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
362 return ERR_ALERT | ERR_FATAL;
363 }
364
365 newsrv->puid = atol(args[*cur_arg + 1]);
366 newsrv->conf.id.key = newsrv->puid;
367
368 if (newsrv->puid <= 0) {
369 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
370 return ERR_ALERT | ERR_FATAL;
371 }
372
373 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
374 if (node) {
375 struct server *target = container_of(node, struct server, conf.id);
376 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
377 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
378 target->id);
379 return ERR_ALERT | ERR_FATAL;
380 }
381
382 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200383 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200384 return 0;
385}
386
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100387/* Parse the "namespace" server keyword */
388static int srv_parse_namespace(char **args, int *cur_arg,
389 struct proxy *curproxy, struct server *newsrv, char **err)
390{
391#ifdef CONFIG_HAP_NS
392 char *arg;
393
394 arg = args[*cur_arg + 1];
395 if (!*arg) {
396 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
397 return ERR_ALERT | ERR_FATAL;
398 }
399
400 if (!strcmp(arg, "*")) {
401 /* Use the namespace associated with the connection (if present). */
402 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
403 return 0;
404 }
405
406 /*
407 * As this parser may be called several times for the same 'default-server'
408 * object, or for a new 'server' instance deriving from a 'default-server'
409 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
410 */
411 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
412
413 newsrv->netns = netns_store_lookup(arg, strlen(arg));
414 if (!newsrv->netns)
415 newsrv->netns = netns_store_insert(arg);
416
417 if (!newsrv->netns) {
418 memprintf(err, "Cannot open namespace '%s'", arg);
419 return ERR_ALERT | ERR_FATAL;
420 }
421
422 return 0;
423#else
424 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
425 return ERR_ALERT | ERR_FATAL;
426#endif
427}
428
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100429/* Parse the "no-agent-check" server keyword */
430static int srv_parse_no_agent_check(char **args, int *cur_arg,
431 struct proxy *curproxy, struct server *newsrv, char **err)
432{
433 free_check(&newsrv->agent);
434 newsrv->agent.inter = 0;
435 newsrv->agent.port = 0;
436 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
437 newsrv->do_agent = 0;
438 return 0;
439}
440
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100441/* Parse the "no-backup" server keyword */
442static int srv_parse_no_backup(char **args, int *cur_arg,
443 struct proxy *curproxy, struct server *newsrv, char **err)
444{
445 newsrv->flags &= ~SRV_F_BACKUP;
446 return 0;
447}
448
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100449/* Parse the "no-check" server keyword */
450static int srv_parse_no_check(char **args, int *cur_arg,
451 struct proxy *curproxy, struct server *newsrv, char **err)
452{
453 free_check(&newsrv->check);
454 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
455 newsrv->do_check = 0;
456 return 0;
457}
458
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100459/* Parse the "no-check-send-proxy" server keyword */
460static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
461 struct proxy *curproxy, struct server *newsrv, char **err)
462{
463 newsrv->check.send_proxy = 0;
464 return 0;
465}
466
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100467/* Disable server PROXY protocol flags. */
468static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
469{
470 srv->pp_opts &= ~flags;
471 return 0;
472}
473
474/* Parse the "no-send-proxy" server keyword */
475static int srv_parse_no_send_proxy(char **args, int *cur_arg,
476 struct proxy *curproxy, struct server *newsrv, char **err)
477{
478 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
479}
480
481/* Parse the "no-send-proxy-v2" server keyword */
482static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
483 struct proxy *curproxy, struct server *newsrv, char **err)
484{
485 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
486}
487
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100488/* Parse the "non-stick" server keyword */
489static int srv_parse_non_stick(char **args, int *cur_arg,
490 struct proxy *curproxy, struct server *newsrv, char **err)
491{
492 newsrv->flags |= SRV_F_NON_STICK;
493 return 0;
494}
495
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100496/* Enable server PROXY protocol flags. */
497static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
498{
499 srv->pp_opts |= flags;
500 return 0;
501}
502
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100503/* parse the "proxy-v2-options" */
504static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
505 struct proxy *px, struct server *newsrv, char **err)
506{
507 char *p, *n;
508 for (p = args[*cur_arg+1]; p; p = n) {
509 n = strchr(p, ',');
510 if (n)
511 *n++ = '\0';
512 if (!strcmp(p, "ssl")) {
513 newsrv->pp_opts |= SRV_PP_V2_SSL;
514 } else if (!strcmp(p, "cert-cn")) {
515 newsrv->pp_opts |= SRV_PP_V2_SSL;
516 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100517 } else if (!strcmp(p, "cert-key")) {
518 newsrv->pp_opts |= SRV_PP_V2_SSL;
519 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
520 } else if (!strcmp(p, "cert-sig")) {
521 newsrv->pp_opts |= SRV_PP_V2_SSL;
522 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
523 } else if (!strcmp(p, "ssl-cipher")) {
524 newsrv->pp_opts |= SRV_PP_V2_SSL;
525 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100526 } else if (!strcmp(p, "authority")) {
527 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100528 } else if (!strcmp(p, "crc32c")) {
529 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100530 } else
531 goto fail;
532 }
533 return 0;
534 fail:
535 if (err)
536 memprintf(err, "'%s' : proxy v2 option not implemented", p);
537 return ERR_ALERT | ERR_FATAL;
538}
539
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100540/* Parse the "observe" server keyword */
541static int srv_parse_observe(char **args, int *cur_arg,
542 struct proxy *curproxy, struct server *newsrv, char **err)
543{
544 char *arg;
545
546 arg = args[*cur_arg + 1];
547 if (!*arg) {
548 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
549 return ERR_ALERT | ERR_FATAL;
550 }
551
552 if (!strcmp(arg, "none")) {
553 newsrv->observe = HANA_OBS_NONE;
554 }
555 else if (!strcmp(arg, "layer4")) {
556 newsrv->observe = HANA_OBS_LAYER4;
557 }
558 else if (!strcmp(arg, "layer7")) {
559 if (curproxy->mode != PR_MODE_HTTP) {
560 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
561 return ERR_ALERT;
562 }
563 newsrv->observe = HANA_OBS_LAYER7;
564 }
565 else {
566 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
567 "but got '%s'\n", args[*cur_arg], arg);
568 return ERR_ALERT | ERR_FATAL;
569 }
570
571 return 0;
572}
573
Frédéric Lécaille16186232017-03-14 16:42:49 +0100574/* Parse the "redir" server keyword */
575static int srv_parse_redir(char **args, int *cur_arg,
576 struct proxy *curproxy, struct server *newsrv, char **err)
577{
578 char *arg;
579
580 arg = args[*cur_arg + 1];
581 if (!*arg) {
582 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
583 return ERR_ALERT | ERR_FATAL;
584 }
585
586 free(newsrv->rdr_pfx);
587 newsrv->rdr_pfx = strdup(arg);
588 newsrv->rdr_len = strlen(arg);
589
590 return 0;
591}
592
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100593/* Parse the "send-proxy" server keyword */
594static int srv_parse_send_proxy(char **args, int *cur_arg,
595 struct proxy *curproxy, struct server *newsrv, char **err)
596{
597 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
598}
599
600/* Parse the "send-proxy-v2" server keyword */
601static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
602 struct proxy *curproxy, struct server *newsrv, char **err)
603{
604 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
605}
606
Frédéric Lécailledba97072017-03-17 15:33:50 +0100607
608/* Parse the "source" server keyword */
609static int srv_parse_source(char **args, int *cur_arg,
610 struct proxy *curproxy, struct server *newsrv, char **err)
611{
612 char *errmsg;
613 int port_low, port_high;
614 struct sockaddr_storage *sk;
615 struct protocol *proto;
616
617 errmsg = NULL;
618
619 if (!*args[*cur_arg + 1]) {
620 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
621 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
622 goto err;
623 }
624
625 /* 'sk' is statically allocated (no need to be freed). */
626 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
627 if (!sk) {
628 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
629 goto err;
630 }
631
632 proto = protocol_by_family(sk->ss_family);
633 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100634 ha_alert("'%s %s' : connect() not supported for this address family.\n",
635 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100636 goto err;
637 }
638
639 newsrv->conn_src.opts |= CO_SRC_BIND;
640 newsrv->conn_src.source_addr = *sk;
641
642 if (port_low != port_high) {
643 int i;
644
645 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100646 ha_alert("'%s' does not support port offsets (found '%s').\n",
647 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100648 goto err;
649 }
650
651 if (port_low <= 0 || port_low > 65535 ||
652 port_high <= 0 || port_high > 65535 ||
653 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100654 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 +0100655 goto err;
656 }
657 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
658 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
659 newsrv->conn_src.sport_range->ports[i] = port_low + i;
660 }
661
662 *cur_arg += 2;
663 while (*(args[*cur_arg])) {
664 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
665#if defined(CONFIG_HAP_TRANSPARENT)
666 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100667 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
668 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100669 goto err;
670 }
671 if (!strcmp(args[*cur_arg + 1], "client")) {
672 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
673 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
674 }
675 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
676 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
677 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
678 }
679 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
680 char *name, *end;
681
682 name = args[*cur_arg + 1] + 7;
683 while (isspace(*name))
684 name++;
685
686 end = name;
687 while (*end && !isspace(*end) && *end != ',' && *end != ')')
688 end++;
689
690 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
691 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
692 free(newsrv->conn_src.bind_hdr_name);
693 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
694 newsrv->conn_src.bind_hdr_len = end - name;
695 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
696 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
697 newsrv->conn_src.bind_hdr_occ = -1;
698
699 /* now look for an occurrence number */
700 while (isspace(*end))
701 end++;
702 if (*end == ',') {
703 end++;
704 name = end;
705 if (*end == '-')
706 end++;
707 while (isdigit((int)*end))
708 end++;
709 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
710 }
711
712 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100713 ha_alert("usesrc hdr_ip(name,num) does not support negative"
714 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100715 goto err;
716 }
717 }
718 else {
719 struct sockaddr_storage *sk;
720 int port1, port2;
721
722 /* 'sk' is statically allocated (no need to be freed). */
723 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
724 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100725 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100726 goto err;
727 }
728
729 proto = protocol_by_family(sk->ss_family);
730 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100731 ha_alert("'%s %s' : connect() not supported for this address family.\n",
732 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100733 goto err;
734 }
735
736 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100737 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
738 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100739 goto err;
740 }
741 newsrv->conn_src.tproxy_addr = *sk;
742 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
743 }
744 global.last_checks |= LSTCHK_NETADM;
745 *cur_arg += 2;
746 continue;
747#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100748 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 +0100749 goto err;
750#endif /* defined(CONFIG_HAP_TRANSPARENT) */
751 } /* "usesrc" */
752
753 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
754#ifdef SO_BINDTODEVICE
755 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100756 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100757 goto err;
758 }
759 free(newsrv->conn_src.iface_name);
760 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
761 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
762 global.last_checks |= LSTCHK_NETADM;
763#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100764 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100765 goto err;
766#endif
767 *cur_arg += 2;
768 continue;
769 }
770 /* this keyword in not an option of "source" */
771 break;
772 } /* while */
773
774 return 0;
775
776 err:
777 free(errmsg);
778 return ERR_ALERT | ERR_FATAL;
779}
780
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100781/* Parse the "stick" server keyword */
782static int srv_parse_stick(char **args, int *cur_arg,
783 struct proxy *curproxy, struct server *newsrv, char **err)
784{
785 newsrv->flags &= ~SRV_F_NON_STICK;
786 return 0;
787}
788
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100789/* Parse the "track" server keyword */
790static int srv_parse_track(char **args, int *cur_arg,
791 struct proxy *curproxy, struct server *newsrv, char **err)
792{
793 char *arg;
794
795 arg = args[*cur_arg + 1];
796 if (!*arg) {
797 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
798 return ERR_ALERT | ERR_FATAL;
799 }
800
801 free(newsrv->trackit);
802 newsrv->trackit = strdup(arg);
803
804 return 0;
805}
806
Frédéric Lécailledba97072017-03-17 15:33:50 +0100807
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200808/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200809 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200810 * shutdown.
811 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200812void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200813{
Willy Tarreau87b09662015-04-03 00:22:06 +0200814 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200815
Willy Tarreau87b09662015-04-03 00:22:06 +0200816 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
817 if (stream->srv_conn == srv)
818 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200819}
820
821/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200822 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200823 * the reason for the shutdown.
824 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200825void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200826{
827 struct server *srv;
828
829 for (srv = px->srv; srv != NULL; srv = srv->next)
830 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200831 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200832}
833
Willy Tarreaubda92272014-05-20 21:55:30 +0200834/* Appends some information to a message string related to a server going UP or
835 * DOWN. If both <forced> and <reason> are null and the server tracks another
836 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200837 * If <check> is non-null, an entire string describing the check result will be
838 * appended after a comma and a space (eg: to report some information from the
839 * check that changed the state). In the other case, the string will be built
840 * using the check results stored into the struct server if present.
841 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200842 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200843 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200844void srv_append_status(struct buffer *msg, struct server *s,
845 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200846{
Emeric Brun5a133512017-10-19 14:42:30 +0200847 short status = s->op_st_chg.status;
848 short code = s->op_st_chg.code;
849 long duration = s->op_st_chg.duration;
850 char *desc = s->op_st_chg.reason;
851
852 if (check) {
853 status = check->status;
854 code = check->code;
855 duration = check->duration;
856 desc = check->desc;
857 }
858
859 if (status != -1) {
860 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
861
862 if (status >= HCHK_STATUS_L57DATA)
863 chunk_appendf(msg, ", code: %d", code);
864
865 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200866 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200867
868 chunk_appendf(msg, ", info: \"");
869
870 chunk_initlen(&src, desc, 0, strlen(desc));
871 chunk_asciiencode(msg, &src, '"');
872
873 chunk_appendf(msg, "\"");
874 }
875
876 if (duration >= 0)
877 chunk_appendf(msg, ", check duration: %ldms", duration);
878 }
879 else if (desc && *desc) {
880 chunk_appendf(msg, ", %s", desc);
881 }
882 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200883 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200884 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200885
886 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200887 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200888 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
889 " %d sessions active, %d requeued, %d remaining in queue",
890 s->proxy->srv_act, s->proxy->srv_bck,
891 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
892 s->cur_sess, xferred, s->nbpend);
893 else
894 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
895 " %d sessions requeued, %d total in queue",
896 s->proxy->srv_act, s->proxy->srv_bck,
897 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
898 xferred, s->nbpend);
899 }
900}
901
Emeric Brun5a133512017-10-19 14:42:30 +0200902/* Marks server <s> down, regardless of its checks' statuses. The server is
903 * registered in a list to postpone the counting of the remaining servers on
904 * the proxy and transfers queued streams whenever possible to other servers at
905 * a sync point. Maintenance servers are ignored. It stores the <reason> if
906 * non-null as the reason for going down or the available data from the check
907 * struct to recompute this reason later.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200908 */
Emeric Brun5a133512017-10-19 14:42:30 +0200909void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200910{
911 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200912
Emeric Brun64cc49c2017-10-03 14:46:45 +0200913 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200914 return;
915
Emeric Brun52a91d32017-08-31 14:41:55 +0200916 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200917 *s->op_st_chg.reason = 0;
918 s->op_st_chg.status = -1;
919 if (reason) {
920 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
921 }
922 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100923 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200924 s->op_st_chg.code = check->code;
925 s->op_st_chg.status = check->status;
926 s->op_st_chg.duration = check->duration;
927 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200928
Emeric Brun9f0b4582017-10-23 14:39:51 +0200929 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100930 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200931 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100932 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200933 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +0200934
935 /* propagate changes */
936 thread_isolate();
937 srv_update_status(s);
938 thread_release();
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200939}
940
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200941/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200942 * in maintenance. The server is registered in a list to postpone the counting
943 * of the remaining servers on the proxy and tries to grab requests from the
944 * proxy at a sync point. Maintenance servers are ignored. It stores the
945 * <reason> if non-null as the reason for going down or the available data
946 * from the check struct to recompute this reason later.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200947 */
Emeric Brun5a133512017-10-19 14:42:30 +0200948void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200949{
950 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200951
Emeric Brun64cc49c2017-10-03 14:46:45 +0200952 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200953 return;
954
Emeric Brun52a91d32017-08-31 14:41:55 +0200955 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200956 return;
957
Emeric Brun52a91d32017-08-31 14:41:55 +0200958 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200959 *s->op_st_chg.reason = 0;
960 s->op_st_chg.status = -1;
961 if (reason) {
962 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
963 }
964 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100965 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200966 s->op_st_chg.code = check->code;
967 s->op_st_chg.status = check->status;
968 s->op_st_chg.duration = check->duration;
969 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200970
Emeric Brun64cc49c2017-10-03 14:46:45 +0200971 if (s->slowstart <= 0)
972 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200973
Emeric Brun9f0b4582017-10-23 14:39:51 +0200974 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100975 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200976 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100977 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200978 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +0200979
980 /* propagate changes */
981 thread_isolate();
982 srv_update_status(s);
983 thread_release();
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200984}
985
Willy Tarreau8eb77842014-05-21 13:54:57 +0200986/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +0200987 * isn't in maintenance. The server is registered in a list to postpone the
988 * counting of the remaining servers on the proxy and tries to grab requests
989 * from the proxy. Maintenance servers are ignored. It stores the
990 * <reason> if non-null as the reason for going down or the available data
991 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +0200992 * up. Note that it makes use of the trash to build the log strings, so <reason>
993 * must not be placed there.
994 */
Emeric Brun5a133512017-10-19 14:42:30 +0200995void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200996{
997 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200998
Emeric Brun64cc49c2017-10-03 14:46:45 +0200999 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001000 return;
1001
Emeric Brun52a91d32017-08-31 14:41:55 +02001002 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001003 return;
1004
Emeric Brun52a91d32017-08-31 14:41:55 +02001005 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001006 *s->op_st_chg.reason = 0;
1007 s->op_st_chg.status = -1;
1008 if (reason) {
1009 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1010 }
1011 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001012 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001013 s->op_st_chg.code = check->code;
1014 s->op_st_chg.status = check->status;
1015 s->op_st_chg.duration = check->duration;
1016 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001017
Emeric Brun9f0b4582017-10-23 14:39:51 +02001018 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001019 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001020 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001021 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001022 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001023
1024 /* propagate changes */
1025 thread_isolate();
1026 srv_update_status(s);
1027 thread_release();
Willy Tarreau8eb77842014-05-21 13:54:57 +02001028}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001029
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001030/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1031 * enforce either maint mode or drain mode. It is not allowed to set more than
1032 * one flag at once. The equivalent "inherited" flag is propagated to all
1033 * tracking servers. Maintenance mode disables health checks (but not agent
1034 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001035 * is done. If <cause> is non-null, it will be displayed at the end of the log
1036 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001037 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001038void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001039{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001040 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001041
1042 if (!mode)
1043 return;
1044
1045 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001046 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001047 return;
1048
Emeric Brun52a91d32017-08-31 14:41:55 +02001049 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001050 if (cause)
1051 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1052
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001053 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001054 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1055 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001056 goto end;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001057
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001058 /* compute the inherited flag to propagate */
1059 if (mode & SRV_ADMF_MAINT)
1060 mode = SRV_ADMF_IMAINT;
1061 else if (mode & SRV_ADMF_DRAIN)
1062 mode = SRV_ADMF_IDRAIN;
1063
Emeric Brun9f0b4582017-10-23 14:39:51 +02001064 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001065 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001066 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001067 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001068 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001069
1070 end:
1071 /* propagate changes */
1072 thread_isolate();
1073 srv_update_status(s);
1074 thread_release();
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001075}
1076
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001077/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1078 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1079 * than one flag at once. The equivalent "inherited" flag is propagated to all
1080 * tracking servers. Leaving maintenance mode re-enables health checks. When
1081 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001082 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001083void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001084{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001085 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001086
1087 if (!mode)
1088 return;
1089
1090 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001091 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001092 return;
1093
Emeric Brun52a91d32017-08-31 14:41:55 +02001094 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001095
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001096 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001097 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1098 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001099 goto end;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001100
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001101 if (mode & SRV_ADMF_MAINT)
1102 mode = SRV_ADMF_IMAINT;
1103 else if (mode & SRV_ADMF_DRAIN)
1104 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001105
Emeric Brun9f0b4582017-10-23 14:39:51 +02001106 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001107 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001108 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001109 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001110 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001111
1112 end:
1113 /* propagate changes */
1114 thread_isolate();
1115 srv_update_status(s);
1116 thread_release();
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001117}
1118
Willy Tarreau757478e2016-11-03 19:22:19 +01001119/* principle: propagate maint and drain to tracking servers. This is useful
1120 * upon startup so that inherited states are correct.
1121 */
1122static void srv_propagate_admin_state(struct server *srv)
1123{
1124 struct server *srv2;
1125
1126 if (!srv->trackers)
1127 return;
1128
1129 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001130 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001131 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001132 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001133
Emeric Brun52a91d32017-08-31 14:41:55 +02001134 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001135 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001136 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001137 }
1138}
1139
1140/* Compute and propagate the admin states for all servers in proxy <px>.
1141 * Only servers *not* tracking another one are considered, because other
1142 * ones will be handled when the server they track is visited.
1143 */
1144void srv_compute_all_admin_states(struct proxy *px)
1145{
1146 struct server *srv;
1147
1148 for (srv = px->srv; srv; srv = srv->next) {
1149 if (srv->track)
1150 continue;
1151 srv_propagate_admin_state(srv);
1152 }
1153}
1154
Willy Tarreaudff55432012-10-10 17:51:05 +02001155/* Note: must not be declared <const> as its list will be overwritten.
1156 * Please take care of keeping this list alphabetically sorted, doing so helps
1157 * all code contributors.
1158 * Optional keywords are also declared with a NULL ->parse() function so that
1159 * the config parser can report an appropriate error when a known keyword was
1160 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001161 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001162 */
1163static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001164 { "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 +01001165 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001166 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001167 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001168 { "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 +01001169 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001170 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1171 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001172 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001173 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001174 { "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 +01001175 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001176 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001177 { "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 +01001178 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1179 { "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 +01001180 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001181 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001182 { "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 +01001183 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001184 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1185 { "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 +02001186 { "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 +01001187 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001188 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001189 { NULL, NULL, 0 },
1190}};
1191
1192__attribute__((constructor))
1193static void __listener_init(void)
1194{
1195 srv_register_keywords(&srv_kws);
1196}
1197
Willy Tarreau004e0452013-11-21 11:22:01 +01001198/* Recomputes the server's eweight based on its state, uweight, the current time,
1199 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001200 * state is automatically disabled if the time is elapsed. If <must_update> is
1201 * not zero, the update will be propagated immediately.
Willy Tarreau004e0452013-11-21 11:22:01 +01001202 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001203void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001204{
1205 struct proxy *px = sv->proxy;
1206 unsigned w;
1207
1208 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1209 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001210 if (sv->next_state == SRV_ST_STARTING)
1211 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001212 }
1213
1214 /* We must take care of not pushing the server to full throttle during slow starts.
1215 * It must also start immediately, at least at the minimal step when leaving maintenance.
1216 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001217 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001218 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1219 else
1220 w = px->lbprm.wdiv;
1221
Emeric Brun52a91d32017-08-31 14:41:55 +02001222 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001223
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001224 /* propagate changes only if needed (i.e. not recursively) */
1225 if (must_update) {
1226 thread_isolate();
1227 srv_update_status(sv);
1228 thread_release();
1229 }
Willy Tarreau004e0452013-11-21 11:22:01 +01001230}
1231
Willy Tarreaubaaee002006-06-26 02:48:02 +02001232/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001233 * Parses weight_str and configures sv accordingly.
1234 * Returns NULL on success, error message string otherwise.
1235 */
1236const char *server_parse_weight_change_request(struct server *sv,
1237 const char *weight_str)
1238{
1239 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001240 long int w;
1241 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001242
1243 px = sv->proxy;
1244
1245 /* if the weight is terminated with '%', it is set relative to
1246 * the initial weight, otherwise it is absolute.
1247 */
1248 if (!*weight_str)
1249 return "Require <weight> or <weight%>.\n";
1250
Simon Hormanb796afa2013-02-12 10:45:53 +09001251 w = strtol(weight_str, &end, 10);
1252 if (end == weight_str)
1253 return "Empty weight string empty or preceded by garbage";
1254 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001255 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001256 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001257 /* Avoid integer overflow */
1258 if (w > 25600)
1259 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001260 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001261 if (w > 256)
1262 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001263 }
1264 else if (w < 0 || w > 256)
1265 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001266 else if (end[0] != '\0')
1267 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001268
1269 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1270 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1271
1272 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001273 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001274
1275 return NULL;
1276}
1277
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001278/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001279 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1280 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001281 * Returns:
1282 * - error string on error
1283 * - NULL on success
1284 */
1285const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001286 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001287{
1288 unsigned char ip[INET6_ADDRSTRLEN];
1289
1290 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001291 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001292 return NULL;
1293 }
1294 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001295 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001296 return NULL;
1297 }
1298
1299 return "Could not understand IP address format.\n";
1300}
1301
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001302const char *server_parse_maxconn_change_request(struct server *sv,
1303 const char *maxconn_str)
1304{
1305 long int v;
1306 char *end;
1307
1308 if (!*maxconn_str)
1309 return "Require <maxconn>.\n";
1310
1311 v = strtol(maxconn_str, &end, 10);
1312 if (end == maxconn_str)
1313 return "maxconn string empty or preceded by garbage";
1314 else if (end[0] != '\0')
1315 return "Trailing garbage in maxconn string";
1316
1317 if (sv->maxconn == sv->minconn) { // static maxconn
1318 sv->maxconn = sv->minconn = v;
1319 } else { // dynamic maxconn
1320 sv->maxconn = v;
1321 }
1322
1323 if (may_dequeue_tasks(sv, sv->proxy))
1324 process_srv_queue(sv);
1325
1326 return NULL;
1327}
1328
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001329#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001330static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1331 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001332{
1333 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001334 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001335 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001336 NULL,
1337 };
1338
1339 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001340 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001341
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001342 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1343}
1344
1345static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1346{
1347 struct sample_expr *expr;
1348
1349 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 +01001350 if (!expr) {
1351 memprintf(err, "error detected while parsing sni expression : %s", *err);
1352 return ERR_ALERT | ERR_FATAL;
1353 }
1354
1355 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1356 memprintf(err, "error detected while parsing sni expression : "
1357 " fetch method '%s' extracts information from '%s', "
1358 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001359 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001360 return ERR_ALERT | ERR_FATAL;
1361 }
1362
1363 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1364 release_sample_expr(newsrv->ssl_ctx.sni);
1365 newsrv->ssl_ctx.sni = expr;
1366
1367 return 0;
1368}
1369#endif
1370
1371static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1372{
1373 if (err && *err) {
1374 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001375 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 +01001376 }
1377 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001378 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1379 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001380}
1381
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001382static void srv_conn_src_sport_range_cpy(struct server *srv,
1383 struct server *src)
1384{
1385 int range_sz;
1386
1387 range_sz = src->conn_src.sport_range->size;
1388 if (range_sz > 0) {
1389 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1390 if (srv->conn_src.sport_range != NULL) {
1391 int i;
1392
1393 for (i = 0; i < range_sz; i++) {
1394 srv->conn_src.sport_range->ports[i] =
1395 src->conn_src.sport_range->ports[i];
1396 }
1397 }
1398 }
1399}
1400
1401/*
1402 * Copy <src> server connection source settings to <srv> server everything needed.
1403 */
1404static void srv_conn_src_cpy(struct server *srv, struct server *src)
1405{
1406 srv->conn_src.opts = src->conn_src.opts;
1407 srv->conn_src.source_addr = src->conn_src.source_addr;
1408
1409 /* Source port range copy. */
1410 if (src->conn_src.sport_range != NULL)
1411 srv_conn_src_sport_range_cpy(srv, src);
1412
1413#ifdef CONFIG_HAP_TRANSPARENT
1414 if (src->conn_src.bind_hdr_name != NULL) {
1415 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1416 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1417 }
1418 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1419 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1420#endif
1421 if (src->conn_src.iface_name != NULL)
1422 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1423}
1424
1425/*
1426 * Copy <src> server SSL settings to <srv> server allocating
1427 * everything needed.
1428 */
1429#if defined(USE_OPENSSL)
1430static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1431{
1432 if (src->ssl_ctx.ca_file != NULL)
1433 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1434 if (src->ssl_ctx.crl_file != NULL)
1435 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1436 if (src->ssl_ctx.client_crt != NULL)
1437 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1438
1439 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1440
1441 if (src->ssl_ctx.verify_host != NULL)
1442 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1443 if (src->ssl_ctx.ciphers != NULL)
1444 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1445 if (src->sni_expr != NULL)
1446 srv->sni_expr = strdup(src->sni_expr);
1447}
1448#endif
1449
1450/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001451 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001452 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001453 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001454 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001455static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001456{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001457 char *hostname_dn;
1458 int hostname_len, hostname_dn_len;
1459
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001460 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001461 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001462
Christopher Faulet67957bd2017-09-27 11:00:59 +02001463 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001464 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001465 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1466 hostname_dn, trash.size);
1467 if (hostname_dn_len == -1)
1468 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001469
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001470
Christopher Faulet67957bd2017-09-27 11:00:59 +02001471 free(srv->hostname);
1472 free(srv->hostname_dn);
1473 srv->hostname = strdup(hostname);
1474 srv->hostname_dn = strdup(hostname_dn);
1475 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001476 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001477 goto err;
1478
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001479 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001480
1481 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001482 free(srv->hostname); srv->hostname = NULL;
1483 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001484 return -1;
1485}
1486
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001487/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001488 * Copy <src> server settings to <srv> server allocating
1489 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001490 * This function is not supposed to be called at any time, but only
1491 * during server settings parsing or during server allocations from
1492 * a server template, and just after having calloc()'ed a new server.
1493 * So, <src> may only be a default server (when parsing server settings)
1494 * or a server template (during server allocations from a server template).
1495 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1496 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001497 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001498static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001499{
1500 /* Connection source settings copy */
1501 srv_conn_src_cpy(srv, src);
1502
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001503 if (srv_tmpl) {
1504 srv->addr = src->addr;
1505 srv->svc_port = src->svc_port;
1506 }
1507
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001508 srv->pp_opts = src->pp_opts;
1509 if (src->rdr_pfx != NULL) {
1510 srv->rdr_pfx = strdup(src->rdr_pfx);
1511 srv->rdr_len = src->rdr_len;
1512 }
1513 if (src->cookie != NULL) {
1514 srv->cookie = strdup(src->cookie);
1515 srv->cklen = src->cklen;
1516 }
1517 srv->use_ssl = src->use_ssl;
1518 srv->check.addr = srv->agent.addr = src->check.addr;
1519 srv->check.use_ssl = src->check.use_ssl;
1520 srv->check.port = src->check.port;
1521 /* Note: 'flags' field has potentially been already initialized. */
1522 srv->flags |= src->flags;
1523 srv->do_check = src->do_check;
1524 srv->do_agent = src->do_agent;
1525 if (srv->check.port)
1526 srv->flags |= SRV_F_CHECKPORT;
1527 srv->check.inter = src->check.inter;
1528 srv->check.fastinter = src->check.fastinter;
1529 srv->check.downinter = src->check.downinter;
1530 srv->agent.use_ssl = src->agent.use_ssl;
1531 srv->agent.port = src->agent.port;
1532 if (src->agent.send_string != NULL)
1533 srv->agent.send_string = strdup(src->agent.send_string);
1534 srv->agent.send_string_len = src->agent.send_string_len;
1535 srv->agent.inter = src->agent.inter;
1536 srv->agent.fastinter = src->agent.fastinter;
1537 srv->agent.downinter = src->agent.downinter;
1538 srv->maxqueue = src->maxqueue;
1539 srv->minconn = src->minconn;
1540 srv->maxconn = src->maxconn;
1541 srv->slowstart = src->slowstart;
1542 srv->observe = src->observe;
1543 srv->onerror = src->onerror;
1544 srv->onmarkeddown = src->onmarkeddown;
1545 srv->onmarkedup = src->onmarkedup;
1546 if (src->trackit != NULL)
1547 srv->trackit = strdup(src->trackit);
1548 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1549 srv->uweight = srv->iweight = src->iweight;
1550
1551 srv->check.send_proxy = src->check.send_proxy;
1552 /* health: up, but will fall down at first failure */
1553 srv->check.rise = srv->check.health = src->check.rise;
1554 srv->check.fall = src->check.fall;
1555
1556 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001557 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1558 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1559 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001560 srv->check.state |= CHK_ST_PAUSED;
1561 srv->check.health = 0;
1562 }
1563
1564 /* health: up but will fall down at first failure */
1565 srv->agent.rise = srv->agent.health = src->agent.rise;
1566 srv->agent.fall = src->agent.fall;
1567
1568 if (src->resolvers_id != NULL)
1569 srv->resolvers_id = strdup(src->resolvers_id);
1570 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001571 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001572 if (srv->dns_opts.family_prio == AF_UNSPEC)
1573 srv->dns_opts.family_prio = AF_INET6;
1574 memcpy(srv->dns_opts.pref_net,
1575 src->dns_opts.pref_net,
1576 sizeof srv->dns_opts.pref_net);
1577 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1578
1579 srv->init_addr_methods = src->init_addr_methods;
1580 srv->init_addr = src->init_addr;
1581#if defined(USE_OPENSSL)
1582 srv_ssl_settings_cpy(srv, src);
1583#endif
1584#ifdef TCP_USER_TIMEOUT
1585 srv->tcp_ut = src->tcp_ut;
1586#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001587 if (srv_tmpl)
1588 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001589}
1590
1591static struct server *new_server(struct proxy *proxy)
1592{
1593 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001594 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001595
1596 srv = calloc(1, sizeof *srv);
1597 if (!srv)
1598 return NULL;
1599
1600 srv->obj_type = OBJ_TYPE_SERVER;
1601 srv->proxy = proxy;
1602 LIST_INIT(&srv->actconns);
1603 LIST_INIT(&srv->pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02001604
1605 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1606 goto free_srv;
1607 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1608 goto free_priv_conns;
1609 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1610 goto free_idle_conns;
1611
1612 for (i = 0; i < global.nbthread; i++) {
1613 LIST_INIT(&srv->priv_conns[i]);
1614 LIST_INIT(&srv->idle_conns[i]);
1615 LIST_INIT(&srv->safe_conns[i]);
1616 }
1617
Emeric Brun52a91d32017-08-31 14:41:55 +02001618 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001619 srv->last_change = now.tv_sec;
1620
1621 srv->check.status = HCHK_STATUS_INI;
1622 srv->check.server = srv;
1623 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1624
1625 srv->agent.status = HCHK_STATUS_INI;
1626 srv->agent.server = srv;
1627 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1628
1629 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001630
1631 free_idle_conns:
1632 free(srv->idle_conns);
1633 free_priv_conns:
1634 free(srv->priv_conns);
1635 free_srv:
1636 free(srv);
1637 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001638}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001639
1640/*
1641 * Validate <srv> server health-check settings.
1642 * Returns 0 if everything is OK, -1 if not.
1643 */
1644static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1645{
1646 struct tcpcheck_rule *r = NULL;
1647 struct list *l;
1648
1649 /*
1650 * We need at least a service port, a check port or the first tcp-check rule must
1651 * be a 'connect' one when checking an IPv4/IPv6 server.
1652 */
1653 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1654 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1655 return 0;
1656
1657 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1658 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001659 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1660 "Check has been disabled.\n",
1661 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001662 return -1;
1663 }
1664
1665 /* search the first action (connect / send / expect) in the list */
1666 l = &srv->proxy->tcpcheck_rules;
1667 list_for_each_entry(r, l, list) {
1668 if (r->action != TCPCHK_ACT_COMMENT)
1669 break;
1670 }
1671
1672 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001673 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1674 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1675 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001676 return -1;
1677 }
1678
1679 /* scan the tcp-check ruleset to ensure a port has been configured */
1680 l = &srv->proxy->tcpcheck_rules;
1681 list_for_each_entry(r, l, list) {
1682 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001683 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1684 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1685 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001686 return -1;
1687 }
1688 }
1689
1690 return 0;
1691}
1692
1693/*
1694 * Initialize <srv> health-check structure.
1695 * Returns the error string in case of memory allocation failure, NULL if not.
1696 */
1697static const char *do_health_check_init(struct server *srv, int check_type, int state)
1698{
1699 const char *ret;
1700
1701 if (!srv->do_check)
1702 return NULL;
1703
1704 ret = init_check(&srv->check, check_type);
1705 if (ret)
1706 return ret;
1707
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001708 srv->check.state |= state;
1709 global.maxsock++;
1710
1711 return NULL;
1712}
1713
1714static int server_health_check_init(const char *file, int linenum,
1715 struct server *srv, struct proxy *curproxy)
1716{
1717 const char *ret;
1718
1719 if (!srv->do_check)
1720 return 0;
1721
1722 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001723 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1724 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001725 return ERR_ALERT | ERR_FATAL;
1726 }
1727
1728 if (server_healthcheck_validate(file, linenum, srv) < 0)
1729 return ERR_ALERT | ERR_ABORT;
1730
1731 /* note: check type will be set during the config review phase */
1732 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1733 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001734 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001735 return ERR_ALERT | ERR_ABORT;
1736 }
1737
1738 return 0;
1739}
1740
1741/*
1742 * Initialize <srv> agent check structure.
1743 * Returns the error string in case of memory allocation failure, NULL if not.
1744 */
1745static const char *do_server_agent_check_init(struct server *srv, int state)
1746{
1747 const char *ret;
1748
1749 if (!srv->do_agent)
1750 return NULL;
1751
1752 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1753 if (ret)
1754 return ret;
1755
1756 if (!srv->agent.inter)
1757 srv->agent.inter = srv->check.inter;
1758
1759 srv->agent.state |= state;
1760 global.maxsock++;
1761
1762 return NULL;
1763}
1764
1765static int server_agent_check_init(const char *file, int linenum,
1766 struct server *srv, struct proxy *curproxy)
1767{
1768 const char *ret;
1769
1770 if (!srv->do_agent)
1771 return 0;
1772
1773 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001774 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 +02001775 file, linenum, srv->id);
1776 return ERR_ALERT | ERR_FATAL;
1777 }
1778
1779 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1780 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001781 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001782 return ERR_ALERT | ERR_ABORT;
1783 }
1784
1785 return 0;
1786}
1787
1788#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1789static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1790 struct server *srv, struct proxy *proxy)
1791{
1792 int ret;
1793 char *err = NULL;
1794
1795 if (!srv->sni_expr)
1796 return 0;
1797
1798 ret = server_parse_sni_expr(srv, proxy, &err);
1799 if (!ret)
1800 return 0;
1801
1802 display_parser_err(file, linenum, args, cur_arg, &err);
1803 free(err);
1804
1805 return ret;
1806}
1807#endif
1808
1809/*
1810 * Server initializations finalization.
1811 * Initialize health check, agent check and SNI expression if enabled.
1812 * Must not be called for a default server instance.
1813 */
1814static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1815 struct server *srv, struct proxy *px)
1816{
1817 int ret;
1818
1819 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1820 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1821 return ret;
1822 }
1823
1824#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1825 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1826 return ret;
1827#endif
1828
1829 if (srv->flags & SRV_F_BACKUP)
1830 px->srv_bck++;
1831 else
1832 px->srv_act++;
1833 srv_lb_commit_status(srv);
1834
1835 return 0;
1836}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001837
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001838/*
1839 * Parse as much as possible such a range string argument: low[-high]
1840 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1841 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1842 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1843 * Returns 0 if succeeded, -1 if not.
1844 */
1845static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1846{
1847 char *nb_high_arg;
1848
1849 *nb_high = 0;
1850 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001851 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001852
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001853 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001854 *nb_high_arg++ = '\0';
1855 *nb_high = atoi(nb_high_arg);
1856 }
1857 else {
1858 *nb_high += *nb_low;
1859 *nb_low = 1;
1860 }
1861
1862 if (*nb_low < 0 || *nb_high < *nb_low)
1863 return -1;
1864
1865 return 0;
1866}
1867
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001868static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1869{
1870 chunk_printf(&trash, "%s%d", prefix, nb);
1871 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001872 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001873}
1874
1875/*
1876 * Initialize as much as possible servers from <srv> server template.
1877 * Note that a server template is a special server with
1878 * a few different parameters than a server which has
1879 * been parsed mostly the same way as a server.
1880 * Returns the number of servers succesfully allocated,
1881 * 'srv' template included.
1882 */
1883static int server_template_init(struct server *srv, struct proxy *px)
1884{
1885 int i;
1886 struct server *newsrv;
1887
1888 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1889 int check_init_state;
1890 int agent_init_state;
1891
1892 newsrv = new_server(px);
1893 if (!newsrv)
1894 goto err;
1895
1896 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001897 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001898#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1899 if (newsrv->sni_expr) {
1900 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1901 if (!newsrv->ssl_ctx.sni)
1902 goto err;
1903 }
1904#endif
1905 /* Set this new server ID. */
1906 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1907
1908 /* Initial checks states. */
1909 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1910 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1911
1912 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1913 do_server_agent_check_init(newsrv, agent_init_state))
1914 goto err;
1915
1916 /* Linked backwards first. This will be restablished after parsing. */
1917 newsrv->next = px->srv;
1918 px->srv = newsrv;
1919 }
1920 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1921
1922 return i - srv->tmpl_info.nb_low;
1923
1924 err:
1925 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1926 if (newsrv) {
1927#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1928 release_sample_expr(newsrv->ssl_ctx.sni);
1929#endif
1930 free_check(&newsrv->agent);
1931 free_check(&newsrv->check);
1932 }
1933 free(newsrv);
1934 return i - srv->tmpl_info.nb_low;
1935}
1936
Willy Tarreau272adea2014-03-31 10:39:59 +02001937int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1938{
1939 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001940 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001941 char *errmsg = NULL;
1942 int err_code = 0;
1943 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001944 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001945
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001946 if (!strcmp(args[0], "server") ||
1947 !strcmp(args[0], "default-server") ||
1948 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001949 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001950 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001951 int srv = !defsrv && !strcmp(args[0], "server");
1952 int srv_tmpl = !defsrv && !srv;
1953 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001954
1955 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001956 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001957 err_code |= ERR_ALERT | ERR_FATAL;
1958 goto out;
1959 }
1960 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02001961 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02001962
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001963 /* There is no mandatory first arguments for default server. */
1964 if (srv) {
1965 if (!*args[2]) {
1966 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001967 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001968 file, linenum, args[0]);
1969 err_code |= ERR_ALERT | ERR_FATAL;
1970 goto out;
1971 }
1972
1973 err = invalid_char(args[1]);
1974 }
1975 else if (srv_tmpl) {
1976 if (!*args[3]) {
1977 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001978 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 +02001979 file, linenum, args[0]);
1980 err_code |= ERR_ALERT | ERR_FATAL;
1981 goto out;
1982 }
1983
1984 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001985 }
1986
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001987 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001988 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 +02001989 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001990 err_code |= ERR_ALERT | ERR_FATAL;
1991 goto out;
1992 }
1993
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001994 cur_arg = 2;
1995 if (srv_tmpl) {
1996 /* Parse server-template <nb | range> arg. */
1997 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001998 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001999 file, linenum, args[0], args[cur_arg]);
2000 err_code |= ERR_ALERT | ERR_FATAL;
2001 goto out;
2002 }
2003 cur_arg++;
2004 }
2005
Willy Tarreau272adea2014-03-31 10:39:59 +02002006 if (!defsrv) {
2007 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002008 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002009 struct protocol *proto;
2010
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002011 newsrv = new_server(curproxy);
2012 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002013 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002014 err_code |= ERR_ALERT | ERR_ABORT;
2015 goto out;
2016 }
2017
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002018 if (srv_tmpl) {
2019 newsrv->tmpl_info.nb_low = tmpl_range_low;
2020 newsrv->tmpl_info.nb_high = tmpl_range_high;
2021 }
2022
Willy Tarreau272adea2014-03-31 10:39:59 +02002023 /* the servers are linked backwards first */
2024 newsrv->next = curproxy->srv;
2025 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002026 newsrv->conf.file = strdup(file);
2027 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002028 /* Note: for a server template, its id is its prefix.
2029 * This is a temporary id which will be used for server allocations to come
2030 * after parsing.
2031 */
2032 if (srv)
2033 newsrv->id = strdup(args[1]);
2034 else
2035 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002036
2037 /* several ways to check the port component :
2038 * - IP => port=+0, relative (IPv4 only)
2039 * - IP: => port=+0, relative
2040 * - IP:N => port=N, absolute
2041 * - IP:+N => port=+N, relative
2042 * - IP:-N => port=-N, relative
2043 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002044 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002045 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002046 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002047 err_code |= ERR_ALERT | ERR_FATAL;
2048 goto out;
2049 }
2050
2051 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002052 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002053 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002054 file, linenum, args[0], args[1]);
2055 err_code |= ERR_ALERT | ERR_FATAL;
2056 goto out;
2057 }
2058
2059 if (!port1 || !port2) {
2060 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002061 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002062 }
2063 else if (port1 != port2) {
2064 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002065 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002066 file, linenum, args[0], args[1], args[2]);
2067 err_code |= ERR_ALERT | ERR_FATAL;
2068 goto out;
2069 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002070
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002071 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002072 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002073 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002074 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002075 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2076 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2077 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002078 err_code |= ERR_ALERT | ERR_FATAL;
2079 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002080 }
2081 }
2082 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002083 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002084 file, linenum, newsrv->id);
2085 err_code |= ERR_ALERT | ERR_FATAL;
2086 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002087 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002088 }
2089
Willy Tarreau272adea2014-03-31 10:39:59 +02002090 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002091 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002092
Olivier Houchard8da5f982017-08-04 18:35:36 +02002093 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002094 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002095 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002096 err_code |= ERR_ALERT | ERR_FATAL;
2097 goto out;
2098 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002099
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002100 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002101 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002102 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002103 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002104 } else {
2105 newsrv = &curproxy->defsrv;
2106 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002107 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002108 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002109 }
2110
2111 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002112 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002113 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2114 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002115 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002116 file, linenum, *err, newsrv->id);
2117 err_code |= ERR_ALERT | ERR_FATAL;
2118 goto out;
2119 }
2120 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002121 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002122 file, linenum, val, args[cur_arg], newsrv->id);
2123 err_code |= ERR_ALERT | ERR_FATAL;
2124 goto out;
2125 }
2126 newsrv->agent.inter = val;
2127 cur_arg += 2;
2128 }
Misiekea849332017-01-09 09:39:51 +01002129 else if (!strcmp(args[cur_arg], "agent-addr")) {
2130 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002131 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002132 goto out;
2133 }
2134
2135 cur_arg += 2;
2136 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002137 else if (!strcmp(args[cur_arg], "agent-port")) {
2138 global.maxsock++;
2139 newsrv->agent.port = atol(args[cur_arg + 1]);
2140 cur_arg += 2;
2141 }
James Brown55f9ff12015-10-21 18:19:05 -07002142 else if (!strcmp(args[cur_arg], "agent-send")) {
2143 global.maxsock++;
2144 free(newsrv->agent.send_string);
2145 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2146 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2147 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2148 cur_arg += 2;
2149 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002150 else if (!strcmp(args[cur_arg], "init-addr")) {
2151 char *p, *end;
2152 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002153 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002154
2155 newsrv->init_addr_methods = 0;
2156 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2157
2158 for (p = args[cur_arg + 1]; *p; p = end) {
2159 /* cut on next comma */
2160 for (end = p; *end && *end != ','; end++);
2161 if (*end)
2162 *(end++) = 0;
2163
Willy Tarreau4310d362016-11-02 15:05:56 +01002164 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002165 if (!strcmp(p, "libc")) {
2166 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2167 }
2168 else if (!strcmp(p, "last")) {
2169 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2170 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002171 else if (!strcmp(p, "none")) {
2172 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2173 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002174 else if (str2ip2(p, &sa, 0)) {
2175 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002176 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002177 file, linenum, args[cur_arg], p);
2178 err_code |= ERR_ALERT | ERR_FATAL;
2179 goto out;
2180 }
2181 newsrv->init_addr = sa;
2182 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2183 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002184 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002185 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 +02002186 file, linenum, args[cur_arg], p);
2187 err_code |= ERR_ALERT | ERR_FATAL;
2188 goto out;
2189 }
2190 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002191 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002192 file, linenum, args[cur_arg], p);
2193 err_code |= ERR_ALERT | ERR_FATAL;
2194 goto out;
2195 }
2196 }
2197 cur_arg += 2;
2198 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002199 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002200 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002201 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2202 cur_arg += 2;
2203 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002204 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2205 char *p, *end;
2206
2207 for (p = args[cur_arg + 1]; *p; p = end) {
2208 /* cut on next comma */
2209 for (end = p; *end && *end != ','; end++);
2210 if (*end)
2211 *(end++) = 0;
2212
2213 if (!strcmp(p, "allow-dup-ip")) {
2214 newsrv->dns_opts.accept_duplicate_ip = 1;
2215 }
2216 else if (!strcmp(p, "prevent-dup-ip")) {
2217 newsrv->dns_opts.accept_duplicate_ip = 0;
2218 }
2219 else {
2220 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2221 file, linenum, args[cur_arg], p);
2222 err_code |= ERR_ALERT | ERR_FATAL;
2223 goto out;
2224 }
2225 }
2226
2227 cur_arg += 2;
2228 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002229 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2230 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002231 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002232 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002233 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002234 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002235 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002236 file, linenum, args[cur_arg]);
2237 err_code |= ERR_ALERT | ERR_FATAL;
2238 goto out;
2239 }
2240 cur_arg += 2;
2241 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002242 else if (!strcmp(args[cur_arg], "resolve-net")) {
2243 char *p, *e;
2244 unsigned char mask;
2245 struct dns_options *opt;
2246
2247 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002248 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002249 file, linenum, args[cur_arg]);
2250 err_code |= ERR_ALERT | ERR_FATAL;
2251 goto out;
2252 }
2253
2254 opt = &newsrv->dns_opts;
2255
2256 /* Split arguments by comma, and convert it from ipv4 or ipv6
2257 * string network in in_addr or in6_addr.
2258 */
2259 p = args[cur_arg + 1];
2260 e = p;
2261 while (*p != '\0') {
2262 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002263 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002264 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002265 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2266 err_code |= ERR_ALERT | ERR_FATAL;
2267 goto out;
2268 }
2269 /* look for end or comma. */
2270 while (*e != ',' && *e != '\0')
2271 e++;
2272 if (*e == ',') {
2273 *e = '\0';
2274 e++;
2275 }
2276 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2277 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2278 /* Try to convert input string from ipv4 or ipv6 network. */
2279 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2280 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2281 &mask)) {
2282 /* Try to convert input string from ipv6 network. */
2283 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2284 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2285 } else {
2286 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002287 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002288 file, linenum, args[cur_arg], p);
2289 err_code |= ERR_ALERT | ERR_FATAL;
2290 goto out;
2291 }
2292 opt->pref_net_nb++;
2293 p = e;
2294 }
2295
2296 cur_arg += 2;
2297 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002298 else if (!strcmp(args[cur_arg], "rise")) {
2299 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002300 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002301 file, linenum, args[cur_arg]);
2302 err_code |= ERR_ALERT | ERR_FATAL;
2303 goto out;
2304 }
2305
2306 newsrv->check.rise = atol(args[cur_arg + 1]);
2307 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002308 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002309 file, linenum, args[cur_arg]);
2310 err_code |= ERR_ALERT | ERR_FATAL;
2311 goto out;
2312 }
2313
2314 if (newsrv->check.health)
2315 newsrv->check.health = newsrv->check.rise;
2316 cur_arg += 2;
2317 }
2318 else if (!strcmp(args[cur_arg], "fall")) {
2319 newsrv->check.fall = atol(args[cur_arg + 1]);
2320
2321 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002322 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002323 file, linenum, args[cur_arg]);
2324 err_code |= ERR_ALERT | ERR_FATAL;
2325 goto out;
2326 }
2327
2328 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002329 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002330 file, linenum, args[cur_arg]);
2331 err_code |= ERR_ALERT | ERR_FATAL;
2332 goto out;
2333 }
2334
2335 cur_arg += 2;
2336 }
2337 else if (!strcmp(args[cur_arg], "inter")) {
2338 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2339 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002340 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002341 file, linenum, *err, newsrv->id);
2342 err_code |= ERR_ALERT | ERR_FATAL;
2343 goto out;
2344 }
2345 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002346 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002347 file, linenum, val, args[cur_arg], newsrv->id);
2348 err_code |= ERR_ALERT | ERR_FATAL;
2349 goto out;
2350 }
2351 newsrv->check.inter = val;
2352 cur_arg += 2;
2353 }
2354 else if (!strcmp(args[cur_arg], "fastinter")) {
2355 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2356 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002357 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002358 file, linenum, *err, newsrv->id);
2359 err_code |= ERR_ALERT | ERR_FATAL;
2360 goto out;
2361 }
2362 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002363 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002364 file, linenum, val, args[cur_arg], newsrv->id);
2365 err_code |= ERR_ALERT | ERR_FATAL;
2366 goto out;
2367 }
2368 newsrv->check.fastinter = val;
2369 cur_arg += 2;
2370 }
2371 else if (!strcmp(args[cur_arg], "downinter")) {
2372 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2373 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002374 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002375 file, linenum, *err, newsrv->id);
2376 err_code |= ERR_ALERT | ERR_FATAL;
2377 goto out;
2378 }
2379 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002380 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002381 file, linenum, val, args[cur_arg], newsrv->id);
2382 err_code |= ERR_ALERT | ERR_FATAL;
2383 goto out;
2384 }
2385 newsrv->check.downinter = val;
2386 cur_arg += 2;
2387 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002388 else if (!strcmp(args[cur_arg], "port")) {
2389 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002390 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002391 cur_arg += 2;
2392 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002393 else if (!strcmp(args[cur_arg], "weight")) {
2394 int w;
2395 w = atol(args[cur_arg + 1]);
2396 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002397 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002398 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2399 err_code |= ERR_ALERT | ERR_FATAL;
2400 goto out;
2401 }
2402 newsrv->uweight = newsrv->iweight = w;
2403 cur_arg += 2;
2404 }
2405 else if (!strcmp(args[cur_arg], "minconn")) {
2406 newsrv->minconn = atol(args[cur_arg + 1]);
2407 cur_arg += 2;
2408 }
2409 else if (!strcmp(args[cur_arg], "maxconn")) {
2410 newsrv->maxconn = atol(args[cur_arg + 1]);
2411 cur_arg += 2;
2412 }
2413 else if (!strcmp(args[cur_arg], "maxqueue")) {
2414 newsrv->maxqueue = atol(args[cur_arg + 1]);
2415 cur_arg += 2;
2416 }
2417 else if (!strcmp(args[cur_arg], "slowstart")) {
2418 /* slowstart is stored in seconds */
2419 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2420 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002421 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002422 file, linenum, *err, newsrv->id);
2423 err_code |= ERR_ALERT | ERR_FATAL;
2424 goto out;
2425 }
2426 newsrv->slowstart = (val + 999) / 1000;
2427 cur_arg += 2;
2428 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002429 else if (!strcmp(args[cur_arg], "on-error")) {
2430 if (!strcmp(args[cur_arg + 1], "fastinter"))
2431 newsrv->onerror = HANA_ONERR_FASTINTER;
2432 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2433 newsrv->onerror = HANA_ONERR_FAILCHK;
2434 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2435 newsrv->onerror = HANA_ONERR_SUDDTH;
2436 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2437 newsrv->onerror = HANA_ONERR_MARKDWN;
2438 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002439 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002440 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2441 file, linenum, args[cur_arg], args[cur_arg + 1]);
2442 err_code |= ERR_ALERT | ERR_FATAL;
2443 goto out;
2444 }
2445
2446 cur_arg += 2;
2447 }
2448 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2449 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2450 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2451 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002452 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002453 file, linenum, args[cur_arg], args[cur_arg + 1]);
2454 err_code |= ERR_ALERT | ERR_FATAL;
2455 goto out;
2456 }
2457
2458 cur_arg += 2;
2459 }
2460 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2461 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2462 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2463 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002464 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002465 file, linenum, args[cur_arg], args[cur_arg + 1]);
2466 err_code |= ERR_ALERT | ERR_FATAL;
2467 goto out;
2468 }
2469
2470 cur_arg += 2;
2471 }
2472 else if (!strcmp(args[cur_arg], "error-limit")) {
2473 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002474 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002475 file, linenum, args[cur_arg]);
2476 err_code |= ERR_ALERT | ERR_FATAL;
2477 goto out;
2478 }
2479
2480 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2481
2482 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002483 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002484 file, linenum, args[cur_arg]);
2485 err_code |= ERR_ALERT | ERR_FATAL;
2486 goto out;
2487 }
2488 cur_arg += 2;
2489 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002490 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002491 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002492 file, linenum, "usesrc", "source");
2493 err_code |= ERR_ALERT | ERR_FATAL;
2494 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002495 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002496 else {
2497 static int srv_dumped;
2498 struct srv_kw *kw;
2499 char *err;
2500
2501 kw = srv_find_kw(args[cur_arg]);
2502 if (kw) {
2503 char *err = NULL;
2504 int code;
2505
2506 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002507 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 +02002508 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002509 if (kw->skip != -1)
2510 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002511 err_code |= ERR_ALERT | ERR_FATAL;
2512 goto out;
2513 }
2514
2515 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002516 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002517 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002518 if (kw->skip != -1)
2519 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002520 err_code |= ERR_ALERT;
2521 continue;
2522 }
2523
2524 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2525 err_code |= code;
2526
2527 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002528 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002529 if (code & ERR_FATAL) {
2530 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002531 if (kw->skip != -1)
2532 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002533 goto out;
2534 }
2535 }
2536 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002537 if (kw->skip != -1)
2538 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002539 continue;
2540 }
2541
2542 err = NULL;
2543 if (!srv_dumped) {
2544 srv_dump_kws(&err);
2545 indent_msg(&err, 4);
2546 srv_dumped = 1;
2547 }
2548
Christopher Faulet767a84b2017-11-24 16:50:31 +01002549 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002550 file, linenum, args[0], args[1], args[cur_arg],
2551 err ? " Registered keywords :" : "", err ? err : "");
2552 free(err);
2553
2554 err_code |= ERR_ALERT | ERR_FATAL;
2555 goto out;
2556 }
2557 }
2558
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002559 if (!defsrv)
2560 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2561 if (err_code & ERR_FATAL)
2562 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002563 if (srv_tmpl)
2564 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002565 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002566 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002567 return 0;
2568
2569 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002570 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002571 free(errmsg);
2572 return err_code;
2573}
2574
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002575/* Returns a pointer to the first server matching either id <id>.
2576 * NULL is returned if no match is found.
2577 * the lookup is performed in the backend <bk>
2578 */
2579struct server *server_find_by_id(struct proxy *bk, int id)
2580{
2581 struct eb32_node *eb32;
2582 struct server *curserver;
2583
2584 if (!bk || (id ==0))
2585 return NULL;
2586
2587 /* <bk> has no backend capabilities, so it can't have a server */
2588 if (!(bk->cap & PR_CAP_BE))
2589 return NULL;
2590
2591 curserver = NULL;
2592
2593 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2594 if (eb32)
2595 curserver = container_of(eb32, struct server, conf.id);
2596
2597 return curserver;
2598}
2599
2600/* Returns a pointer to the first server matching either name <name>, or id
2601 * if <name> starts with a '#'. NULL is returned if no match is found.
2602 * the lookup is performed in the backend <bk>
2603 */
2604struct server *server_find_by_name(struct proxy *bk, const char *name)
2605{
2606 struct server *curserver;
2607
2608 if (!bk || !name)
2609 return NULL;
2610
2611 /* <bk> has no backend capabilities, so it can't have a server */
2612 if (!(bk->cap & PR_CAP_BE))
2613 return NULL;
2614
2615 curserver = NULL;
2616 if (*name == '#') {
2617 curserver = server_find_by_id(bk, atoi(name + 1));
2618 if (curserver)
2619 return curserver;
2620 }
2621 else {
2622 curserver = bk->srv;
2623
2624 while (curserver && (strcmp(curserver->id, name) != 0))
2625 curserver = curserver->next;
2626
2627 if (curserver)
2628 return curserver;
2629 }
2630
2631 return NULL;
2632}
2633
2634struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2635{
2636 struct server *byname;
2637 struct server *byid;
2638
2639 if (!name && !id)
2640 return NULL;
2641
2642 if (diff)
2643 *diff = 0;
2644
2645 byname = byid = NULL;
2646
2647 if (name) {
2648 byname = server_find_by_name(bk, name);
2649 if (byname && (!id || byname->puid == id))
2650 return byname;
2651 }
2652
2653 /* remaining possibilities :
2654 * - name not set
2655 * - name set but not found
2656 * - name found but ID doesn't match
2657 */
2658 if (id) {
2659 byid = server_find_by_id(bk, id);
2660 if (byid) {
2661 if (byname) {
2662 /* use id only if forced by configuration */
2663 if (byid->flags & SRV_F_FORCED_ID) {
2664 if (diff)
2665 *diff |= 2;
2666 return byid;
2667 }
2668 else {
2669 if (diff)
2670 *diff |= 1;
2671 return byname;
2672 }
2673 }
2674
2675 /* remaining possibilities:
2676 * - name not set
2677 * - name set but not found
2678 */
2679 if (name && diff)
2680 *diff |= 2;
2681 return byid;
2682 }
2683
2684 /* id bot found */
2685 if (byname) {
2686 if (diff)
2687 *diff |= 1;
2688 return byname;
2689 }
2690 }
2691
2692 return NULL;
2693}
2694
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002695/* Update a server state using the parameters available in the params list */
2696static void srv_update_state(struct server *srv, int version, char **params)
2697{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002698 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002699 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002700
2701 /* fields since version 1
2702 * and common to all other upcoming versions
2703 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002704 enum srv_state srv_op_state;
2705 enum srv_admin srv_admin_state;
2706 unsigned srv_uweight, srv_iweight;
2707 unsigned long srv_last_time_change;
2708 short srv_check_status;
2709 enum chk_result srv_check_result;
2710 int srv_check_health;
2711 int srv_check_state, srv_agent_state;
2712 int bk_f_forced_id;
2713 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002714 int fqdn_set_by_cli;
2715 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002716 const char *port_str;
2717 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002718
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002719 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002720 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002721 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002722 switch (version) {
2723 case 1:
2724 /*
2725 * now we can proceed with server's state update:
2726 * srv_addr: params[0]
2727 * srv_op_state: params[1]
2728 * srv_admin_state: params[2]
2729 * srv_uweight: params[3]
2730 * srv_iweight: params[4]
2731 * srv_last_time_change: params[5]
2732 * srv_check_status: params[6]
2733 * srv_check_result: params[7]
2734 * srv_check_health: params[8]
2735 * srv_check_state: params[9]
2736 * srv_agent_state: params[10]
2737 * bk_f_forced_id: params[11]
2738 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002739 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002740 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002741 */
2742
2743 /* validating srv_op_state */
2744 p = NULL;
2745 errno = 0;
2746 srv_op_state = strtol(params[1], &p, 10);
2747 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2748 (srv_op_state != SRV_ST_STOPPED &&
2749 srv_op_state != SRV_ST_STARTING &&
2750 srv_op_state != SRV_ST_RUNNING &&
2751 srv_op_state != SRV_ST_STOPPING)) {
2752 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2753 }
2754
2755 /* validating srv_admin_state */
2756 p = NULL;
2757 errno = 0;
2758 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002759 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002760
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002761 /* inherited statuses will be recomputed later.
2762 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2763 */
2764 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002765
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002766 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2767 (srv_admin_state != 0 &&
2768 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002769 srv_admin_state != SRV_ADMF_CMAINT &&
2770 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002771 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002772 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002773 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2774 }
2775
2776 /* validating srv_uweight */
2777 p = NULL;
2778 errno = 0;
2779 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002780 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002781 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2782
2783 /* validating srv_iweight */
2784 p = NULL;
2785 errno = 0;
2786 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002787 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002788 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2789
2790 /* validating srv_last_time_change */
2791 p = NULL;
2792 errno = 0;
2793 srv_last_time_change = strtol(params[5], &p, 10);
2794 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2795 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2796
2797 /* validating srv_check_status */
2798 p = NULL;
2799 errno = 0;
2800 srv_check_status = strtol(params[6], &p, 10);
2801 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2802 (srv_check_status >= HCHK_STATUS_SIZE))
2803 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2804
2805 /* validating srv_check_result */
2806 p = NULL;
2807 errno = 0;
2808 srv_check_result = strtol(params[7], &p, 10);
2809 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2810 (srv_check_result != CHK_RES_UNKNOWN &&
2811 srv_check_result != CHK_RES_NEUTRAL &&
2812 srv_check_result != CHK_RES_FAILED &&
2813 srv_check_result != CHK_RES_PASSED &&
2814 srv_check_result != CHK_RES_CONDPASS)) {
2815 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2816 }
2817
2818 /* validating srv_check_health */
2819 p = NULL;
2820 errno = 0;
2821 srv_check_health = strtol(params[8], &p, 10);
2822 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2823 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2824
2825 /* validating srv_check_state */
2826 p = NULL;
2827 errno = 0;
2828 srv_check_state = strtol(params[9], &p, 10);
2829 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2830 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2831 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2832
2833 /* validating srv_agent_state */
2834 p = NULL;
2835 errno = 0;
2836 srv_agent_state = strtol(params[10], &p, 10);
2837 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2838 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2839 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2840
2841 /* validating bk_f_forced_id */
2842 p = NULL;
2843 errno = 0;
2844 bk_f_forced_id = strtol(params[11], &p, 10);
2845 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2846 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2847
2848 /* validating srv_f_forced_id */
2849 p = NULL;
2850 errno = 0;
2851 srv_f_forced_id = strtol(params[12], &p, 10);
2852 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2853 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2854
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002855 /* validating srv_fqdn */
2856 fqdn = params[13];
2857 if (fqdn && *fqdn == '-')
2858 fqdn = NULL;
2859 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2860 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2861 fqdn = NULL;
2862 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002863
Frédéric Lécaille31694712017-08-01 08:47:19 +02002864 port_str = params[14];
2865 if (port_str) {
2866 port = strl2uic(port_str, strlen(port_str));
2867 if (port > USHRT_MAX) {
2868 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2869 port_str = NULL;
2870 }
2871 }
2872
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002873 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002874 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002875 goto out;
2876
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002877 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002878 /* recover operational state and apply it to this server
2879 * and all servers tracking this one */
2880 switch (srv_op_state) {
2881 case SRV_ST_STOPPED:
2882 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002883 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002884 break;
2885 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002886 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002887 break;
2888 case SRV_ST_STOPPING:
2889 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002890 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002891 break;
2892 case SRV_ST_RUNNING:
2893 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002894 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002895 break;
2896 }
2897
2898 /* When applying server state, the following rules apply:
2899 * - in case of a configuration change, we apply the setting from the new
2900 * configuration, regardless of old running state
2901 * - if no configuration change, we apply old running state only if old running
2902 * state is different from new configuration state
2903 */
2904 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002905 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2906 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002907 srv_adm_set_maint(srv);
2908 else
2909 srv_adm_set_ready(srv);
2910 }
2911 /* configuration is the same, let's compate old running state and new conf state */
2912 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002913 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002914 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002915 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002916 srv_adm_set_ready(srv);
2917 }
2918 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002919 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002920 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002921 * (srv->iweight is the weight set up in configuration).
2922 * There are two possible reasons for FDRAIN to have been present :
2923 * - previous config weight was zero
2924 * - "set server b/s drain" was sent to the CLI
2925 *
2926 * In the first case, we simply want to drop this drain state
2927 * if the new weight is not zero anymore, meaning the administrator
2928 * has intentionally turned the weight back to a positive value to
2929 * enable the server again after an operation. In the second case,
2930 * the drain state was forced on the CLI regardless of the config's
2931 * weight so we don't want a change to the config weight to lose this
2932 * status. What this means is :
2933 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2934 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002935 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002936 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002937 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002938 }
2939
2940 srv->last_change = date.tv_sec - srv_last_time_change;
2941 srv->check.status = srv_check_status;
2942 srv->check.result = srv_check_result;
2943 srv->check.health = srv_check_health;
2944
2945 /* Only case we want to apply is removing ENABLED flag which could have been
2946 * done by the "disable health" command over the stats socket
2947 */
2948 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2949 (srv_check_state & CHK_ST_CONFIGURED) &&
2950 !(srv_check_state & CHK_ST_ENABLED))
2951 srv->check.state &= ~CHK_ST_ENABLED;
2952
2953 /* Only case we want to apply is removing ENABLED flag which could have been
2954 * done by the "disable agent" command over the stats socket
2955 */
2956 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2957 (srv_agent_state & CHK_ST_CONFIGURED) &&
2958 !(srv_agent_state & CHK_ST_ENABLED))
2959 srv->agent.state &= ~CHK_ST_ENABLED;
2960
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002961 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2962 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002963 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002964 * It means that a configuration file change has precedence over a unix socket change
2965 * for server's weight
2966 *
2967 * by default, HAProxy applies the following weight when parsing the configuration
2968 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002969 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002970 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002971 srv->uweight = srv_uweight;
2972 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02002973 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002974
Willy Tarreaue5a60682016-11-09 14:54:53 +01002975 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04002976 if (strcmp(params[0], "-"))
2977 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002978
2979 if (fqdn && srv->hostname) {
2980 if (!strcmp(srv->hostname, fqdn)) {
2981 /* Here we reset the 'set from stats socket FQDN' flag
2982 * to support such transitions:
2983 * Let's say initial FQDN value is foo1 (in configuration file).
2984 * - FQDN changed from stats socket, from foo1 to foo2 value,
2985 * - FQDN changed again from file configuration (with the same previous value
2986 set from stats socket, from foo1 to foo2 value),
2987 * - reload for any other reason than a FQDN modification,
2988 * the configuration file FQDN matches the fqdn server state file value.
2989 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
2990 * any futher FQDN modification.
2991 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002992 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002993 }
2994 else {
2995 /* If the FDQN has been changed from stats socket,
2996 * apply fqdn state file value (which is the value set
2997 * from stats socket).
2998 */
2999 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003000 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003001 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003002 }
3003 }
3004 }
3005
Frédéric Lécaille31694712017-08-01 08:47:19 +02003006 if (port_str)
3007 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003008 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003009
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003010 break;
3011 default:
3012 chunk_appendf(msg, ", version '%d' not supported", version);
3013 }
3014
3015 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003016 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003017 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003018 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003019 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003020 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003021}
3022
3023/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3024 * For each proxy, it does the following:
3025 * - opens its server state file (either one or local one)
3026 * - read whole file, line by line
3027 * - analyse each line to check if it matches our current backend:
3028 * - backend name matches
3029 * - backend id matches if id is forced and name doesn't match
3030 * - if the server pointed by the line is found, then state is applied
3031 *
3032 * If the running backend uuid or id differs from the state file, then HAProxy reports
3033 * a warning.
3034 */
3035void apply_server_state(void)
3036{
3037 char *cur, *end;
3038 char mybuf[SRV_STATE_LINE_MAXLEN];
3039 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003040 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3041 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003042 int arg, srv_arg, version, diff;
3043 FILE *f;
3044 char *filepath;
3045 char globalfilepath[MAXPATHLEN + 1];
3046 char localfilepath[MAXPATHLEN + 1];
3047 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003048 struct proxy *curproxy, *bk;
3049 struct server *srv;
3050
3051 globalfilepathlen = 0;
3052 /* create the globalfilepath variable */
3053 if (global.server_state_file) {
3054 /* absolute path or no base directory provided */
3055 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3056 len = strlen(global.server_state_file);
3057 if (len > MAXPATHLEN) {
3058 globalfilepathlen = 0;
3059 goto globalfileerror;
3060 }
3061 memcpy(globalfilepath, global.server_state_file, len);
3062 globalfilepath[len] = '\0';
3063 globalfilepathlen = len;
3064 }
3065 else if (global.server_state_base) {
3066 len = strlen(global.server_state_base);
3067 globalfilepathlen += len;
3068
3069 if (globalfilepathlen > MAXPATHLEN) {
3070 globalfilepathlen = 0;
3071 goto globalfileerror;
3072 }
3073 strncpy(globalfilepath, global.server_state_base, len);
3074 globalfilepath[globalfilepathlen] = 0;
3075
3076 /* append a slash if needed */
3077 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3078 if (globalfilepathlen + 1 > MAXPATHLEN) {
3079 globalfilepathlen = 0;
3080 goto globalfileerror;
3081 }
3082 globalfilepath[globalfilepathlen++] = '/';
3083 }
3084
3085 len = strlen(global.server_state_file);
3086 if (globalfilepathlen + len > MAXPATHLEN) {
3087 globalfilepathlen = 0;
3088 goto globalfileerror;
3089 }
3090 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3091 globalfilepathlen += len;
3092 globalfilepath[globalfilepathlen++] = 0;
3093 }
3094 }
3095 globalfileerror:
3096 if (globalfilepathlen == 0)
3097 globalfilepath[0] = '\0';
3098
3099 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003100 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003101 /* servers are only in backends */
3102 if (!(curproxy->cap & PR_CAP_BE))
3103 continue;
3104 fileopenerr = 0;
3105 filepath = NULL;
3106
3107 /* search server state file path and name */
3108 switch (curproxy->load_server_state_from_file) {
3109 /* read servers state from global file */
3110 case PR_SRV_STATE_FILE_GLOBAL:
3111 /* there was an error while generating global server state file path */
3112 if (globalfilepathlen == 0)
3113 continue;
3114 filepath = globalfilepath;
3115 fileopenerr = 1;
3116 break;
3117 /* this backend has its own file */
3118 case PR_SRV_STATE_FILE_LOCAL:
3119 localfilepathlen = 0;
3120 localfilepath[0] = '\0';
3121 len = 0;
3122 /* create the localfilepath variable */
3123 /* absolute path or no base directory provided */
3124 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3125 len = strlen(curproxy->server_state_file_name);
3126 if (len > MAXPATHLEN) {
3127 localfilepathlen = 0;
3128 goto localfileerror;
3129 }
3130 memcpy(localfilepath, curproxy->server_state_file_name, len);
3131 localfilepath[len] = '\0';
3132 localfilepathlen = len;
3133 }
3134 else if (global.server_state_base) {
3135 len = strlen(global.server_state_base);
3136 localfilepathlen += len;
3137
3138 if (localfilepathlen > MAXPATHLEN) {
3139 localfilepathlen = 0;
3140 goto localfileerror;
3141 }
3142 strncpy(localfilepath, global.server_state_base, len);
3143 localfilepath[localfilepathlen] = 0;
3144
3145 /* append a slash if needed */
3146 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3147 if (localfilepathlen + 1 > MAXPATHLEN) {
3148 localfilepathlen = 0;
3149 goto localfileerror;
3150 }
3151 localfilepath[localfilepathlen++] = '/';
3152 }
3153
3154 len = strlen(curproxy->server_state_file_name);
3155 if (localfilepathlen + len > MAXPATHLEN) {
3156 localfilepathlen = 0;
3157 goto localfileerror;
3158 }
3159 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3160 localfilepathlen += len;
3161 localfilepath[localfilepathlen++] = 0;
3162 }
3163 filepath = localfilepath;
3164 localfileerror:
3165 if (localfilepathlen == 0)
3166 localfilepath[0] = '\0';
3167
3168 break;
3169 case PR_SRV_STATE_FILE_NONE:
3170 default:
3171 continue;
3172 }
3173
3174 /* preload global state file */
3175 errno = 0;
3176 f = fopen(filepath, "r");
3177 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003178 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003179 if (!f)
3180 continue;
3181
3182 mybuf[0] = '\0';
3183 mybuflen = 0;
3184 version = 0;
3185
3186 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003187 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003188 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003189 goto fileclose;
3190 }
3191
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003192 cur = mybuf;
3193 version = atoi(cur);
3194 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3195 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003196 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003197
3198 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3199 int bk_f_forced_id = 0;
3200 int check_id = 0;
3201 int check_name = 0;
3202
3203 mybuflen = strlen(mybuf);
3204 cur = mybuf;
3205 end = cur + mybuflen;
3206
3207 bk = NULL;
3208 srv = NULL;
3209
3210 /* we need at least one character */
3211 if (mybuflen == 0)
3212 continue;
3213
3214 /* ignore blank characters at the beginning of the line */
3215 while (isspace(*cur))
3216 ++cur;
3217
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003218 /* Ignore empty or commented lines */
3219 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003220 continue;
3221
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003222 /* truncated lines */
3223 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003224 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003225 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003226 }
3227
3228 /* Removes trailing '\n' */
3229 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003230
3231 /* we're now ready to move the line into *srv_params[] */
3232 params[0] = cur;
3233 arg = 1;
3234 srv_arg = 0;
3235 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3236 if (isspace(*cur)) {
3237 *cur = '\0';
3238 ++cur;
3239 while (isspace(*cur))
3240 ++cur;
3241 switch (version) {
3242 case 1:
3243 /*
3244 * srv_addr: params[4] => srv_params[0]
3245 * srv_op_state: params[5] => srv_params[1]
3246 * srv_admin_state: params[6] => srv_params[2]
3247 * srv_uweight: params[7] => srv_params[3]
3248 * srv_iweight: params[8] => srv_params[4]
3249 * srv_last_time_change: params[9] => srv_params[5]
3250 * srv_check_status: params[10] => srv_params[6]
3251 * srv_check_result: params[11] => srv_params[7]
3252 * srv_check_health: params[12] => srv_params[8]
3253 * srv_check_state: params[13] => srv_params[9]
3254 * srv_agent_state: params[14] => srv_params[10]
3255 * bk_f_forced_id: params[15] => srv_params[11]
3256 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003257 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003258 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003259 */
3260 if (arg >= 4) {
3261 srv_params[srv_arg] = cur;
3262 ++srv_arg;
3263 }
3264 break;
3265 }
3266
3267 params[arg] = cur;
3268 ++arg;
3269 }
3270 else {
3271 ++cur;
3272 }
3273 }
3274
3275 /* if line is incomplete line, then ignore it.
3276 * otherwise, update useful flags */
3277 switch (version) {
3278 case 1:
3279 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3280 continue;
3281 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3282 check_id = (atoi(params[0]) == curproxy->uuid);
3283 check_name = (strcmp(curproxy->id, params[1]) == 0);
3284 break;
3285 }
3286
3287 diff = 0;
3288 bk = curproxy;
3289
3290 /* if backend can't be found, let's continue */
3291 if (!check_id && !check_name)
3292 continue;
3293 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003294 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 +02003295 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3296 }
3297 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003298 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 +02003299 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3300 /* if name doesn't match, we still want to update curproxy if the backend id
3301 * was forced in previous the previous configuration */
3302 if (!bk_f_forced_id)
3303 continue;
3304 }
3305
3306 /* look for the server by its id: param[2] */
3307 /* else look for the server by its name: param[3] */
3308 diff = 0;
3309 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3310
3311 if (!srv) {
3312 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003313 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3314 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003315 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3316 params[3], params[2], params[0], params[1]);
3317 continue;
3318 }
3319 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003320 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 +02003321 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 +02003322 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003323 }
3324 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003325 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 +02003326 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 +02003327 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003328 }
3329
3330 /* now we can proceed with server's state update */
3331 srv_update_state(srv, version, srv_params);
3332 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003333fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003334 fclose(f);
3335 }
3336}
3337
Simon Horman7d09b9a2013-02-12 10:45:51 +09003338/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003339 * update a server's current IP address.
3340 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3341 * ip is in network format.
3342 * updater is a string which contains an information about the requester of the update.
3343 * updater is used if not NULL.
3344 *
3345 * A log line and a stderr warning message is generated based on server's backend options.
3346 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003347int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003348{
3349 /* generates a log line and a warning on stderr */
3350 if (1) {
3351 /* book enough space for both IPv4 and IPv6 */
3352 char oldip[INET6_ADDRSTRLEN];
3353 char newip[INET6_ADDRSTRLEN];
3354
3355 memset(oldip, '\0', INET6_ADDRSTRLEN);
3356 memset(newip, '\0', INET6_ADDRSTRLEN);
3357
3358 /* copy old IP address in a string */
3359 switch (s->addr.ss_family) {
3360 case AF_INET:
3361 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3362 break;
3363 case AF_INET6:
3364 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3365 break;
3366 };
3367
3368 /* copy new IP address in a string */
3369 switch (ip_sin_family) {
3370 case AF_INET:
3371 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3372 break;
3373 case AF_INET6:
3374 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3375 break;
3376 };
3377
3378 /* save log line into a buffer */
3379 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3380 s->proxy->id, s->id, oldip, newip, updater);
3381
3382 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003383 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003384
3385 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003386 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003387 }
3388
3389 /* save the new IP family */
3390 s->addr.ss_family = ip_sin_family;
3391 /* save the new IP address */
3392 switch (ip_sin_family) {
3393 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003394 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003395 break;
3396 case AF_INET6:
3397 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3398 break;
3399 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003400 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003401
3402 return 0;
3403}
3404
3405/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003406 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3407 *
3408 * Caller can pass its name through <updater> to get it integrated in the response message
3409 * returned by the function.
3410 *
3411 * The function first does the following, in that order:
3412 * - validates the new addr and/or port
3413 * - checks if an update is required (new IP or port is different than current ones)
3414 * - checks the update is allowed:
3415 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3416 * - allow all changes if no CHECKS are configured
3417 * - if CHECK is configured:
3418 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3419 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3420 * conditions are met
3421 */
3422const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3423{
3424 struct sockaddr_storage sa;
3425 int ret, port_change_required;
3426 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003427 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003428 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003429 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003430
3431 msg = get_trash_chunk();
3432 chunk_reset(msg);
3433
3434 if (addr) {
3435 memset(&sa, 0, sizeof(struct sockaddr_storage));
3436 if (str2ip2(addr, &sa, 0) == NULL) {
3437 chunk_printf(msg, "Invalid addr '%s'", addr);
3438 goto out;
3439 }
3440
3441 /* changes are allowed on AF_INET* families only */
3442 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3443 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3444 goto out;
3445 }
3446
3447 /* collecting data currently setup */
3448 memset(current_addr, '\0', sizeof(current_addr));
3449 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3450 /* changes are allowed on AF_INET* families only */
3451 if ((ret != AF_INET) && (ret != AF_INET6)) {
3452 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3453 goto out;
3454 }
3455
3456 /* applying ADDR changes if required and allowed
3457 * ipcmp returns 0 when both ADDR are the same
3458 */
3459 if (ipcmp(&s->addr, &sa) == 0) {
3460 chunk_appendf(msg, "no need to change the addr");
3461 goto port;
3462 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003463 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003464 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003465
3466 /* we also need to update check's ADDR only if it uses the server's one */
3467 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003468 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003469 }
3470
3471 /* we also need to update agent ADDR only if it use the server's one */
3472 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003473 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003474 }
3475
3476 /* update report for caller */
3477 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3478 }
3479
3480 port:
3481 if (port) {
3482 char sign = '\0';
3483 char *endptr;
3484
3485 if (addr)
3486 chunk_appendf(msg, ", ");
3487
3488 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003489 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003490
3491 /* check if PORT change is required */
3492 port_change_required = 0;
3493
3494 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003495 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003496 new_port = strtol(port, &endptr, 10);
3497 if ((errno != 0) || (port == endptr)) {
3498 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3499 goto out;
3500 }
3501
3502 /* check if caller triggers a port mapped or offset */
3503 if (sign == '-' || (sign == '+')) {
3504 /* check if server currently uses port map */
3505 if (!(s->flags & SRV_F_MAPPORTS)) {
3506 /* switch from fixed port to port map mandatorily triggers
3507 * a port change */
3508 port_change_required = 1;
3509 /* check is configured
3510 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3511 * prevent PORT change if check doesn't have it's dedicated port while switching
3512 * to port mapping */
3513 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3514 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.");
3515 goto out;
3516 }
3517 }
3518 /* we're already using port maps */
3519 else {
3520 port_change_required = current_port != new_port;
3521 }
3522 }
3523 /* fixed port */
3524 else {
3525 port_change_required = current_port != new_port;
3526 }
3527
3528 /* applying PORT changes if required and update response message */
3529 if (port_change_required) {
3530 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003531 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003532 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003533
3534 /* prepare message */
3535 chunk_appendf(msg, "port changed from '");
3536 if (s->flags & SRV_F_MAPPORTS)
3537 chunk_appendf(msg, "+");
3538 chunk_appendf(msg, "%d' to '", current_port);
3539
3540 if (sign == '-') {
3541 s->flags |= SRV_F_MAPPORTS;
3542 chunk_appendf(msg, "%c", sign);
3543 /* just use for result output */
3544 new_port = -new_port;
3545 }
3546 else if (sign == '+') {
3547 s->flags |= SRV_F_MAPPORTS;
3548 chunk_appendf(msg, "%c", sign);
3549 }
3550 else {
3551 s->flags &= ~SRV_F_MAPPORTS;
3552 }
3553
3554 chunk_appendf(msg, "%d'", new_port);
3555
3556 /* we also need to update health checks port only if it uses server's realport */
3557 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3558 s->check.port = new_port;
3559 }
3560 }
3561 else {
3562 chunk_appendf(msg, "no need to change the port");
3563 }
3564 }
3565
3566out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003567 if (changed)
3568 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003569 if (updater)
3570 chunk_appendf(msg, " by '%s'", updater);
3571 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003572 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003573}
3574
3575
3576/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003577 * update server status based on result of name resolution
3578 * returns:
3579 * 0 if server status is updated
3580 * 1 if server status has not changed
3581 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003582int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003583{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003584 struct dns_resolvers *resolvers = s->resolvers;
3585 struct dns_resolution *resolution = s->dns_requester->resolution;
3586 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003587
3588 switch (resolution->status) {
3589 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003590 /* status when HAProxy has just (re)started.
3591 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003592 break;
3593
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003594 case RSLV_STATUS_VALID:
3595 /*
3596 * resume health checks
3597 * server will be turned back on if health check is safe
3598 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003599 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003600 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003601 return 1;
3602 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3603 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003604 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003605 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003606
Emeric Brun52a91d32017-08-31 14:41:55 +02003607 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003608 return 1;
3609 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3610 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3611 s->proxy->id, s->id);
3612
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003613 ha_warning("%s.\n", trash.area);
3614 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003615 return 0;
3616
3617 case RSLV_STATUS_NX:
3618 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003619 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3620 if (!tick_is_expired(exp, now_ms))
3621 break;
3622
3623 if (s->next_admin & SRV_ADMF_RMAINT)
3624 return 1;
3625 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3626 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003627
3628 case RSLV_STATUS_TIMEOUT:
3629 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003630 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3631 if (!tick_is_expired(exp, now_ms))
3632 break;
3633
3634 if (s->next_admin & SRV_ADMF_RMAINT)
3635 return 1;
3636 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3637 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003638
3639 case RSLV_STATUS_REFUSED:
3640 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003641 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3642 if (!tick_is_expired(exp, now_ms))
3643 break;
3644
3645 if (s->next_admin & SRV_ADMF_RMAINT)
3646 return 1;
3647 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3648 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003649
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003650 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003651 /* stop server if resolution failed for a long enough period */
3652 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3653 if (!tick_is_expired(exp, now_ms))
3654 break;
3655
3656 if (s->next_admin & SRV_ADMF_RMAINT)
3657 return 1;
3658 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3659 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003660 }
3661
3662 return 1;
3663}
3664
3665/*
3666 * Server Name Resolution valid response callback
3667 * It expects:
3668 * - <nameserver>: the name server which answered the valid response
3669 * - <response>: buffer containing a valid DNS response
3670 * - <response_len>: size of <response>
3671 * It performs the following actions:
3672 * - ignore response if current ip found and server family not met
3673 * - update with first new ip found if family is met and current IP is not found
3674 * returns:
3675 * 0 on error
3676 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003677 *
3678 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003679 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003680int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003681{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003682 struct server *s = NULL;
3683 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003684 void *serverip, *firstip;
3685 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003686 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003687 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003688 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003689
Christopher Faulet67957bd2017-09-27 11:00:59 +02003690 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003691 if (!s)
3692 return 1;
3693
Christopher Faulet67957bd2017-09-27 11:00:59 +02003694 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003695
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003696 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003697 firstip = NULL; /* pointer to the first valid response found */
3698 /* it will be used as the new IP if a change is required */
3699 firstip_sin_family = AF_UNSPEC;
3700 serverip = NULL; /* current server IP address */
3701
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003702 /* initializing server IP pointer */
3703 server_sin_family = s->addr.ss_family;
3704 switch (server_sin_family) {
3705 case AF_INET:
3706 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3707 break;
3708
3709 case AF_INET6:
3710 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3711 break;
3712
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003713 case AF_UNSPEC:
3714 break;
3715
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003716 default:
3717 goto invalid;
3718 }
3719
Baptiste Assmann729c9012017-05-22 15:13:10 +02003720 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003721 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003722 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003723
3724 switch (ret) {
3725 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003726 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003727
3728 case DNS_UPD_SRVIP_NOT_FOUND:
3729 goto save_ip;
3730
3731 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003732 goto invalid;
3733
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003734 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003735 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003736 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003737
Baptiste Assmannfad03182015-10-28 02:03:32 +01003738 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003739 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003740 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003741 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003742
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003743 default:
3744 goto invalid;
3745
3746 }
3747
3748 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003749 if (nameserver) {
3750 nameserver->counters.update++;
3751 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003752 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003753 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003754 else
3755 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003756 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003757
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003758 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003759 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003760 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003761
3762 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003763 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003764 nameserver->counters.invalid++;
3765 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003766 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003767 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003768 return 0;
3769}
3770
3771/*
3772 * Server Name Resolution error management callback
3773 * returns:
3774 * 0 on error
3775 * 1 when no error or safe ignore
3776 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003777int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003778{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003779 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003780
Christopher Faulet67957bd2017-09-27 11:00:59 +02003781 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003782 if (!s)
3783 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003784 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003785 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003786 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003787 return 1;
3788}
3789
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003790/*
3791 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003792 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003793 * It returns a pointer to the first server found or NULL if <ip> is not yet
3794 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003795 *
3796 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003797 */
3798struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3799{
3800 struct server *tmpsrv;
3801 struct proxy *be;
3802
3803 if (!srv)
3804 return NULL;
3805
3806 be = srv->proxy;
3807 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003808 /* we found the current server is the same, ignore it */
3809 if (srv == tmpsrv)
3810 continue;
3811
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003812 /* We want to compare the IP in the record with the IP of the servers in the
3813 * same backend, only if:
3814 * * DNS resolution is enabled on the server
3815 * * the hostname used for the resolution by our server is the same than the
3816 * one used for the server found in the backend
3817 * * the server found in the backend is not our current server
3818 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003819 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003820 if ((tmpsrv->hostname_dn == NULL) ||
3821 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3822 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003823 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003824 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003825 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003826 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003827
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003828 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003829 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003830 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003831 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003832 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003833
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003834 /* At this point, we have 2 different servers using the same DNS hostname
3835 * for their respective resolution.
3836 */
3837 if (*ip_family == tmpsrv->addr.ss_family &&
3838 ((tmpsrv->addr.ss_family == AF_INET &&
3839 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3840 (tmpsrv->addr.ss_family == AF_INET6 &&
3841 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003842 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003843 return tmpsrv;
3844 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003845 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003846 }
3847
Emeric Brune9fd6b52017-11-02 17:20:39 +01003848
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003849 return NULL;
3850}
3851
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003852/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3853 * resolver. This is suited for initial address configuration. Returns 0 on
3854 * success otherwise a non-zero error code. In case of error, *err_code, if
3855 * not NULL, is filled up.
3856 */
3857int srv_set_addr_via_libc(struct server *srv, int *err_code)
3858{
3859 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003860 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003861 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003862 return 1;
3863 }
3864 return 0;
3865}
3866
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003867/* Set the server's FDQN (->hostname) from <hostname>.
3868 * Returns -1 if failed, 0 if not.
3869 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003870int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003871{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003872 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003873 char *hostname_dn;
3874 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003875
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003876 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003877 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003878 /* run time DNS resolution was not active for this server
3879 * and we can't enable it at run time for now.
3880 */
3881 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003882 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003883
3884 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003885 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003886 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003887 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3888 hostname_dn, trash.size);
3889 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003890 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003891
Christopher Faulet67957bd2017-09-27 11:00:59 +02003892 resolution = srv->dns_requester->resolution;
3893 if (resolution &&
3894 resolution->hostname_dn &&
3895 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02003896 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003897
Christopher Faulet67957bd2017-09-27 11:00:59 +02003898 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003899
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003900 free(srv->hostname);
3901 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003902 srv->hostname = strdup(hostname);
3903 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003904 srv->hostname_dn_len = hostname_dn_len;
3905 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003906 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003907
Olivier Houchard55dcdf42017-11-06 15:15:04 +01003908 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003909 goto err;
3910
3911 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003912 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003913 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003914 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003915
3916 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003917 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003918 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003919 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003920}
3921
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003922/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3923 * from the state file. This is suited for initial address configuration.
3924 * Returns 0 on success otherwise a non-zero error code. In case of error,
3925 * *err_code, if not NULL, is filled up.
3926 */
3927static int srv_apply_lastaddr(struct server *srv, int *err_code)
3928{
3929 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3930 if (err_code)
3931 *err_code |= ERR_WARN;
3932 return 1;
3933 }
3934 return 0;
3935}
3936
Willy Tarreau25e51522016-11-04 15:10:17 +01003937/* returns 0 if no error, otherwise a combination of ERR_* flags */
3938static int srv_iterate_initaddr(struct server *srv)
3939{
3940 int return_code = 0;
3941 int err_code;
3942 unsigned int methods;
3943
3944 methods = srv->init_addr_methods;
3945 if (!methods) { // default to "last,libc"
3946 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3947 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3948 }
3949
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003950 /* "-dr" : always append "none" so that server addresses resolution
3951 * failures are silently ignored, this is convenient to validate some
3952 * configs out of their environment.
3953 */
3954 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3955 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3956
Willy Tarreau25e51522016-11-04 15:10:17 +01003957 while (methods) {
3958 err_code = 0;
3959 switch (srv_get_next_initaddr(&methods)) {
3960 case SRV_IADDR_LAST:
3961 if (!srv->lastaddr)
3962 continue;
3963 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003964 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003965 return_code |= err_code;
3966 break;
3967
3968 case SRV_IADDR_LIBC:
3969 if (!srv->hostname)
3970 continue;
3971 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003972 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003973 return_code |= err_code;
3974 break;
3975
Willy Tarreau37ebe122016-11-04 15:17:58 +01003976 case SRV_IADDR_NONE:
3977 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003978 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003979 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3980 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003981 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003982 return return_code;
3983
Willy Tarreau4310d362016-11-02 15:05:56 +01003984 case SRV_IADDR_IP:
3985 ipcpy(&srv->init_addr, &srv->addr);
3986 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003987 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3988 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01003989 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003990 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003991
Willy Tarreau25e51522016-11-04 15:10:17 +01003992 default: /* unhandled method */
3993 break;
3994 }
3995 }
3996
3997 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003998 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01003999 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4000 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004001 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004002 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004003 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4004 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004005
4006 return_code |= ERR_ALERT | ERR_FATAL;
4007 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004008out:
4009 srv_set_dyncookie(srv);
4010 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004011}
4012
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004013/*
4014 * This function parses all backends and all servers within each backend
4015 * and performs servers' addr resolution based on information provided by:
4016 * - configuration file
4017 * - server-state file (states provided by an 'old' haproxy process)
4018 *
4019 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4020 */
4021int srv_init_addr(void)
4022{
4023 struct proxy *curproxy;
4024 int return_code = 0;
4025
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004026 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004027 while (curproxy) {
4028 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004029
4030 /* servers are in backend only */
4031 if (!(curproxy->cap & PR_CAP_BE))
4032 goto srv_init_addr_next;
4033
Willy Tarreau25e51522016-11-04 15:10:17 +01004034 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004035 if (srv->hostname)
4036 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004037
4038 srv_init_addr_next:
4039 curproxy = curproxy->next;
4040 }
4041
4042 return return_code;
4043}
4044
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004045const 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 +02004046{
4047
Willy Tarreau83061a82018-07-13 11:56:34 +02004048 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004049
4050 msg = get_trash_chunk();
4051 chunk_reset(msg);
4052
Olivier Houchard8da5f982017-08-04 18:35:36 +02004053 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004054 chunk_appendf(msg, "no need to change the FDQN");
4055 goto out;
4056 }
4057
4058 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4059 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4060 goto out;
4061 }
4062
4063 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4064 server->proxy->id, server->id, server->hostname, fqdn);
4065
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004066 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004067 chunk_reset(msg);
4068 chunk_appendf(msg, "could not update %s/%s FQDN",
4069 server->proxy->id, server->id);
4070 goto out;
4071 }
4072
4073 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004074 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004075
4076 out:
4077 if (updater)
4078 chunk_appendf(msg, " by '%s'", updater);
4079 chunk_appendf(msg, "\n");
4080
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004081 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004082}
4083
4084
Willy Tarreau21b069d2016-11-23 17:15:08 +01004085/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4086 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004087 * 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 +01004088 * used for CLI commands requiring a server name.
4089 * Important: the <arg> is modified to remove the '/'.
4090 */
4091struct server *cli_find_server(struct appctx *appctx, char *arg)
4092{
4093 struct proxy *px;
4094 struct server *sv;
4095 char *line;
4096
4097 /* split "backend/server" and make <line> point to server */
4098 for (line = arg; *line; line++)
4099 if (*line == '/') {
4100 *line++ = '\0';
4101 break;
4102 }
4103
4104 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004105 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004106 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004107 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004108 return NULL;
4109 }
4110
4111 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004112 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004113 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004114 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004115 return NULL;
4116 }
4117
4118 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004119 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004120 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004121 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004122 return NULL;
4123 }
4124
4125 return sv;
4126}
4127
William Lallemand222baf22016-11-19 02:00:33 +01004128
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004129static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004130{
4131 struct server *sv;
4132 const char *warning;
4133
4134 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4135 return 1;
4136
4137 sv = cli_find_server(appctx, args[2]);
4138 if (!sv)
4139 return 1;
4140
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004141 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004142
William Lallemand222baf22016-11-19 02:00:33 +01004143 if (strcmp(args[3], "weight") == 0) {
4144 warning = server_parse_weight_change_request(sv, args[4]);
4145 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004146 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004147 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004148 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004149 }
4150 }
4151 else if (strcmp(args[3], "state") == 0) {
4152 if (strcmp(args[4], "ready") == 0)
4153 srv_adm_set_ready(sv);
4154 else if (strcmp(args[4], "drain") == 0)
4155 srv_adm_set_drain(sv);
4156 else if (strcmp(args[4], "maint") == 0)
4157 srv_adm_set_maint(sv);
4158 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004159 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004160 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004161 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004162 }
4163 }
4164 else if (strcmp(args[3], "health") == 0) {
4165 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004166 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004167 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004168 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004169 }
4170 else if (strcmp(args[4], "up") == 0) {
4171 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004172 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004173 }
4174 else if (strcmp(args[4], "stopping") == 0) {
4175 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004176 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004177 }
4178 else if (strcmp(args[4], "down") == 0) {
4179 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004180 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004181 }
4182 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004183 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004184 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004185 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004186 }
4187 }
4188 else if (strcmp(args[3], "agent") == 0) {
4189 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004190 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004191 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004192 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004193 }
4194 else if (strcmp(args[4], "up") == 0) {
4195 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004196 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004197 }
4198 else if (strcmp(args[4], "down") == 0) {
4199 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004200 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004201 }
4202 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004203 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004204 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004205 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004206 }
4207 }
Misiek2da082d2017-01-09 09:40:42 +01004208 else if (strcmp(args[3], "agent-addr") == 0) {
4209 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004210 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004211 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4212 appctx->st0 = CLI_ST_PRINT;
4213 } else {
4214 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004215 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004216 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4217 appctx->st0 = CLI_ST_PRINT;
4218 }
4219 }
4220 }
4221 else if (strcmp(args[3], "agent-send") == 0) {
4222 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004223 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004224 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4225 appctx->st0 = CLI_ST_PRINT;
4226 } else {
4227 char *nss = strdup(args[4]);
4228 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004229 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004230 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4231 appctx->st0 = CLI_ST_PRINT;
4232 } else {
4233 free(sv->agent.send_string);
4234 sv->agent.send_string = nss;
4235 sv->agent.send_string_len = strlen(args[4]);
4236 }
4237 }
4238 }
William Lallemand222baf22016-11-19 02:00:33 +01004239 else if (strcmp(args[3], "check-port") == 0) {
4240 int i = 0;
4241 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004242 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004243 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004244 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004245 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004246 }
4247 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004248 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004249 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004250 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004251 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004252 }
4253 /* prevent the update of port to 0 if MAPPORTS are in use */
4254 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004255 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004256 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004257 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004258 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004259 }
4260 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004261 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004262 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004263 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004264 }
4265 else if (strcmp(args[3], "addr") == 0) {
4266 char *addr = NULL;
4267 char *port = NULL;
4268 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004269 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004270 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004271 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004272 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004273 }
4274 else {
4275 addr = args[4];
4276 }
4277 if (strcmp(args[5], "port") == 0) {
4278 port = args[6];
4279 }
4280 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4281 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004282 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004283 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004284 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004285 }
4286 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4287 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004288 else if (strcmp(args[3], "fqdn") == 0) {
4289 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004290 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004291 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4292 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004293 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004294 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004295 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004296 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004297 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004298 appctx->ctx.cli.msg = warning;
4299 appctx->st0 = CLI_ST_PRINT;
4300 }
4301 }
William Lallemand222baf22016-11-19 02:00:33 +01004302 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004303 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004304 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 +01004305 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004306 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004307 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004308 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004309 return 1;
4310}
4311
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004312static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004313{
4314 struct stream_interface *si = appctx->owner;
4315 struct proxy *px;
4316 struct server *sv;
4317 char *line;
4318
4319
4320 /* split "backend/server" and make <line> point to server */
4321 for (line = args[2]; *line; line++)
4322 if (*line == '/') {
4323 *line++ = '\0';
4324 break;
4325 }
4326
4327 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004328 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004329 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004330 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004331 return 1;
4332 }
4333
4334 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004335 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004336 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004337 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004338 return 1;
4339 }
4340
4341 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004342 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4343 sv->iweight);
4344 if (ci_putstr(si_ic(si), trash.area) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004345 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004346 return 0;
4347 }
William Lallemand6b160942016-11-22 12:34:35 +01004348 return 1;
4349}
4350
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004351static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004352{
4353 struct server *sv;
4354 const char *warning;
4355
4356 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4357 return 1;
4358
4359 sv = cli_find_server(appctx, args[2]);
4360 if (!sv)
4361 return 1;
4362
4363 warning = server_parse_weight_change_request(sv, args[3]);
4364 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004365 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004366 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004367 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004368 }
4369 return 1;
4370}
4371
Willy Tarreaub8026272016-11-23 11:26:56 +01004372/* parse a "set maxconn server" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004373static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004374{
4375 struct server *sv;
4376 const char *warning;
4377
4378 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4379 return 1;
4380
4381 sv = cli_find_server(appctx, args[3]);
4382 if (!sv)
4383 return 1;
4384
4385 warning = server_parse_maxconn_change_request(sv, args[4]);
4386 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004387 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004388 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004389 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004390 }
4391 return 1;
4392}
William Lallemand6b160942016-11-22 12:34:35 +01004393
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004394/* parse a "disable agent" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004395static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004396{
4397 struct server *sv;
4398
4399 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4400 return 1;
4401
4402 sv = cli_find_server(appctx, args[2]);
4403 if (!sv)
4404 return 1;
4405
4406 sv->agent.state &= ~CHK_ST_ENABLED;
4407 return 1;
4408}
4409
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004410/* parse a "disable health" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004411static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004412{
4413 struct server *sv;
4414
4415 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4416 return 1;
4417
4418 sv = cli_find_server(appctx, args[2]);
4419 if (!sv)
4420 return 1;
4421
4422 sv->check.state &= ~CHK_ST_ENABLED;
4423 return 1;
4424}
4425
Willy Tarreauffb4d582016-11-24 12:47:00 +01004426/* parse a "disable server" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004427static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004428{
4429 struct server *sv;
4430
4431 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4432 return 1;
4433
4434 sv = cli_find_server(appctx, args[2]);
4435 if (!sv)
4436 return 1;
4437
4438 srv_adm_set_maint(sv);
4439 return 1;
4440}
4441
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004442/* parse a "enable agent" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004443static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004444{
4445 struct server *sv;
4446
4447 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4448 return 1;
4449
4450 sv = cli_find_server(appctx, args[2]);
4451 if (!sv)
4452 return 1;
4453
4454 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004455 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004456 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004457 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004458 return 1;
4459 }
4460
4461 sv->agent.state |= CHK_ST_ENABLED;
4462 return 1;
4463}
4464
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004465/* parse a "enable health" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004466static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004467{
4468 struct server *sv;
4469
4470 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4471 return 1;
4472
4473 sv = cli_find_server(appctx, args[2]);
4474 if (!sv)
4475 return 1;
4476
4477 sv->check.state |= CHK_ST_ENABLED;
4478 return 1;
4479}
4480
Willy Tarreauffb4d582016-11-24 12:47:00 +01004481/* parse a "enable server" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004482static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004483{
4484 struct server *sv;
4485
4486 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4487 return 1;
4488
4489 sv = cli_find_server(appctx, args[2]);
4490 if (!sv)
4491 return 1;
4492
4493 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004494 if (!(sv->flags & SRV_F_COOKIESET)
4495 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4496 sv->cookie)
4497 srv_check_for_dup_dyncookie(sv);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004498 return 1;
4499}
4500
William Lallemand222baf22016-11-19 02:00:33 +01004501/* register cli keywords */
4502static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004503 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004504 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004505 { { "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 +01004506 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004507 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004508 { { "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 +01004509 { { "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 +01004510 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004511 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4512 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4513
William Lallemand222baf22016-11-19 02:00:33 +01004514 {{},}
4515}};
4516
4517__attribute__((constructor))
4518static void __server_init(void)
4519{
4520 cli_register_kw(&cli_kws);
4521}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004522
Emeric Brun64cc49c2017-10-03 14:46:45 +02004523/*
4524 * This function applies server's status changes, it is
4525 * is designed to be called asynchronously.
4526 *
4527 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004528static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004529{
4530 struct check *check = &s->check;
4531 int xferred;
4532 struct proxy *px = s->proxy;
4533 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4534 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4535 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004536 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004537
Emeric Brun64cc49c2017-10-03 14:46:45 +02004538 /* If currently main is not set we try to apply pending state changes */
4539 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4540 int next_admin;
4541
4542 /* Backup next admin */
4543 next_admin = s->next_admin;
4544
4545 /* restore current admin state */
4546 s->next_admin = s->cur_admin;
4547
4548 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4549 s->last_change = now.tv_sec;
4550 if (s->proxy->lbprm.set_server_status_down)
4551 s->proxy->lbprm.set_server_status_down(s);
4552
4553 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4554 srv_shutdown_streams(s, SF_ERR_DOWN);
4555
4556 /* we might have streams queued on this server and waiting for
4557 * a connection. Those which are redispatchable will be queued
4558 * to another server or to the proxy itself.
4559 */
4560 xferred = pendconn_redistribute(s);
4561
4562 tmptrash = alloc_trash_chunk();
4563 if (tmptrash) {
4564 chunk_printf(tmptrash,
4565 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4566 s->proxy->id, s->id);
4567
Emeric Brun5a133512017-10-19 14:42:30 +02004568 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004569 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004570
4571 /* we don't send an alert if the server was previously paused */
4572 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004573 send_log(s->proxy, log_level, "%s.\n",
4574 tmptrash->area);
4575 send_email_alert(s, log_level, "%s",
4576 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004577 free_trash_chunk(tmptrash);
4578 tmptrash = NULL;
4579 }
4580 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4581 set_backend_down(s->proxy);
4582
4583 s->counters.down_trans++;
4584 }
4585 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4586 s->last_change = now.tv_sec;
4587 if (s->proxy->lbprm.set_server_status_down)
4588 s->proxy->lbprm.set_server_status_down(s);
4589
4590 /* we might have streams queued on this server and waiting for
4591 * a connection. Those which are redispatchable will be queued
4592 * to another server or to the proxy itself.
4593 */
4594 xferred = pendconn_redistribute(s);
4595
4596 tmptrash = alloc_trash_chunk();
4597 if (tmptrash) {
4598 chunk_printf(tmptrash,
4599 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4600 s->proxy->id, s->id);
4601
Emeric Brun5a133512017-10-19 14:42:30 +02004602 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004603
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004604 ha_warning("%s.\n", tmptrash->area);
4605 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4606 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004607 free_trash_chunk(tmptrash);
4608 tmptrash = NULL;
4609 }
4610
4611 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4612 set_backend_down(s->proxy);
4613 }
4614 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4615 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4616 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4617 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4618 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4619 s->proxy->last_change = now.tv_sec;
4620 }
4621
4622 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4623 s->down_time += now.tv_sec - s->last_change;
4624
4625 s->last_change = now.tv_sec;
4626 if (s->next_state == SRV_ST_STARTING)
4627 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4628
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004629 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004630 /* now propagate the status change to any LB algorithms */
4631 if (px->lbprm.update_server_eweight)
4632 px->lbprm.update_server_eweight(s);
4633 else if (srv_willbe_usable(s)) {
4634 if (px->lbprm.set_server_status_up)
4635 px->lbprm.set_server_status_up(s);
4636 }
4637 else {
4638 if (px->lbprm.set_server_status_down)
4639 px->lbprm.set_server_status_down(s);
4640 }
4641
4642 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4643 * and it's not a backup server and its effective weight is > 0,
4644 * then it can accept new connections, so we shut down all streams
4645 * on all backup servers.
4646 */
4647 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4648 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4649 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4650
4651 /* check if we can handle some connections queued at the proxy. We
4652 * will take as many as we can handle.
4653 */
4654 xferred = pendconn_grab_from_px(s);
4655
4656 tmptrash = alloc_trash_chunk();
4657 if (tmptrash) {
4658 chunk_printf(tmptrash,
4659 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4660 s->proxy->id, s->id);
4661
Emeric Brun5a133512017-10-19 14:42:30 +02004662 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004663 ha_warning("%s.\n", tmptrash->area);
4664 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4665 tmptrash->area);
4666 send_email_alert(s, LOG_NOTICE, "%s",
4667 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004668 free_trash_chunk(tmptrash);
4669 tmptrash = NULL;
4670 }
4671
4672 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4673 set_backend_down(s->proxy);
4674 }
4675 else if (s->cur_eweight != s->next_eweight) {
4676 /* now propagate the status change to any LB algorithms */
4677 if (px->lbprm.update_server_eweight)
4678 px->lbprm.update_server_eweight(s);
4679 else if (srv_willbe_usable(s)) {
4680 if (px->lbprm.set_server_status_up)
4681 px->lbprm.set_server_status_up(s);
4682 }
4683 else {
4684 if (px->lbprm.set_server_status_down)
4685 px->lbprm.set_server_status_down(s);
4686 }
4687
4688 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4689 set_backend_down(s->proxy);
4690 }
4691
4692 s->next_admin = next_admin;
4693 }
4694
Emeric Brun5a133512017-10-19 14:42:30 +02004695 /* reset operational state change */
4696 *s->op_st_chg.reason = 0;
4697 s->op_st_chg.status = s->op_st_chg.code = -1;
4698 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004699
4700 /* Now we try to apply pending admin changes */
4701
4702 /* Maintenance must also disable health checks */
4703 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4704 if (s->check.state & CHK_ST_ENABLED) {
4705 s->check.state |= CHK_ST_PAUSED;
4706 check->health = 0;
4707 }
4708
4709 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004710 tmptrash = alloc_trash_chunk();
4711 if (tmptrash) {
4712 chunk_printf(tmptrash,
4713 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4714 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4715 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004716
Olivier Houchard796a2b32017-10-24 17:42:47 +02004717 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004718
Olivier Houchard796a2b32017-10-24 17:42:47 +02004719 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004720 ha_warning("%s.\n", tmptrash->area);
4721 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4722 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004723 }
4724 free_trash_chunk(tmptrash);
4725 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004726 }
Emeric Brun8f298292017-12-06 16:47:17 +01004727 /* commit new admin status */
4728
4729 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004730 }
4731 else { /* server was still running */
4732 check->health = 0; /* failure */
4733 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004734
4735 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004736 if (s->proxy->lbprm.set_server_status_down)
4737 s->proxy->lbprm.set_server_status_down(s);
4738
Emeric Brun64cc49c2017-10-03 14:46:45 +02004739 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4740 srv_shutdown_streams(s, SF_ERR_DOWN);
4741
4742 /* we might have streams queued on this server and waiting for
4743 * a connection. Those which are redispatchable will be queued
4744 * to another server or to the proxy itself.
4745 */
4746 xferred = pendconn_redistribute(s);
4747
4748 tmptrash = alloc_trash_chunk();
4749 if (tmptrash) {
4750 chunk_printf(tmptrash,
4751 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4752 s->flags & SRV_F_BACKUP ? "Backup " : "",
4753 s->proxy->id, s->id,
4754 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4755
4756 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4757
4758 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004759 ha_warning("%s.\n", tmptrash->area);
4760 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4761 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004762 }
4763 free_trash_chunk(tmptrash);
4764 tmptrash = NULL;
4765 }
4766 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4767 set_backend_down(s->proxy);
4768
4769 s->counters.down_trans++;
4770 }
4771 }
4772 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4773 /* OK here we're leaving maintenance, we have many things to check,
4774 * because the server might possibly be coming back up depending on
4775 * its state. In practice, leaving maintenance means that we should
4776 * immediately turn to UP (more or less the slowstart) under the
4777 * following conditions :
4778 * - server is neither checked nor tracked
4779 * - server tracks another server which is not checked
4780 * - server tracks another server which is already up
4781 * Which sums up as something simpler :
4782 * "either the tracking server is up or the server's checks are disabled
4783 * or up". Otherwise we only re-enable health checks. There's a special
4784 * case associated to the stopping state which can be inherited. Note
4785 * that the server might still be in drain mode, which is naturally dealt
4786 * with by the lower level functions.
4787 */
4788
4789 if (s->check.state & CHK_ST_ENABLED) {
4790 s->check.state &= ~CHK_ST_PAUSED;
4791 check->health = check->rise; /* start OK but check immediately */
4792 }
4793
4794 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4795 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4796 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4797 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4798 s->next_state = SRV_ST_STOPPING;
4799 }
4800 else {
4801 s->next_state = SRV_ST_STARTING;
4802 if (s->slowstart > 0)
4803 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4804 else
4805 s->next_state = SRV_ST_RUNNING;
4806 }
4807
4808 }
4809
4810 tmptrash = alloc_trash_chunk();
4811 if (tmptrash) {
4812 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4813 chunk_printf(tmptrash,
4814 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4815 s->flags & SRV_F_BACKUP ? "Backup " : "",
4816 s->proxy->id, s->id,
4817 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4818 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4819 }
4820 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4821 chunk_printf(tmptrash,
4822 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4823 s->flags & SRV_F_BACKUP ? "Backup " : "",
4824 s->proxy->id, s->id, s->hostname,
4825 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4826 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4827 }
4828 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4829 chunk_printf(tmptrash,
4830 "%sServer %s/%s is %s/%s (leaving maintenance)",
4831 s->flags & SRV_F_BACKUP ? "Backup " : "",
4832 s->proxy->id, s->id,
4833 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4834 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4835 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004836 ha_warning("%s.\n", tmptrash->area);
4837 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4838 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004839 free_trash_chunk(tmptrash);
4840 tmptrash = NULL;
4841 }
4842
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004843 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004844 /* now propagate the status change to any LB algorithms */
4845 if (px->lbprm.update_server_eweight)
4846 px->lbprm.update_server_eweight(s);
4847 else if (srv_willbe_usable(s)) {
4848 if (px->lbprm.set_server_status_up)
4849 px->lbprm.set_server_status_up(s);
4850 }
4851 else {
4852 if (px->lbprm.set_server_status_down)
4853 px->lbprm.set_server_status_down(s);
4854 }
4855
4856 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4857 set_backend_down(s->proxy);
4858
Willy Tarreau6a78e612018-08-07 10:14:53 +02004859 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4860 * and it's not a backup server and its effective weight is > 0,
4861 * then it can accept new connections, so we shut down all streams
4862 * on all backup servers.
4863 */
4864 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4865 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4866 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4867
4868 /* check if we can handle some connections queued at the proxy. We
4869 * will take as many as we can handle.
4870 */
4871 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004872 }
4873 else if (s->next_admin & SRV_ADMF_MAINT) {
4874 /* remaining in maintenance mode, let's inform precisely about the
4875 * situation.
4876 */
4877 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4878 tmptrash = alloc_trash_chunk();
4879 if (tmptrash) {
4880 chunk_printf(tmptrash,
4881 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4882 s->flags & SRV_F_BACKUP ? "Backup " : "",
4883 s->proxy->id, s->id);
4884
4885 if (s->track) /* normally it's mandatory here */
4886 chunk_appendf(tmptrash, " via %s/%s",
4887 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004888 ha_warning("%s.\n", tmptrash->area);
4889 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4890 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004891 free_trash_chunk(tmptrash);
4892 tmptrash = NULL;
4893 }
4894 }
4895 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4896 tmptrash = alloc_trash_chunk();
4897 if (tmptrash) {
4898 chunk_printf(tmptrash,
4899 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4900 s->flags & SRV_F_BACKUP ? "Backup " : "",
4901 s->proxy->id, s->id, s->hostname);
4902
4903 if (s->track) /* normally it's mandatory here */
4904 chunk_appendf(tmptrash, " via %s/%s",
4905 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004906 ha_warning("%s.\n", tmptrash->area);
4907 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4908 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004909 free_trash_chunk(tmptrash);
4910 tmptrash = NULL;
4911 }
4912 }
4913 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4914 tmptrash = alloc_trash_chunk();
4915 if (tmptrash) {
4916 chunk_printf(tmptrash,
4917 "%sServer %s/%s remains in forced maintenance",
4918 s->flags & SRV_F_BACKUP ? "Backup " : "",
4919 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004920 ha_warning("%s.\n", tmptrash->area);
4921 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4922 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004923 free_trash_chunk(tmptrash);
4924 tmptrash = NULL;
4925 }
4926 }
4927 /* don't report anything when leaving drain mode and remaining in maintenance */
4928
4929 s->cur_admin = s->next_admin;
4930 }
4931
4932 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4933 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4934 /* drain state is applied only if not yet in maint */
4935
4936 s->last_change = now.tv_sec;
4937 if (px->lbprm.set_server_status_down)
4938 px->lbprm.set_server_status_down(s);
4939
4940 /* we might have streams queued on this server and waiting for
4941 * a connection. Those which are redispatchable will be queued
4942 * to another server or to the proxy itself.
4943 */
4944 xferred = pendconn_redistribute(s);
4945
4946 tmptrash = alloc_trash_chunk();
4947 if (tmptrash) {
4948 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4949 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4950 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4951
4952 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4953
4954 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004955 ha_warning("%s.\n", tmptrash->area);
4956 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4957 tmptrash->area);
4958 send_email_alert(s, LOG_NOTICE, "%s",
4959 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004960 }
4961 free_trash_chunk(tmptrash);
4962 tmptrash = NULL;
4963 }
4964
4965 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4966 set_backend_down(s->proxy);
4967 }
4968 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4969 /* OK completely leaving drain mode */
4970 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4971 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4972 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4973 s->proxy->last_change = now.tv_sec;
4974 }
4975
4976 if (s->last_change < now.tv_sec) // ignore negative times
4977 s->down_time += now.tv_sec - s->last_change;
4978 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004979 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004980
4981 tmptrash = alloc_trash_chunk();
4982 if (tmptrash) {
4983 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4984 chunk_printf(tmptrash,
4985 "%sServer %s/%s is %s (leaving forced drain)",
4986 s->flags & SRV_F_BACKUP ? "Backup " : "",
4987 s->proxy->id, s->id,
4988 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4989 }
4990 else {
4991 chunk_printf(tmptrash,
4992 "%sServer %s/%s is %s (leaving drain)",
4993 s->flags & SRV_F_BACKUP ? "Backup " : "",
4994 s->proxy->id, s->id,
4995 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4996 if (s->track) /* normally it's mandatory here */
4997 chunk_appendf(tmptrash, " via %s/%s",
4998 s->track->proxy->id, s->track->id);
4999 }
5000
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005001 ha_warning("%s.\n", tmptrash->area);
5002 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5003 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005004 free_trash_chunk(tmptrash);
5005 tmptrash = NULL;
5006 }
5007
5008 /* now propagate the status change to any LB algorithms */
5009 if (px->lbprm.update_server_eweight)
5010 px->lbprm.update_server_eweight(s);
5011 else if (srv_willbe_usable(s)) {
5012 if (px->lbprm.set_server_status_up)
5013 px->lbprm.set_server_status_up(s);
5014 }
5015 else {
5016 if (px->lbprm.set_server_status_down)
5017 px->lbprm.set_server_status_down(s);
5018 }
5019 }
5020 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5021 /* remaining in drain mode after removing one of its flags */
5022
5023 tmptrash = alloc_trash_chunk();
5024 if (tmptrash) {
5025 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5026 chunk_printf(tmptrash,
5027 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5028 s->flags & SRV_F_BACKUP ? "Backup " : "",
5029 s->proxy->id, s->id);
5030
5031 if (s->track) /* normally it's mandatory here */
5032 chunk_appendf(tmptrash, " via %s/%s",
5033 s->track->proxy->id, s->track->id);
5034 }
5035 else {
5036 chunk_printf(tmptrash,
5037 "%sServer %s/%s remains in forced drain mode",
5038 s->flags & SRV_F_BACKUP ? "Backup " : "",
5039 s->proxy->id, s->id);
5040 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005041 ha_warning("%s.\n", tmptrash->area);
5042 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5043 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005044 free_trash_chunk(tmptrash);
5045 tmptrash = NULL;
5046 }
5047
5048 /* commit new admin status */
5049
5050 s->cur_admin = s->next_admin;
5051 }
5052 }
5053
5054 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005055 *s->adm_st_chg_cause = 0;
5056}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005057
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005058/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005059 * Local variables:
5060 * c-indent-level: 8
5061 * c-basic-offset: 8
5062 * End:
5063 */