blob: 4498fd878793253fa218a32276ba24979a3ec146 [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
Emeric Brun64cc49c2017-10-03 14:46:45 +020047struct list updated_servers = LIST_HEAD_INIT(updated_servers);
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010048__decl_hathreads(HA_SPINLOCK_T updated_servers_lock);
Christopher Faulet5d42e092017-10-16 12:00:40 +020049
50static void srv_register_update(struct server *srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020051static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010052static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010053static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
Willy Tarreau21faa912012-10-10 08:27:36 +020055/* List head of all known server keywords */
56static struct srv_kw_list srv_keywords = {
57 .list = LIST_HEAD_INIT(srv_keywords.list)
58};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020059
Simon Hormana3608442013-11-01 16:46:15 +090060int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020061{
Emeric Brun52a91d32017-08-31 14:41:55 +020062 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020063 return s->down_time;
64
65 return now.tv_sec - s->last_change + s->down_time;
66}
Willy Tarreaubaaee002006-06-26 02:48:02 +020067
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050068int srv_lastsession(const struct server *s)
69{
70 if (s->counters.last_sess)
71 return now.tv_sec - s->counters.last_sess;
72
73 return -1;
74}
75
Simon Horman4a741432013-02-23 15:35:38 +090076int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020077{
Simon Horman4a741432013-02-23 15:35:38 +090078 const struct server *s = check->server;
79
Willy Tarreauff5ae352013-12-11 20:36:34 +010080 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090081 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010082
Emeric Brun52a91d32017-08-31 14:41:55 +020083 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090084 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010085
Simon Horman4a741432013-02-23 15:35:38 +090086 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010087}
88
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010089/*
90 * Check that we did not get a hash collision.
91 * Unlikely, but it can happen.
92 */
93static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +010094{
95 struct proxy *p = s->proxy;
96 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010097
98 for (tmpserv = p->srv; tmpserv != NULL;
99 tmpserv = tmpserv->next) {
100 if (tmpserv == s)
101 continue;
102 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
103 continue;
104 if (tmpserv->cookie &&
105 strcmp(tmpserv->cookie, s->cookie) == 0) {
106 ha_warning("We generated two equal cookies for two different servers.\n"
107 "Please change the secret key for '%s'.\n",
108 s->proxy->id);
109 }
110 }
111
112}
113
114void srv_set_dyncookie(struct server *s)
115{
116 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100117 char *tmpbuf;
118 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100119 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100120 size_t buffer_len;
121 int addr_len;
122 int port;
123
124 if ((s->flags & SRV_F_COOKIESET) ||
125 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
126 s->proxy->dyncookie_key == NULL)
127 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100128 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100129
130 if (s->addr.ss_family != AF_INET &&
131 s->addr.ss_family != AF_INET6)
132 return;
133 /*
134 * Buffer to calculate the cookie value.
135 * The buffer contains the secret key + the server IP address
136 * + the TCP port.
137 */
138 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
139 /*
140 * The TCP port should use only 2 bytes, but is stored in
141 * an unsigned int in struct server, so let's use 4, to be
142 * on the safe side.
143 */
144 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200145 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100146 memcpy(tmpbuf, p->dyncookie_key, key_len);
147 memcpy(&(tmpbuf[key_len]),
148 s->addr.ss_family == AF_INET ?
149 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
150 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
151 addr_len);
152 /*
153 * Make sure it's the same across all the load balancers,
154 * no matter their endianness.
155 */
156 port = htonl(s->svc_port);
157 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
158 hash_value = XXH64(tmpbuf, buffer_len, 0);
159 memprintf(&s->cookie, "%016llx", hash_value);
160 if (!s->cookie)
161 return;
162 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100163
164 /* Don't bother checking if the dyncookie is duplicated if
165 * the server is marked as "disabled", maybe it doesn't have
166 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100167 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100168 if (!(s->next_admin & SRV_ADMF_FMAINT))
169 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100170}
171
Willy Tarreau21faa912012-10-10 08:27:36 +0200172/*
173 * Registers the server keyword list <kwl> as a list of valid keywords for next
174 * parsing sessions.
175 */
176void srv_register_keywords(struct srv_kw_list *kwl)
177{
178 LIST_ADDQ(&srv_keywords.list, &kwl->list);
179}
180
181/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
182 * keyword is found with a NULL ->parse() function, then an attempt is made to
183 * find one with a valid ->parse() function. This way it is possible to declare
184 * platform-dependant, known keywords as NULL, then only declare them as valid
185 * if some options are met. Note that if the requested keyword contains an
186 * opening parenthesis, everything from this point is ignored.
187 */
188struct srv_kw *srv_find_kw(const char *kw)
189{
190 int index;
191 const char *kwend;
192 struct srv_kw_list *kwl;
193 struct srv_kw *ret = NULL;
194
195 kwend = strchr(kw, '(');
196 if (!kwend)
197 kwend = kw + strlen(kw);
198
199 list_for_each_entry(kwl, &srv_keywords.list, list) {
200 for (index = 0; kwl->kw[index].kw != NULL; index++) {
201 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
202 kwl->kw[index].kw[kwend-kw] == 0) {
203 if (kwl->kw[index].parse)
204 return &kwl->kw[index]; /* found it !*/
205 else
206 ret = &kwl->kw[index]; /* may be OK */
207 }
208 }
209 }
210 return ret;
211}
212
213/* Dumps all registered "server" keywords to the <out> string pointer. The
214 * unsupported keywords are only dumped if their supported form was not
215 * found.
216 */
217void srv_dump_kws(char **out)
218{
219 struct srv_kw_list *kwl;
220 int index;
221
222 *out = NULL;
223 list_for_each_entry(kwl, &srv_keywords.list, list) {
224 for (index = 0; kwl->kw[index].kw != NULL; index++) {
225 if (kwl->kw[index].parse ||
226 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
227 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
228 kwl->scope,
229 kwl->kw[index].kw,
230 kwl->kw[index].skip ? " <arg>" : "",
231 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
232 kwl->kw[index].parse ? "" : " (not supported)");
233 }
234 }
235 }
236}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100237
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100238/* Parse the "addr" server keyword */
239static int srv_parse_addr(char **args, int *cur_arg,
240 struct proxy *curproxy, struct server *newsrv, char **err)
241{
242 char *errmsg, *arg;
243 struct sockaddr_storage *sk;
244 int port1, port2;
245 struct protocol *proto;
246
247 errmsg = NULL;
248 arg = args[*cur_arg + 1];
249
250 if (!*arg) {
251 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
252 goto err;
253 }
254
255 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
256 if (!sk) {
257 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
258 goto err;
259 }
260
261 proto = protocol_by_family(sk->ss_family);
262 if (!proto || !proto->connect) {
263 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
264 args[*cur_arg], arg);
265 goto err;
266 }
267
268 if (port1 != port2) {
269 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
270 args[*cur_arg], arg);
271 goto err;
272 }
273
274 newsrv->check.addr = newsrv->agent.addr = *sk;
275 newsrv->flags |= SRV_F_CHECKADDR;
276 newsrv->flags |= SRV_F_AGENTADDR;
277
278 return 0;
279
280 err:
281 free(errmsg);
282 return ERR_ALERT | ERR_FATAL;
283}
284
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100285/* Parse the "agent-check" server keyword */
286static int srv_parse_agent_check(char **args, int *cur_arg,
287 struct proxy *curproxy, struct server *newsrv, char **err)
288{
289 newsrv->do_agent = 1;
290 return 0;
291}
292
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100293/* Parse the "backup" server keyword */
294static int srv_parse_backup(char **args, int *cur_arg,
295 struct proxy *curproxy, struct server *newsrv, char **err)
296{
297 newsrv->flags |= SRV_F_BACKUP;
298 return 0;
299}
300
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100301/* Parse the "check" server keyword */
302static int srv_parse_check(char **args, int *cur_arg,
303 struct proxy *curproxy, struct server *newsrv, char **err)
304{
305 newsrv->do_check = 1;
306 return 0;
307}
308
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100309/* Parse the "check-send-proxy" server keyword */
310static int srv_parse_check_send_proxy(char **args, int *cur_arg,
311 struct proxy *curproxy, struct server *newsrv, char **err)
312{
313 newsrv->check.send_proxy = 1;
314 return 0;
315}
316
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100317/* Parse the "cookie" server keyword */
318static int srv_parse_cookie(char **args, int *cur_arg,
319 struct proxy *curproxy, struct server *newsrv, char **err)
320{
321 char *arg;
322
323 arg = args[*cur_arg + 1];
324 if (!*arg) {
325 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
326 return ERR_ALERT | ERR_FATAL;
327 }
328
329 free(newsrv->cookie);
330 newsrv->cookie = strdup(arg);
331 newsrv->cklen = strlen(arg);
332 newsrv->flags |= SRV_F_COOKIESET;
333 return 0;
334}
335
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100336/* Parse the "disabled" server keyword */
337static int srv_parse_disabled(char **args, int *cur_arg,
338 struct proxy *curproxy, struct server *newsrv, char **err)
339{
Emeric Brun52a91d32017-08-31 14:41:55 +0200340 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
341 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100342 newsrv->check.state |= CHK_ST_PAUSED;
343 newsrv->check.health = 0;
344 return 0;
345}
346
347/* Parse the "enabled" server keyword */
348static int srv_parse_enabled(char **args, int *cur_arg,
349 struct proxy *curproxy, struct server *newsrv, char **err)
350{
Emeric Brun52a91d32017-08-31 14:41:55 +0200351 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
352 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100353 newsrv->check.state &= ~CHK_ST_PAUSED;
354 newsrv->check.health = newsrv->check.rise;
355 return 0;
356}
357
Willy Tarreaudff55432012-10-10 17:51:05 +0200358/* parse the "id" server keyword */
359static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
360{
361 struct eb32_node *node;
362
363 if (!*args[*cur_arg + 1]) {
364 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
365 return ERR_ALERT | ERR_FATAL;
366 }
367
368 newsrv->puid = atol(args[*cur_arg + 1]);
369 newsrv->conf.id.key = newsrv->puid;
370
371 if (newsrv->puid <= 0) {
372 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
373 return ERR_ALERT | ERR_FATAL;
374 }
375
376 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
377 if (node) {
378 struct server *target = container_of(node, struct server, conf.id);
379 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
380 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
381 target->id);
382 return ERR_ALERT | ERR_FATAL;
383 }
384
385 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200386 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200387 return 0;
388}
389
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100390/* Parse the "namespace" server keyword */
391static int srv_parse_namespace(char **args, int *cur_arg,
392 struct proxy *curproxy, struct server *newsrv, char **err)
393{
394#ifdef CONFIG_HAP_NS
395 char *arg;
396
397 arg = args[*cur_arg + 1];
398 if (!*arg) {
399 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
400 return ERR_ALERT | ERR_FATAL;
401 }
402
403 if (!strcmp(arg, "*")) {
404 /* Use the namespace associated with the connection (if present). */
405 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
406 return 0;
407 }
408
409 /*
410 * As this parser may be called several times for the same 'default-server'
411 * object, or for a new 'server' instance deriving from a 'default-server'
412 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
413 */
414 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
415
416 newsrv->netns = netns_store_lookup(arg, strlen(arg));
417 if (!newsrv->netns)
418 newsrv->netns = netns_store_insert(arg);
419
420 if (!newsrv->netns) {
421 memprintf(err, "Cannot open namespace '%s'", arg);
422 return ERR_ALERT | ERR_FATAL;
423 }
424
425 return 0;
426#else
427 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
428 return ERR_ALERT | ERR_FATAL;
429#endif
430}
431
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100432/* Parse the "no-agent-check" server keyword */
433static int srv_parse_no_agent_check(char **args, int *cur_arg,
434 struct proxy *curproxy, struct server *newsrv, char **err)
435{
436 free_check(&newsrv->agent);
437 newsrv->agent.inter = 0;
438 newsrv->agent.port = 0;
439 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
440 newsrv->do_agent = 0;
441 return 0;
442}
443
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100444/* Parse the "no-backup" server keyword */
445static int srv_parse_no_backup(char **args, int *cur_arg,
446 struct proxy *curproxy, struct server *newsrv, char **err)
447{
448 newsrv->flags &= ~SRV_F_BACKUP;
449 return 0;
450}
451
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100452/* Parse the "no-check" server keyword */
453static int srv_parse_no_check(char **args, int *cur_arg,
454 struct proxy *curproxy, struct server *newsrv, char **err)
455{
456 free_check(&newsrv->check);
457 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
458 newsrv->do_check = 0;
459 return 0;
460}
461
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100462/* Parse the "no-check-send-proxy" server keyword */
463static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
464 struct proxy *curproxy, struct server *newsrv, char **err)
465{
466 newsrv->check.send_proxy = 0;
467 return 0;
468}
469
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100470/* Disable server PROXY protocol flags. */
471static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
472{
473 srv->pp_opts &= ~flags;
474 return 0;
475}
476
477/* Parse the "no-send-proxy" server keyword */
478static int srv_parse_no_send_proxy(char **args, int *cur_arg,
479 struct proxy *curproxy, struct server *newsrv, char **err)
480{
481 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
482}
483
484/* Parse the "no-send-proxy-v2" server keyword */
485static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
486 struct proxy *curproxy, struct server *newsrv, char **err)
487{
488 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
489}
490
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100491/* Parse the "non-stick" server keyword */
492static int srv_parse_non_stick(char **args, int *cur_arg,
493 struct proxy *curproxy, struct server *newsrv, char **err)
494{
495 newsrv->flags |= SRV_F_NON_STICK;
496 return 0;
497}
498
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100499/* Enable server PROXY protocol flags. */
500static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
501{
502 srv->pp_opts |= flags;
503 return 0;
504}
505
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100506/* parse the "proxy-v2-options" */
507static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
508 struct proxy *px, struct server *newsrv, char **err)
509{
510 char *p, *n;
511 for (p = args[*cur_arg+1]; p; p = n) {
512 n = strchr(p, ',');
513 if (n)
514 *n++ = '\0';
515 if (!strcmp(p, "ssl")) {
516 newsrv->pp_opts |= SRV_PP_V2_SSL;
517 } else if (!strcmp(p, "cert-cn")) {
518 newsrv->pp_opts |= SRV_PP_V2_SSL;
519 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100520 } else if (!strcmp(p, "cert-key")) {
521 newsrv->pp_opts |= SRV_PP_V2_SSL;
522 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
523 } else if (!strcmp(p, "cert-sig")) {
524 newsrv->pp_opts |= SRV_PP_V2_SSL;
525 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
526 } else if (!strcmp(p, "ssl-cipher")) {
527 newsrv->pp_opts |= SRV_PP_V2_SSL;
528 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100529 } else if (!strcmp(p, "authority")) {
530 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100531 } else if (!strcmp(p, "crc32c")) {
532 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100533 } else
534 goto fail;
535 }
536 return 0;
537 fail:
538 if (err)
539 memprintf(err, "'%s' : proxy v2 option not implemented", p);
540 return ERR_ALERT | ERR_FATAL;
541}
542
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100543/* Parse the "observe" server keyword */
544static int srv_parse_observe(char **args, int *cur_arg,
545 struct proxy *curproxy, struct server *newsrv, char **err)
546{
547 char *arg;
548
549 arg = args[*cur_arg + 1];
550 if (!*arg) {
551 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
552 return ERR_ALERT | ERR_FATAL;
553 }
554
555 if (!strcmp(arg, "none")) {
556 newsrv->observe = HANA_OBS_NONE;
557 }
558 else if (!strcmp(arg, "layer4")) {
559 newsrv->observe = HANA_OBS_LAYER4;
560 }
561 else if (!strcmp(arg, "layer7")) {
562 if (curproxy->mode != PR_MODE_HTTP) {
563 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
564 return ERR_ALERT;
565 }
566 newsrv->observe = HANA_OBS_LAYER7;
567 }
568 else {
569 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
570 "but got '%s'\n", args[*cur_arg], arg);
571 return ERR_ALERT | ERR_FATAL;
572 }
573
574 return 0;
575}
576
Frédéric Lécaille16186232017-03-14 16:42:49 +0100577/* Parse the "redir" server keyword */
578static int srv_parse_redir(char **args, int *cur_arg,
579 struct proxy *curproxy, struct server *newsrv, char **err)
580{
581 char *arg;
582
583 arg = args[*cur_arg + 1];
584 if (!*arg) {
585 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
586 return ERR_ALERT | ERR_FATAL;
587 }
588
589 free(newsrv->rdr_pfx);
590 newsrv->rdr_pfx = strdup(arg);
591 newsrv->rdr_len = strlen(arg);
592
593 return 0;
594}
595
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100596/* Parse the "send-proxy" server keyword */
597static int srv_parse_send_proxy(char **args, int *cur_arg,
598 struct proxy *curproxy, struct server *newsrv, char **err)
599{
600 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
601}
602
603/* Parse the "send-proxy-v2" server keyword */
604static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
605 struct proxy *curproxy, struct server *newsrv, char **err)
606{
607 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
608}
609
Frédéric Lécailledba97072017-03-17 15:33:50 +0100610
611/* Parse the "source" server keyword */
612static int srv_parse_source(char **args, int *cur_arg,
613 struct proxy *curproxy, struct server *newsrv, char **err)
614{
615 char *errmsg;
616 int port_low, port_high;
617 struct sockaddr_storage *sk;
618 struct protocol *proto;
619
620 errmsg = NULL;
621
622 if (!*args[*cur_arg + 1]) {
623 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
624 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
625 goto err;
626 }
627
628 /* 'sk' is statically allocated (no need to be freed). */
629 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
630 if (!sk) {
631 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
632 goto err;
633 }
634
635 proto = protocol_by_family(sk->ss_family);
636 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100637 ha_alert("'%s %s' : connect() not supported for this address family.\n",
638 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100639 goto err;
640 }
641
642 newsrv->conn_src.opts |= CO_SRC_BIND;
643 newsrv->conn_src.source_addr = *sk;
644
645 if (port_low != port_high) {
646 int i;
647
648 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100649 ha_alert("'%s' does not support port offsets (found '%s').\n",
650 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100651 goto err;
652 }
653
654 if (port_low <= 0 || port_low > 65535 ||
655 port_high <= 0 || port_high > 65535 ||
656 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100657 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 +0100658 goto err;
659 }
660 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
661 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
662 newsrv->conn_src.sport_range->ports[i] = port_low + i;
663 }
664
665 *cur_arg += 2;
666 while (*(args[*cur_arg])) {
667 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
668#if defined(CONFIG_HAP_TRANSPARENT)
669 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100670 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
671 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100672 goto err;
673 }
674 if (!strcmp(args[*cur_arg + 1], "client")) {
675 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
676 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
677 }
678 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
679 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
680 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
681 }
682 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
683 char *name, *end;
684
685 name = args[*cur_arg + 1] + 7;
686 while (isspace(*name))
687 name++;
688
689 end = name;
690 while (*end && !isspace(*end) && *end != ',' && *end != ')')
691 end++;
692
693 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
694 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
695 free(newsrv->conn_src.bind_hdr_name);
696 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
697 newsrv->conn_src.bind_hdr_len = end - name;
698 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
699 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
700 newsrv->conn_src.bind_hdr_occ = -1;
701
702 /* now look for an occurrence number */
703 while (isspace(*end))
704 end++;
705 if (*end == ',') {
706 end++;
707 name = end;
708 if (*end == '-')
709 end++;
710 while (isdigit((int)*end))
711 end++;
712 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
713 }
714
715 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100716 ha_alert("usesrc hdr_ip(name,num) does not support negative"
717 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100718 goto err;
719 }
720 }
721 else {
722 struct sockaddr_storage *sk;
723 int port1, port2;
724
725 /* 'sk' is statically allocated (no need to be freed). */
726 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
727 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100728 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100729 goto err;
730 }
731
732 proto = protocol_by_family(sk->ss_family);
733 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100734 ha_alert("'%s %s' : connect() not supported for this address family.\n",
735 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100736 goto err;
737 }
738
739 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100740 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
741 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100742 goto err;
743 }
744 newsrv->conn_src.tproxy_addr = *sk;
745 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
746 }
747 global.last_checks |= LSTCHK_NETADM;
748 *cur_arg += 2;
749 continue;
750#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100751 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 +0100752 goto err;
753#endif /* defined(CONFIG_HAP_TRANSPARENT) */
754 } /* "usesrc" */
755
756 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
757#ifdef SO_BINDTODEVICE
758 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100759 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100760 goto err;
761 }
762 free(newsrv->conn_src.iface_name);
763 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
764 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
765 global.last_checks |= LSTCHK_NETADM;
766#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100767 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100768 goto err;
769#endif
770 *cur_arg += 2;
771 continue;
772 }
773 /* this keyword in not an option of "source" */
774 break;
775 } /* while */
776
777 return 0;
778
779 err:
780 free(errmsg);
781 return ERR_ALERT | ERR_FATAL;
782}
783
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100784/* Parse the "stick" server keyword */
785static int srv_parse_stick(char **args, int *cur_arg,
786 struct proxy *curproxy, struct server *newsrv, char **err)
787{
788 newsrv->flags &= ~SRV_F_NON_STICK;
789 return 0;
790}
791
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100792/* Parse the "track" server keyword */
793static int srv_parse_track(char **args, int *cur_arg,
794 struct proxy *curproxy, struct server *newsrv, char **err)
795{
796 char *arg;
797
798 arg = args[*cur_arg + 1];
799 if (!*arg) {
800 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
801 return ERR_ALERT | ERR_FATAL;
802 }
803
804 free(newsrv->trackit);
805 newsrv->trackit = strdup(arg);
806
807 return 0;
808}
809
Frédéric Lécailledba97072017-03-17 15:33:50 +0100810
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200811/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200812 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200813 * shutdown.
814 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200815void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200816{
Willy Tarreau87b09662015-04-03 00:22:06 +0200817 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200818
Willy Tarreau87b09662015-04-03 00:22:06 +0200819 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
820 if (stream->srv_conn == srv)
821 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200822}
823
824/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200825 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200826 * the reason for the shutdown.
827 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200828void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200829{
830 struct server *srv;
831
832 for (srv = px->srv; srv != NULL; srv = srv->next)
833 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200834 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200835}
836
Willy Tarreaubda92272014-05-20 21:55:30 +0200837/* Appends some information to a message string related to a server going UP or
838 * DOWN. If both <forced> and <reason> are null and the server tracks another
839 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200840 * If <check> is non-null, an entire string describing the check result will be
841 * appended after a comma and a space (eg: to report some information from the
842 * check that changed the state). In the other case, the string will be built
843 * using the check results stored into the struct server if present.
844 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200845 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200846 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200847void srv_append_status(struct buffer *msg, struct server *s,
848 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200849{
Emeric Brun5a133512017-10-19 14:42:30 +0200850 short status = s->op_st_chg.status;
851 short code = s->op_st_chg.code;
852 long duration = s->op_st_chg.duration;
853 char *desc = s->op_st_chg.reason;
854
855 if (check) {
856 status = check->status;
857 code = check->code;
858 duration = check->duration;
859 desc = check->desc;
860 }
861
862 if (status != -1) {
863 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
864
865 if (status >= HCHK_STATUS_L57DATA)
866 chunk_appendf(msg, ", code: %d", code);
867
868 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200869 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200870
871 chunk_appendf(msg, ", info: \"");
872
873 chunk_initlen(&src, desc, 0, strlen(desc));
874 chunk_asciiencode(msg, &src, '"');
875
876 chunk_appendf(msg, "\"");
877 }
878
879 if (duration >= 0)
880 chunk_appendf(msg, ", check duration: %ldms", duration);
881 }
882 else if (desc && *desc) {
883 chunk_appendf(msg, ", %s", desc);
884 }
885 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200886 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200887 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200888
889 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200890 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200891 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
892 " %d sessions active, %d requeued, %d remaining in queue",
893 s->proxy->srv_act, s->proxy->srv_bck,
894 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
895 s->cur_sess, xferred, s->nbpend);
896 else
897 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
898 " %d sessions requeued, %d total in queue",
899 s->proxy->srv_act, s->proxy->srv_bck,
900 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
901 xferred, s->nbpend);
902 }
903}
904
Emeric Brun5a133512017-10-19 14:42:30 +0200905/* Marks server <s> down, regardless of its checks' statuses. The server is
906 * registered in a list to postpone the counting of the remaining servers on
907 * the proxy and transfers queued streams whenever possible to other servers at
908 * a sync point. Maintenance servers are ignored. It stores the <reason> if
909 * non-null as the reason for going down or the available data from the check
910 * struct to recompute this reason later.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200911 */
Emeric Brun5a133512017-10-19 14:42:30 +0200912void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200913{
914 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200915
Emeric Brun64cc49c2017-10-03 14:46:45 +0200916 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200917 return;
918
Emeric Brun52a91d32017-08-31 14:41:55 +0200919 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200920 *s->op_st_chg.reason = 0;
921 s->op_st_chg.status = -1;
922 if (reason) {
923 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
924 }
925 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100926 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200927 s->op_st_chg.code = check->code;
928 s->op_st_chg.status = check->status;
929 s->op_st_chg.duration = check->duration;
930 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200931
Christopher Faulet5d42e092017-10-16 12:00:40 +0200932 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200933 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100934 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200935 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100936 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200937 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200938}
939
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200940/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200941 * in maintenance. The server is registered in a list to postpone the counting
942 * of the remaining servers on the proxy and tries to grab requests from the
943 * proxy at a sync point. Maintenance servers are ignored. It stores the
944 * <reason> if non-null as the reason for going down or the available data
945 * from the check struct to recompute this reason later.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200946 */
Emeric Brun5a133512017-10-19 14:42:30 +0200947void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200948{
949 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200950
Emeric Brun64cc49c2017-10-03 14:46:45 +0200951 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200952 return;
953
Emeric Brun52a91d32017-08-31 14:41:55 +0200954 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200955 return;
956
Emeric Brun52a91d32017-08-31 14:41:55 +0200957 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200958 *s->op_st_chg.reason = 0;
959 s->op_st_chg.status = -1;
960 if (reason) {
961 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
962 }
963 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100964 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200965 s->op_st_chg.code = check->code;
966 s->op_st_chg.status = check->status;
967 s->op_st_chg.duration = check->duration;
968 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200969
Emeric Brun64cc49c2017-10-03 14:46:45 +0200970 if (s->slowstart <= 0)
971 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200972
Christopher Faulet5d42e092017-10-16 12:00:40 +0200973 srv_register_update(s);
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 Tarreaudbd5e782014-05-20 22:46:35 +0200979}
980
Willy Tarreau8eb77842014-05-21 13:54:57 +0200981/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +0200982 * isn't in maintenance. The server is registered in a list to postpone the
983 * counting of the remaining servers on the proxy and tries to grab requests
984 * from the proxy. Maintenance servers are ignored. It stores the
985 * <reason> if non-null as the reason for going down or the available data
986 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +0200987 * up. Note that it makes use of the trash to build the log strings, so <reason>
988 * must not be placed there.
989 */
Emeric Brun5a133512017-10-19 14:42:30 +0200990void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200991{
992 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200993
Emeric Brun64cc49c2017-10-03 14:46:45 +0200994 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200995 return;
996
Emeric Brun52a91d32017-08-31 14:41:55 +0200997 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200998 return;
999
Emeric Brun52a91d32017-08-31 14:41:55 +02001000 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001001 *s->op_st_chg.reason = 0;
1002 s->op_st_chg.status = -1;
1003 if (reason) {
1004 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1005 }
1006 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001007 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001008 s->op_st_chg.code = check->code;
1009 s->op_st_chg.status = check->status;
1010 s->op_st_chg.duration = check->duration;
1011 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001012
Christopher Faulet5d42e092017-10-16 12:00:40 +02001013 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001014 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001015 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001016 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001017 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001018 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001019}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001020
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001021/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1022 * enforce either maint mode or drain mode. It is not allowed to set more than
1023 * one flag at once. The equivalent "inherited" flag is propagated to all
1024 * tracking servers. Maintenance mode disables health checks (but not agent
1025 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001026 * is done. If <cause> is non-null, it will be displayed at the end of the log
1027 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001028 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001029void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001030{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001031 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001032
1033 if (!mode)
1034 return;
1035
1036 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001037 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001038 return;
1039
Emeric Brun52a91d32017-08-31 14:41:55 +02001040 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001041 if (cause)
1042 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1043
Christopher Faulet5d42e092017-10-16 12:00:40 +02001044 srv_register_update(s);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001045
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001046 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001047 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1048 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001049 return;
1050
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001051 /* compute the inherited flag to propagate */
1052 if (mode & SRV_ADMF_MAINT)
1053 mode = SRV_ADMF_IMAINT;
1054 else if (mode & SRV_ADMF_DRAIN)
1055 mode = SRV_ADMF_IDRAIN;
1056
Emeric Brun9f0b4582017-10-23 14:39:51 +02001057 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001058 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001059 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001060 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001061 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001062}
1063
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001064/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1065 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1066 * than one flag at once. The equivalent "inherited" flag is propagated to all
1067 * tracking servers. Leaving maintenance mode re-enables health checks. When
1068 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001069 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001070void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001071{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001072 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001073
1074 if (!mode)
1075 return;
1076
1077 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001078 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001079 return;
1080
Emeric Brun52a91d32017-08-31 14:41:55 +02001081 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001082
Christopher Faulet5d42e092017-10-16 12:00:40 +02001083 srv_register_update(s);
1084
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001085 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001086 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1087 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001088 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001089
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001090 if (mode & SRV_ADMF_MAINT)
1091 mode = SRV_ADMF_IMAINT;
1092 else if (mode & SRV_ADMF_DRAIN)
1093 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001094
Emeric Brun9f0b4582017-10-23 14:39:51 +02001095 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001096 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001097 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001098 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001099 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001100}
1101
Willy Tarreau757478e2016-11-03 19:22:19 +01001102/* principle: propagate maint and drain to tracking servers. This is useful
1103 * upon startup so that inherited states are correct.
1104 */
1105static void srv_propagate_admin_state(struct server *srv)
1106{
1107 struct server *srv2;
1108
1109 if (!srv->trackers)
1110 return;
1111
1112 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001113 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001114 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001115 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001116
Emeric Brun52a91d32017-08-31 14:41:55 +02001117 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001118 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001119 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001120 }
1121}
1122
1123/* Compute and propagate the admin states for all servers in proxy <px>.
1124 * Only servers *not* tracking another one are considered, because other
1125 * ones will be handled when the server they track is visited.
1126 */
1127void srv_compute_all_admin_states(struct proxy *px)
1128{
1129 struct server *srv;
1130
1131 for (srv = px->srv; srv; srv = srv->next) {
1132 if (srv->track)
1133 continue;
1134 srv_propagate_admin_state(srv);
1135 }
1136}
1137
Willy Tarreaudff55432012-10-10 17:51:05 +02001138/* Note: must not be declared <const> as its list will be overwritten.
1139 * Please take care of keeping this list alphabetically sorted, doing so helps
1140 * all code contributors.
1141 * Optional keywords are also declared with a NULL ->parse() function so that
1142 * the config parser can report an appropriate error when a known keyword was
1143 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001144 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001145 */
1146static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001147 { "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 +01001148 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001149 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001150 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001151 { "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 +01001152 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001153 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1154 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001155 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001156 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001157 { "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 +01001158 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001159 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001160 { "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 +01001161 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1162 { "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 +01001163 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001164 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001165 { "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 +01001166 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001167 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1168 { "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 +02001169 { "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 +01001170 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001171 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001172 { NULL, NULL, 0 },
1173}};
1174
1175__attribute__((constructor))
1176static void __listener_init(void)
1177{
1178 srv_register_keywords(&srv_kws);
1179}
1180
Willy Tarreau004e0452013-11-21 11:22:01 +01001181/* Recomputes the server's eweight based on its state, uweight, the current time,
1182 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1183 * state is automatically disabled if the time is elapsed.
1184 */
1185void server_recalc_eweight(struct server *sv)
1186{
1187 struct proxy *px = sv->proxy;
1188 unsigned w;
1189
1190 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1191 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001192 if (sv->next_state == SRV_ST_STARTING)
1193 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001194 }
1195
1196 /* We must take care of not pushing the server to full throttle during slow starts.
1197 * It must also start immediately, at least at the minimal step when leaving maintenance.
1198 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001199 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001200 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1201 else
1202 w = px->lbprm.wdiv;
1203
Emeric Brun52a91d32017-08-31 14:41:55 +02001204 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001205
Christopher Faulet5d42e092017-10-16 12:00:40 +02001206 srv_register_update(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001207}
1208
Willy Tarreaubaaee002006-06-26 02:48:02 +02001209/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001210 * Parses weight_str and configures sv accordingly.
1211 * Returns NULL on success, error message string otherwise.
1212 */
1213const char *server_parse_weight_change_request(struct server *sv,
1214 const char *weight_str)
1215{
1216 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001217 long int w;
1218 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001219
1220 px = sv->proxy;
1221
1222 /* if the weight is terminated with '%', it is set relative to
1223 * the initial weight, otherwise it is absolute.
1224 */
1225 if (!*weight_str)
1226 return "Require <weight> or <weight%>.\n";
1227
Simon Hormanb796afa2013-02-12 10:45:53 +09001228 w = strtol(weight_str, &end, 10);
1229 if (end == weight_str)
1230 return "Empty weight string empty or preceded by garbage";
1231 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001232 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001233 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001234 /* Avoid integer overflow */
1235 if (w > 25600)
1236 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001237 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001238 if (w > 256)
1239 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001240 }
1241 else if (w < 0 || w > 256)
1242 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001243 else if (end[0] != '\0')
1244 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001245
1246 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1247 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1248
1249 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001250 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001251
1252 return NULL;
1253}
1254
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001255/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001256 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1257 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001258 * Returns:
1259 * - error string on error
1260 * - NULL on success
1261 */
1262const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001263 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001264{
1265 unsigned char ip[INET6_ADDRSTRLEN];
1266
1267 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001268 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001269 return NULL;
1270 }
1271 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001272 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001273 return NULL;
1274 }
1275
1276 return "Could not understand IP address format.\n";
1277}
1278
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001279const char *server_parse_maxconn_change_request(struct server *sv,
1280 const char *maxconn_str)
1281{
1282 long int v;
1283 char *end;
1284
1285 if (!*maxconn_str)
1286 return "Require <maxconn>.\n";
1287
1288 v = strtol(maxconn_str, &end, 10);
1289 if (end == maxconn_str)
1290 return "maxconn string empty or preceded by garbage";
1291 else if (end[0] != '\0')
1292 return "Trailing garbage in maxconn string";
1293
1294 if (sv->maxconn == sv->minconn) { // static maxconn
1295 sv->maxconn = sv->minconn = v;
1296 } else { // dynamic maxconn
1297 sv->maxconn = v;
1298 }
1299
1300 if (may_dequeue_tasks(sv, sv->proxy))
1301 process_srv_queue(sv);
1302
1303 return NULL;
1304}
1305
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001306#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001307static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1308 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001309{
1310 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001311 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001312 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001313 NULL,
1314 };
1315
1316 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001317 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001318
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001319 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1320}
1321
1322static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1323{
1324 struct sample_expr *expr;
1325
1326 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 +01001327 if (!expr) {
1328 memprintf(err, "error detected while parsing sni expression : %s", *err);
1329 return ERR_ALERT | ERR_FATAL;
1330 }
1331
1332 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1333 memprintf(err, "error detected while parsing sni expression : "
1334 " fetch method '%s' extracts information from '%s', "
1335 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001336 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001337 return ERR_ALERT | ERR_FATAL;
1338 }
1339
1340 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1341 release_sample_expr(newsrv->ssl_ctx.sni);
1342 newsrv->ssl_ctx.sni = expr;
1343
1344 return 0;
1345}
1346#endif
1347
1348static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1349{
1350 if (err && *err) {
1351 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001352 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 +01001353 }
1354 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001355 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1356 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001357}
1358
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001359static void srv_conn_src_sport_range_cpy(struct server *srv,
1360 struct server *src)
1361{
1362 int range_sz;
1363
1364 range_sz = src->conn_src.sport_range->size;
1365 if (range_sz > 0) {
1366 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1367 if (srv->conn_src.sport_range != NULL) {
1368 int i;
1369
1370 for (i = 0; i < range_sz; i++) {
1371 srv->conn_src.sport_range->ports[i] =
1372 src->conn_src.sport_range->ports[i];
1373 }
1374 }
1375 }
1376}
1377
1378/*
1379 * Copy <src> server connection source settings to <srv> server everything needed.
1380 */
1381static void srv_conn_src_cpy(struct server *srv, struct server *src)
1382{
1383 srv->conn_src.opts = src->conn_src.opts;
1384 srv->conn_src.source_addr = src->conn_src.source_addr;
1385
1386 /* Source port range copy. */
1387 if (src->conn_src.sport_range != NULL)
1388 srv_conn_src_sport_range_cpy(srv, src);
1389
1390#ifdef CONFIG_HAP_TRANSPARENT
1391 if (src->conn_src.bind_hdr_name != NULL) {
1392 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1393 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1394 }
1395 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1396 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1397#endif
1398 if (src->conn_src.iface_name != NULL)
1399 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1400}
1401
1402/*
1403 * Copy <src> server SSL settings to <srv> server allocating
1404 * everything needed.
1405 */
1406#if defined(USE_OPENSSL)
1407static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1408{
1409 if (src->ssl_ctx.ca_file != NULL)
1410 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1411 if (src->ssl_ctx.crl_file != NULL)
1412 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1413 if (src->ssl_ctx.client_crt != NULL)
1414 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1415
1416 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1417
1418 if (src->ssl_ctx.verify_host != NULL)
1419 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1420 if (src->ssl_ctx.ciphers != NULL)
1421 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1422 if (src->sni_expr != NULL)
1423 srv->sni_expr = strdup(src->sni_expr);
1424}
1425#endif
1426
1427/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001428 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001429 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001430 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001431 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001432static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001433{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001434 char *hostname_dn;
1435 int hostname_len, hostname_dn_len;
1436
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001437 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001438 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001439
Christopher Faulet67957bd2017-09-27 11:00:59 +02001440 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001441 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001442 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1443 hostname_dn, trash.size);
1444 if (hostname_dn_len == -1)
1445 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001446
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001447
Christopher Faulet67957bd2017-09-27 11:00:59 +02001448 free(srv->hostname);
1449 free(srv->hostname_dn);
1450 srv->hostname = strdup(hostname);
1451 srv->hostname_dn = strdup(hostname_dn);
1452 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001453 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001454 goto err;
1455
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001456 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001457
1458 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001459 free(srv->hostname); srv->hostname = NULL;
1460 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001461 return -1;
1462}
1463
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001464/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001465 * Copy <src> server settings to <srv> server allocating
1466 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001467 * This function is not supposed to be called at any time, but only
1468 * during server settings parsing or during server allocations from
1469 * a server template, and just after having calloc()'ed a new server.
1470 * So, <src> may only be a default server (when parsing server settings)
1471 * or a server template (during server allocations from a server template).
1472 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1473 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001474 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001475static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001476{
1477 /* Connection source settings copy */
1478 srv_conn_src_cpy(srv, src);
1479
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001480 if (srv_tmpl) {
1481 srv->addr = src->addr;
1482 srv->svc_port = src->svc_port;
1483 }
1484
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001485 srv->pp_opts = src->pp_opts;
1486 if (src->rdr_pfx != NULL) {
1487 srv->rdr_pfx = strdup(src->rdr_pfx);
1488 srv->rdr_len = src->rdr_len;
1489 }
1490 if (src->cookie != NULL) {
1491 srv->cookie = strdup(src->cookie);
1492 srv->cklen = src->cklen;
1493 }
1494 srv->use_ssl = src->use_ssl;
1495 srv->check.addr = srv->agent.addr = src->check.addr;
1496 srv->check.use_ssl = src->check.use_ssl;
1497 srv->check.port = src->check.port;
1498 /* Note: 'flags' field has potentially been already initialized. */
1499 srv->flags |= src->flags;
1500 srv->do_check = src->do_check;
1501 srv->do_agent = src->do_agent;
1502 if (srv->check.port)
1503 srv->flags |= SRV_F_CHECKPORT;
1504 srv->check.inter = src->check.inter;
1505 srv->check.fastinter = src->check.fastinter;
1506 srv->check.downinter = src->check.downinter;
1507 srv->agent.use_ssl = src->agent.use_ssl;
1508 srv->agent.port = src->agent.port;
1509 if (src->agent.send_string != NULL)
1510 srv->agent.send_string = strdup(src->agent.send_string);
1511 srv->agent.send_string_len = src->agent.send_string_len;
1512 srv->agent.inter = src->agent.inter;
1513 srv->agent.fastinter = src->agent.fastinter;
1514 srv->agent.downinter = src->agent.downinter;
1515 srv->maxqueue = src->maxqueue;
1516 srv->minconn = src->minconn;
1517 srv->maxconn = src->maxconn;
1518 srv->slowstart = src->slowstart;
1519 srv->observe = src->observe;
1520 srv->onerror = src->onerror;
1521 srv->onmarkeddown = src->onmarkeddown;
1522 srv->onmarkedup = src->onmarkedup;
1523 if (src->trackit != NULL)
1524 srv->trackit = strdup(src->trackit);
1525 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1526 srv->uweight = srv->iweight = src->iweight;
1527
1528 srv->check.send_proxy = src->check.send_proxy;
1529 /* health: up, but will fall down at first failure */
1530 srv->check.rise = srv->check.health = src->check.rise;
1531 srv->check.fall = src->check.fall;
1532
1533 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001534 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1535 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1536 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001537 srv->check.state |= CHK_ST_PAUSED;
1538 srv->check.health = 0;
1539 }
1540
1541 /* health: up but will fall down at first failure */
1542 srv->agent.rise = srv->agent.health = src->agent.rise;
1543 srv->agent.fall = src->agent.fall;
1544
1545 if (src->resolvers_id != NULL)
1546 srv->resolvers_id = strdup(src->resolvers_id);
1547 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001548 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001549 if (srv->dns_opts.family_prio == AF_UNSPEC)
1550 srv->dns_opts.family_prio = AF_INET6;
1551 memcpy(srv->dns_opts.pref_net,
1552 src->dns_opts.pref_net,
1553 sizeof srv->dns_opts.pref_net);
1554 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1555
1556 srv->init_addr_methods = src->init_addr_methods;
1557 srv->init_addr = src->init_addr;
1558#if defined(USE_OPENSSL)
1559 srv_ssl_settings_cpy(srv, src);
1560#endif
1561#ifdef TCP_USER_TIMEOUT
1562 srv->tcp_ut = src->tcp_ut;
1563#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001564 if (srv_tmpl)
1565 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001566}
1567
1568static struct server *new_server(struct proxy *proxy)
1569{
1570 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001571 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001572
1573 srv = calloc(1, sizeof *srv);
1574 if (!srv)
1575 return NULL;
1576
1577 srv->obj_type = OBJ_TYPE_SERVER;
1578 srv->proxy = proxy;
1579 LIST_INIT(&srv->actconns);
1580 LIST_INIT(&srv->pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02001581
1582 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1583 goto free_srv;
1584 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1585 goto free_priv_conns;
1586 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1587 goto free_idle_conns;
1588
1589 for (i = 0; i < global.nbthread; i++) {
1590 LIST_INIT(&srv->priv_conns[i]);
1591 LIST_INIT(&srv->idle_conns[i]);
1592 LIST_INIT(&srv->safe_conns[i]);
1593 }
1594
Emeric Brun64cc49c2017-10-03 14:46:45 +02001595 LIST_INIT(&srv->update_status);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001596
Emeric Brun52a91d32017-08-31 14:41:55 +02001597 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001598 srv->last_change = now.tv_sec;
1599
1600 srv->check.status = HCHK_STATUS_INI;
1601 srv->check.server = srv;
1602 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1603
1604 srv->agent.status = HCHK_STATUS_INI;
1605 srv->agent.server = srv;
1606 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1607
1608 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001609
1610 free_idle_conns:
1611 free(srv->idle_conns);
1612 free_priv_conns:
1613 free(srv->priv_conns);
1614 free_srv:
1615 free(srv);
1616 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001617}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001618
1619/*
1620 * Validate <srv> server health-check settings.
1621 * Returns 0 if everything is OK, -1 if not.
1622 */
1623static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1624{
1625 struct tcpcheck_rule *r = NULL;
1626 struct list *l;
1627
1628 /*
1629 * We need at least a service port, a check port or the first tcp-check rule must
1630 * be a 'connect' one when checking an IPv4/IPv6 server.
1631 */
1632 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1633 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1634 return 0;
1635
1636 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1637 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001638 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1639 "Check has been disabled.\n",
1640 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001641 return -1;
1642 }
1643
1644 /* search the first action (connect / send / expect) in the list */
1645 l = &srv->proxy->tcpcheck_rules;
1646 list_for_each_entry(r, l, list) {
1647 if (r->action != TCPCHK_ACT_COMMENT)
1648 break;
1649 }
1650
1651 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001652 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1653 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1654 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001655 return -1;
1656 }
1657
1658 /* scan the tcp-check ruleset to ensure a port has been configured */
1659 l = &srv->proxy->tcpcheck_rules;
1660 list_for_each_entry(r, l, list) {
1661 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001662 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1663 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1664 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001665 return -1;
1666 }
1667 }
1668
1669 return 0;
1670}
1671
1672/*
1673 * Initialize <srv> health-check structure.
1674 * Returns the error string in case of memory allocation failure, NULL if not.
1675 */
1676static const char *do_health_check_init(struct server *srv, int check_type, int state)
1677{
1678 const char *ret;
1679
1680 if (!srv->do_check)
1681 return NULL;
1682
1683 ret = init_check(&srv->check, check_type);
1684 if (ret)
1685 return ret;
1686
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001687 srv->check.state |= state;
1688 global.maxsock++;
1689
1690 return NULL;
1691}
1692
1693static int server_health_check_init(const char *file, int linenum,
1694 struct server *srv, struct proxy *curproxy)
1695{
1696 const char *ret;
1697
1698 if (!srv->do_check)
1699 return 0;
1700
1701 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001702 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1703 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001704 return ERR_ALERT | ERR_FATAL;
1705 }
1706
1707 if (server_healthcheck_validate(file, linenum, srv) < 0)
1708 return ERR_ALERT | ERR_ABORT;
1709
1710 /* note: check type will be set during the config review phase */
1711 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1712 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001713 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001714 return ERR_ALERT | ERR_ABORT;
1715 }
1716
1717 return 0;
1718}
1719
1720/*
1721 * Initialize <srv> agent check structure.
1722 * Returns the error string in case of memory allocation failure, NULL if not.
1723 */
1724static const char *do_server_agent_check_init(struct server *srv, int state)
1725{
1726 const char *ret;
1727
1728 if (!srv->do_agent)
1729 return NULL;
1730
1731 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1732 if (ret)
1733 return ret;
1734
1735 if (!srv->agent.inter)
1736 srv->agent.inter = srv->check.inter;
1737
1738 srv->agent.state |= state;
1739 global.maxsock++;
1740
1741 return NULL;
1742}
1743
1744static int server_agent_check_init(const char *file, int linenum,
1745 struct server *srv, struct proxy *curproxy)
1746{
1747 const char *ret;
1748
1749 if (!srv->do_agent)
1750 return 0;
1751
1752 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001753 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 +02001754 file, linenum, srv->id);
1755 return ERR_ALERT | ERR_FATAL;
1756 }
1757
1758 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1759 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001760 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001761 return ERR_ALERT | ERR_ABORT;
1762 }
1763
1764 return 0;
1765}
1766
1767#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1768static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1769 struct server *srv, struct proxy *proxy)
1770{
1771 int ret;
1772 char *err = NULL;
1773
1774 if (!srv->sni_expr)
1775 return 0;
1776
1777 ret = server_parse_sni_expr(srv, proxy, &err);
1778 if (!ret)
1779 return 0;
1780
1781 display_parser_err(file, linenum, args, cur_arg, &err);
1782 free(err);
1783
1784 return ret;
1785}
1786#endif
1787
1788/*
1789 * Server initializations finalization.
1790 * Initialize health check, agent check and SNI expression if enabled.
1791 * Must not be called for a default server instance.
1792 */
1793static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1794 struct server *srv, struct proxy *px)
1795{
1796 int ret;
1797
1798 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1799 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1800 return ret;
1801 }
1802
1803#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1804 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1805 return ret;
1806#endif
1807
1808 if (srv->flags & SRV_F_BACKUP)
1809 px->srv_bck++;
1810 else
1811 px->srv_act++;
1812 srv_lb_commit_status(srv);
1813
1814 return 0;
1815}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001816
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001817/*
1818 * Parse as much as possible such a range string argument: low[-high]
1819 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1820 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1821 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1822 * Returns 0 if succeeded, -1 if not.
1823 */
1824static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1825{
1826 char *nb_high_arg;
1827
1828 *nb_high = 0;
1829 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001830 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001831
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001832 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001833 *nb_high_arg++ = '\0';
1834 *nb_high = atoi(nb_high_arg);
1835 }
1836 else {
1837 *nb_high += *nb_low;
1838 *nb_low = 1;
1839 }
1840
1841 if (*nb_low < 0 || *nb_high < *nb_low)
1842 return -1;
1843
1844 return 0;
1845}
1846
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001847static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1848{
1849 chunk_printf(&trash, "%s%d", prefix, nb);
1850 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001851 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001852}
1853
1854/*
1855 * Initialize as much as possible servers from <srv> server template.
1856 * Note that a server template is a special server with
1857 * a few different parameters than a server which has
1858 * been parsed mostly the same way as a server.
1859 * Returns the number of servers succesfully allocated,
1860 * 'srv' template included.
1861 */
1862static int server_template_init(struct server *srv, struct proxy *px)
1863{
1864 int i;
1865 struct server *newsrv;
1866
1867 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1868 int check_init_state;
1869 int agent_init_state;
1870
1871 newsrv = new_server(px);
1872 if (!newsrv)
1873 goto err;
1874
1875 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001876 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001877#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1878 if (newsrv->sni_expr) {
1879 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1880 if (!newsrv->ssl_ctx.sni)
1881 goto err;
1882 }
1883#endif
1884 /* Set this new server ID. */
1885 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1886
1887 /* Initial checks states. */
1888 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1889 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1890
1891 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1892 do_server_agent_check_init(newsrv, agent_init_state))
1893 goto err;
1894
1895 /* Linked backwards first. This will be restablished after parsing. */
1896 newsrv->next = px->srv;
1897 px->srv = newsrv;
1898 }
1899 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1900
1901 return i - srv->tmpl_info.nb_low;
1902
1903 err:
1904 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1905 if (newsrv) {
1906#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1907 release_sample_expr(newsrv->ssl_ctx.sni);
1908#endif
1909 free_check(&newsrv->agent);
1910 free_check(&newsrv->check);
1911 }
1912 free(newsrv);
1913 return i - srv->tmpl_info.nb_low;
1914}
1915
Willy Tarreau272adea2014-03-31 10:39:59 +02001916int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1917{
1918 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001919 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001920 char *errmsg = NULL;
1921 int err_code = 0;
1922 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001923 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001924
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001925 if (!strcmp(args[0], "server") ||
1926 !strcmp(args[0], "default-server") ||
1927 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001928 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001929 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001930 int srv = !defsrv && !strcmp(args[0], "server");
1931 int srv_tmpl = !defsrv && !srv;
1932 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001933
1934 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001935 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001936 err_code |= ERR_ALERT | ERR_FATAL;
1937 goto out;
1938 }
1939 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02001940 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02001941
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001942 /* There is no mandatory first arguments for default server. */
1943 if (srv) {
1944 if (!*args[2]) {
1945 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001946 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001947 file, linenum, args[0]);
1948 err_code |= ERR_ALERT | ERR_FATAL;
1949 goto out;
1950 }
1951
1952 err = invalid_char(args[1]);
1953 }
1954 else if (srv_tmpl) {
1955 if (!*args[3]) {
1956 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001957 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 +02001958 file, linenum, args[0]);
1959 err_code |= ERR_ALERT | ERR_FATAL;
1960 goto out;
1961 }
1962
1963 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001964 }
1965
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001966 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001967 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 +02001968 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001969 err_code |= ERR_ALERT | ERR_FATAL;
1970 goto out;
1971 }
1972
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001973 cur_arg = 2;
1974 if (srv_tmpl) {
1975 /* Parse server-template <nb | range> arg. */
1976 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001977 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001978 file, linenum, args[0], args[cur_arg]);
1979 err_code |= ERR_ALERT | ERR_FATAL;
1980 goto out;
1981 }
1982 cur_arg++;
1983 }
1984
Willy Tarreau272adea2014-03-31 10:39:59 +02001985 if (!defsrv) {
1986 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001987 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001988 struct protocol *proto;
1989
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001990 newsrv = new_server(curproxy);
1991 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001992 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02001993 err_code |= ERR_ALERT | ERR_ABORT;
1994 goto out;
1995 }
1996
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001997 if (srv_tmpl) {
1998 newsrv->tmpl_info.nb_low = tmpl_range_low;
1999 newsrv->tmpl_info.nb_high = tmpl_range_high;
2000 }
2001
Willy Tarreau272adea2014-03-31 10:39:59 +02002002 /* the servers are linked backwards first */
2003 newsrv->next = curproxy->srv;
2004 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002005 newsrv->conf.file = strdup(file);
2006 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002007 /* Note: for a server template, its id is its prefix.
2008 * This is a temporary id which will be used for server allocations to come
2009 * after parsing.
2010 */
2011 if (srv)
2012 newsrv->id = strdup(args[1]);
2013 else
2014 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002015
2016 /* several ways to check the port component :
2017 * - IP => port=+0, relative (IPv4 only)
2018 * - IP: => port=+0, relative
2019 * - IP:N => port=N, absolute
2020 * - IP:+N => port=+N, relative
2021 * - IP:-N => port=-N, relative
2022 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002023 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002024 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002025 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002026 err_code |= ERR_ALERT | ERR_FATAL;
2027 goto out;
2028 }
2029
2030 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002031 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002032 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002033 file, linenum, args[0], args[1]);
2034 err_code |= ERR_ALERT | ERR_FATAL;
2035 goto out;
2036 }
2037
2038 if (!port1 || !port2) {
2039 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002040 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002041 }
2042 else if (port1 != port2) {
2043 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002044 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002045 file, linenum, args[0], args[1], args[2]);
2046 err_code |= ERR_ALERT | ERR_FATAL;
2047 goto out;
2048 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002049
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002050 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002051 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002052 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002053 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002054 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2055 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2056 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002057 err_code |= ERR_ALERT | ERR_FATAL;
2058 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002059 }
2060 }
2061 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002062 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002063 file, linenum, newsrv->id);
2064 err_code |= ERR_ALERT | ERR_FATAL;
2065 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002066 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002067 }
2068
Willy Tarreau272adea2014-03-31 10:39:59 +02002069 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002070 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002071
Olivier Houchard8da5f982017-08-04 18:35:36 +02002072 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002073 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002074 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002075 err_code |= ERR_ALERT | ERR_FATAL;
2076 goto out;
2077 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002078
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002079 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002080 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002081 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002082 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002083 } else {
2084 newsrv = &curproxy->defsrv;
2085 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002086 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002087 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002088 }
2089
2090 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002091 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002092 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2093 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002094 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002095 file, linenum, *err, newsrv->id);
2096 err_code |= ERR_ALERT | ERR_FATAL;
2097 goto out;
2098 }
2099 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002100 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002101 file, linenum, val, args[cur_arg], newsrv->id);
2102 err_code |= ERR_ALERT | ERR_FATAL;
2103 goto out;
2104 }
2105 newsrv->agent.inter = val;
2106 cur_arg += 2;
2107 }
Misiekea849332017-01-09 09:39:51 +01002108 else if (!strcmp(args[cur_arg], "agent-addr")) {
2109 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002110 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002111 goto out;
2112 }
2113
2114 cur_arg += 2;
2115 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002116 else if (!strcmp(args[cur_arg], "agent-port")) {
2117 global.maxsock++;
2118 newsrv->agent.port = atol(args[cur_arg + 1]);
2119 cur_arg += 2;
2120 }
James Brown55f9ff12015-10-21 18:19:05 -07002121 else if (!strcmp(args[cur_arg], "agent-send")) {
2122 global.maxsock++;
2123 free(newsrv->agent.send_string);
2124 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2125 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2126 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2127 cur_arg += 2;
2128 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002129 else if (!strcmp(args[cur_arg], "init-addr")) {
2130 char *p, *end;
2131 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002132 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002133
2134 newsrv->init_addr_methods = 0;
2135 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2136
2137 for (p = args[cur_arg + 1]; *p; p = end) {
2138 /* cut on next comma */
2139 for (end = p; *end && *end != ','; end++);
2140 if (*end)
2141 *(end++) = 0;
2142
Willy Tarreau4310d362016-11-02 15:05:56 +01002143 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002144 if (!strcmp(p, "libc")) {
2145 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2146 }
2147 else if (!strcmp(p, "last")) {
2148 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2149 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002150 else if (!strcmp(p, "none")) {
2151 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2152 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002153 else if (str2ip2(p, &sa, 0)) {
2154 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002155 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002156 file, linenum, args[cur_arg], p);
2157 err_code |= ERR_ALERT | ERR_FATAL;
2158 goto out;
2159 }
2160 newsrv->init_addr = sa;
2161 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2162 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002163 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002164 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 +02002165 file, linenum, args[cur_arg], p);
2166 err_code |= ERR_ALERT | ERR_FATAL;
2167 goto out;
2168 }
2169 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002170 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002171 file, linenum, args[cur_arg], p);
2172 err_code |= ERR_ALERT | ERR_FATAL;
2173 goto out;
2174 }
2175 }
2176 cur_arg += 2;
2177 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002178 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002179 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002180 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2181 cur_arg += 2;
2182 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002183 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2184 char *p, *end;
2185
2186 for (p = args[cur_arg + 1]; *p; p = end) {
2187 /* cut on next comma */
2188 for (end = p; *end && *end != ','; end++);
2189 if (*end)
2190 *(end++) = 0;
2191
2192 if (!strcmp(p, "allow-dup-ip")) {
2193 newsrv->dns_opts.accept_duplicate_ip = 1;
2194 }
2195 else if (!strcmp(p, "prevent-dup-ip")) {
2196 newsrv->dns_opts.accept_duplicate_ip = 0;
2197 }
2198 else {
2199 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2200 file, linenum, args[cur_arg], p);
2201 err_code |= ERR_ALERT | ERR_FATAL;
2202 goto out;
2203 }
2204 }
2205
2206 cur_arg += 2;
2207 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002208 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2209 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002210 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002211 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002212 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002213 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002214 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002215 file, linenum, args[cur_arg]);
2216 err_code |= ERR_ALERT | ERR_FATAL;
2217 goto out;
2218 }
2219 cur_arg += 2;
2220 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002221 else if (!strcmp(args[cur_arg], "resolve-net")) {
2222 char *p, *e;
2223 unsigned char mask;
2224 struct dns_options *opt;
2225
2226 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002227 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002228 file, linenum, args[cur_arg]);
2229 err_code |= ERR_ALERT | ERR_FATAL;
2230 goto out;
2231 }
2232
2233 opt = &newsrv->dns_opts;
2234
2235 /* Split arguments by comma, and convert it from ipv4 or ipv6
2236 * string network in in_addr or in6_addr.
2237 */
2238 p = args[cur_arg + 1];
2239 e = p;
2240 while (*p != '\0') {
2241 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002242 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002243 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002244 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2245 err_code |= ERR_ALERT | ERR_FATAL;
2246 goto out;
2247 }
2248 /* look for end or comma. */
2249 while (*e != ',' && *e != '\0')
2250 e++;
2251 if (*e == ',') {
2252 *e = '\0';
2253 e++;
2254 }
2255 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2256 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2257 /* Try to convert input string from ipv4 or ipv6 network. */
2258 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2259 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2260 &mask)) {
2261 /* Try to convert input string from ipv6 network. */
2262 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2263 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2264 } else {
2265 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002266 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002267 file, linenum, args[cur_arg], p);
2268 err_code |= ERR_ALERT | ERR_FATAL;
2269 goto out;
2270 }
2271 opt->pref_net_nb++;
2272 p = e;
2273 }
2274
2275 cur_arg += 2;
2276 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002277 else if (!strcmp(args[cur_arg], "rise")) {
2278 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002279 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002280 file, linenum, args[cur_arg]);
2281 err_code |= ERR_ALERT | ERR_FATAL;
2282 goto out;
2283 }
2284
2285 newsrv->check.rise = atol(args[cur_arg + 1]);
2286 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002287 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002288 file, linenum, args[cur_arg]);
2289 err_code |= ERR_ALERT | ERR_FATAL;
2290 goto out;
2291 }
2292
2293 if (newsrv->check.health)
2294 newsrv->check.health = newsrv->check.rise;
2295 cur_arg += 2;
2296 }
2297 else if (!strcmp(args[cur_arg], "fall")) {
2298 newsrv->check.fall = atol(args[cur_arg + 1]);
2299
2300 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002301 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002302 file, linenum, args[cur_arg]);
2303 err_code |= ERR_ALERT | ERR_FATAL;
2304 goto out;
2305 }
2306
2307 if (newsrv->check.fall <= 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 cur_arg += 2;
2315 }
2316 else if (!strcmp(args[cur_arg], "inter")) {
2317 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2318 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002319 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002320 file, linenum, *err, newsrv->id);
2321 err_code |= ERR_ALERT | ERR_FATAL;
2322 goto out;
2323 }
2324 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002325 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002326 file, linenum, val, args[cur_arg], newsrv->id);
2327 err_code |= ERR_ALERT | ERR_FATAL;
2328 goto out;
2329 }
2330 newsrv->check.inter = val;
2331 cur_arg += 2;
2332 }
2333 else if (!strcmp(args[cur_arg], "fastinter")) {
2334 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2335 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002336 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002337 file, linenum, *err, newsrv->id);
2338 err_code |= ERR_ALERT | ERR_FATAL;
2339 goto out;
2340 }
2341 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002342 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002343 file, linenum, val, args[cur_arg], newsrv->id);
2344 err_code |= ERR_ALERT | ERR_FATAL;
2345 goto out;
2346 }
2347 newsrv->check.fastinter = val;
2348 cur_arg += 2;
2349 }
2350 else if (!strcmp(args[cur_arg], "downinter")) {
2351 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2352 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002353 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002354 file, linenum, *err, newsrv->id);
2355 err_code |= ERR_ALERT | ERR_FATAL;
2356 goto out;
2357 }
2358 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002359 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002360 file, linenum, val, args[cur_arg], newsrv->id);
2361 err_code |= ERR_ALERT | ERR_FATAL;
2362 goto out;
2363 }
2364 newsrv->check.downinter = val;
2365 cur_arg += 2;
2366 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002367 else if (!strcmp(args[cur_arg], "port")) {
2368 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002369 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002370 cur_arg += 2;
2371 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002372 else if (!strcmp(args[cur_arg], "weight")) {
2373 int w;
2374 w = atol(args[cur_arg + 1]);
2375 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002376 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002377 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2378 err_code |= ERR_ALERT | ERR_FATAL;
2379 goto out;
2380 }
2381 newsrv->uweight = newsrv->iweight = w;
2382 cur_arg += 2;
2383 }
2384 else if (!strcmp(args[cur_arg], "minconn")) {
2385 newsrv->minconn = atol(args[cur_arg + 1]);
2386 cur_arg += 2;
2387 }
2388 else if (!strcmp(args[cur_arg], "maxconn")) {
2389 newsrv->maxconn = atol(args[cur_arg + 1]);
2390 cur_arg += 2;
2391 }
2392 else if (!strcmp(args[cur_arg], "maxqueue")) {
2393 newsrv->maxqueue = atol(args[cur_arg + 1]);
2394 cur_arg += 2;
2395 }
2396 else if (!strcmp(args[cur_arg], "slowstart")) {
2397 /* slowstart is stored in seconds */
2398 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2399 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002400 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002401 file, linenum, *err, newsrv->id);
2402 err_code |= ERR_ALERT | ERR_FATAL;
2403 goto out;
2404 }
2405 newsrv->slowstart = (val + 999) / 1000;
2406 cur_arg += 2;
2407 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002408 else if (!strcmp(args[cur_arg], "on-error")) {
2409 if (!strcmp(args[cur_arg + 1], "fastinter"))
2410 newsrv->onerror = HANA_ONERR_FASTINTER;
2411 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2412 newsrv->onerror = HANA_ONERR_FAILCHK;
2413 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2414 newsrv->onerror = HANA_ONERR_SUDDTH;
2415 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2416 newsrv->onerror = HANA_ONERR_MARKDWN;
2417 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002418 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002419 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2420 file, linenum, args[cur_arg], args[cur_arg + 1]);
2421 err_code |= ERR_ALERT | ERR_FATAL;
2422 goto out;
2423 }
2424
2425 cur_arg += 2;
2426 }
2427 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2428 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2429 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2430 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002431 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002432 file, linenum, args[cur_arg], args[cur_arg + 1]);
2433 err_code |= ERR_ALERT | ERR_FATAL;
2434 goto out;
2435 }
2436
2437 cur_arg += 2;
2438 }
2439 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2440 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2441 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2442 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002443 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002444 file, linenum, args[cur_arg], args[cur_arg + 1]);
2445 err_code |= ERR_ALERT | ERR_FATAL;
2446 goto out;
2447 }
2448
2449 cur_arg += 2;
2450 }
2451 else if (!strcmp(args[cur_arg], "error-limit")) {
2452 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002453 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002454 file, linenum, args[cur_arg]);
2455 err_code |= ERR_ALERT | ERR_FATAL;
2456 goto out;
2457 }
2458
2459 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2460
2461 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002462 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002463 file, linenum, args[cur_arg]);
2464 err_code |= ERR_ALERT | ERR_FATAL;
2465 goto out;
2466 }
2467 cur_arg += 2;
2468 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002469 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002470 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002471 file, linenum, "usesrc", "source");
2472 err_code |= ERR_ALERT | ERR_FATAL;
2473 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002474 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002475 else {
2476 static int srv_dumped;
2477 struct srv_kw *kw;
2478 char *err;
2479
2480 kw = srv_find_kw(args[cur_arg]);
2481 if (kw) {
2482 char *err = NULL;
2483 int code;
2484
2485 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002486 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 +02002487 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002488 if (kw->skip != -1)
2489 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002490 err_code |= ERR_ALERT | ERR_FATAL;
2491 goto out;
2492 }
2493
2494 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002495 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002496 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002497 if (kw->skip != -1)
2498 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002499 err_code |= ERR_ALERT;
2500 continue;
2501 }
2502
2503 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2504 err_code |= code;
2505
2506 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002507 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002508 if (code & ERR_FATAL) {
2509 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002510 if (kw->skip != -1)
2511 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002512 goto out;
2513 }
2514 }
2515 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002516 if (kw->skip != -1)
2517 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002518 continue;
2519 }
2520
2521 err = NULL;
2522 if (!srv_dumped) {
2523 srv_dump_kws(&err);
2524 indent_msg(&err, 4);
2525 srv_dumped = 1;
2526 }
2527
Christopher Faulet767a84b2017-11-24 16:50:31 +01002528 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002529 file, linenum, args[0], args[1], args[cur_arg],
2530 err ? " Registered keywords :" : "", err ? err : "");
2531 free(err);
2532
2533 err_code |= ERR_ALERT | ERR_FATAL;
2534 goto out;
2535 }
2536 }
2537
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002538 if (!defsrv)
2539 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2540 if (err_code & ERR_FATAL)
2541 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002542 if (srv_tmpl)
2543 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002544 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002545 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002546 return 0;
2547
2548 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002549 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002550 free(errmsg);
2551 return err_code;
2552}
2553
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002554/* Returns a pointer to the first server matching either id <id>.
2555 * NULL is returned if no match is found.
2556 * the lookup is performed in the backend <bk>
2557 */
2558struct server *server_find_by_id(struct proxy *bk, int id)
2559{
2560 struct eb32_node *eb32;
2561 struct server *curserver;
2562
2563 if (!bk || (id ==0))
2564 return NULL;
2565
2566 /* <bk> has no backend capabilities, so it can't have a server */
2567 if (!(bk->cap & PR_CAP_BE))
2568 return NULL;
2569
2570 curserver = NULL;
2571
2572 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2573 if (eb32)
2574 curserver = container_of(eb32, struct server, conf.id);
2575
2576 return curserver;
2577}
2578
2579/* Returns a pointer to the first server matching either name <name>, or id
2580 * if <name> starts with a '#'. NULL is returned if no match is found.
2581 * the lookup is performed in the backend <bk>
2582 */
2583struct server *server_find_by_name(struct proxy *bk, const char *name)
2584{
2585 struct server *curserver;
2586
2587 if (!bk || !name)
2588 return NULL;
2589
2590 /* <bk> has no backend capabilities, so it can't have a server */
2591 if (!(bk->cap & PR_CAP_BE))
2592 return NULL;
2593
2594 curserver = NULL;
2595 if (*name == '#') {
2596 curserver = server_find_by_id(bk, atoi(name + 1));
2597 if (curserver)
2598 return curserver;
2599 }
2600 else {
2601 curserver = bk->srv;
2602
2603 while (curserver && (strcmp(curserver->id, name) != 0))
2604 curserver = curserver->next;
2605
2606 if (curserver)
2607 return curserver;
2608 }
2609
2610 return NULL;
2611}
2612
2613struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2614{
2615 struct server *byname;
2616 struct server *byid;
2617
2618 if (!name && !id)
2619 return NULL;
2620
2621 if (diff)
2622 *diff = 0;
2623
2624 byname = byid = NULL;
2625
2626 if (name) {
2627 byname = server_find_by_name(bk, name);
2628 if (byname && (!id || byname->puid == id))
2629 return byname;
2630 }
2631
2632 /* remaining possibilities :
2633 * - name not set
2634 * - name set but not found
2635 * - name found but ID doesn't match
2636 */
2637 if (id) {
2638 byid = server_find_by_id(bk, id);
2639 if (byid) {
2640 if (byname) {
2641 /* use id only if forced by configuration */
2642 if (byid->flags & SRV_F_FORCED_ID) {
2643 if (diff)
2644 *diff |= 2;
2645 return byid;
2646 }
2647 else {
2648 if (diff)
2649 *diff |= 1;
2650 return byname;
2651 }
2652 }
2653
2654 /* remaining possibilities:
2655 * - name not set
2656 * - name set but not found
2657 */
2658 if (name && diff)
2659 *diff |= 2;
2660 return byid;
2661 }
2662
2663 /* id bot found */
2664 if (byname) {
2665 if (diff)
2666 *diff |= 1;
2667 return byname;
2668 }
2669 }
2670
2671 return NULL;
2672}
2673
Christopher Faulet5d42e092017-10-16 12:00:40 +02002674/* Registers changes to be applied asynchronously */
2675static void srv_register_update(struct server *srv)
2676{
2677 if (LIST_ISEMPTY(&srv->update_status)) {
2678 THREAD_WANT_SYNC();
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002679 HA_SPIN_LOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
Christopher Faulet5d42e092017-10-16 12:00:40 +02002680 if (LIST_ISEMPTY(&srv->update_status))
2681 LIST_ADDQ(&updated_servers, &srv->update_status);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002682 HA_SPIN_UNLOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
Christopher Faulet5d42e092017-10-16 12:00:40 +02002683 }
2684}
2685
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002686/* Update a server state using the parameters available in the params list */
2687static void srv_update_state(struct server *srv, int version, char **params)
2688{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002689 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002690 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002691
2692 /* fields since version 1
2693 * and common to all other upcoming versions
2694 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002695 enum srv_state srv_op_state;
2696 enum srv_admin srv_admin_state;
2697 unsigned srv_uweight, srv_iweight;
2698 unsigned long srv_last_time_change;
2699 short srv_check_status;
2700 enum chk_result srv_check_result;
2701 int srv_check_health;
2702 int srv_check_state, srv_agent_state;
2703 int bk_f_forced_id;
2704 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002705 int fqdn_set_by_cli;
2706 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002707 const char *port_str;
2708 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002709
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002710 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002711 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002712 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002713 switch (version) {
2714 case 1:
2715 /*
2716 * now we can proceed with server's state update:
2717 * srv_addr: params[0]
2718 * srv_op_state: params[1]
2719 * srv_admin_state: params[2]
2720 * srv_uweight: params[3]
2721 * srv_iweight: params[4]
2722 * srv_last_time_change: params[5]
2723 * srv_check_status: params[6]
2724 * srv_check_result: params[7]
2725 * srv_check_health: params[8]
2726 * srv_check_state: params[9]
2727 * srv_agent_state: params[10]
2728 * bk_f_forced_id: params[11]
2729 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002730 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002731 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002732 */
2733
2734 /* validating srv_op_state */
2735 p = NULL;
2736 errno = 0;
2737 srv_op_state = strtol(params[1], &p, 10);
2738 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2739 (srv_op_state != SRV_ST_STOPPED &&
2740 srv_op_state != SRV_ST_STARTING &&
2741 srv_op_state != SRV_ST_RUNNING &&
2742 srv_op_state != SRV_ST_STOPPING)) {
2743 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2744 }
2745
2746 /* validating srv_admin_state */
2747 p = NULL;
2748 errno = 0;
2749 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002750 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002751
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002752 /* inherited statuses will be recomputed later.
2753 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2754 */
2755 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002756
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002757 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2758 (srv_admin_state != 0 &&
2759 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002760 srv_admin_state != SRV_ADMF_CMAINT &&
2761 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002762 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002763 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002764 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2765 }
2766
2767 /* validating srv_uweight */
2768 p = NULL;
2769 errno = 0;
2770 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002771 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002772 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2773
2774 /* validating srv_iweight */
2775 p = NULL;
2776 errno = 0;
2777 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002778 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002779 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2780
2781 /* validating srv_last_time_change */
2782 p = NULL;
2783 errno = 0;
2784 srv_last_time_change = strtol(params[5], &p, 10);
2785 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2786 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2787
2788 /* validating srv_check_status */
2789 p = NULL;
2790 errno = 0;
2791 srv_check_status = strtol(params[6], &p, 10);
2792 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2793 (srv_check_status >= HCHK_STATUS_SIZE))
2794 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2795
2796 /* validating srv_check_result */
2797 p = NULL;
2798 errno = 0;
2799 srv_check_result = strtol(params[7], &p, 10);
2800 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2801 (srv_check_result != CHK_RES_UNKNOWN &&
2802 srv_check_result != CHK_RES_NEUTRAL &&
2803 srv_check_result != CHK_RES_FAILED &&
2804 srv_check_result != CHK_RES_PASSED &&
2805 srv_check_result != CHK_RES_CONDPASS)) {
2806 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2807 }
2808
2809 /* validating srv_check_health */
2810 p = NULL;
2811 errno = 0;
2812 srv_check_health = strtol(params[8], &p, 10);
2813 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2814 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2815
2816 /* validating srv_check_state */
2817 p = NULL;
2818 errno = 0;
2819 srv_check_state = strtol(params[9], &p, 10);
2820 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2821 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2822 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2823
2824 /* validating srv_agent_state */
2825 p = NULL;
2826 errno = 0;
2827 srv_agent_state = strtol(params[10], &p, 10);
2828 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2829 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2830 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2831
2832 /* validating bk_f_forced_id */
2833 p = NULL;
2834 errno = 0;
2835 bk_f_forced_id = strtol(params[11], &p, 10);
2836 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2837 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2838
2839 /* validating srv_f_forced_id */
2840 p = NULL;
2841 errno = 0;
2842 srv_f_forced_id = strtol(params[12], &p, 10);
2843 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2844 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2845
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002846 /* validating srv_fqdn */
2847 fqdn = params[13];
2848 if (fqdn && *fqdn == '-')
2849 fqdn = NULL;
2850 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2851 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2852 fqdn = NULL;
2853 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002854
Frédéric Lécaille31694712017-08-01 08:47:19 +02002855 port_str = params[14];
2856 if (port_str) {
2857 port = strl2uic(port_str, strlen(port_str));
2858 if (port > USHRT_MAX) {
2859 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2860 port_str = NULL;
2861 }
2862 }
2863
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002864 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002865 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002866 goto out;
2867
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002868 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002869 /* recover operational state and apply it to this server
2870 * and all servers tracking this one */
2871 switch (srv_op_state) {
2872 case SRV_ST_STOPPED:
2873 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002874 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002875 break;
2876 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002877 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002878 break;
2879 case SRV_ST_STOPPING:
2880 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002881 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002882 break;
2883 case SRV_ST_RUNNING:
2884 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002885 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002886 break;
2887 }
2888
2889 /* When applying server state, the following rules apply:
2890 * - in case of a configuration change, we apply the setting from the new
2891 * configuration, regardless of old running state
2892 * - if no configuration change, we apply old running state only if old running
2893 * state is different from new configuration state
2894 */
2895 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002896 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2897 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002898 srv_adm_set_maint(srv);
2899 else
2900 srv_adm_set_ready(srv);
2901 }
2902 /* configuration is the same, let's compate old running state and new conf state */
2903 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002904 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002905 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002906 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002907 srv_adm_set_ready(srv);
2908 }
2909 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002910 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002911 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002912 * (srv->iweight is the weight set up in configuration).
2913 * There are two possible reasons for FDRAIN to have been present :
2914 * - previous config weight was zero
2915 * - "set server b/s drain" was sent to the CLI
2916 *
2917 * In the first case, we simply want to drop this drain state
2918 * if the new weight is not zero anymore, meaning the administrator
2919 * has intentionally turned the weight back to a positive value to
2920 * enable the server again after an operation. In the second case,
2921 * the drain state was forced on the CLI regardless of the config's
2922 * weight so we don't want a change to the config weight to lose this
2923 * status. What this means is :
2924 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2925 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002926 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002927 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002928 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002929 }
2930
2931 srv->last_change = date.tv_sec - srv_last_time_change;
2932 srv->check.status = srv_check_status;
2933 srv->check.result = srv_check_result;
2934 srv->check.health = srv_check_health;
2935
2936 /* Only case we want to apply is removing ENABLED flag which could have been
2937 * done by the "disable health" command over the stats socket
2938 */
2939 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2940 (srv_check_state & CHK_ST_CONFIGURED) &&
2941 !(srv_check_state & CHK_ST_ENABLED))
2942 srv->check.state &= ~CHK_ST_ENABLED;
2943
2944 /* Only case we want to apply is removing ENABLED flag which could have been
2945 * done by the "disable agent" command over the stats socket
2946 */
2947 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2948 (srv_agent_state & CHK_ST_CONFIGURED) &&
2949 !(srv_agent_state & CHK_ST_ENABLED))
2950 srv->agent.state &= ~CHK_ST_ENABLED;
2951
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002952 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2953 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002954 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002955 * It means that a configuration file change has precedence over a unix socket change
2956 * for server's weight
2957 *
2958 * by default, HAProxy applies the following weight when parsing the configuration
2959 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002960 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002961 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002962 srv->uweight = srv_uweight;
2963 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002964 server_recalc_eweight(srv);
2965
Willy Tarreaue5a60682016-11-09 14:54:53 +01002966 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04002967 if (strcmp(params[0], "-"))
2968 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002969
2970 if (fqdn && srv->hostname) {
2971 if (!strcmp(srv->hostname, fqdn)) {
2972 /* Here we reset the 'set from stats socket FQDN' flag
2973 * to support such transitions:
2974 * Let's say initial FQDN value is foo1 (in configuration file).
2975 * - FQDN changed from stats socket, from foo1 to foo2 value,
2976 * - FQDN changed again from file configuration (with the same previous value
2977 set from stats socket, from foo1 to foo2 value),
2978 * - reload for any other reason than a FQDN modification,
2979 * the configuration file FQDN matches the fqdn server state file value.
2980 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
2981 * any futher FQDN modification.
2982 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002983 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002984 }
2985 else {
2986 /* If the FDQN has been changed from stats socket,
2987 * apply fqdn state file value (which is the value set
2988 * from stats socket).
2989 */
2990 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002991 srv_set_fqdn(srv, fqdn, 0);
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 }
2995 }
2996
Frédéric Lécaille31694712017-08-01 08:47:19 +02002997 if (port_str)
2998 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002999 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003000
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003001 break;
3002 default:
3003 chunk_appendf(msg, ", version '%d' not supported", version);
3004 }
3005
3006 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003007 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003008 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003009 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003010 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003011 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003012}
3013
3014/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3015 * For each proxy, it does the following:
3016 * - opens its server state file (either one or local one)
3017 * - read whole file, line by line
3018 * - analyse each line to check if it matches our current backend:
3019 * - backend name matches
3020 * - backend id matches if id is forced and name doesn't match
3021 * - if the server pointed by the line is found, then state is applied
3022 *
3023 * If the running backend uuid or id differs from the state file, then HAProxy reports
3024 * a warning.
3025 */
3026void apply_server_state(void)
3027{
3028 char *cur, *end;
3029 char mybuf[SRV_STATE_LINE_MAXLEN];
3030 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003031 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3032 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003033 int arg, srv_arg, version, diff;
3034 FILE *f;
3035 char *filepath;
3036 char globalfilepath[MAXPATHLEN + 1];
3037 char localfilepath[MAXPATHLEN + 1];
3038 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003039 struct proxy *curproxy, *bk;
3040 struct server *srv;
3041
3042 globalfilepathlen = 0;
3043 /* create the globalfilepath variable */
3044 if (global.server_state_file) {
3045 /* absolute path or no base directory provided */
3046 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3047 len = strlen(global.server_state_file);
3048 if (len > MAXPATHLEN) {
3049 globalfilepathlen = 0;
3050 goto globalfileerror;
3051 }
3052 memcpy(globalfilepath, global.server_state_file, len);
3053 globalfilepath[len] = '\0';
3054 globalfilepathlen = len;
3055 }
3056 else if (global.server_state_base) {
3057 len = strlen(global.server_state_base);
3058 globalfilepathlen += len;
3059
3060 if (globalfilepathlen > MAXPATHLEN) {
3061 globalfilepathlen = 0;
3062 goto globalfileerror;
3063 }
3064 strncpy(globalfilepath, global.server_state_base, len);
3065 globalfilepath[globalfilepathlen] = 0;
3066
3067 /* append a slash if needed */
3068 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3069 if (globalfilepathlen + 1 > MAXPATHLEN) {
3070 globalfilepathlen = 0;
3071 goto globalfileerror;
3072 }
3073 globalfilepath[globalfilepathlen++] = '/';
3074 }
3075
3076 len = strlen(global.server_state_file);
3077 if (globalfilepathlen + len > MAXPATHLEN) {
3078 globalfilepathlen = 0;
3079 goto globalfileerror;
3080 }
3081 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3082 globalfilepathlen += len;
3083 globalfilepath[globalfilepathlen++] = 0;
3084 }
3085 }
3086 globalfileerror:
3087 if (globalfilepathlen == 0)
3088 globalfilepath[0] = '\0';
3089
3090 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003091 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003092 /* servers are only in backends */
3093 if (!(curproxy->cap & PR_CAP_BE))
3094 continue;
3095 fileopenerr = 0;
3096 filepath = NULL;
3097
3098 /* search server state file path and name */
3099 switch (curproxy->load_server_state_from_file) {
3100 /* read servers state from global file */
3101 case PR_SRV_STATE_FILE_GLOBAL:
3102 /* there was an error while generating global server state file path */
3103 if (globalfilepathlen == 0)
3104 continue;
3105 filepath = globalfilepath;
3106 fileopenerr = 1;
3107 break;
3108 /* this backend has its own file */
3109 case PR_SRV_STATE_FILE_LOCAL:
3110 localfilepathlen = 0;
3111 localfilepath[0] = '\0';
3112 len = 0;
3113 /* create the localfilepath variable */
3114 /* absolute path or no base directory provided */
3115 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3116 len = strlen(curproxy->server_state_file_name);
3117 if (len > MAXPATHLEN) {
3118 localfilepathlen = 0;
3119 goto localfileerror;
3120 }
3121 memcpy(localfilepath, curproxy->server_state_file_name, len);
3122 localfilepath[len] = '\0';
3123 localfilepathlen = len;
3124 }
3125 else if (global.server_state_base) {
3126 len = strlen(global.server_state_base);
3127 localfilepathlen += len;
3128
3129 if (localfilepathlen > MAXPATHLEN) {
3130 localfilepathlen = 0;
3131 goto localfileerror;
3132 }
3133 strncpy(localfilepath, global.server_state_base, len);
3134 localfilepath[localfilepathlen] = 0;
3135
3136 /* append a slash if needed */
3137 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3138 if (localfilepathlen + 1 > MAXPATHLEN) {
3139 localfilepathlen = 0;
3140 goto localfileerror;
3141 }
3142 localfilepath[localfilepathlen++] = '/';
3143 }
3144
3145 len = strlen(curproxy->server_state_file_name);
3146 if (localfilepathlen + len > MAXPATHLEN) {
3147 localfilepathlen = 0;
3148 goto localfileerror;
3149 }
3150 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3151 localfilepathlen += len;
3152 localfilepath[localfilepathlen++] = 0;
3153 }
3154 filepath = localfilepath;
3155 localfileerror:
3156 if (localfilepathlen == 0)
3157 localfilepath[0] = '\0';
3158
3159 break;
3160 case PR_SRV_STATE_FILE_NONE:
3161 default:
3162 continue;
3163 }
3164
3165 /* preload global state file */
3166 errno = 0;
3167 f = fopen(filepath, "r");
3168 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003169 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003170 if (!f)
3171 continue;
3172
3173 mybuf[0] = '\0';
3174 mybuflen = 0;
3175 version = 0;
3176
3177 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003178 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003179 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003180 goto fileclose;
3181 }
3182
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003183 cur = mybuf;
3184 version = atoi(cur);
3185 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3186 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003187 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003188
3189 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3190 int bk_f_forced_id = 0;
3191 int check_id = 0;
3192 int check_name = 0;
3193
3194 mybuflen = strlen(mybuf);
3195 cur = mybuf;
3196 end = cur + mybuflen;
3197
3198 bk = NULL;
3199 srv = NULL;
3200
3201 /* we need at least one character */
3202 if (mybuflen == 0)
3203 continue;
3204
3205 /* ignore blank characters at the beginning of the line */
3206 while (isspace(*cur))
3207 ++cur;
3208
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003209 /* Ignore empty or commented lines */
3210 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003211 continue;
3212
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003213 /* truncated lines */
3214 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003215 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003216 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003217 }
3218
3219 /* Removes trailing '\n' */
3220 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003221
3222 /* we're now ready to move the line into *srv_params[] */
3223 params[0] = cur;
3224 arg = 1;
3225 srv_arg = 0;
3226 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3227 if (isspace(*cur)) {
3228 *cur = '\0';
3229 ++cur;
3230 while (isspace(*cur))
3231 ++cur;
3232 switch (version) {
3233 case 1:
3234 /*
3235 * srv_addr: params[4] => srv_params[0]
3236 * srv_op_state: params[5] => srv_params[1]
3237 * srv_admin_state: params[6] => srv_params[2]
3238 * srv_uweight: params[7] => srv_params[3]
3239 * srv_iweight: params[8] => srv_params[4]
3240 * srv_last_time_change: params[9] => srv_params[5]
3241 * srv_check_status: params[10] => srv_params[6]
3242 * srv_check_result: params[11] => srv_params[7]
3243 * srv_check_health: params[12] => srv_params[8]
3244 * srv_check_state: params[13] => srv_params[9]
3245 * srv_agent_state: params[14] => srv_params[10]
3246 * bk_f_forced_id: params[15] => srv_params[11]
3247 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003248 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003249 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003250 */
3251 if (arg >= 4) {
3252 srv_params[srv_arg] = cur;
3253 ++srv_arg;
3254 }
3255 break;
3256 }
3257
3258 params[arg] = cur;
3259 ++arg;
3260 }
3261 else {
3262 ++cur;
3263 }
3264 }
3265
3266 /* if line is incomplete line, then ignore it.
3267 * otherwise, update useful flags */
3268 switch (version) {
3269 case 1:
3270 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3271 continue;
3272 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3273 check_id = (atoi(params[0]) == curproxy->uuid);
3274 check_name = (strcmp(curproxy->id, params[1]) == 0);
3275 break;
3276 }
3277
3278 diff = 0;
3279 bk = curproxy;
3280
3281 /* if backend can't be found, let's continue */
3282 if (!check_id && !check_name)
3283 continue;
3284 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003285 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 +02003286 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3287 }
3288 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003289 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 +02003290 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3291 /* if name doesn't match, we still want to update curproxy if the backend id
3292 * was forced in previous the previous configuration */
3293 if (!bk_f_forced_id)
3294 continue;
3295 }
3296
3297 /* look for the server by its id: param[2] */
3298 /* else look for the server by its name: param[3] */
3299 diff = 0;
3300 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3301
3302 if (!srv) {
3303 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003304 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3305 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003306 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3307 params[3], params[2], params[0], params[1]);
3308 continue;
3309 }
3310 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003311 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 +02003312 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 +02003313 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003314 }
3315 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003316 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 +02003317 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 +02003318 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003319 }
3320
3321 /* now we can proceed with server's state update */
3322 srv_update_state(srv, version, srv_params);
3323 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003324fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003325 fclose(f);
3326 }
3327}
3328
Simon Horman7d09b9a2013-02-12 10:45:51 +09003329/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003330 * update a server's current IP address.
3331 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3332 * ip is in network format.
3333 * updater is a string which contains an information about the requester of the update.
3334 * updater is used if not NULL.
3335 *
3336 * A log line and a stderr warning message is generated based on server's backend options.
3337 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003338int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003339{
3340 /* generates a log line and a warning on stderr */
3341 if (1) {
3342 /* book enough space for both IPv4 and IPv6 */
3343 char oldip[INET6_ADDRSTRLEN];
3344 char newip[INET6_ADDRSTRLEN];
3345
3346 memset(oldip, '\0', INET6_ADDRSTRLEN);
3347 memset(newip, '\0', INET6_ADDRSTRLEN);
3348
3349 /* copy old IP address in a string */
3350 switch (s->addr.ss_family) {
3351 case AF_INET:
3352 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3353 break;
3354 case AF_INET6:
3355 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3356 break;
3357 };
3358
3359 /* copy new IP address in a string */
3360 switch (ip_sin_family) {
3361 case AF_INET:
3362 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3363 break;
3364 case AF_INET6:
3365 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3366 break;
3367 };
3368
3369 /* save log line into a buffer */
3370 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3371 s->proxy->id, s->id, oldip, newip, updater);
3372
3373 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003374 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003375
3376 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003377 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003378 }
3379
3380 /* save the new IP family */
3381 s->addr.ss_family = ip_sin_family;
3382 /* save the new IP address */
3383 switch (ip_sin_family) {
3384 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003385 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003386 break;
3387 case AF_INET6:
3388 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3389 break;
3390 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003391 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003392
3393 return 0;
3394}
3395
3396/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003397 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3398 *
3399 * Caller can pass its name through <updater> to get it integrated in the response message
3400 * returned by the function.
3401 *
3402 * The function first does the following, in that order:
3403 * - validates the new addr and/or port
3404 * - checks if an update is required (new IP or port is different than current ones)
3405 * - checks the update is allowed:
3406 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3407 * - allow all changes if no CHECKS are configured
3408 * - if CHECK is configured:
3409 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3410 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3411 * conditions are met
3412 */
3413const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3414{
3415 struct sockaddr_storage sa;
3416 int ret, port_change_required;
3417 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003418 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003419 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003420 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003421
3422 msg = get_trash_chunk();
3423 chunk_reset(msg);
3424
3425 if (addr) {
3426 memset(&sa, 0, sizeof(struct sockaddr_storage));
3427 if (str2ip2(addr, &sa, 0) == NULL) {
3428 chunk_printf(msg, "Invalid addr '%s'", addr);
3429 goto out;
3430 }
3431
3432 /* changes are allowed on AF_INET* families only */
3433 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3434 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3435 goto out;
3436 }
3437
3438 /* collecting data currently setup */
3439 memset(current_addr, '\0', sizeof(current_addr));
3440 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3441 /* changes are allowed on AF_INET* families only */
3442 if ((ret != AF_INET) && (ret != AF_INET6)) {
3443 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3444 goto out;
3445 }
3446
3447 /* applying ADDR changes if required and allowed
3448 * ipcmp returns 0 when both ADDR are the same
3449 */
3450 if (ipcmp(&s->addr, &sa) == 0) {
3451 chunk_appendf(msg, "no need to change the addr");
3452 goto port;
3453 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003454 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003455 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003456
3457 /* we also need to update check's ADDR only if it uses the server's one */
3458 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003459 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003460 }
3461
3462 /* we also need to update agent ADDR only if it use the server's one */
3463 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003464 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003465 }
3466
3467 /* update report for caller */
3468 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3469 }
3470
3471 port:
3472 if (port) {
3473 char sign = '\0';
3474 char *endptr;
3475
3476 if (addr)
3477 chunk_appendf(msg, ", ");
3478
3479 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003480 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003481
3482 /* check if PORT change is required */
3483 port_change_required = 0;
3484
3485 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003486 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003487 new_port = strtol(port, &endptr, 10);
3488 if ((errno != 0) || (port == endptr)) {
3489 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3490 goto out;
3491 }
3492
3493 /* check if caller triggers a port mapped or offset */
3494 if (sign == '-' || (sign == '+')) {
3495 /* check if server currently uses port map */
3496 if (!(s->flags & SRV_F_MAPPORTS)) {
3497 /* switch from fixed port to port map mandatorily triggers
3498 * a port change */
3499 port_change_required = 1;
3500 /* check is configured
3501 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3502 * prevent PORT change if check doesn't have it's dedicated port while switching
3503 * to port mapping */
3504 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3505 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.");
3506 goto out;
3507 }
3508 }
3509 /* we're already using port maps */
3510 else {
3511 port_change_required = current_port != new_port;
3512 }
3513 }
3514 /* fixed port */
3515 else {
3516 port_change_required = current_port != new_port;
3517 }
3518
3519 /* applying PORT changes if required and update response message */
3520 if (port_change_required) {
3521 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003522 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003523 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003524
3525 /* prepare message */
3526 chunk_appendf(msg, "port changed from '");
3527 if (s->flags & SRV_F_MAPPORTS)
3528 chunk_appendf(msg, "+");
3529 chunk_appendf(msg, "%d' to '", current_port);
3530
3531 if (sign == '-') {
3532 s->flags |= SRV_F_MAPPORTS;
3533 chunk_appendf(msg, "%c", sign);
3534 /* just use for result output */
3535 new_port = -new_port;
3536 }
3537 else if (sign == '+') {
3538 s->flags |= SRV_F_MAPPORTS;
3539 chunk_appendf(msg, "%c", sign);
3540 }
3541 else {
3542 s->flags &= ~SRV_F_MAPPORTS;
3543 }
3544
3545 chunk_appendf(msg, "%d'", new_port);
3546
3547 /* we also need to update health checks port only if it uses server's realport */
3548 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3549 s->check.port = new_port;
3550 }
3551 }
3552 else {
3553 chunk_appendf(msg, "no need to change the port");
3554 }
3555 }
3556
3557out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003558 if (changed)
3559 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003560 if (updater)
3561 chunk_appendf(msg, " by '%s'", updater);
3562 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003563 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003564}
3565
3566
3567/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003568 * update server status based on result of name resolution
3569 * returns:
3570 * 0 if server status is updated
3571 * 1 if server status has not changed
3572 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003573int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003574{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003575 struct dns_resolvers *resolvers = s->resolvers;
3576 struct dns_resolution *resolution = s->dns_requester->resolution;
3577 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003578
3579 switch (resolution->status) {
3580 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003581 /* status when HAProxy has just (re)started.
3582 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003583 break;
3584
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003585 case RSLV_STATUS_VALID:
3586 /*
3587 * resume health checks
3588 * server will be turned back on if health check is safe
3589 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003590 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003591 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003592 return 1;
3593 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3594 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003595 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003596 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003597
Emeric Brun52a91d32017-08-31 14:41:55 +02003598 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003599 return 1;
3600 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3601 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3602 s->proxy->id, s->id);
3603
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003604 ha_warning("%s.\n", trash.area);
3605 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003606 return 0;
3607
3608 case RSLV_STATUS_NX:
3609 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003610 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3611 if (!tick_is_expired(exp, now_ms))
3612 break;
3613
3614 if (s->next_admin & SRV_ADMF_RMAINT)
3615 return 1;
3616 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3617 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003618
3619 case RSLV_STATUS_TIMEOUT:
3620 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003621 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3622 if (!tick_is_expired(exp, now_ms))
3623 break;
3624
3625 if (s->next_admin & SRV_ADMF_RMAINT)
3626 return 1;
3627 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3628 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003629
3630 case RSLV_STATUS_REFUSED:
3631 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003632 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3633 if (!tick_is_expired(exp, now_ms))
3634 break;
3635
3636 if (s->next_admin & SRV_ADMF_RMAINT)
3637 return 1;
3638 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3639 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003640
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003641 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003642 /* stop server if resolution failed for a long enough period */
3643 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3644 if (!tick_is_expired(exp, now_ms))
3645 break;
3646
3647 if (s->next_admin & SRV_ADMF_RMAINT)
3648 return 1;
3649 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3650 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003651 }
3652
3653 return 1;
3654}
3655
3656/*
3657 * Server Name Resolution valid response callback
3658 * It expects:
3659 * - <nameserver>: the name server which answered the valid response
3660 * - <response>: buffer containing a valid DNS response
3661 * - <response_len>: size of <response>
3662 * It performs the following actions:
3663 * - ignore response if current ip found and server family not met
3664 * - update with first new ip found if family is met and current IP is not found
3665 * returns:
3666 * 0 on error
3667 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003668 *
3669 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003670 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003671int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003672{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003673 struct server *s = NULL;
3674 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003675 void *serverip, *firstip;
3676 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003677 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003678 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003679 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003680
Christopher Faulet67957bd2017-09-27 11:00:59 +02003681 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003682 if (!s)
3683 return 1;
3684
Christopher Faulet67957bd2017-09-27 11:00:59 +02003685 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003686
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003687 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003688 firstip = NULL; /* pointer to the first valid response found */
3689 /* it will be used as the new IP if a change is required */
3690 firstip_sin_family = AF_UNSPEC;
3691 serverip = NULL; /* current server IP address */
3692
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003693 /* initializing server IP pointer */
3694 server_sin_family = s->addr.ss_family;
3695 switch (server_sin_family) {
3696 case AF_INET:
3697 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3698 break;
3699
3700 case AF_INET6:
3701 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3702 break;
3703
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003704 case AF_UNSPEC:
3705 break;
3706
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003707 default:
3708 goto invalid;
3709 }
3710
Baptiste Assmann729c9012017-05-22 15:13:10 +02003711 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003712 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003713 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003714
3715 switch (ret) {
3716 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003717 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003718
3719 case DNS_UPD_SRVIP_NOT_FOUND:
3720 goto save_ip;
3721
3722 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003723 goto invalid;
3724
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003725 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003726 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003727 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003728
Baptiste Assmannfad03182015-10-28 02:03:32 +01003729 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003730 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003731 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003732 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003733
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003734 default:
3735 goto invalid;
3736
3737 }
3738
3739 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003740 if (nameserver) {
3741 nameserver->counters.update++;
3742 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003743 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003744 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003745 else
3746 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003747 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003748
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003749 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003750 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003751 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003752
3753 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003754 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003755 nameserver->counters.invalid++;
3756 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003757 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003758 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003759 return 0;
3760}
3761
3762/*
3763 * Server Name Resolution error management callback
3764 * returns:
3765 * 0 on error
3766 * 1 when no error or safe ignore
3767 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003768int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003769{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003770 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003771
Christopher Faulet67957bd2017-09-27 11:00:59 +02003772 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003773 if (!s)
3774 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003775 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003776 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003777 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003778 return 1;
3779}
3780
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003781/*
3782 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003783 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003784 * It returns a pointer to the first server found or NULL if <ip> is not yet
3785 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003786 *
3787 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003788 */
3789struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3790{
3791 struct server *tmpsrv;
3792 struct proxy *be;
3793
3794 if (!srv)
3795 return NULL;
3796
3797 be = srv->proxy;
3798 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003799 /* we found the current server is the same, ignore it */
3800 if (srv == tmpsrv)
3801 continue;
3802
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003803 /* We want to compare the IP in the record with the IP of the servers in the
3804 * same backend, only if:
3805 * * DNS resolution is enabled on the server
3806 * * the hostname used for the resolution by our server is the same than the
3807 * one used for the server found in the backend
3808 * * the server found in the backend is not our current server
3809 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003810 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003811 if ((tmpsrv->hostname_dn == NULL) ||
3812 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3813 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003814 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003815 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003816 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003817 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003818
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003819 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003820 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003821 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003822 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003823 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003824
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003825 /* At this point, we have 2 different servers using the same DNS hostname
3826 * for their respective resolution.
3827 */
3828 if (*ip_family == tmpsrv->addr.ss_family &&
3829 ((tmpsrv->addr.ss_family == AF_INET &&
3830 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3831 (tmpsrv->addr.ss_family == AF_INET6 &&
3832 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003833 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003834 return tmpsrv;
3835 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003836 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003837 }
3838
Emeric Brune9fd6b52017-11-02 17:20:39 +01003839
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003840 return NULL;
3841}
3842
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003843/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3844 * resolver. This is suited for initial address configuration. Returns 0 on
3845 * success otherwise a non-zero error code. In case of error, *err_code, if
3846 * not NULL, is filled up.
3847 */
3848int srv_set_addr_via_libc(struct server *srv, int *err_code)
3849{
3850 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003851 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003852 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003853 return 1;
3854 }
3855 return 0;
3856}
3857
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003858/* Set the server's FDQN (->hostname) from <hostname>.
3859 * Returns -1 if failed, 0 if not.
3860 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003861int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003862{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003863 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003864 char *hostname_dn;
3865 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003866
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003867 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003868 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003869 /* run time DNS resolution was not active for this server
3870 * and we can't enable it at run time for now.
3871 */
3872 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003873 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003874
3875 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003876 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003877 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003878 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3879 hostname_dn, trash.size);
3880 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003881 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003882
Christopher Faulet67957bd2017-09-27 11:00:59 +02003883 resolution = srv->dns_requester->resolution;
3884 if (resolution &&
3885 resolution->hostname_dn &&
3886 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02003887 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003888
Christopher Faulet67957bd2017-09-27 11:00:59 +02003889 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003890
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003891 free(srv->hostname);
3892 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003893 srv->hostname = strdup(hostname);
3894 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003895 srv->hostname_dn_len = hostname_dn_len;
3896 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003897 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003898
Olivier Houchard55dcdf42017-11-06 15:15:04 +01003899 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003900 goto err;
3901
3902 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003903 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003904 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003905 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003906
3907 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003908 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003909 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003910 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003911}
3912
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003913/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3914 * from the state file. This is suited for initial address configuration.
3915 * Returns 0 on success otherwise a non-zero error code. In case of error,
3916 * *err_code, if not NULL, is filled up.
3917 */
3918static int srv_apply_lastaddr(struct server *srv, int *err_code)
3919{
3920 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3921 if (err_code)
3922 *err_code |= ERR_WARN;
3923 return 1;
3924 }
3925 return 0;
3926}
3927
Willy Tarreau25e51522016-11-04 15:10:17 +01003928/* returns 0 if no error, otherwise a combination of ERR_* flags */
3929static int srv_iterate_initaddr(struct server *srv)
3930{
3931 int return_code = 0;
3932 int err_code;
3933 unsigned int methods;
3934
3935 methods = srv->init_addr_methods;
3936 if (!methods) { // default to "last,libc"
3937 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3938 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3939 }
3940
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003941 /* "-dr" : always append "none" so that server addresses resolution
3942 * failures are silently ignored, this is convenient to validate some
3943 * configs out of their environment.
3944 */
3945 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3946 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3947
Willy Tarreau25e51522016-11-04 15:10:17 +01003948 while (methods) {
3949 err_code = 0;
3950 switch (srv_get_next_initaddr(&methods)) {
3951 case SRV_IADDR_LAST:
3952 if (!srv->lastaddr)
3953 continue;
3954 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003955 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003956 return_code |= err_code;
3957 break;
3958
3959 case SRV_IADDR_LIBC:
3960 if (!srv->hostname)
3961 continue;
3962 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003963 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003964 return_code |= err_code;
3965 break;
3966
Willy Tarreau37ebe122016-11-04 15:17:58 +01003967 case SRV_IADDR_NONE:
3968 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003969 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003970 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3971 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003972 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003973 return return_code;
3974
Willy Tarreau4310d362016-11-02 15:05:56 +01003975 case SRV_IADDR_IP:
3976 ipcpy(&srv->init_addr, &srv->addr);
3977 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003978 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3979 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01003980 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003981 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003982
Willy Tarreau25e51522016-11-04 15:10:17 +01003983 default: /* unhandled method */
3984 break;
3985 }
3986 }
3987
3988 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003989 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01003990 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3991 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003992 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003993 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01003994 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3995 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003996
3997 return_code |= ERR_ALERT | ERR_FATAL;
3998 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003999out:
4000 srv_set_dyncookie(srv);
4001 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004002}
4003
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004004/*
4005 * This function parses all backends and all servers within each backend
4006 * and performs servers' addr resolution based on information provided by:
4007 * - configuration file
4008 * - server-state file (states provided by an 'old' haproxy process)
4009 *
4010 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4011 */
4012int srv_init_addr(void)
4013{
4014 struct proxy *curproxy;
4015 int return_code = 0;
4016
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004017 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004018 while (curproxy) {
4019 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004020
4021 /* servers are in backend only */
4022 if (!(curproxy->cap & PR_CAP_BE))
4023 goto srv_init_addr_next;
4024
Willy Tarreau25e51522016-11-04 15:10:17 +01004025 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004026 if (srv->hostname)
4027 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004028
4029 srv_init_addr_next:
4030 curproxy = curproxy->next;
4031 }
4032
4033 return return_code;
4034}
4035
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004036const 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 +02004037{
4038
Willy Tarreau83061a82018-07-13 11:56:34 +02004039 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004040
4041 msg = get_trash_chunk();
4042 chunk_reset(msg);
4043
Olivier Houchard8da5f982017-08-04 18:35:36 +02004044 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004045 chunk_appendf(msg, "no need to change the FDQN");
4046 goto out;
4047 }
4048
4049 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4050 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4051 goto out;
4052 }
4053
4054 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4055 server->proxy->id, server->id, server->hostname, fqdn);
4056
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004057 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004058 chunk_reset(msg);
4059 chunk_appendf(msg, "could not update %s/%s FQDN",
4060 server->proxy->id, server->id);
4061 goto out;
4062 }
4063
4064 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004065 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004066
4067 out:
4068 if (updater)
4069 chunk_appendf(msg, " by '%s'", updater);
4070 chunk_appendf(msg, "\n");
4071
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004072 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004073}
4074
4075
Willy Tarreau21b069d2016-11-23 17:15:08 +01004076/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4077 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004078 * 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 +01004079 * used for CLI commands requiring a server name.
4080 * Important: the <arg> is modified to remove the '/'.
4081 */
4082struct server *cli_find_server(struct appctx *appctx, char *arg)
4083{
4084 struct proxy *px;
4085 struct server *sv;
4086 char *line;
4087
4088 /* split "backend/server" and make <line> point to server */
4089 for (line = arg; *line; line++)
4090 if (*line == '/') {
4091 *line++ = '\0';
4092 break;
4093 }
4094
4095 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004096 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004097 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004098 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004099 return NULL;
4100 }
4101
4102 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004103 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004104 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004105 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004106 return NULL;
4107 }
4108
4109 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004110 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004111 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004112 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004113 return NULL;
4114 }
4115
4116 return sv;
4117}
4118
William Lallemand222baf22016-11-19 02:00:33 +01004119
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004120static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004121{
4122 struct server *sv;
4123 const char *warning;
4124
4125 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4126 return 1;
4127
4128 sv = cli_find_server(appctx, args[2]);
4129 if (!sv)
4130 return 1;
4131
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004132 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004133
William Lallemand222baf22016-11-19 02:00:33 +01004134 if (strcmp(args[3], "weight") == 0) {
4135 warning = server_parse_weight_change_request(sv, args[4]);
4136 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004137 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004138 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004139 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004140 }
4141 }
4142 else if (strcmp(args[3], "state") == 0) {
4143 if (strcmp(args[4], "ready") == 0)
4144 srv_adm_set_ready(sv);
4145 else if (strcmp(args[4], "drain") == 0)
4146 srv_adm_set_drain(sv);
4147 else if (strcmp(args[4], "maint") == 0)
4148 srv_adm_set_maint(sv);
4149 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004150 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004151 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004152 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004153 }
4154 }
4155 else if (strcmp(args[3], "health") == 0) {
4156 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004157 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004158 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004159 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004160 }
4161 else if (strcmp(args[4], "up") == 0) {
4162 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004163 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004164 }
4165 else if (strcmp(args[4], "stopping") == 0) {
4166 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004167 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004168 }
4169 else if (strcmp(args[4], "down") == 0) {
4170 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004171 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004172 }
4173 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004174 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004175 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004176 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004177 }
4178 }
4179 else if (strcmp(args[3], "agent") == 0) {
4180 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004181 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004182 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004183 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004184 }
4185 else if (strcmp(args[4], "up") == 0) {
4186 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004187 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004188 }
4189 else if (strcmp(args[4], "down") == 0) {
4190 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004191 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004192 }
4193 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004194 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004195 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004196 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004197 }
4198 }
Misiek2da082d2017-01-09 09:40:42 +01004199 else if (strcmp(args[3], "agent-addr") == 0) {
4200 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004201 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004202 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4203 appctx->st0 = CLI_ST_PRINT;
4204 } else {
4205 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004206 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004207 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4208 appctx->st0 = CLI_ST_PRINT;
4209 }
4210 }
4211 }
4212 else if (strcmp(args[3], "agent-send") == 0) {
4213 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004214 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004215 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4216 appctx->st0 = CLI_ST_PRINT;
4217 } else {
4218 char *nss = strdup(args[4]);
4219 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004220 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004221 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4222 appctx->st0 = CLI_ST_PRINT;
4223 } else {
4224 free(sv->agent.send_string);
4225 sv->agent.send_string = nss;
4226 sv->agent.send_string_len = strlen(args[4]);
4227 }
4228 }
4229 }
William Lallemand222baf22016-11-19 02:00:33 +01004230 else if (strcmp(args[3], "check-port") == 0) {
4231 int i = 0;
4232 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004233 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004234 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004235 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004236 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004237 }
4238 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004239 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004240 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004241 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004242 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004243 }
4244 /* prevent the update of port to 0 if MAPPORTS are in use */
4245 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004246 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004247 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004248 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004249 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004250 }
4251 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004252 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004253 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004254 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004255 }
4256 else if (strcmp(args[3], "addr") == 0) {
4257 char *addr = NULL;
4258 char *port = NULL;
4259 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004260 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004261 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004262 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004263 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004264 }
4265 else {
4266 addr = args[4];
4267 }
4268 if (strcmp(args[5], "port") == 0) {
4269 port = args[6];
4270 }
4271 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4272 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004273 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004274 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004275 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004276 }
4277 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4278 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004279 else if (strcmp(args[3], "fqdn") == 0) {
4280 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004281 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004282 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4283 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004284 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004285 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004286 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004287 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004288 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004289 appctx->ctx.cli.msg = warning;
4290 appctx->st0 = CLI_ST_PRINT;
4291 }
4292 }
William Lallemand222baf22016-11-19 02:00:33 +01004293 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004294 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004295 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 +01004296 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004297 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004298 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004299 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004300 return 1;
4301}
4302
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004303static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004304{
4305 struct stream_interface *si = appctx->owner;
4306 struct proxy *px;
4307 struct server *sv;
4308 char *line;
4309
4310
4311 /* split "backend/server" and make <line> point to server */
4312 for (line = args[2]; *line; line++)
4313 if (*line == '/') {
4314 *line++ = '\0';
4315 break;
4316 }
4317
4318 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004319 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004320 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004321 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004322 return 1;
4323 }
4324
4325 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004326 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004327 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004328 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004329 return 1;
4330 }
4331
4332 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004333 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4334 sv->iweight);
4335 if (ci_putstr(si_ic(si), trash.area) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004336 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004337 return 0;
4338 }
William Lallemand6b160942016-11-22 12:34:35 +01004339 return 1;
4340}
4341
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004342static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004343{
4344 struct server *sv;
4345 const char *warning;
4346
4347 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4348 return 1;
4349
4350 sv = cli_find_server(appctx, args[2]);
4351 if (!sv)
4352 return 1;
4353
4354 warning = server_parse_weight_change_request(sv, args[3]);
4355 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004356 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004357 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004358 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004359 }
4360 return 1;
4361}
4362
Willy Tarreaub8026272016-11-23 11:26:56 +01004363/* parse a "set maxconn server" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004364static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004365{
4366 struct server *sv;
4367 const char *warning;
4368
4369 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4370 return 1;
4371
4372 sv = cli_find_server(appctx, args[3]);
4373 if (!sv)
4374 return 1;
4375
4376 warning = server_parse_maxconn_change_request(sv, args[4]);
4377 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004378 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004379 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004380 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004381 }
4382 return 1;
4383}
William Lallemand6b160942016-11-22 12:34:35 +01004384
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004385/* parse a "disable agent" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004386static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004387{
4388 struct server *sv;
4389
4390 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4391 return 1;
4392
4393 sv = cli_find_server(appctx, args[2]);
4394 if (!sv)
4395 return 1;
4396
4397 sv->agent.state &= ~CHK_ST_ENABLED;
4398 return 1;
4399}
4400
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004401/* parse a "disable health" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004402static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004403{
4404 struct server *sv;
4405
4406 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4407 return 1;
4408
4409 sv = cli_find_server(appctx, args[2]);
4410 if (!sv)
4411 return 1;
4412
4413 sv->check.state &= ~CHK_ST_ENABLED;
4414 return 1;
4415}
4416
Willy Tarreauffb4d582016-11-24 12:47:00 +01004417/* parse a "disable server" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004418static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004419{
4420 struct server *sv;
4421
4422 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4423 return 1;
4424
4425 sv = cli_find_server(appctx, args[2]);
4426 if (!sv)
4427 return 1;
4428
4429 srv_adm_set_maint(sv);
4430 return 1;
4431}
4432
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004433/* parse a "enable agent" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004434static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004435{
4436 struct server *sv;
4437
4438 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4439 return 1;
4440
4441 sv = cli_find_server(appctx, args[2]);
4442 if (!sv)
4443 return 1;
4444
4445 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004446 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004447 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004448 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004449 return 1;
4450 }
4451
4452 sv->agent.state |= CHK_ST_ENABLED;
4453 return 1;
4454}
4455
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004456/* parse a "enable health" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004457static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004458{
4459 struct server *sv;
4460
4461 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4462 return 1;
4463
4464 sv = cli_find_server(appctx, args[2]);
4465 if (!sv)
4466 return 1;
4467
4468 sv->check.state |= CHK_ST_ENABLED;
4469 return 1;
4470}
4471
Willy Tarreauffb4d582016-11-24 12:47:00 +01004472/* parse a "enable server" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004473static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004474{
4475 struct server *sv;
4476
4477 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4478 return 1;
4479
4480 sv = cli_find_server(appctx, args[2]);
4481 if (!sv)
4482 return 1;
4483
4484 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004485 if (!(sv->flags & SRV_F_COOKIESET)
4486 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4487 sv->cookie)
4488 srv_check_for_dup_dyncookie(sv);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004489 return 1;
4490}
4491
William Lallemand222baf22016-11-19 02:00:33 +01004492/* register cli keywords */
4493static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004494 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004495 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004496 { { "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 +01004497 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004498 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004499 { { "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 +01004500 { { "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 +01004501 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004502 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4503 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4504
William Lallemand222baf22016-11-19 02:00:33 +01004505 {{},}
4506}};
4507
4508__attribute__((constructor))
4509static void __server_init(void)
4510{
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004511 HA_SPIN_INIT(&updated_servers_lock);
William Lallemand222baf22016-11-19 02:00:33 +01004512 cli_register_kw(&cli_kws);
4513}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004514
Emeric Brun64cc49c2017-10-03 14:46:45 +02004515/*
4516 * This function applies server's status changes, it is
4517 * is designed to be called asynchronously.
4518 *
4519 */
4520void srv_update_status(struct server *s)
4521{
4522 struct check *check = &s->check;
4523 int xferred;
4524 struct proxy *px = s->proxy;
4525 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4526 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4527 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004528 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004529
4530
4531 /* If currently main is not set we try to apply pending state changes */
4532 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4533 int next_admin;
4534
4535 /* Backup next admin */
4536 next_admin = s->next_admin;
4537
4538 /* restore current admin state */
4539 s->next_admin = s->cur_admin;
4540
4541 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4542 s->last_change = now.tv_sec;
4543 if (s->proxy->lbprm.set_server_status_down)
4544 s->proxy->lbprm.set_server_status_down(s);
4545
4546 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4547 srv_shutdown_streams(s, SF_ERR_DOWN);
4548
4549 /* we might have streams queued on this server and waiting for
4550 * a connection. Those which are redispatchable will be queued
4551 * to another server or to the proxy itself.
4552 */
4553 xferred = pendconn_redistribute(s);
4554
4555 tmptrash = alloc_trash_chunk();
4556 if (tmptrash) {
4557 chunk_printf(tmptrash,
4558 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4559 s->proxy->id, s->id);
4560
Emeric Brun5a133512017-10-19 14:42:30 +02004561 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004562 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004563
4564 /* we don't send an alert if the server was previously paused */
4565 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004566 send_log(s->proxy, log_level, "%s.\n",
4567 tmptrash->area);
4568 send_email_alert(s, log_level, "%s",
4569 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004570 free_trash_chunk(tmptrash);
4571 tmptrash = NULL;
4572 }
4573 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4574 set_backend_down(s->proxy);
4575
4576 s->counters.down_trans++;
4577 }
4578 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4579 s->last_change = now.tv_sec;
4580 if (s->proxy->lbprm.set_server_status_down)
4581 s->proxy->lbprm.set_server_status_down(s);
4582
4583 /* we might have streams queued on this server and waiting for
4584 * a connection. Those which are redispatchable will be queued
4585 * to another server or to the proxy itself.
4586 */
4587 xferred = pendconn_redistribute(s);
4588
4589 tmptrash = alloc_trash_chunk();
4590 if (tmptrash) {
4591 chunk_printf(tmptrash,
4592 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4593 s->proxy->id, s->id);
4594
Emeric Brun5a133512017-10-19 14:42:30 +02004595 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004596
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004597 ha_warning("%s.\n", tmptrash->area);
4598 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4599 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004600 free_trash_chunk(tmptrash);
4601 tmptrash = NULL;
4602 }
4603
4604 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4605 set_backend_down(s->proxy);
4606 }
4607 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4608 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4609 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4610 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4611 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4612 s->proxy->last_change = now.tv_sec;
4613 }
4614
4615 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4616 s->down_time += now.tv_sec - s->last_change;
4617
4618 s->last_change = now.tv_sec;
4619 if (s->next_state == SRV_ST_STARTING)
4620 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4621
4622 server_recalc_eweight(s);
4623 /* now propagate the status change to any LB algorithms */
4624 if (px->lbprm.update_server_eweight)
4625 px->lbprm.update_server_eweight(s);
4626 else if (srv_willbe_usable(s)) {
4627 if (px->lbprm.set_server_status_up)
4628 px->lbprm.set_server_status_up(s);
4629 }
4630 else {
4631 if (px->lbprm.set_server_status_down)
4632 px->lbprm.set_server_status_down(s);
4633 }
4634
4635 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4636 * and it's not a backup server and its effective weight is > 0,
4637 * then it can accept new connections, so we shut down all streams
4638 * on all backup servers.
4639 */
4640 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4641 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4642 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4643
4644 /* check if we can handle some connections queued at the proxy. We
4645 * will take as many as we can handle.
4646 */
4647 xferred = pendconn_grab_from_px(s);
4648
4649 tmptrash = alloc_trash_chunk();
4650 if (tmptrash) {
4651 chunk_printf(tmptrash,
4652 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4653 s->proxy->id, s->id);
4654
Emeric Brun5a133512017-10-19 14:42:30 +02004655 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004656 ha_warning("%s.\n", tmptrash->area);
4657 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4658 tmptrash->area);
4659 send_email_alert(s, LOG_NOTICE, "%s",
4660 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004661 free_trash_chunk(tmptrash);
4662 tmptrash = NULL;
4663 }
4664
4665 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4666 set_backend_down(s->proxy);
4667 }
4668 else if (s->cur_eweight != s->next_eweight) {
4669 /* now propagate the status change to any LB algorithms */
4670 if (px->lbprm.update_server_eweight)
4671 px->lbprm.update_server_eweight(s);
4672 else if (srv_willbe_usable(s)) {
4673 if (px->lbprm.set_server_status_up)
4674 px->lbprm.set_server_status_up(s);
4675 }
4676 else {
4677 if (px->lbprm.set_server_status_down)
4678 px->lbprm.set_server_status_down(s);
4679 }
4680
4681 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4682 set_backend_down(s->proxy);
4683 }
4684
4685 s->next_admin = next_admin;
4686 }
4687
Emeric Brun5a133512017-10-19 14:42:30 +02004688 /* reset operational state change */
4689 *s->op_st_chg.reason = 0;
4690 s->op_st_chg.status = s->op_st_chg.code = -1;
4691 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004692
4693 /* Now we try to apply pending admin changes */
4694
4695 /* Maintenance must also disable health checks */
4696 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4697 if (s->check.state & CHK_ST_ENABLED) {
4698 s->check.state |= CHK_ST_PAUSED;
4699 check->health = 0;
4700 }
4701
4702 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004703 tmptrash = alloc_trash_chunk();
4704 if (tmptrash) {
4705 chunk_printf(tmptrash,
4706 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4707 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4708 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004709
Olivier Houchard796a2b32017-10-24 17:42:47 +02004710 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004711
Olivier Houchard796a2b32017-10-24 17:42:47 +02004712 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004713 ha_warning("%s.\n", tmptrash->area);
4714 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4715 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004716 }
4717 free_trash_chunk(tmptrash);
4718 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004719 }
Emeric Brun8f298292017-12-06 16:47:17 +01004720 /* commit new admin status */
4721
4722 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004723 }
4724 else { /* server was still running */
4725 check->health = 0; /* failure */
4726 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004727
4728 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004729 if (s->proxy->lbprm.set_server_status_down)
4730 s->proxy->lbprm.set_server_status_down(s);
4731
Emeric Brun64cc49c2017-10-03 14:46:45 +02004732 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4733 srv_shutdown_streams(s, SF_ERR_DOWN);
4734
4735 /* we might have streams queued on this server and waiting for
4736 * a connection. Those which are redispatchable will be queued
4737 * to another server or to the proxy itself.
4738 */
4739 xferred = pendconn_redistribute(s);
4740
4741 tmptrash = alloc_trash_chunk();
4742 if (tmptrash) {
4743 chunk_printf(tmptrash,
4744 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4745 s->flags & SRV_F_BACKUP ? "Backup " : "",
4746 s->proxy->id, s->id,
4747 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4748
4749 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4750
4751 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004752 ha_warning("%s.\n", tmptrash->area);
4753 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4754 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004755 }
4756 free_trash_chunk(tmptrash);
4757 tmptrash = NULL;
4758 }
4759 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4760 set_backend_down(s->proxy);
4761
4762 s->counters.down_trans++;
4763 }
4764 }
4765 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4766 /* OK here we're leaving maintenance, we have many things to check,
4767 * because the server might possibly be coming back up depending on
4768 * its state. In practice, leaving maintenance means that we should
4769 * immediately turn to UP (more or less the slowstart) under the
4770 * following conditions :
4771 * - server is neither checked nor tracked
4772 * - server tracks another server which is not checked
4773 * - server tracks another server which is already up
4774 * Which sums up as something simpler :
4775 * "either the tracking server is up or the server's checks are disabled
4776 * or up". Otherwise we only re-enable health checks. There's a special
4777 * case associated to the stopping state which can be inherited. Note
4778 * that the server might still be in drain mode, which is naturally dealt
4779 * with by the lower level functions.
4780 */
4781
4782 if (s->check.state & CHK_ST_ENABLED) {
4783 s->check.state &= ~CHK_ST_PAUSED;
4784 check->health = check->rise; /* start OK but check immediately */
4785 }
4786
4787 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4788 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4789 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4790 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4791 s->next_state = SRV_ST_STOPPING;
4792 }
4793 else {
4794 s->next_state = SRV_ST_STARTING;
4795 if (s->slowstart > 0)
4796 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4797 else
4798 s->next_state = SRV_ST_RUNNING;
4799 }
4800
4801 }
4802
4803 tmptrash = alloc_trash_chunk();
4804 if (tmptrash) {
4805 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4806 chunk_printf(tmptrash,
4807 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4808 s->flags & SRV_F_BACKUP ? "Backup " : "",
4809 s->proxy->id, s->id,
4810 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4811 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4812 }
4813 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4814 chunk_printf(tmptrash,
4815 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4816 s->flags & SRV_F_BACKUP ? "Backup " : "",
4817 s->proxy->id, s->id, s->hostname,
4818 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4819 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4820 }
4821 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4822 chunk_printf(tmptrash,
4823 "%sServer %s/%s is %s/%s (leaving maintenance)",
4824 s->flags & SRV_F_BACKUP ? "Backup " : "",
4825 s->proxy->id, s->id,
4826 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4827 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4828 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004829 ha_warning("%s.\n", tmptrash->area);
4830 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4831 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004832 free_trash_chunk(tmptrash);
4833 tmptrash = NULL;
4834 }
4835
4836 server_recalc_eweight(s);
4837 /* now propagate the status change to any LB algorithms */
4838 if (px->lbprm.update_server_eweight)
4839 px->lbprm.update_server_eweight(s);
4840 else if (srv_willbe_usable(s)) {
4841 if (px->lbprm.set_server_status_up)
4842 px->lbprm.set_server_status_up(s);
4843 }
4844 else {
4845 if (px->lbprm.set_server_status_down)
4846 px->lbprm.set_server_status_down(s);
4847 }
4848
4849 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4850 set_backend_down(s->proxy);
4851
4852 }
4853 else if (s->next_admin & SRV_ADMF_MAINT) {
4854 /* remaining in maintenance mode, let's inform precisely about the
4855 * situation.
4856 */
4857 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4858 tmptrash = alloc_trash_chunk();
4859 if (tmptrash) {
4860 chunk_printf(tmptrash,
4861 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4862 s->flags & SRV_F_BACKUP ? "Backup " : "",
4863 s->proxy->id, s->id);
4864
4865 if (s->track) /* normally it's mandatory here */
4866 chunk_appendf(tmptrash, " via %s/%s",
4867 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004868 ha_warning("%s.\n", tmptrash->area);
4869 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4870 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004871 free_trash_chunk(tmptrash);
4872 tmptrash = NULL;
4873 }
4874 }
4875 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4876 tmptrash = alloc_trash_chunk();
4877 if (tmptrash) {
4878 chunk_printf(tmptrash,
4879 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4880 s->flags & SRV_F_BACKUP ? "Backup " : "",
4881 s->proxy->id, s->id, s->hostname);
4882
4883 if (s->track) /* normally it's mandatory here */
4884 chunk_appendf(tmptrash, " via %s/%s",
4885 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004886 ha_warning("%s.\n", tmptrash->area);
4887 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4888 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004889 free_trash_chunk(tmptrash);
4890 tmptrash = NULL;
4891 }
4892 }
4893 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4894 tmptrash = alloc_trash_chunk();
4895 if (tmptrash) {
4896 chunk_printf(tmptrash,
4897 "%sServer %s/%s remains in forced maintenance",
4898 s->flags & SRV_F_BACKUP ? "Backup " : "",
4899 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004900 ha_warning("%s.\n", tmptrash->area);
4901 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4902 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004903 free_trash_chunk(tmptrash);
4904 tmptrash = NULL;
4905 }
4906 }
4907 /* don't report anything when leaving drain mode and remaining in maintenance */
4908
4909 s->cur_admin = s->next_admin;
4910 }
4911
4912 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4913 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4914 /* drain state is applied only if not yet in maint */
4915
4916 s->last_change = now.tv_sec;
4917 if (px->lbprm.set_server_status_down)
4918 px->lbprm.set_server_status_down(s);
4919
4920 /* we might have streams queued on this server and waiting for
4921 * a connection. Those which are redispatchable will be queued
4922 * to another server or to the proxy itself.
4923 */
4924 xferred = pendconn_redistribute(s);
4925
4926 tmptrash = alloc_trash_chunk();
4927 if (tmptrash) {
4928 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4929 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4930 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4931
4932 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4933
4934 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004935 ha_warning("%s.\n", tmptrash->area);
4936 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4937 tmptrash->area);
4938 send_email_alert(s, LOG_NOTICE, "%s",
4939 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004940 }
4941 free_trash_chunk(tmptrash);
4942 tmptrash = NULL;
4943 }
4944
4945 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4946 set_backend_down(s->proxy);
4947 }
4948 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4949 /* OK completely leaving drain mode */
4950 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4951 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4952 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4953 s->proxy->last_change = now.tv_sec;
4954 }
4955
4956 if (s->last_change < now.tv_sec) // ignore negative times
4957 s->down_time += now.tv_sec - s->last_change;
4958 s->last_change = now.tv_sec;
4959 server_recalc_eweight(s);
4960
4961 tmptrash = alloc_trash_chunk();
4962 if (tmptrash) {
4963 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4964 chunk_printf(tmptrash,
4965 "%sServer %s/%s is %s (leaving forced drain)",
4966 s->flags & SRV_F_BACKUP ? "Backup " : "",
4967 s->proxy->id, s->id,
4968 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4969 }
4970 else {
4971 chunk_printf(tmptrash,
4972 "%sServer %s/%s is %s (leaving drain)",
4973 s->flags & SRV_F_BACKUP ? "Backup " : "",
4974 s->proxy->id, s->id,
4975 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4976 if (s->track) /* normally it's mandatory here */
4977 chunk_appendf(tmptrash, " via %s/%s",
4978 s->track->proxy->id, s->track->id);
4979 }
4980
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004981 ha_warning("%s.\n", tmptrash->area);
4982 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4983 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004984 free_trash_chunk(tmptrash);
4985 tmptrash = NULL;
4986 }
4987
4988 /* now propagate the status change to any LB algorithms */
4989 if (px->lbprm.update_server_eweight)
4990 px->lbprm.update_server_eweight(s);
4991 else if (srv_willbe_usable(s)) {
4992 if (px->lbprm.set_server_status_up)
4993 px->lbprm.set_server_status_up(s);
4994 }
4995 else {
4996 if (px->lbprm.set_server_status_down)
4997 px->lbprm.set_server_status_down(s);
4998 }
4999 }
5000 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5001 /* remaining in drain mode after removing one of its flags */
5002
5003 tmptrash = alloc_trash_chunk();
5004 if (tmptrash) {
5005 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5006 chunk_printf(tmptrash,
5007 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5008 s->flags & SRV_F_BACKUP ? "Backup " : "",
5009 s->proxy->id, s->id);
5010
5011 if (s->track) /* normally it's mandatory here */
5012 chunk_appendf(tmptrash, " via %s/%s",
5013 s->track->proxy->id, s->track->id);
5014 }
5015 else {
5016 chunk_printf(tmptrash,
5017 "%sServer %s/%s remains in forced drain mode",
5018 s->flags & SRV_F_BACKUP ? "Backup " : "",
5019 s->proxy->id, s->id);
5020 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005021 ha_warning("%s.\n", tmptrash->area);
5022 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5023 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005024 free_trash_chunk(tmptrash);
5025 tmptrash = NULL;
5026 }
5027
5028 /* commit new admin status */
5029
5030 s->cur_admin = s->next_admin;
5031 }
5032 }
5033
5034 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005035 *s->adm_st_chg_cause = 0;
5036}
5037/*
5038 * This function loops on servers registered for asynchronous
5039 * status changes
Christopher Faulet5d42e092017-10-16 12:00:40 +02005040 *
5041 * NOTE: No needs to lock <updated_servers> list because it is called inside the
5042 * sync point.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005043 */
5044void servers_update_status(void) {
5045 struct server *s, *stmp;
5046
5047 list_for_each_entry_safe(s, stmp, &updated_servers, update_status) {
5048 srv_update_status(s);
5049 LIST_DEL(&s->update_status);
5050 LIST_INIT(&s->update_status);
5051 }
5052}
5053
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005054/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005055 * Local variables:
5056 * c-indent-level: 8
5057 * c-basic-offset: 8
5058 * End:
5059 */