blob: cf041764e38350213db800560f2e8e9123c5b86d [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;
145 tmpbuf = trash.str;
146 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;
520 } else
521 goto fail;
522 }
523 return 0;
524 fail:
525 if (err)
526 memprintf(err, "'%s' : proxy v2 option not implemented", p);
527 return ERR_ALERT | ERR_FATAL;
528}
529
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100530/* Parse the "observe" server keyword */
531static int srv_parse_observe(char **args, int *cur_arg,
532 struct proxy *curproxy, struct server *newsrv, char **err)
533{
534 char *arg;
535
536 arg = args[*cur_arg + 1];
537 if (!*arg) {
538 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
539 return ERR_ALERT | ERR_FATAL;
540 }
541
542 if (!strcmp(arg, "none")) {
543 newsrv->observe = HANA_OBS_NONE;
544 }
545 else if (!strcmp(arg, "layer4")) {
546 newsrv->observe = HANA_OBS_LAYER4;
547 }
548 else if (!strcmp(arg, "layer7")) {
549 if (curproxy->mode != PR_MODE_HTTP) {
550 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
551 return ERR_ALERT;
552 }
553 newsrv->observe = HANA_OBS_LAYER7;
554 }
555 else {
556 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
557 "but got '%s'\n", args[*cur_arg], arg);
558 return ERR_ALERT | ERR_FATAL;
559 }
560
561 return 0;
562}
563
Frédéric Lécaille16186232017-03-14 16:42:49 +0100564/* Parse the "redir" server keyword */
565static int srv_parse_redir(char **args, int *cur_arg,
566 struct proxy *curproxy, struct server *newsrv, char **err)
567{
568 char *arg;
569
570 arg = args[*cur_arg + 1];
571 if (!*arg) {
572 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
573 return ERR_ALERT | ERR_FATAL;
574 }
575
576 free(newsrv->rdr_pfx);
577 newsrv->rdr_pfx = strdup(arg);
578 newsrv->rdr_len = strlen(arg);
579
580 return 0;
581}
582
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100583/* Parse the "send-proxy" server keyword */
584static int srv_parse_send_proxy(char **args, int *cur_arg,
585 struct proxy *curproxy, struct server *newsrv, char **err)
586{
587 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
588}
589
590/* Parse the "send-proxy-v2" server keyword */
591static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
592 struct proxy *curproxy, struct server *newsrv, char **err)
593{
594 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
595}
596
Frédéric Lécailledba97072017-03-17 15:33:50 +0100597
598/* Parse the "source" server keyword */
599static int srv_parse_source(char **args, int *cur_arg,
600 struct proxy *curproxy, struct server *newsrv, char **err)
601{
602 char *errmsg;
603 int port_low, port_high;
604 struct sockaddr_storage *sk;
605 struct protocol *proto;
606
607 errmsg = NULL;
608
609 if (!*args[*cur_arg + 1]) {
610 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
611 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
612 goto err;
613 }
614
615 /* 'sk' is statically allocated (no need to be freed). */
616 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
617 if (!sk) {
618 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
619 goto err;
620 }
621
622 proto = protocol_by_family(sk->ss_family);
623 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100624 ha_alert("'%s %s' : connect() not supported for this address family.\n",
625 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100626 goto err;
627 }
628
629 newsrv->conn_src.opts |= CO_SRC_BIND;
630 newsrv->conn_src.source_addr = *sk;
631
632 if (port_low != port_high) {
633 int i;
634
635 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100636 ha_alert("'%s' does not support port offsets (found '%s').\n",
637 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100638 goto err;
639 }
640
641 if (port_low <= 0 || port_low > 65535 ||
642 port_high <= 0 || port_high > 65535 ||
643 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100644 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 +0100645 goto err;
646 }
647 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
648 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
649 newsrv->conn_src.sport_range->ports[i] = port_low + i;
650 }
651
652 *cur_arg += 2;
653 while (*(args[*cur_arg])) {
654 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
655#if defined(CONFIG_HAP_TRANSPARENT)
656 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100657 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
658 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100659 goto err;
660 }
661 if (!strcmp(args[*cur_arg + 1], "client")) {
662 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
663 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
664 }
665 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
666 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
667 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
668 }
669 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
670 char *name, *end;
671
672 name = args[*cur_arg + 1] + 7;
673 while (isspace(*name))
674 name++;
675
676 end = name;
677 while (*end && !isspace(*end) && *end != ',' && *end != ')')
678 end++;
679
680 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
681 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
682 free(newsrv->conn_src.bind_hdr_name);
683 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
684 newsrv->conn_src.bind_hdr_len = end - name;
685 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
686 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
687 newsrv->conn_src.bind_hdr_occ = -1;
688
689 /* now look for an occurrence number */
690 while (isspace(*end))
691 end++;
692 if (*end == ',') {
693 end++;
694 name = end;
695 if (*end == '-')
696 end++;
697 while (isdigit((int)*end))
698 end++;
699 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
700 }
701
702 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100703 ha_alert("usesrc hdr_ip(name,num) does not support negative"
704 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100705 goto err;
706 }
707 }
708 else {
709 struct sockaddr_storage *sk;
710 int port1, port2;
711
712 /* 'sk' is statically allocated (no need to be freed). */
713 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
714 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100715 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100716 goto err;
717 }
718
719 proto = protocol_by_family(sk->ss_family);
720 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100721 ha_alert("'%s %s' : connect() not supported for this address family.\n",
722 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100723 goto err;
724 }
725
726 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100727 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
728 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100729 goto err;
730 }
731 newsrv->conn_src.tproxy_addr = *sk;
732 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
733 }
734 global.last_checks |= LSTCHK_NETADM;
735 *cur_arg += 2;
736 continue;
737#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100738 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 +0100739 goto err;
740#endif /* defined(CONFIG_HAP_TRANSPARENT) */
741 } /* "usesrc" */
742
743 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
744#ifdef SO_BINDTODEVICE
745 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100746 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100747 goto err;
748 }
749 free(newsrv->conn_src.iface_name);
750 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
751 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
752 global.last_checks |= LSTCHK_NETADM;
753#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100754 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100755 goto err;
756#endif
757 *cur_arg += 2;
758 continue;
759 }
760 /* this keyword in not an option of "source" */
761 break;
762 } /* while */
763
764 return 0;
765
766 err:
767 free(errmsg);
768 return ERR_ALERT | ERR_FATAL;
769}
770
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100771/* Parse the "stick" server keyword */
772static int srv_parse_stick(char **args, int *cur_arg,
773 struct proxy *curproxy, struct server *newsrv, char **err)
774{
775 newsrv->flags &= ~SRV_F_NON_STICK;
776 return 0;
777}
778
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100779/* Parse the "track" server keyword */
780static int srv_parse_track(char **args, int *cur_arg,
781 struct proxy *curproxy, struct server *newsrv, char **err)
782{
783 char *arg;
784
785 arg = args[*cur_arg + 1];
786 if (!*arg) {
787 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
788 return ERR_ALERT | ERR_FATAL;
789 }
790
791 free(newsrv->trackit);
792 newsrv->trackit = strdup(arg);
793
794 return 0;
795}
796
Frédéric Lécailledba97072017-03-17 15:33:50 +0100797
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200798/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200799 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200800 * shutdown.
801 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200802void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200803{
Willy Tarreau87b09662015-04-03 00:22:06 +0200804 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200805
Willy Tarreau87b09662015-04-03 00:22:06 +0200806 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
807 if (stream->srv_conn == srv)
808 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200809}
810
811/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200812 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200813 * the reason for the shutdown.
814 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200815void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200816{
817 struct server *srv;
818
819 for (srv = px->srv; srv != NULL; srv = srv->next)
820 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200821 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200822}
823
Willy Tarreaubda92272014-05-20 21:55:30 +0200824/* Appends some information to a message string related to a server going UP or
825 * DOWN. If both <forced> and <reason> are null and the server tracks another
826 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200827 * If <check> is non-null, an entire string describing the check result will be
828 * appended after a comma and a space (eg: to report some information from the
829 * check that changed the state). In the other case, the string will be built
830 * using the check results stored into the struct server if present.
831 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200832 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200833 */
Emeric Brun5a133512017-10-19 14:42:30 +0200834void srv_append_status(struct chunk *msg, struct server *s, struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200835{
Emeric Brun5a133512017-10-19 14:42:30 +0200836 short status = s->op_st_chg.status;
837 short code = s->op_st_chg.code;
838 long duration = s->op_st_chg.duration;
839 char *desc = s->op_st_chg.reason;
840
841 if (check) {
842 status = check->status;
843 code = check->code;
844 duration = check->duration;
845 desc = check->desc;
846 }
847
848 if (status != -1) {
849 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
850
851 if (status >= HCHK_STATUS_L57DATA)
852 chunk_appendf(msg, ", code: %d", code);
853
854 if (desc && *desc) {
855 struct chunk src;
856
857 chunk_appendf(msg, ", info: \"");
858
859 chunk_initlen(&src, desc, 0, strlen(desc));
860 chunk_asciiencode(msg, &src, '"');
861
862 chunk_appendf(msg, "\"");
863 }
864
865 if (duration >= 0)
866 chunk_appendf(msg, ", check duration: %ldms", duration);
867 }
868 else if (desc && *desc) {
869 chunk_appendf(msg, ", %s", desc);
870 }
871 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200872 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200873 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200874
875 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200876 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200877 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
878 " %d sessions active, %d requeued, %d remaining in queue",
879 s->proxy->srv_act, s->proxy->srv_bck,
880 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
881 s->cur_sess, xferred, s->nbpend);
882 else
883 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
884 " %d sessions requeued, %d total in queue",
885 s->proxy->srv_act, s->proxy->srv_bck,
886 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
887 xferred, s->nbpend);
888 }
889}
890
Emeric Brun5a133512017-10-19 14:42:30 +0200891/* Marks server <s> down, regardless of its checks' statuses. The server is
892 * registered in a list to postpone the counting of the remaining servers on
893 * the proxy and transfers queued streams whenever possible to other servers at
894 * a sync point. Maintenance servers are ignored. It stores the <reason> if
895 * non-null as the reason for going down or the available data from the check
896 * struct to recompute this reason later.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200897 */
Emeric Brun5a133512017-10-19 14:42:30 +0200898void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200899{
900 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200901
Emeric Brun64cc49c2017-10-03 14:46:45 +0200902 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200903 return;
904
Emeric Brun52a91d32017-08-31 14:41:55 +0200905 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200906 *s->op_st_chg.reason = 0;
907 s->op_st_chg.status = -1;
908 if (reason) {
909 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
910 }
911 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100912 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200913 s->op_st_chg.code = check->code;
914 s->op_st_chg.status = check->status;
915 s->op_st_chg.duration = check->duration;
916 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200917
Christopher Faulet5d42e092017-10-16 12:00:40 +0200918 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200919 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100920 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200921 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100922 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200923 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200924}
925
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200926/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200927 * in maintenance. The server is registered in a list to postpone the counting
928 * of the remaining servers on the proxy and tries to grab requests from the
929 * proxy at a sync point. Maintenance servers are ignored. It stores the
930 * <reason> if non-null as the reason for going down or the available data
931 * from the check struct to recompute this reason later.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200932 */
Emeric Brun5a133512017-10-19 14:42:30 +0200933void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200934{
935 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200936
Emeric Brun64cc49c2017-10-03 14:46:45 +0200937 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200938 return;
939
Emeric Brun52a91d32017-08-31 14:41:55 +0200940 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200941 return;
942
Emeric Brun52a91d32017-08-31 14:41:55 +0200943 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200944 *s->op_st_chg.reason = 0;
945 s->op_st_chg.status = -1;
946 if (reason) {
947 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
948 }
949 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100950 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200951 s->op_st_chg.code = check->code;
952 s->op_st_chg.status = check->status;
953 s->op_st_chg.duration = check->duration;
954 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200955
Emeric Brun64cc49c2017-10-03 14:46:45 +0200956 if (s->slowstart <= 0)
957 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200958
Christopher Faulet5d42e092017-10-16 12:00:40 +0200959 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200960 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100961 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200962 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100963 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200964 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200965}
966
Willy Tarreau8eb77842014-05-21 13:54:57 +0200967/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +0200968 * isn't in maintenance. The server is registered in a list to postpone the
969 * counting of the remaining servers on the proxy and tries to grab requests
970 * from the proxy. Maintenance servers are ignored. It stores the
971 * <reason> if non-null as the reason for going down or the available data
972 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +0200973 * up. Note that it makes use of the trash to build the log strings, so <reason>
974 * must not be placed there.
975 */
Emeric Brun5a133512017-10-19 14:42:30 +0200976void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200977{
978 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200979
Emeric Brun64cc49c2017-10-03 14:46:45 +0200980 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200981 return;
982
Emeric Brun52a91d32017-08-31 14:41:55 +0200983 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200984 return;
985
Emeric Brun52a91d32017-08-31 14:41:55 +0200986 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +0200987 *s->op_st_chg.reason = 0;
988 s->op_st_chg.status = -1;
989 if (reason) {
990 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
991 }
992 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100993 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200994 s->op_st_chg.code = check->code;
995 s->op_st_chg.status = check->status;
996 s->op_st_chg.duration = check->duration;
997 }
Willy Tarreau8eb77842014-05-21 13:54:57 +0200998
Christopher Faulet5d42e092017-10-16 12:00:40 +0200999 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001000 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001001 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001002 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001003 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001004 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001005}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001006
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001007/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1008 * enforce either maint mode or drain mode. It is not allowed to set more than
1009 * one flag at once. The equivalent "inherited" flag is propagated to all
1010 * tracking servers. Maintenance mode disables health checks (but not agent
1011 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001012 * is done. If <cause> is non-null, it will be displayed at the end of the log
1013 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001014 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001015void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001016{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001017 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001018
1019 if (!mode)
1020 return;
1021
1022 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001023 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001024 return;
1025
Emeric Brun52a91d32017-08-31 14:41:55 +02001026 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001027 if (cause)
1028 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1029
Christopher Faulet5d42e092017-10-16 12:00:40 +02001030 srv_register_update(s);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001031
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001032 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001033 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1034 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001035 return;
1036
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001037 /* compute the inherited flag to propagate */
1038 if (mode & SRV_ADMF_MAINT)
1039 mode = SRV_ADMF_IMAINT;
1040 else if (mode & SRV_ADMF_DRAIN)
1041 mode = SRV_ADMF_IDRAIN;
1042
Emeric Brun9f0b4582017-10-23 14:39:51 +02001043 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001044 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001045 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001046 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001047 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001048}
1049
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001050/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1051 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1052 * than one flag at once. The equivalent "inherited" flag is propagated to all
1053 * tracking servers. Leaving maintenance mode re-enables health checks. When
1054 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001055 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001056void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001057{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001058 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001059
1060 if (!mode)
1061 return;
1062
1063 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001064 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001065 return;
1066
Emeric Brun52a91d32017-08-31 14:41:55 +02001067 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001068
Christopher Faulet5d42e092017-10-16 12:00:40 +02001069 srv_register_update(s);
1070
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001071 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001072 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1073 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001074 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001075
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001076 if (mode & SRV_ADMF_MAINT)
1077 mode = SRV_ADMF_IMAINT;
1078 else if (mode & SRV_ADMF_DRAIN)
1079 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001080
Emeric Brun9f0b4582017-10-23 14:39:51 +02001081 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001082 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001083 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001084 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001085 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001086}
1087
Willy Tarreau757478e2016-11-03 19:22:19 +01001088/* principle: propagate maint and drain to tracking servers. This is useful
1089 * upon startup so that inherited states are correct.
1090 */
1091static void srv_propagate_admin_state(struct server *srv)
1092{
1093 struct server *srv2;
1094
1095 if (!srv->trackers)
1096 return;
1097
1098 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001099 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001100 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001101 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001102
Emeric Brun52a91d32017-08-31 14:41:55 +02001103 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001104 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001105 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001106 }
1107}
1108
1109/* Compute and propagate the admin states for all servers in proxy <px>.
1110 * Only servers *not* tracking another one are considered, because other
1111 * ones will be handled when the server they track is visited.
1112 */
1113void srv_compute_all_admin_states(struct proxy *px)
1114{
1115 struct server *srv;
1116
1117 for (srv = px->srv; srv; srv = srv->next) {
1118 if (srv->track)
1119 continue;
1120 srv_propagate_admin_state(srv);
1121 }
1122}
1123
Willy Tarreaudff55432012-10-10 17:51:05 +02001124/* Note: must not be declared <const> as its list will be overwritten.
1125 * Please take care of keeping this list alphabetically sorted, doing so helps
1126 * all code contributors.
1127 * Optional keywords are also declared with a NULL ->parse() function so that
1128 * the config parser can report an appropriate error when a known keyword was
1129 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001130 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001131 */
1132static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001133 { "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 +01001134 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001135 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001136 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001137 { "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 +01001138 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001139 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1140 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001141 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001142 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001143 { "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 +01001144 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001145 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001146 { "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 +01001147 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1148 { "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 +01001149 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001150 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001151 { "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 +01001152 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001153 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1154 { "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 +02001155 { "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 +01001156 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001157 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001158 { NULL, NULL, 0 },
1159}};
1160
1161__attribute__((constructor))
1162static void __listener_init(void)
1163{
1164 srv_register_keywords(&srv_kws);
1165}
1166
Willy Tarreau004e0452013-11-21 11:22:01 +01001167/* Recomputes the server's eweight based on its state, uweight, the current time,
1168 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1169 * state is automatically disabled if the time is elapsed.
1170 */
1171void server_recalc_eweight(struct server *sv)
1172{
1173 struct proxy *px = sv->proxy;
1174 unsigned w;
1175
1176 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1177 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001178 if (sv->next_state == SRV_ST_STARTING)
1179 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001180 }
1181
1182 /* We must take care of not pushing the server to full throttle during slow starts.
1183 * It must also start immediately, at least at the minimal step when leaving maintenance.
1184 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001185 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001186 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1187 else
1188 w = px->lbprm.wdiv;
1189
Emeric Brun52a91d32017-08-31 14:41:55 +02001190 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001191
Christopher Faulet5d42e092017-10-16 12:00:40 +02001192 srv_register_update(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001193}
1194
Willy Tarreaubaaee002006-06-26 02:48:02 +02001195/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001196 * Parses weight_str and configures sv accordingly.
1197 * Returns NULL on success, error message string otherwise.
1198 */
1199const char *server_parse_weight_change_request(struct server *sv,
1200 const char *weight_str)
1201{
1202 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001203 long int w;
1204 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001205
1206 px = sv->proxy;
1207
1208 /* if the weight is terminated with '%', it is set relative to
1209 * the initial weight, otherwise it is absolute.
1210 */
1211 if (!*weight_str)
1212 return "Require <weight> or <weight%>.\n";
1213
Simon Hormanb796afa2013-02-12 10:45:53 +09001214 w = strtol(weight_str, &end, 10);
1215 if (end == weight_str)
1216 return "Empty weight string empty or preceded by garbage";
1217 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001218 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001219 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001220 /* Avoid integer overflow */
1221 if (w > 25600)
1222 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001223 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001224 if (w > 256)
1225 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001226 }
1227 else if (w < 0 || w > 256)
1228 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001229 else if (end[0] != '\0')
1230 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001231
1232 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1233 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1234
1235 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001236 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001237
1238 return NULL;
1239}
1240
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001241/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001242 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1243 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001244 * Returns:
1245 * - error string on error
1246 * - NULL on success
1247 */
1248const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001249 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001250{
1251 unsigned char ip[INET6_ADDRSTRLEN];
1252
1253 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001254 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001255 return NULL;
1256 }
1257 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001258 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001259 return NULL;
1260 }
1261
1262 return "Could not understand IP address format.\n";
1263}
1264
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001265const char *server_parse_maxconn_change_request(struct server *sv,
1266 const char *maxconn_str)
1267{
1268 long int v;
1269 char *end;
1270
1271 if (!*maxconn_str)
1272 return "Require <maxconn>.\n";
1273
1274 v = strtol(maxconn_str, &end, 10);
1275 if (end == maxconn_str)
1276 return "maxconn string empty or preceded by garbage";
1277 else if (end[0] != '\0')
1278 return "Trailing garbage in maxconn string";
1279
1280 if (sv->maxconn == sv->minconn) { // static maxconn
1281 sv->maxconn = sv->minconn = v;
1282 } else { // dynamic maxconn
1283 sv->maxconn = v;
1284 }
1285
1286 if (may_dequeue_tasks(sv, sv->proxy))
1287 process_srv_queue(sv);
1288
1289 return NULL;
1290}
1291
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001292#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001293static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1294 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001295{
1296 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001297 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001298 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001299 NULL,
1300 };
1301
1302 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001303 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001304
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001305 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1306}
1307
1308static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1309{
1310 struct sample_expr *expr;
1311
1312 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 +01001313 if (!expr) {
1314 memprintf(err, "error detected while parsing sni expression : %s", *err);
1315 return ERR_ALERT | ERR_FATAL;
1316 }
1317
1318 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1319 memprintf(err, "error detected while parsing sni expression : "
1320 " fetch method '%s' extracts information from '%s', "
1321 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001322 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001323 return ERR_ALERT | ERR_FATAL;
1324 }
1325
1326 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1327 release_sample_expr(newsrv->ssl_ctx.sni);
1328 newsrv->ssl_ctx.sni = expr;
1329
1330 return 0;
1331}
1332#endif
1333
1334static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1335{
1336 if (err && *err) {
1337 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001338 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 +01001339 }
1340 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001341 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1342 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001343}
1344
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001345static void srv_conn_src_sport_range_cpy(struct server *srv,
1346 struct server *src)
1347{
1348 int range_sz;
1349
1350 range_sz = src->conn_src.sport_range->size;
1351 if (range_sz > 0) {
1352 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1353 if (srv->conn_src.sport_range != NULL) {
1354 int i;
1355
1356 for (i = 0; i < range_sz; i++) {
1357 srv->conn_src.sport_range->ports[i] =
1358 src->conn_src.sport_range->ports[i];
1359 }
1360 }
1361 }
1362}
1363
1364/*
1365 * Copy <src> server connection source settings to <srv> server everything needed.
1366 */
1367static void srv_conn_src_cpy(struct server *srv, struct server *src)
1368{
1369 srv->conn_src.opts = src->conn_src.opts;
1370 srv->conn_src.source_addr = src->conn_src.source_addr;
1371
1372 /* Source port range copy. */
1373 if (src->conn_src.sport_range != NULL)
1374 srv_conn_src_sport_range_cpy(srv, src);
1375
1376#ifdef CONFIG_HAP_TRANSPARENT
1377 if (src->conn_src.bind_hdr_name != NULL) {
1378 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1379 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1380 }
1381 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1382 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1383#endif
1384 if (src->conn_src.iface_name != NULL)
1385 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1386}
1387
1388/*
1389 * Copy <src> server SSL settings to <srv> server allocating
1390 * everything needed.
1391 */
1392#if defined(USE_OPENSSL)
1393static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1394{
1395 if (src->ssl_ctx.ca_file != NULL)
1396 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1397 if (src->ssl_ctx.crl_file != NULL)
1398 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1399 if (src->ssl_ctx.client_crt != NULL)
1400 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1401
1402 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1403
1404 if (src->ssl_ctx.verify_host != NULL)
1405 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1406 if (src->ssl_ctx.ciphers != NULL)
1407 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1408 if (src->sni_expr != NULL)
1409 srv->sni_expr = strdup(src->sni_expr);
1410}
1411#endif
1412
1413/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001414 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001415 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001416 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001417 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001418static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001419{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001420 char *hostname_dn;
1421 int hostname_len, hostname_dn_len;
1422
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001423 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001424 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001425
Christopher Faulet67957bd2017-09-27 11:00:59 +02001426 hostname_len = strlen(hostname);
1427 hostname_dn = trash.str;
1428 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1429 hostname_dn, trash.size);
1430 if (hostname_dn_len == -1)
1431 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001432
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001433
Christopher Faulet67957bd2017-09-27 11:00:59 +02001434 free(srv->hostname);
1435 free(srv->hostname_dn);
1436 srv->hostname = strdup(hostname);
1437 srv->hostname_dn = strdup(hostname_dn);
1438 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001439 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001440 goto err;
1441
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001442 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001443
1444 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001445 free(srv->hostname); srv->hostname = NULL;
1446 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001447 return -1;
1448}
1449
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001450/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001451 * Copy <src> server settings to <srv> server allocating
1452 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001453 * This function is not supposed to be called at any time, but only
1454 * during server settings parsing or during server allocations from
1455 * a server template, and just after having calloc()'ed a new server.
1456 * So, <src> may only be a default server (when parsing server settings)
1457 * or a server template (during server allocations from a server template).
1458 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1459 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001460 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001461static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001462{
1463 /* Connection source settings copy */
1464 srv_conn_src_cpy(srv, src);
1465
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001466 if (srv_tmpl) {
1467 srv->addr = src->addr;
1468 srv->svc_port = src->svc_port;
1469 }
1470
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001471 srv->pp_opts = src->pp_opts;
1472 if (src->rdr_pfx != NULL) {
1473 srv->rdr_pfx = strdup(src->rdr_pfx);
1474 srv->rdr_len = src->rdr_len;
1475 }
1476 if (src->cookie != NULL) {
1477 srv->cookie = strdup(src->cookie);
1478 srv->cklen = src->cklen;
1479 }
1480 srv->use_ssl = src->use_ssl;
1481 srv->check.addr = srv->agent.addr = src->check.addr;
1482 srv->check.use_ssl = src->check.use_ssl;
1483 srv->check.port = src->check.port;
1484 /* Note: 'flags' field has potentially been already initialized. */
1485 srv->flags |= src->flags;
1486 srv->do_check = src->do_check;
1487 srv->do_agent = src->do_agent;
1488 if (srv->check.port)
1489 srv->flags |= SRV_F_CHECKPORT;
1490 srv->check.inter = src->check.inter;
1491 srv->check.fastinter = src->check.fastinter;
1492 srv->check.downinter = src->check.downinter;
1493 srv->agent.use_ssl = src->agent.use_ssl;
1494 srv->agent.port = src->agent.port;
1495 if (src->agent.send_string != NULL)
1496 srv->agent.send_string = strdup(src->agent.send_string);
1497 srv->agent.send_string_len = src->agent.send_string_len;
1498 srv->agent.inter = src->agent.inter;
1499 srv->agent.fastinter = src->agent.fastinter;
1500 srv->agent.downinter = src->agent.downinter;
1501 srv->maxqueue = src->maxqueue;
1502 srv->minconn = src->minconn;
1503 srv->maxconn = src->maxconn;
1504 srv->slowstart = src->slowstart;
1505 srv->observe = src->observe;
1506 srv->onerror = src->onerror;
1507 srv->onmarkeddown = src->onmarkeddown;
1508 srv->onmarkedup = src->onmarkedup;
1509 if (src->trackit != NULL)
1510 srv->trackit = strdup(src->trackit);
1511 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1512 srv->uweight = srv->iweight = src->iweight;
1513
1514 srv->check.send_proxy = src->check.send_proxy;
1515 /* health: up, but will fall down at first failure */
1516 srv->check.rise = srv->check.health = src->check.rise;
1517 srv->check.fall = src->check.fall;
1518
1519 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001520 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1521 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1522 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001523 srv->check.state |= CHK_ST_PAUSED;
1524 srv->check.health = 0;
1525 }
1526
1527 /* health: up but will fall down at first failure */
1528 srv->agent.rise = srv->agent.health = src->agent.rise;
1529 srv->agent.fall = src->agent.fall;
1530
1531 if (src->resolvers_id != NULL)
1532 srv->resolvers_id = strdup(src->resolvers_id);
1533 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1534 if (srv->dns_opts.family_prio == AF_UNSPEC)
1535 srv->dns_opts.family_prio = AF_INET6;
1536 memcpy(srv->dns_opts.pref_net,
1537 src->dns_opts.pref_net,
1538 sizeof srv->dns_opts.pref_net);
1539 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1540
1541 srv->init_addr_methods = src->init_addr_methods;
1542 srv->init_addr = src->init_addr;
1543#if defined(USE_OPENSSL)
1544 srv_ssl_settings_cpy(srv, src);
1545#endif
1546#ifdef TCP_USER_TIMEOUT
1547 srv->tcp_ut = src->tcp_ut;
1548#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001549 if (srv_tmpl)
1550 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001551}
1552
1553static struct server *new_server(struct proxy *proxy)
1554{
1555 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001556 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001557
1558 srv = calloc(1, sizeof *srv);
1559 if (!srv)
1560 return NULL;
1561
1562 srv->obj_type = OBJ_TYPE_SERVER;
1563 srv->proxy = proxy;
1564 LIST_INIT(&srv->actconns);
1565 LIST_INIT(&srv->pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02001566
1567 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1568 goto free_srv;
1569 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1570 goto free_priv_conns;
1571 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1572 goto free_idle_conns;
1573
1574 for (i = 0; i < global.nbthread; i++) {
1575 LIST_INIT(&srv->priv_conns[i]);
1576 LIST_INIT(&srv->idle_conns[i]);
1577 LIST_INIT(&srv->safe_conns[i]);
1578 }
1579
Emeric Brun64cc49c2017-10-03 14:46:45 +02001580 LIST_INIT(&srv->update_status);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001581
Emeric Brun52a91d32017-08-31 14:41:55 +02001582 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001583 srv->last_change = now.tv_sec;
1584
1585 srv->check.status = HCHK_STATUS_INI;
1586 srv->check.server = srv;
1587 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1588
1589 srv->agent.status = HCHK_STATUS_INI;
1590 srv->agent.server = srv;
1591 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1592
1593 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001594
1595 free_idle_conns:
1596 free(srv->idle_conns);
1597 free_priv_conns:
1598 free(srv->priv_conns);
1599 free_srv:
1600 free(srv);
1601 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001602}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001603
1604/*
1605 * Validate <srv> server health-check settings.
1606 * Returns 0 if everything is OK, -1 if not.
1607 */
1608static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1609{
1610 struct tcpcheck_rule *r = NULL;
1611 struct list *l;
1612
1613 /*
1614 * We need at least a service port, a check port or the first tcp-check rule must
1615 * be a 'connect' one when checking an IPv4/IPv6 server.
1616 */
1617 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1618 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1619 return 0;
1620
1621 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1622 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001623 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1624 "Check has been disabled.\n",
1625 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001626 return -1;
1627 }
1628
1629 /* search the first action (connect / send / expect) in the list */
1630 l = &srv->proxy->tcpcheck_rules;
1631 list_for_each_entry(r, l, list) {
1632 if (r->action != TCPCHK_ACT_COMMENT)
1633 break;
1634 }
1635
1636 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001637 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1638 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1639 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001640 return -1;
1641 }
1642
1643 /* scan the tcp-check ruleset to ensure a port has been configured */
1644 l = &srv->proxy->tcpcheck_rules;
1645 list_for_each_entry(r, l, list) {
1646 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001647 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1648 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1649 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001650 return -1;
1651 }
1652 }
1653
1654 return 0;
1655}
1656
1657/*
1658 * Initialize <srv> health-check structure.
1659 * Returns the error string in case of memory allocation failure, NULL if not.
1660 */
1661static const char *do_health_check_init(struct server *srv, int check_type, int state)
1662{
1663 const char *ret;
1664
1665 if (!srv->do_check)
1666 return NULL;
1667
1668 ret = init_check(&srv->check, check_type);
1669 if (ret)
1670 return ret;
1671
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001672 srv->check.state |= state;
1673 global.maxsock++;
1674
1675 return NULL;
1676}
1677
1678static int server_health_check_init(const char *file, int linenum,
1679 struct server *srv, struct proxy *curproxy)
1680{
1681 const char *ret;
1682
1683 if (!srv->do_check)
1684 return 0;
1685
1686 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001687 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1688 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001689 return ERR_ALERT | ERR_FATAL;
1690 }
1691
1692 if (server_healthcheck_validate(file, linenum, srv) < 0)
1693 return ERR_ALERT | ERR_ABORT;
1694
1695 /* note: check type will be set during the config review phase */
1696 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1697 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001698 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001699 return ERR_ALERT | ERR_ABORT;
1700 }
1701
1702 return 0;
1703}
1704
1705/*
1706 * Initialize <srv> agent check structure.
1707 * Returns the error string in case of memory allocation failure, NULL if not.
1708 */
1709static const char *do_server_agent_check_init(struct server *srv, int state)
1710{
1711 const char *ret;
1712
1713 if (!srv->do_agent)
1714 return NULL;
1715
1716 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1717 if (ret)
1718 return ret;
1719
1720 if (!srv->agent.inter)
1721 srv->agent.inter = srv->check.inter;
1722
1723 srv->agent.state |= state;
1724 global.maxsock++;
1725
1726 return NULL;
1727}
1728
1729static int server_agent_check_init(const char *file, int linenum,
1730 struct server *srv, struct proxy *curproxy)
1731{
1732 const char *ret;
1733
1734 if (!srv->do_agent)
1735 return 0;
1736
1737 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001738 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 +02001739 file, linenum, srv->id);
1740 return ERR_ALERT | ERR_FATAL;
1741 }
1742
1743 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1744 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001745 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001746 return ERR_ALERT | ERR_ABORT;
1747 }
1748
1749 return 0;
1750}
1751
1752#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1753static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1754 struct server *srv, struct proxy *proxy)
1755{
1756 int ret;
1757 char *err = NULL;
1758
1759 if (!srv->sni_expr)
1760 return 0;
1761
1762 ret = server_parse_sni_expr(srv, proxy, &err);
1763 if (!ret)
1764 return 0;
1765
1766 display_parser_err(file, linenum, args, cur_arg, &err);
1767 free(err);
1768
1769 return ret;
1770}
1771#endif
1772
1773/*
1774 * Server initializations finalization.
1775 * Initialize health check, agent check and SNI expression if enabled.
1776 * Must not be called for a default server instance.
1777 */
1778static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1779 struct server *srv, struct proxy *px)
1780{
1781 int ret;
1782
1783 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1784 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1785 return ret;
1786 }
1787
1788#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1789 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1790 return ret;
1791#endif
1792
1793 if (srv->flags & SRV_F_BACKUP)
1794 px->srv_bck++;
1795 else
1796 px->srv_act++;
1797 srv_lb_commit_status(srv);
1798
1799 return 0;
1800}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001801
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001802/*
1803 * Parse as much as possible such a range string argument: low[-high]
1804 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1805 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1806 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1807 * Returns 0 if succeeded, -1 if not.
1808 */
1809static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1810{
1811 char *nb_high_arg;
1812
1813 *nb_high = 0;
1814 chunk_printf(&trash, "%s", arg);
1815 *nb_low = atoi(trash.str);
1816
1817 if ((nb_high_arg = strchr(trash.str, '-'))) {
1818 *nb_high_arg++ = '\0';
1819 *nb_high = atoi(nb_high_arg);
1820 }
1821 else {
1822 *nb_high += *nb_low;
1823 *nb_low = 1;
1824 }
1825
1826 if (*nb_low < 0 || *nb_high < *nb_low)
1827 return -1;
1828
1829 return 0;
1830}
1831
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001832static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1833{
1834 chunk_printf(&trash, "%s%d", prefix, nb);
1835 free(srv->id);
1836 srv->id = strdup(trash.str);
1837}
1838
1839/*
1840 * Initialize as much as possible servers from <srv> server template.
1841 * Note that a server template is a special server with
1842 * a few different parameters than a server which has
1843 * been parsed mostly the same way as a server.
1844 * Returns the number of servers succesfully allocated,
1845 * 'srv' template included.
1846 */
1847static int server_template_init(struct server *srv, struct proxy *px)
1848{
1849 int i;
1850 struct server *newsrv;
1851
1852 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1853 int check_init_state;
1854 int agent_init_state;
1855
1856 newsrv = new_server(px);
1857 if (!newsrv)
1858 goto err;
1859
1860 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001861 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001862#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1863 if (newsrv->sni_expr) {
1864 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1865 if (!newsrv->ssl_ctx.sni)
1866 goto err;
1867 }
1868#endif
1869 /* Set this new server ID. */
1870 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1871
1872 /* Initial checks states. */
1873 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1874 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1875
1876 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1877 do_server_agent_check_init(newsrv, agent_init_state))
1878 goto err;
1879
1880 /* Linked backwards first. This will be restablished after parsing. */
1881 newsrv->next = px->srv;
1882 px->srv = newsrv;
1883 }
1884 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1885
1886 return i - srv->tmpl_info.nb_low;
1887
1888 err:
1889 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1890 if (newsrv) {
1891#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1892 release_sample_expr(newsrv->ssl_ctx.sni);
1893#endif
1894 free_check(&newsrv->agent);
1895 free_check(&newsrv->check);
1896 }
1897 free(newsrv);
1898 return i - srv->tmpl_info.nb_low;
1899}
1900
Willy Tarreau272adea2014-03-31 10:39:59 +02001901int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1902{
1903 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001904 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001905 char *errmsg = NULL;
1906 int err_code = 0;
1907 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001908 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001909
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001910 if (!strcmp(args[0], "server") ||
1911 !strcmp(args[0], "default-server") ||
1912 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001913 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001914 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001915 int srv = !defsrv && !strcmp(args[0], "server");
1916 int srv_tmpl = !defsrv && !srv;
1917 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001918
1919 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001920 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001921 err_code |= ERR_ALERT | ERR_FATAL;
1922 goto out;
1923 }
1924 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1925 err_code |= ERR_ALERT | ERR_FATAL;
1926
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001927 /* There is no mandatory first arguments for default server. */
1928 if (srv) {
1929 if (!*args[2]) {
1930 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001931 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001932 file, linenum, args[0]);
1933 err_code |= ERR_ALERT | ERR_FATAL;
1934 goto out;
1935 }
1936
1937 err = invalid_char(args[1]);
1938 }
1939 else if (srv_tmpl) {
1940 if (!*args[3]) {
1941 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001942 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 +02001943 file, linenum, args[0]);
1944 err_code |= ERR_ALERT | ERR_FATAL;
1945 goto out;
1946 }
1947
1948 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001949 }
1950
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001951 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001952 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 +02001953 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001954 err_code |= ERR_ALERT | ERR_FATAL;
1955 goto out;
1956 }
1957
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001958 cur_arg = 2;
1959 if (srv_tmpl) {
1960 /* Parse server-template <nb | range> arg. */
1961 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001962 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001963 file, linenum, args[0], args[cur_arg]);
1964 err_code |= ERR_ALERT | ERR_FATAL;
1965 goto out;
1966 }
1967 cur_arg++;
1968 }
1969
Willy Tarreau272adea2014-03-31 10:39:59 +02001970 if (!defsrv) {
1971 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001972 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001973 struct protocol *proto;
1974
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001975 newsrv = new_server(curproxy);
1976 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001977 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02001978 err_code |= ERR_ALERT | ERR_ABORT;
1979 goto out;
1980 }
1981
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001982 if (srv_tmpl) {
1983 newsrv->tmpl_info.nb_low = tmpl_range_low;
1984 newsrv->tmpl_info.nb_high = tmpl_range_high;
1985 }
1986
Willy Tarreau272adea2014-03-31 10:39:59 +02001987 /* the servers are linked backwards first */
1988 newsrv->next = curproxy->srv;
1989 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02001990 newsrv->conf.file = strdup(file);
1991 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001992 /* Note: for a server template, its id is its prefix.
1993 * This is a temporary id which will be used for server allocations to come
1994 * after parsing.
1995 */
1996 if (srv)
1997 newsrv->id = strdup(args[1]);
1998 else
1999 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002000
2001 /* several ways to check the port component :
2002 * - IP => port=+0, relative (IPv4 only)
2003 * - IP: => port=+0, relative
2004 * - IP:N => port=N, absolute
2005 * - IP:+N => port=+N, relative
2006 * - IP:-N => port=-N, relative
2007 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002008 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002009 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002010 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002011 err_code |= ERR_ALERT | ERR_FATAL;
2012 goto out;
2013 }
2014
2015 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002016 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002017 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002018 file, linenum, args[0], args[1]);
2019 err_code |= ERR_ALERT | ERR_FATAL;
2020 goto out;
2021 }
2022
2023 if (!port1 || !port2) {
2024 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002025 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002026 }
2027 else if (port1 != port2) {
2028 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002029 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002030 file, linenum, args[0], args[1], args[2]);
2031 err_code |= ERR_ALERT | ERR_FATAL;
2032 goto out;
2033 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002034
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002035 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002036 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002037 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002038 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002039 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2040 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2041 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002042 err_code |= ERR_ALERT | ERR_FATAL;
2043 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002044 }
2045 }
2046 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002047 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002048 file, linenum, newsrv->id);
2049 err_code |= ERR_ALERT | ERR_FATAL;
2050 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002051 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002052 }
2053
Willy Tarreau272adea2014-03-31 10:39:59 +02002054 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002055 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002056
Olivier Houchard8da5f982017-08-04 18:35:36 +02002057 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002058 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002059 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002060 err_code |= ERR_ALERT | ERR_FATAL;
2061 goto out;
2062 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002063
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002064 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002065 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002066 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002067 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002068 } else {
2069 newsrv = &curproxy->defsrv;
2070 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002071 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002072 }
2073
2074 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002075 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002076 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2077 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002078 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002079 file, linenum, *err, newsrv->id);
2080 err_code |= ERR_ALERT | ERR_FATAL;
2081 goto out;
2082 }
2083 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002084 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002085 file, linenum, val, args[cur_arg], newsrv->id);
2086 err_code |= ERR_ALERT | ERR_FATAL;
2087 goto out;
2088 }
2089 newsrv->agent.inter = val;
2090 cur_arg += 2;
2091 }
Misiekea849332017-01-09 09:39:51 +01002092 else if (!strcmp(args[cur_arg], "agent-addr")) {
2093 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002094 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002095 goto out;
2096 }
2097
2098 cur_arg += 2;
2099 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002100 else if (!strcmp(args[cur_arg], "agent-port")) {
2101 global.maxsock++;
2102 newsrv->agent.port = atol(args[cur_arg + 1]);
2103 cur_arg += 2;
2104 }
James Brown55f9ff12015-10-21 18:19:05 -07002105 else if (!strcmp(args[cur_arg], "agent-send")) {
2106 global.maxsock++;
2107 free(newsrv->agent.send_string);
2108 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2109 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2110 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2111 cur_arg += 2;
2112 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002113 else if (!strcmp(args[cur_arg], "init-addr")) {
2114 char *p, *end;
2115 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002116 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002117
2118 newsrv->init_addr_methods = 0;
2119 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2120
2121 for (p = args[cur_arg + 1]; *p; p = end) {
2122 /* cut on next comma */
2123 for (end = p; *end && *end != ','; end++);
2124 if (*end)
2125 *(end++) = 0;
2126
Willy Tarreau4310d362016-11-02 15:05:56 +01002127 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002128 if (!strcmp(p, "libc")) {
2129 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2130 }
2131 else if (!strcmp(p, "last")) {
2132 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2133 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002134 else if (!strcmp(p, "none")) {
2135 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2136 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002137 else if (str2ip2(p, &sa, 0)) {
2138 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002139 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002140 file, linenum, args[cur_arg], p);
2141 err_code |= ERR_ALERT | ERR_FATAL;
2142 goto out;
2143 }
2144 newsrv->init_addr = sa;
2145 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2146 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002147 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002148 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 +02002149 file, linenum, args[cur_arg], p);
2150 err_code |= ERR_ALERT | ERR_FATAL;
2151 goto out;
2152 }
2153 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002154 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002155 file, linenum, args[cur_arg], p);
2156 err_code |= ERR_ALERT | ERR_FATAL;
2157 goto out;
2158 }
2159 }
2160 cur_arg += 2;
2161 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002162 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002163 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002164 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2165 cur_arg += 2;
2166 }
2167 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2168 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002169 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002170 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002171 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002172 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002173 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002174 file, linenum, args[cur_arg]);
2175 err_code |= ERR_ALERT | ERR_FATAL;
2176 goto out;
2177 }
2178 cur_arg += 2;
2179 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002180 else if (!strcmp(args[cur_arg], "resolve-net")) {
2181 char *p, *e;
2182 unsigned char mask;
2183 struct dns_options *opt;
2184
2185 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002186 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002187 file, linenum, args[cur_arg]);
2188 err_code |= ERR_ALERT | ERR_FATAL;
2189 goto out;
2190 }
2191
2192 opt = &newsrv->dns_opts;
2193
2194 /* Split arguments by comma, and convert it from ipv4 or ipv6
2195 * string network in in_addr or in6_addr.
2196 */
2197 p = args[cur_arg + 1];
2198 e = p;
2199 while (*p != '\0') {
2200 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002201 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002202 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002203 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2204 err_code |= ERR_ALERT | ERR_FATAL;
2205 goto out;
2206 }
2207 /* look for end or comma. */
2208 while (*e != ',' && *e != '\0')
2209 e++;
2210 if (*e == ',') {
2211 *e = '\0';
2212 e++;
2213 }
2214 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2215 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2216 /* Try to convert input string from ipv4 or ipv6 network. */
2217 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2218 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2219 &mask)) {
2220 /* Try to convert input string from ipv6 network. */
2221 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2222 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2223 } else {
2224 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002225 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002226 file, linenum, args[cur_arg], p);
2227 err_code |= ERR_ALERT | ERR_FATAL;
2228 goto out;
2229 }
2230 opt->pref_net_nb++;
2231 p = e;
2232 }
2233
2234 cur_arg += 2;
2235 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002236 else if (!strcmp(args[cur_arg], "rise")) {
2237 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002238 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002239 file, linenum, args[cur_arg]);
2240 err_code |= ERR_ALERT | ERR_FATAL;
2241 goto out;
2242 }
2243
2244 newsrv->check.rise = atol(args[cur_arg + 1]);
2245 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002246 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002247 file, linenum, args[cur_arg]);
2248 err_code |= ERR_ALERT | ERR_FATAL;
2249 goto out;
2250 }
2251
2252 if (newsrv->check.health)
2253 newsrv->check.health = newsrv->check.rise;
2254 cur_arg += 2;
2255 }
2256 else if (!strcmp(args[cur_arg], "fall")) {
2257 newsrv->check.fall = atol(args[cur_arg + 1]);
2258
2259 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002260 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002261 file, linenum, args[cur_arg]);
2262 err_code |= ERR_ALERT | ERR_FATAL;
2263 goto out;
2264 }
2265
2266 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002267 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002268 file, linenum, args[cur_arg]);
2269 err_code |= ERR_ALERT | ERR_FATAL;
2270 goto out;
2271 }
2272
2273 cur_arg += 2;
2274 }
2275 else if (!strcmp(args[cur_arg], "inter")) {
2276 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2277 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002278 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002279 file, linenum, *err, newsrv->id);
2280 err_code |= ERR_ALERT | ERR_FATAL;
2281 goto out;
2282 }
2283 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002284 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002285 file, linenum, val, args[cur_arg], newsrv->id);
2286 err_code |= ERR_ALERT | ERR_FATAL;
2287 goto out;
2288 }
2289 newsrv->check.inter = val;
2290 cur_arg += 2;
2291 }
2292 else if (!strcmp(args[cur_arg], "fastinter")) {
2293 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2294 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002295 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002296 file, linenum, *err, newsrv->id);
2297 err_code |= ERR_ALERT | ERR_FATAL;
2298 goto out;
2299 }
2300 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002301 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002302 file, linenum, val, args[cur_arg], newsrv->id);
2303 err_code |= ERR_ALERT | ERR_FATAL;
2304 goto out;
2305 }
2306 newsrv->check.fastinter = val;
2307 cur_arg += 2;
2308 }
2309 else if (!strcmp(args[cur_arg], "downinter")) {
2310 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2311 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002312 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002313 file, linenum, *err, newsrv->id);
2314 err_code |= ERR_ALERT | ERR_FATAL;
2315 goto out;
2316 }
2317 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002318 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002319 file, linenum, val, args[cur_arg], newsrv->id);
2320 err_code |= ERR_ALERT | ERR_FATAL;
2321 goto out;
2322 }
2323 newsrv->check.downinter = val;
2324 cur_arg += 2;
2325 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002326 else if (!strcmp(args[cur_arg], "port")) {
2327 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002328 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002329 cur_arg += 2;
2330 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002331 else if (!strcmp(args[cur_arg], "weight")) {
2332 int w;
2333 w = atol(args[cur_arg + 1]);
2334 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002335 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002336 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2337 err_code |= ERR_ALERT | ERR_FATAL;
2338 goto out;
2339 }
2340 newsrv->uweight = newsrv->iweight = w;
2341 cur_arg += 2;
2342 }
2343 else if (!strcmp(args[cur_arg], "minconn")) {
2344 newsrv->minconn = atol(args[cur_arg + 1]);
2345 cur_arg += 2;
2346 }
2347 else if (!strcmp(args[cur_arg], "maxconn")) {
2348 newsrv->maxconn = atol(args[cur_arg + 1]);
2349 cur_arg += 2;
2350 }
2351 else if (!strcmp(args[cur_arg], "maxqueue")) {
2352 newsrv->maxqueue = atol(args[cur_arg + 1]);
2353 cur_arg += 2;
2354 }
2355 else if (!strcmp(args[cur_arg], "slowstart")) {
2356 /* slowstart is stored in seconds */
2357 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2358 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002359 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002360 file, linenum, *err, newsrv->id);
2361 err_code |= ERR_ALERT | ERR_FATAL;
2362 goto out;
2363 }
2364 newsrv->slowstart = (val + 999) / 1000;
2365 cur_arg += 2;
2366 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002367 else if (!strcmp(args[cur_arg], "on-error")) {
2368 if (!strcmp(args[cur_arg + 1], "fastinter"))
2369 newsrv->onerror = HANA_ONERR_FASTINTER;
2370 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2371 newsrv->onerror = HANA_ONERR_FAILCHK;
2372 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2373 newsrv->onerror = HANA_ONERR_SUDDTH;
2374 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2375 newsrv->onerror = HANA_ONERR_MARKDWN;
2376 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002377 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002378 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2379 file, linenum, args[cur_arg], args[cur_arg + 1]);
2380 err_code |= ERR_ALERT | ERR_FATAL;
2381 goto out;
2382 }
2383
2384 cur_arg += 2;
2385 }
2386 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2387 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2388 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2389 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002390 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002391 file, linenum, args[cur_arg], args[cur_arg + 1]);
2392 err_code |= ERR_ALERT | ERR_FATAL;
2393 goto out;
2394 }
2395
2396 cur_arg += 2;
2397 }
2398 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2399 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2400 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2401 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002402 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002403 file, linenum, args[cur_arg], args[cur_arg + 1]);
2404 err_code |= ERR_ALERT | ERR_FATAL;
2405 goto out;
2406 }
2407
2408 cur_arg += 2;
2409 }
2410 else if (!strcmp(args[cur_arg], "error-limit")) {
2411 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002412 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002413 file, linenum, args[cur_arg]);
2414 err_code |= ERR_ALERT | ERR_FATAL;
2415 goto out;
2416 }
2417
2418 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2419
2420 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002421 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002422 file, linenum, args[cur_arg]);
2423 err_code |= ERR_ALERT | ERR_FATAL;
2424 goto out;
2425 }
2426 cur_arg += 2;
2427 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002428 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002429 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002430 file, linenum, "usesrc", "source");
2431 err_code |= ERR_ALERT | ERR_FATAL;
2432 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002433 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002434 else {
2435 static int srv_dumped;
2436 struct srv_kw *kw;
2437 char *err;
2438
2439 kw = srv_find_kw(args[cur_arg]);
2440 if (kw) {
2441 char *err = NULL;
2442 int code;
2443
2444 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002445 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 +02002446 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002447 if (kw->skip != -1)
2448 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002449 err_code |= ERR_ALERT | ERR_FATAL;
2450 goto out;
2451 }
2452
2453 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002454 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002455 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002456 if (kw->skip != -1)
2457 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002458 err_code |= ERR_ALERT;
2459 continue;
2460 }
2461
2462 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2463 err_code |= code;
2464
2465 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002466 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002467 if (code & ERR_FATAL) {
2468 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002469 if (kw->skip != -1)
2470 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002471 goto out;
2472 }
2473 }
2474 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002475 if (kw->skip != -1)
2476 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002477 continue;
2478 }
2479
2480 err = NULL;
2481 if (!srv_dumped) {
2482 srv_dump_kws(&err);
2483 indent_msg(&err, 4);
2484 srv_dumped = 1;
2485 }
2486
Christopher Faulet767a84b2017-11-24 16:50:31 +01002487 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002488 file, linenum, args[0], args[1], args[cur_arg],
2489 err ? " Registered keywords :" : "", err ? err : "");
2490 free(err);
2491
2492 err_code |= ERR_ALERT | ERR_FATAL;
2493 goto out;
2494 }
2495 }
2496
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002497 if (!defsrv)
2498 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2499 if (err_code & ERR_FATAL)
2500 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002501 if (srv_tmpl)
2502 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002503 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002504 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002505 return 0;
2506
2507 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002508 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002509 free(errmsg);
2510 return err_code;
2511}
2512
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002513/* Returns a pointer to the first server matching either id <id>.
2514 * NULL is returned if no match is found.
2515 * the lookup is performed in the backend <bk>
2516 */
2517struct server *server_find_by_id(struct proxy *bk, int id)
2518{
2519 struct eb32_node *eb32;
2520 struct server *curserver;
2521
2522 if (!bk || (id ==0))
2523 return NULL;
2524
2525 /* <bk> has no backend capabilities, so it can't have a server */
2526 if (!(bk->cap & PR_CAP_BE))
2527 return NULL;
2528
2529 curserver = NULL;
2530
2531 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2532 if (eb32)
2533 curserver = container_of(eb32, struct server, conf.id);
2534
2535 return curserver;
2536}
2537
2538/* Returns a pointer to the first server matching either name <name>, or id
2539 * if <name> starts with a '#'. NULL is returned if no match is found.
2540 * the lookup is performed in the backend <bk>
2541 */
2542struct server *server_find_by_name(struct proxy *bk, const char *name)
2543{
2544 struct server *curserver;
2545
2546 if (!bk || !name)
2547 return NULL;
2548
2549 /* <bk> has no backend capabilities, so it can't have a server */
2550 if (!(bk->cap & PR_CAP_BE))
2551 return NULL;
2552
2553 curserver = NULL;
2554 if (*name == '#') {
2555 curserver = server_find_by_id(bk, atoi(name + 1));
2556 if (curserver)
2557 return curserver;
2558 }
2559 else {
2560 curserver = bk->srv;
2561
2562 while (curserver && (strcmp(curserver->id, name) != 0))
2563 curserver = curserver->next;
2564
2565 if (curserver)
2566 return curserver;
2567 }
2568
2569 return NULL;
2570}
2571
2572struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2573{
2574 struct server *byname;
2575 struct server *byid;
2576
2577 if (!name && !id)
2578 return NULL;
2579
2580 if (diff)
2581 *diff = 0;
2582
2583 byname = byid = NULL;
2584
2585 if (name) {
2586 byname = server_find_by_name(bk, name);
2587 if (byname && (!id || byname->puid == id))
2588 return byname;
2589 }
2590
2591 /* remaining possibilities :
2592 * - name not set
2593 * - name set but not found
2594 * - name found but ID doesn't match
2595 */
2596 if (id) {
2597 byid = server_find_by_id(bk, id);
2598 if (byid) {
2599 if (byname) {
2600 /* use id only if forced by configuration */
2601 if (byid->flags & SRV_F_FORCED_ID) {
2602 if (diff)
2603 *diff |= 2;
2604 return byid;
2605 }
2606 else {
2607 if (diff)
2608 *diff |= 1;
2609 return byname;
2610 }
2611 }
2612
2613 /* remaining possibilities:
2614 * - name not set
2615 * - name set but not found
2616 */
2617 if (name && diff)
2618 *diff |= 2;
2619 return byid;
2620 }
2621
2622 /* id bot found */
2623 if (byname) {
2624 if (diff)
2625 *diff |= 1;
2626 return byname;
2627 }
2628 }
2629
2630 return NULL;
2631}
2632
Christopher Faulet5d42e092017-10-16 12:00:40 +02002633/* Registers changes to be applied asynchronously */
2634static void srv_register_update(struct server *srv)
2635{
2636 if (LIST_ISEMPTY(&srv->update_status)) {
2637 THREAD_WANT_SYNC();
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002638 HA_SPIN_LOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
Christopher Faulet5d42e092017-10-16 12:00:40 +02002639 if (LIST_ISEMPTY(&srv->update_status))
2640 LIST_ADDQ(&updated_servers, &srv->update_status);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002641 HA_SPIN_UNLOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
Christopher Faulet5d42e092017-10-16 12:00:40 +02002642 }
2643}
2644
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002645/* Update a server state using the parameters available in the params list */
2646static void srv_update_state(struct server *srv, int version, char **params)
2647{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002648 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002649 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002650
2651 /* fields since version 1
2652 * and common to all other upcoming versions
2653 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002654 enum srv_state srv_op_state;
2655 enum srv_admin srv_admin_state;
2656 unsigned srv_uweight, srv_iweight;
2657 unsigned long srv_last_time_change;
2658 short srv_check_status;
2659 enum chk_result srv_check_result;
2660 int srv_check_health;
2661 int srv_check_state, srv_agent_state;
2662 int bk_f_forced_id;
2663 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002664 int fqdn_set_by_cli;
2665 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002666 const char *port_str;
2667 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002668
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002669 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002670 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002671 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002672 switch (version) {
2673 case 1:
2674 /*
2675 * now we can proceed with server's state update:
2676 * srv_addr: params[0]
2677 * srv_op_state: params[1]
2678 * srv_admin_state: params[2]
2679 * srv_uweight: params[3]
2680 * srv_iweight: params[4]
2681 * srv_last_time_change: params[5]
2682 * srv_check_status: params[6]
2683 * srv_check_result: params[7]
2684 * srv_check_health: params[8]
2685 * srv_check_state: params[9]
2686 * srv_agent_state: params[10]
2687 * bk_f_forced_id: params[11]
2688 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002689 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002690 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002691 */
2692
2693 /* validating srv_op_state */
2694 p = NULL;
2695 errno = 0;
2696 srv_op_state = strtol(params[1], &p, 10);
2697 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2698 (srv_op_state != SRV_ST_STOPPED &&
2699 srv_op_state != SRV_ST_STARTING &&
2700 srv_op_state != SRV_ST_RUNNING &&
2701 srv_op_state != SRV_ST_STOPPING)) {
2702 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2703 }
2704
2705 /* validating srv_admin_state */
2706 p = NULL;
2707 errno = 0;
2708 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002709 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002710
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002711 /* inherited statuses will be recomputed later.
2712 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2713 */
2714 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002715
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002716 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2717 (srv_admin_state != 0 &&
2718 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002719 srv_admin_state != SRV_ADMF_CMAINT &&
2720 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002721 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002722 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002723 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2724 }
2725
2726 /* validating srv_uweight */
2727 p = NULL;
2728 errno = 0;
2729 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002730 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002731 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2732
2733 /* validating srv_iweight */
2734 p = NULL;
2735 errno = 0;
2736 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002737 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002738 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2739
2740 /* validating srv_last_time_change */
2741 p = NULL;
2742 errno = 0;
2743 srv_last_time_change = strtol(params[5], &p, 10);
2744 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2745 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2746
2747 /* validating srv_check_status */
2748 p = NULL;
2749 errno = 0;
2750 srv_check_status = strtol(params[6], &p, 10);
2751 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2752 (srv_check_status >= HCHK_STATUS_SIZE))
2753 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2754
2755 /* validating srv_check_result */
2756 p = NULL;
2757 errno = 0;
2758 srv_check_result = strtol(params[7], &p, 10);
2759 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2760 (srv_check_result != CHK_RES_UNKNOWN &&
2761 srv_check_result != CHK_RES_NEUTRAL &&
2762 srv_check_result != CHK_RES_FAILED &&
2763 srv_check_result != CHK_RES_PASSED &&
2764 srv_check_result != CHK_RES_CONDPASS)) {
2765 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2766 }
2767
2768 /* validating srv_check_health */
2769 p = NULL;
2770 errno = 0;
2771 srv_check_health = strtol(params[8], &p, 10);
2772 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2773 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2774
2775 /* validating srv_check_state */
2776 p = NULL;
2777 errno = 0;
2778 srv_check_state = strtol(params[9], &p, 10);
2779 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2780 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2781 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2782
2783 /* validating srv_agent_state */
2784 p = NULL;
2785 errno = 0;
2786 srv_agent_state = strtol(params[10], &p, 10);
2787 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2788 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2789 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2790
2791 /* validating bk_f_forced_id */
2792 p = NULL;
2793 errno = 0;
2794 bk_f_forced_id = strtol(params[11], &p, 10);
2795 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2796 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2797
2798 /* validating srv_f_forced_id */
2799 p = NULL;
2800 errno = 0;
2801 srv_f_forced_id = strtol(params[12], &p, 10);
2802 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2803 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2804
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002805 /* validating srv_fqdn */
2806 fqdn = params[13];
2807 if (fqdn && *fqdn == '-')
2808 fqdn = NULL;
2809 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2810 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2811 fqdn = NULL;
2812 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002813
Frédéric Lécaille31694712017-08-01 08:47:19 +02002814 port_str = params[14];
2815 if (port_str) {
2816 port = strl2uic(port_str, strlen(port_str));
2817 if (port > USHRT_MAX) {
2818 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2819 port_str = NULL;
2820 }
2821 }
2822
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002823 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002824 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002825 goto out;
2826
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002827 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002828 /* recover operational state and apply it to this server
2829 * and all servers tracking this one */
2830 switch (srv_op_state) {
2831 case SRV_ST_STOPPED:
2832 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002833 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002834 break;
2835 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002836 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002837 break;
2838 case SRV_ST_STOPPING:
2839 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002840 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002841 break;
2842 case SRV_ST_RUNNING:
2843 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002844 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002845 break;
2846 }
2847
2848 /* When applying server state, the following rules apply:
2849 * - in case of a configuration change, we apply the setting from the new
2850 * configuration, regardless of old running state
2851 * - if no configuration change, we apply old running state only if old running
2852 * state is different from new configuration state
2853 */
2854 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002855 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2856 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002857 srv_adm_set_maint(srv);
2858 else
2859 srv_adm_set_ready(srv);
2860 }
2861 /* configuration is the same, let's compate old running state and new conf state */
2862 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002863 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002864 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002865 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002866 srv_adm_set_ready(srv);
2867 }
2868 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002869 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002870 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002871 * (srv->iweight is the weight set up in configuration).
2872 * There are two possible reasons for FDRAIN to have been present :
2873 * - previous config weight was zero
2874 * - "set server b/s drain" was sent to the CLI
2875 *
2876 * In the first case, we simply want to drop this drain state
2877 * if the new weight is not zero anymore, meaning the administrator
2878 * has intentionally turned the weight back to a positive value to
2879 * enable the server again after an operation. In the second case,
2880 * the drain state was forced on the CLI regardless of the config's
2881 * weight so we don't want a change to the config weight to lose this
2882 * status. What this means is :
2883 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2884 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002885 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002886 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002887 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002888 }
2889
2890 srv->last_change = date.tv_sec - srv_last_time_change;
2891 srv->check.status = srv_check_status;
2892 srv->check.result = srv_check_result;
2893 srv->check.health = srv_check_health;
2894
2895 /* Only case we want to apply is removing ENABLED flag which could have been
2896 * done by the "disable health" command over the stats socket
2897 */
2898 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2899 (srv_check_state & CHK_ST_CONFIGURED) &&
2900 !(srv_check_state & CHK_ST_ENABLED))
2901 srv->check.state &= ~CHK_ST_ENABLED;
2902
2903 /* Only case we want to apply is removing ENABLED flag which could have been
2904 * done by the "disable agent" command over the stats socket
2905 */
2906 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2907 (srv_agent_state & CHK_ST_CONFIGURED) &&
2908 !(srv_agent_state & CHK_ST_ENABLED))
2909 srv->agent.state &= ~CHK_ST_ENABLED;
2910
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002911 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2912 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002913 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002914 * It means that a configuration file change has precedence over a unix socket change
2915 * for server's weight
2916 *
2917 * by default, HAProxy applies the following weight when parsing the configuration
2918 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002919 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002920 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002921 srv->uweight = srv_uweight;
2922 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002923 server_recalc_eweight(srv);
2924
Willy Tarreaue5a60682016-11-09 14:54:53 +01002925 /* load server IP address */
2926 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002927
2928 if (fqdn && srv->hostname) {
2929 if (!strcmp(srv->hostname, fqdn)) {
2930 /* Here we reset the 'set from stats socket FQDN' flag
2931 * to support such transitions:
2932 * Let's say initial FQDN value is foo1 (in configuration file).
2933 * - FQDN changed from stats socket, from foo1 to foo2 value,
2934 * - FQDN changed again from file configuration (with the same previous value
2935 set from stats socket, from foo1 to foo2 value),
2936 * - reload for any other reason than a FQDN modification,
2937 * the configuration file FQDN matches the fqdn server state file value.
2938 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
2939 * any futher FQDN modification.
2940 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002941 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002942 }
2943 else {
2944 /* If the FDQN has been changed from stats socket,
2945 * apply fqdn state file value (which is the value set
2946 * from stats socket).
2947 */
2948 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002949 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02002950 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002951 }
2952 }
2953 }
2954
Frédéric Lécaille31694712017-08-01 08:47:19 +02002955 if (port_str)
2956 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002957 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002958
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002959 break;
2960 default:
2961 chunk_appendf(msg, ", version '%d' not supported", version);
2962 }
2963
2964 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002965 if (msg->len) {
2966 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01002967 ha_warning("server-state application failed for server '%s/%s'%s",
2968 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002969 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002970}
2971
2972/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2973 * For each proxy, it does the following:
2974 * - opens its server state file (either one or local one)
2975 * - read whole file, line by line
2976 * - analyse each line to check if it matches our current backend:
2977 * - backend name matches
2978 * - backend id matches if id is forced and name doesn't match
2979 * - if the server pointed by the line is found, then state is applied
2980 *
2981 * If the running backend uuid or id differs from the state file, then HAProxy reports
2982 * a warning.
2983 */
2984void apply_server_state(void)
2985{
2986 char *cur, *end;
2987 char mybuf[SRV_STATE_LINE_MAXLEN];
2988 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002989 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
2990 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002991 int arg, srv_arg, version, diff;
2992 FILE *f;
2993 char *filepath;
2994 char globalfilepath[MAXPATHLEN + 1];
2995 char localfilepath[MAXPATHLEN + 1];
2996 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002997 struct proxy *curproxy, *bk;
2998 struct server *srv;
2999
3000 globalfilepathlen = 0;
3001 /* create the globalfilepath variable */
3002 if (global.server_state_file) {
3003 /* absolute path or no base directory provided */
3004 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3005 len = strlen(global.server_state_file);
3006 if (len > MAXPATHLEN) {
3007 globalfilepathlen = 0;
3008 goto globalfileerror;
3009 }
3010 memcpy(globalfilepath, global.server_state_file, len);
3011 globalfilepath[len] = '\0';
3012 globalfilepathlen = len;
3013 }
3014 else if (global.server_state_base) {
3015 len = strlen(global.server_state_base);
3016 globalfilepathlen += len;
3017
3018 if (globalfilepathlen > MAXPATHLEN) {
3019 globalfilepathlen = 0;
3020 goto globalfileerror;
3021 }
3022 strncpy(globalfilepath, global.server_state_base, len);
3023 globalfilepath[globalfilepathlen] = 0;
3024
3025 /* append a slash if needed */
3026 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3027 if (globalfilepathlen + 1 > MAXPATHLEN) {
3028 globalfilepathlen = 0;
3029 goto globalfileerror;
3030 }
3031 globalfilepath[globalfilepathlen++] = '/';
3032 }
3033
3034 len = strlen(global.server_state_file);
3035 if (globalfilepathlen + len > MAXPATHLEN) {
3036 globalfilepathlen = 0;
3037 goto globalfileerror;
3038 }
3039 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3040 globalfilepathlen += len;
3041 globalfilepath[globalfilepathlen++] = 0;
3042 }
3043 }
3044 globalfileerror:
3045 if (globalfilepathlen == 0)
3046 globalfilepath[0] = '\0';
3047
3048 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003049 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003050 /* servers are only in backends */
3051 if (!(curproxy->cap & PR_CAP_BE))
3052 continue;
3053 fileopenerr = 0;
3054 filepath = NULL;
3055
3056 /* search server state file path and name */
3057 switch (curproxy->load_server_state_from_file) {
3058 /* read servers state from global file */
3059 case PR_SRV_STATE_FILE_GLOBAL:
3060 /* there was an error while generating global server state file path */
3061 if (globalfilepathlen == 0)
3062 continue;
3063 filepath = globalfilepath;
3064 fileopenerr = 1;
3065 break;
3066 /* this backend has its own file */
3067 case PR_SRV_STATE_FILE_LOCAL:
3068 localfilepathlen = 0;
3069 localfilepath[0] = '\0';
3070 len = 0;
3071 /* create the localfilepath variable */
3072 /* absolute path or no base directory provided */
3073 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3074 len = strlen(curproxy->server_state_file_name);
3075 if (len > MAXPATHLEN) {
3076 localfilepathlen = 0;
3077 goto localfileerror;
3078 }
3079 memcpy(localfilepath, curproxy->server_state_file_name, len);
3080 localfilepath[len] = '\0';
3081 localfilepathlen = len;
3082 }
3083 else if (global.server_state_base) {
3084 len = strlen(global.server_state_base);
3085 localfilepathlen += len;
3086
3087 if (localfilepathlen > MAXPATHLEN) {
3088 localfilepathlen = 0;
3089 goto localfileerror;
3090 }
3091 strncpy(localfilepath, global.server_state_base, len);
3092 localfilepath[localfilepathlen] = 0;
3093
3094 /* append a slash if needed */
3095 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3096 if (localfilepathlen + 1 > MAXPATHLEN) {
3097 localfilepathlen = 0;
3098 goto localfileerror;
3099 }
3100 localfilepath[localfilepathlen++] = '/';
3101 }
3102
3103 len = strlen(curproxy->server_state_file_name);
3104 if (localfilepathlen + len > MAXPATHLEN) {
3105 localfilepathlen = 0;
3106 goto localfileerror;
3107 }
3108 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3109 localfilepathlen += len;
3110 localfilepath[localfilepathlen++] = 0;
3111 }
3112 filepath = localfilepath;
3113 localfileerror:
3114 if (localfilepathlen == 0)
3115 localfilepath[0] = '\0';
3116
3117 break;
3118 case PR_SRV_STATE_FILE_NONE:
3119 default:
3120 continue;
3121 }
3122
3123 /* preload global state file */
3124 errno = 0;
3125 f = fopen(filepath, "r");
3126 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003127 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003128 if (!f)
3129 continue;
3130
3131 mybuf[0] = '\0';
3132 mybuflen = 0;
3133 version = 0;
3134
3135 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003136 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003137 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003138 goto fileclose;
3139 }
3140
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003141 cur = mybuf;
3142 version = atoi(cur);
3143 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3144 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003145 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003146
3147 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3148 int bk_f_forced_id = 0;
3149 int check_id = 0;
3150 int check_name = 0;
3151
3152 mybuflen = strlen(mybuf);
3153 cur = mybuf;
3154 end = cur + mybuflen;
3155
3156 bk = NULL;
3157 srv = NULL;
3158
3159 /* we need at least one character */
3160 if (mybuflen == 0)
3161 continue;
3162
3163 /* ignore blank characters at the beginning of the line */
3164 while (isspace(*cur))
3165 ++cur;
3166
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003167 /* Ignore empty or commented lines */
3168 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003169 continue;
3170
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003171 /* truncated lines */
3172 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003173 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003174 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003175 }
3176
3177 /* Removes trailing '\n' */
3178 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003179
3180 /* we're now ready to move the line into *srv_params[] */
3181 params[0] = cur;
3182 arg = 1;
3183 srv_arg = 0;
3184 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3185 if (isspace(*cur)) {
3186 *cur = '\0';
3187 ++cur;
3188 while (isspace(*cur))
3189 ++cur;
3190 switch (version) {
3191 case 1:
3192 /*
3193 * srv_addr: params[4] => srv_params[0]
3194 * srv_op_state: params[5] => srv_params[1]
3195 * srv_admin_state: params[6] => srv_params[2]
3196 * srv_uweight: params[7] => srv_params[3]
3197 * srv_iweight: params[8] => srv_params[4]
3198 * srv_last_time_change: params[9] => srv_params[5]
3199 * srv_check_status: params[10] => srv_params[6]
3200 * srv_check_result: params[11] => srv_params[7]
3201 * srv_check_health: params[12] => srv_params[8]
3202 * srv_check_state: params[13] => srv_params[9]
3203 * srv_agent_state: params[14] => srv_params[10]
3204 * bk_f_forced_id: params[15] => srv_params[11]
3205 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003206 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003207 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003208 */
3209 if (arg >= 4) {
3210 srv_params[srv_arg] = cur;
3211 ++srv_arg;
3212 }
3213 break;
3214 }
3215
3216 params[arg] = cur;
3217 ++arg;
3218 }
3219 else {
3220 ++cur;
3221 }
3222 }
3223
3224 /* if line is incomplete line, then ignore it.
3225 * otherwise, update useful flags */
3226 switch (version) {
3227 case 1:
3228 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3229 continue;
3230 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3231 check_id = (atoi(params[0]) == curproxy->uuid);
3232 check_name = (strcmp(curproxy->id, params[1]) == 0);
3233 break;
3234 }
3235
3236 diff = 0;
3237 bk = curproxy;
3238
3239 /* if backend can't be found, let's continue */
3240 if (!check_id && !check_name)
3241 continue;
3242 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003243 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 +02003244 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3245 }
3246 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003247 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 +02003248 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3249 /* if name doesn't match, we still want to update curproxy if the backend id
3250 * was forced in previous the previous configuration */
3251 if (!bk_f_forced_id)
3252 continue;
3253 }
3254
3255 /* look for the server by its id: param[2] */
3256 /* else look for the server by its name: param[3] */
3257 diff = 0;
3258 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3259
3260 if (!srv) {
3261 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003262 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3263 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003264 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3265 params[3], params[2], params[0], params[1]);
3266 continue;
3267 }
3268 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003269 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 +02003270 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 +02003271 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003272 }
3273 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003274 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 +02003275 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 +02003276 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003277 }
3278
3279 /* now we can proceed with server's state update */
3280 srv_update_state(srv, version, srv_params);
3281 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003282fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003283 fclose(f);
3284 }
3285}
3286
Simon Horman7d09b9a2013-02-12 10:45:51 +09003287/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003288 * update a server's current IP address.
3289 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3290 * ip is in network format.
3291 * updater is a string which contains an information about the requester of the update.
3292 * updater is used if not NULL.
3293 *
3294 * A log line and a stderr warning message is generated based on server's backend options.
3295 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003296int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003297{
3298 /* generates a log line and a warning on stderr */
3299 if (1) {
3300 /* book enough space for both IPv4 and IPv6 */
3301 char oldip[INET6_ADDRSTRLEN];
3302 char newip[INET6_ADDRSTRLEN];
3303
3304 memset(oldip, '\0', INET6_ADDRSTRLEN);
3305 memset(newip, '\0', INET6_ADDRSTRLEN);
3306
3307 /* copy old IP address in a string */
3308 switch (s->addr.ss_family) {
3309 case AF_INET:
3310 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3311 break;
3312 case AF_INET6:
3313 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3314 break;
3315 };
3316
3317 /* copy new IP address in a string */
3318 switch (ip_sin_family) {
3319 case AF_INET:
3320 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3321 break;
3322 case AF_INET6:
3323 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3324 break;
3325 };
3326
3327 /* save log line into a buffer */
3328 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3329 s->proxy->id, s->id, oldip, newip, updater);
3330
3331 /* write the buffer on stderr */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003332 ha_warning("%s.\n", trash.str);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003333
3334 /* send a log */
3335 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3336 }
3337
3338 /* save the new IP family */
3339 s->addr.ss_family = ip_sin_family;
3340 /* save the new IP address */
3341 switch (ip_sin_family) {
3342 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003343 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003344 break;
3345 case AF_INET6:
3346 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3347 break;
3348 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003349 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003350
3351 return 0;
3352}
3353
3354/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003355 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3356 *
3357 * Caller can pass its name through <updater> to get it integrated in the response message
3358 * returned by the function.
3359 *
3360 * The function first does the following, in that order:
3361 * - validates the new addr and/or port
3362 * - checks if an update is required (new IP or port is different than current ones)
3363 * - checks the update is allowed:
3364 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3365 * - allow all changes if no CHECKS are configured
3366 * - if CHECK is configured:
3367 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3368 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3369 * conditions are met
3370 */
3371const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3372{
3373 struct sockaddr_storage sa;
3374 int ret, port_change_required;
3375 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003376 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003377 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003378 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003379
3380 msg = get_trash_chunk();
3381 chunk_reset(msg);
3382
3383 if (addr) {
3384 memset(&sa, 0, sizeof(struct sockaddr_storage));
3385 if (str2ip2(addr, &sa, 0) == NULL) {
3386 chunk_printf(msg, "Invalid addr '%s'", addr);
3387 goto out;
3388 }
3389
3390 /* changes are allowed on AF_INET* families only */
3391 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3392 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3393 goto out;
3394 }
3395
3396 /* collecting data currently setup */
3397 memset(current_addr, '\0', sizeof(current_addr));
3398 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3399 /* changes are allowed on AF_INET* families only */
3400 if ((ret != AF_INET) && (ret != AF_INET6)) {
3401 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3402 goto out;
3403 }
3404
3405 /* applying ADDR changes if required and allowed
3406 * ipcmp returns 0 when both ADDR are the same
3407 */
3408 if (ipcmp(&s->addr, &sa) == 0) {
3409 chunk_appendf(msg, "no need to change the addr");
3410 goto port;
3411 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003412 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003413 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003414
3415 /* we also need to update check's ADDR only if it uses the server's one */
3416 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003417 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003418 }
3419
3420 /* we also need to update agent ADDR only if it use the server's one */
3421 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003422 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003423 }
3424
3425 /* update report for caller */
3426 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3427 }
3428
3429 port:
3430 if (port) {
3431 char sign = '\0';
3432 char *endptr;
3433
3434 if (addr)
3435 chunk_appendf(msg, ", ");
3436
3437 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003438 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003439
3440 /* check if PORT change is required */
3441 port_change_required = 0;
3442
3443 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003444 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003445 new_port = strtol(port, &endptr, 10);
3446 if ((errno != 0) || (port == endptr)) {
3447 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3448 goto out;
3449 }
3450
3451 /* check if caller triggers a port mapped or offset */
3452 if (sign == '-' || (sign == '+')) {
3453 /* check if server currently uses port map */
3454 if (!(s->flags & SRV_F_MAPPORTS)) {
3455 /* switch from fixed port to port map mandatorily triggers
3456 * a port change */
3457 port_change_required = 1;
3458 /* check is configured
3459 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3460 * prevent PORT change if check doesn't have it's dedicated port while switching
3461 * to port mapping */
3462 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3463 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.");
3464 goto out;
3465 }
3466 }
3467 /* we're already using port maps */
3468 else {
3469 port_change_required = current_port != new_port;
3470 }
3471 }
3472 /* fixed port */
3473 else {
3474 port_change_required = current_port != new_port;
3475 }
3476
3477 /* applying PORT changes if required and update response message */
3478 if (port_change_required) {
3479 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003480 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003481 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003482
3483 /* prepare message */
3484 chunk_appendf(msg, "port changed from '");
3485 if (s->flags & SRV_F_MAPPORTS)
3486 chunk_appendf(msg, "+");
3487 chunk_appendf(msg, "%d' to '", current_port);
3488
3489 if (sign == '-') {
3490 s->flags |= SRV_F_MAPPORTS;
3491 chunk_appendf(msg, "%c", sign);
3492 /* just use for result output */
3493 new_port = -new_port;
3494 }
3495 else if (sign == '+') {
3496 s->flags |= SRV_F_MAPPORTS;
3497 chunk_appendf(msg, "%c", sign);
3498 }
3499 else {
3500 s->flags &= ~SRV_F_MAPPORTS;
3501 }
3502
3503 chunk_appendf(msg, "%d'", new_port);
3504
3505 /* we also need to update health checks port only if it uses server's realport */
3506 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3507 s->check.port = new_port;
3508 }
3509 }
3510 else {
3511 chunk_appendf(msg, "no need to change the port");
3512 }
3513 }
3514
3515out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003516 if (changed)
3517 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003518 if (updater)
3519 chunk_appendf(msg, " by '%s'", updater);
3520 chunk_appendf(msg, "\n");
3521 return msg->str;
3522}
3523
3524
3525/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003526 * update server status based on result of name resolution
3527 * returns:
3528 * 0 if server status is updated
3529 * 1 if server status has not changed
3530 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003531int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003532{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003533 struct dns_resolvers *resolvers = s->resolvers;
3534 struct dns_resolution *resolution = s->dns_requester->resolution;
3535 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003536
3537 switch (resolution->status) {
3538 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003539 /* status when HAProxy has just (re)started.
3540 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003541 break;
3542
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003543 case RSLV_STATUS_VALID:
3544 /*
3545 * resume health checks
3546 * server will be turned back on if health check is safe
3547 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003548 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003549 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003550 return 1;
3551 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3552 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003553 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003554 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003555
Emeric Brun52a91d32017-08-31 14:41:55 +02003556 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003557 return 1;
3558 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3559 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3560 s->proxy->id, s->id);
3561
Christopher Faulet767a84b2017-11-24 16:50:31 +01003562 ha_warning("%s.\n", trash.str);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003563 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3564 return 0;
3565
3566 case RSLV_STATUS_NX:
3567 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003568 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3569 if (!tick_is_expired(exp, now_ms))
3570 break;
3571
3572 if (s->next_admin & SRV_ADMF_RMAINT)
3573 return 1;
3574 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3575 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003576
3577 case RSLV_STATUS_TIMEOUT:
3578 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003579 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3580 if (!tick_is_expired(exp, now_ms))
3581 break;
3582
3583 if (s->next_admin & SRV_ADMF_RMAINT)
3584 return 1;
3585 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3586 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003587
3588 case RSLV_STATUS_REFUSED:
3589 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003590 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3591 if (!tick_is_expired(exp, now_ms))
3592 break;
3593
3594 if (s->next_admin & SRV_ADMF_RMAINT)
3595 return 1;
3596 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3597 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003598
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003599 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003600 /* stop server if resolution failed for a long enough period */
3601 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3602 if (!tick_is_expired(exp, now_ms))
3603 break;
3604
3605 if (s->next_admin & SRV_ADMF_RMAINT)
3606 return 1;
3607 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3608 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003609 }
3610
3611 return 1;
3612}
3613
3614/*
3615 * Server Name Resolution valid response callback
3616 * It expects:
3617 * - <nameserver>: the name server which answered the valid response
3618 * - <response>: buffer containing a valid DNS response
3619 * - <response_len>: size of <response>
3620 * It performs the following actions:
3621 * - ignore response if current ip found and server family not met
3622 * - update with first new ip found if family is met and current IP is not found
3623 * returns:
3624 * 0 on error
3625 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003626 *
3627 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003628 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003629int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003630{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003631 struct server *s = NULL;
3632 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003633 void *serverip, *firstip;
3634 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003635 int ret;
3636 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003637 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003638
Christopher Faulet67957bd2017-09-27 11:00:59 +02003639 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003640 if (!s)
3641 return 1;
3642
Christopher Faulet67957bd2017-09-27 11:00:59 +02003643 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003644
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003645 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003646 firstip = NULL; /* pointer to the first valid response found */
3647 /* it will be used as the new IP if a change is required */
3648 firstip_sin_family = AF_UNSPEC;
3649 serverip = NULL; /* current server IP address */
3650
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003651 /* initializing server IP pointer */
3652 server_sin_family = s->addr.ss_family;
3653 switch (server_sin_family) {
3654 case AF_INET:
3655 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3656 break;
3657
3658 case AF_INET6:
3659 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3660 break;
3661
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003662 case AF_UNSPEC:
3663 break;
3664
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003665 default:
3666 goto invalid;
3667 }
3668
Baptiste Assmann729c9012017-05-22 15:13:10 +02003669 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003670 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003671 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003672
3673 switch (ret) {
3674 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003675 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003676
3677 case DNS_UPD_SRVIP_NOT_FOUND:
3678 goto save_ip;
3679
3680 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003681 goto invalid;
3682
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003683 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003684 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003685 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003686
Baptiste Assmannfad03182015-10-28 02:03:32 +01003687 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003688 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003689 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003690 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003691
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003692 default:
3693 goto invalid;
3694
3695 }
3696
3697 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003698 if (nameserver) {
3699 nameserver->counters.update++;
3700 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003701 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003702 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003703 else
3704 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003705 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3706
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003707 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003708 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003709 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003710
3711 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003712 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003713 nameserver->counters.invalid++;
3714 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003715 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003716 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003717 return 0;
3718}
3719
3720/*
3721 * Server Name Resolution error management callback
3722 * returns:
3723 * 0 on error
3724 * 1 when no error or safe ignore
3725 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003726int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003727{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003728 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003729
Christopher Faulet67957bd2017-09-27 11:00:59 +02003730 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003731 if (!s)
3732 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003733 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003734 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003735 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003736 return 1;
3737}
3738
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003739/*
3740 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003741 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003742 * It returns a pointer to the first server found or NULL if <ip> is not yet
3743 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003744 *
3745 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003746 */
3747struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3748{
3749 struct server *tmpsrv;
3750 struct proxy *be;
3751
3752 if (!srv)
3753 return NULL;
3754
3755 be = srv->proxy;
3756 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003757 /* we found the current server is the same, ignore it */
3758 if (srv == tmpsrv)
3759 continue;
3760
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003761 /* We want to compare the IP in the record with the IP of the servers in the
3762 * same backend, only if:
3763 * * DNS resolution is enabled on the server
3764 * * the hostname used for the resolution by our server is the same than the
3765 * one used for the server found in the backend
3766 * * the server found in the backend is not our current server
3767 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003768 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003769 if ((tmpsrv->hostname_dn == NULL) ||
3770 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3771 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003772 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003773 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003774 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003775 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003776
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003777 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003778 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003779 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003780 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003781 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003782
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003783 /* At this point, we have 2 different servers using the same DNS hostname
3784 * for their respective resolution.
3785 */
3786 if (*ip_family == tmpsrv->addr.ss_family &&
3787 ((tmpsrv->addr.ss_family == AF_INET &&
3788 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3789 (tmpsrv->addr.ss_family == AF_INET6 &&
3790 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003791 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003792 return tmpsrv;
3793 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003794 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003795 }
3796
Emeric Brune9fd6b52017-11-02 17:20:39 +01003797
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003798 return NULL;
3799}
3800
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003801/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3802 * resolver. This is suited for initial address configuration. Returns 0 on
3803 * success otherwise a non-zero error code. In case of error, *err_code, if
3804 * not NULL, is filled up.
3805 */
3806int srv_set_addr_via_libc(struct server *srv, int *err_code)
3807{
3808 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003809 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003810 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003811 return 1;
3812 }
3813 return 0;
3814}
3815
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003816/* Set the server's FDQN (->hostname) from <hostname>.
3817 * Returns -1 if failed, 0 if not.
3818 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003819int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003820{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003821 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003822 char *hostname_dn;
3823 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003824
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003825 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003826 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003827 /* run time DNS resolution was not active for this server
3828 * and we can't enable it at run time for now.
3829 */
3830 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003831 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003832
3833 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003834 hostname_len = strlen(hostname);
3835 hostname_dn = trash.str;
3836 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3837 hostname_dn, trash.size);
3838 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003839 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003840
Christopher Faulet67957bd2017-09-27 11:00:59 +02003841 resolution = srv->dns_requester->resolution;
3842 if (resolution &&
3843 resolution->hostname_dn &&
3844 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02003845 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003846
Christopher Faulet67957bd2017-09-27 11:00:59 +02003847 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003848
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003849 free(srv->hostname);
3850 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003851 srv->hostname = strdup(hostname);
3852 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003853 srv->hostname_dn_len = hostname_dn_len;
3854 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003855 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003856
Olivier Houchard55dcdf42017-11-06 15:15:04 +01003857 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003858 goto err;
3859
3860 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003861 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003862 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003863 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003864
3865 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003866 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003867 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003868 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003869}
3870
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003871/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3872 * from the state file. This is suited for initial address configuration.
3873 * Returns 0 on success otherwise a non-zero error code. In case of error,
3874 * *err_code, if not NULL, is filled up.
3875 */
3876static int srv_apply_lastaddr(struct server *srv, int *err_code)
3877{
3878 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3879 if (err_code)
3880 *err_code |= ERR_WARN;
3881 return 1;
3882 }
3883 return 0;
3884}
3885
Willy Tarreau25e51522016-11-04 15:10:17 +01003886/* returns 0 if no error, otherwise a combination of ERR_* flags */
3887static int srv_iterate_initaddr(struct server *srv)
3888{
3889 int return_code = 0;
3890 int err_code;
3891 unsigned int methods;
3892
3893 methods = srv->init_addr_methods;
3894 if (!methods) { // default to "last,libc"
3895 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3896 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3897 }
3898
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003899 /* "-dr" : always append "none" so that server addresses resolution
3900 * failures are silently ignored, this is convenient to validate some
3901 * configs out of their environment.
3902 */
3903 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3904 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3905
Willy Tarreau25e51522016-11-04 15:10:17 +01003906 while (methods) {
3907 err_code = 0;
3908 switch (srv_get_next_initaddr(&methods)) {
3909 case SRV_IADDR_LAST:
3910 if (!srv->lastaddr)
3911 continue;
3912 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003913 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003914 return_code |= err_code;
3915 break;
3916
3917 case SRV_IADDR_LIBC:
3918 if (!srv->hostname)
3919 continue;
3920 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003921 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003922 return_code |= err_code;
3923 break;
3924
Willy Tarreau37ebe122016-11-04 15:17:58 +01003925 case SRV_IADDR_NONE:
3926 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003927 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003928 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3929 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003930 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003931 return return_code;
3932
Willy Tarreau4310d362016-11-02 15:05:56 +01003933 case SRV_IADDR_IP:
3934 ipcpy(&srv->init_addr, &srv->addr);
3935 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003936 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3937 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01003938 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003939 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003940
Willy Tarreau25e51522016-11-04 15:10:17 +01003941 default: /* unhandled method */
3942 break;
3943 }
3944 }
3945
3946 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003947 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01003948 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3949 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003950 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003951 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01003952 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3953 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003954
3955 return_code |= ERR_ALERT | ERR_FATAL;
3956 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003957out:
3958 srv_set_dyncookie(srv);
3959 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003960}
3961
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003962/*
3963 * This function parses all backends and all servers within each backend
3964 * and performs servers' addr resolution based on information provided by:
3965 * - configuration file
3966 * - server-state file (states provided by an 'old' haproxy process)
3967 *
3968 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3969 */
3970int srv_init_addr(void)
3971{
3972 struct proxy *curproxy;
3973 int return_code = 0;
3974
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003975 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003976 while (curproxy) {
3977 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003978
3979 /* servers are in backend only */
3980 if (!(curproxy->cap & PR_CAP_BE))
3981 goto srv_init_addr_next;
3982
Willy Tarreau25e51522016-11-04 15:10:17 +01003983 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003984 if (srv->hostname)
3985 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003986
3987 srv_init_addr_next:
3988 curproxy = curproxy->next;
3989 }
3990
3991 return return_code;
3992}
3993
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003994const 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 +02003995{
3996
3997 struct chunk *msg;
3998
3999 msg = get_trash_chunk();
4000 chunk_reset(msg);
4001
Olivier Houchard8da5f982017-08-04 18:35:36 +02004002 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004003 chunk_appendf(msg, "no need to change the FDQN");
4004 goto out;
4005 }
4006
4007 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4008 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4009 goto out;
4010 }
4011
4012 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4013 server->proxy->id, server->id, server->hostname, fqdn);
4014
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004015 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004016 chunk_reset(msg);
4017 chunk_appendf(msg, "could not update %s/%s FQDN",
4018 server->proxy->id, server->id);
4019 goto out;
4020 }
4021
4022 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004023 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004024
4025 out:
4026 if (updater)
4027 chunk_appendf(msg, " by '%s'", updater);
4028 chunk_appendf(msg, "\n");
4029
4030 return msg->str;
4031}
4032
4033
Willy Tarreau21b069d2016-11-23 17:15:08 +01004034/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4035 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004036 * 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 +01004037 * used for CLI commands requiring a server name.
4038 * Important: the <arg> is modified to remove the '/'.
4039 */
4040struct server *cli_find_server(struct appctx *appctx, char *arg)
4041{
4042 struct proxy *px;
4043 struct server *sv;
4044 char *line;
4045
4046 /* split "backend/server" and make <line> point to server */
4047 for (line = arg; *line; line++)
4048 if (*line == '/') {
4049 *line++ = '\0';
4050 break;
4051 }
4052
4053 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004054 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004055 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004056 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004057 return NULL;
4058 }
4059
4060 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004061 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004062 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004063 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004064 return NULL;
4065 }
4066
4067 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004068 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004069 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004070 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004071 return NULL;
4072 }
4073
4074 return sv;
4075}
4076
William Lallemand222baf22016-11-19 02:00:33 +01004077
4078static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4079{
4080 struct server *sv;
4081 const char *warning;
4082
4083 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4084 return 1;
4085
4086 sv = cli_find_server(appctx, args[2]);
4087 if (!sv)
4088 return 1;
4089
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004090 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004091
William Lallemand222baf22016-11-19 02:00:33 +01004092 if (strcmp(args[3], "weight") == 0) {
4093 warning = server_parse_weight_change_request(sv, args[4]);
4094 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004095 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004096 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004097 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004098 }
4099 }
4100 else if (strcmp(args[3], "state") == 0) {
4101 if (strcmp(args[4], "ready") == 0)
4102 srv_adm_set_ready(sv);
4103 else if (strcmp(args[4], "drain") == 0)
4104 srv_adm_set_drain(sv);
4105 else if (strcmp(args[4], "maint") == 0)
4106 srv_adm_set_maint(sv);
4107 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004108 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004109 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004110 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004111 }
4112 }
4113 else if (strcmp(args[3], "health") == 0) {
4114 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004115 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004116 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004117 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004118 }
4119 else if (strcmp(args[4], "up") == 0) {
4120 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004121 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004122 }
4123 else if (strcmp(args[4], "stopping") == 0) {
4124 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004125 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004126 }
4127 else if (strcmp(args[4], "down") == 0) {
4128 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004129 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004130 }
4131 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004132 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004133 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004134 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004135 }
4136 }
4137 else if (strcmp(args[3], "agent") == 0) {
4138 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004139 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004140 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004141 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004142 }
4143 else if (strcmp(args[4], "up") == 0) {
4144 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004145 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004146 }
4147 else if (strcmp(args[4], "down") == 0) {
4148 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004149 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004150 }
4151 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004152 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004153 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004154 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004155 }
4156 }
Misiek2da082d2017-01-09 09:40:42 +01004157 else if (strcmp(args[3], "agent-addr") == 0) {
4158 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004159 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004160 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4161 appctx->st0 = CLI_ST_PRINT;
4162 } else {
4163 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004164 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004165 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4166 appctx->st0 = CLI_ST_PRINT;
4167 }
4168 }
4169 }
4170 else if (strcmp(args[3], "agent-send") == 0) {
4171 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004172 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004173 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4174 appctx->st0 = CLI_ST_PRINT;
4175 } else {
4176 char *nss = strdup(args[4]);
4177 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004178 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004179 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4180 appctx->st0 = CLI_ST_PRINT;
4181 } else {
4182 free(sv->agent.send_string);
4183 sv->agent.send_string = nss;
4184 sv->agent.send_string_len = strlen(args[4]);
4185 }
4186 }
4187 }
William Lallemand222baf22016-11-19 02:00:33 +01004188 else if (strcmp(args[3], "check-port") == 0) {
4189 int i = 0;
4190 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004191 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004192 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004193 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004194 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004195 }
4196 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004197 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004198 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004199 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004200 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004201 }
4202 /* prevent the update of port to 0 if MAPPORTS are in use */
4203 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004204 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004205 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004206 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004207 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004208 }
4209 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004210 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004211 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004212 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004213 }
4214 else if (strcmp(args[3], "addr") == 0) {
4215 char *addr = NULL;
4216 char *port = NULL;
4217 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004218 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004219 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004220 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004221 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004222 }
4223 else {
4224 addr = args[4];
4225 }
4226 if (strcmp(args[5], "port") == 0) {
4227 port = args[6];
4228 }
4229 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4230 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004231 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004232 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004233 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004234 }
4235 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4236 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004237 else if (strcmp(args[3], "fqdn") == 0) {
4238 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004239 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004240 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4241 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004242 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004243 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004244 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004245 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004246 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004247 appctx->ctx.cli.msg = warning;
4248 appctx->st0 = CLI_ST_PRINT;
4249 }
4250 }
William Lallemand222baf22016-11-19 02:00:33 +01004251 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004252 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004253 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 +01004254 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004255 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004256 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004257 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004258 return 1;
4259}
4260
William Lallemand6b160942016-11-22 12:34:35 +01004261static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4262{
4263 struct stream_interface *si = appctx->owner;
4264 struct proxy *px;
4265 struct server *sv;
4266 char *line;
4267
4268
4269 /* split "backend/server" and make <line> point to server */
4270 for (line = args[2]; *line; line++)
4271 if (*line == '/') {
4272 *line++ = '\0';
4273 break;
4274 }
4275
4276 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004277 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004278 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004279 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004280 return 1;
4281 }
4282
4283 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004284 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004285 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004286 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004287 return 1;
4288 }
4289
4290 /* return server's effective weight at the moment */
4291 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004292 if (ci_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004293 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004294 return 0;
4295 }
William Lallemand6b160942016-11-22 12:34:35 +01004296 return 1;
4297}
4298
4299static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4300{
4301 struct server *sv;
4302 const char *warning;
4303
4304 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4305 return 1;
4306
4307 sv = cli_find_server(appctx, args[2]);
4308 if (!sv)
4309 return 1;
4310
4311 warning = server_parse_weight_change_request(sv, args[3]);
4312 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004313 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004314 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004315 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004316 }
4317 return 1;
4318}
4319
Willy Tarreaub8026272016-11-23 11:26:56 +01004320/* parse a "set maxconn server" command. It always returns 1. */
4321static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4322{
4323 struct server *sv;
4324 const char *warning;
4325
4326 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4327 return 1;
4328
4329 sv = cli_find_server(appctx, args[3]);
4330 if (!sv)
4331 return 1;
4332
4333 warning = server_parse_maxconn_change_request(sv, args[4]);
4334 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004335 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004336 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004337 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004338 }
4339 return 1;
4340}
William Lallemand6b160942016-11-22 12:34:35 +01004341
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004342/* parse a "disable agent" command. It always returns 1. */
4343static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4344{
4345 struct server *sv;
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 sv->agent.state &= ~CHK_ST_ENABLED;
4355 return 1;
4356}
4357
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004358/* parse a "disable health" command. It always returns 1. */
4359static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4360{
4361 struct server *sv;
4362
4363 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4364 return 1;
4365
4366 sv = cli_find_server(appctx, args[2]);
4367 if (!sv)
4368 return 1;
4369
4370 sv->check.state &= ~CHK_ST_ENABLED;
4371 return 1;
4372}
4373
Willy Tarreauffb4d582016-11-24 12:47:00 +01004374/* parse a "disable server" command. It always returns 1. */
4375static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4376{
4377 struct server *sv;
4378
4379 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4380 return 1;
4381
4382 sv = cli_find_server(appctx, args[2]);
4383 if (!sv)
4384 return 1;
4385
4386 srv_adm_set_maint(sv);
4387 return 1;
4388}
4389
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004390/* parse a "enable agent" command. It always returns 1. */
4391static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4392{
4393 struct server *sv;
4394
4395 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4396 return 1;
4397
4398 sv = cli_find_server(appctx, args[2]);
4399 if (!sv)
4400 return 1;
4401
4402 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004403 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004404 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004405 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004406 return 1;
4407 }
4408
4409 sv->agent.state |= CHK_ST_ENABLED;
4410 return 1;
4411}
4412
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004413/* parse a "enable health" command. It always returns 1. */
4414static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4415{
4416 struct server *sv;
4417
4418 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4419 return 1;
4420
4421 sv = cli_find_server(appctx, args[2]);
4422 if (!sv)
4423 return 1;
4424
4425 sv->check.state |= CHK_ST_ENABLED;
4426 return 1;
4427}
4428
Willy Tarreauffb4d582016-11-24 12:47:00 +01004429/* parse a "enable server" command. It always returns 1. */
4430static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4431{
4432 struct server *sv;
4433
4434 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4435 return 1;
4436
4437 sv = cli_find_server(appctx, args[2]);
4438 if (!sv)
4439 return 1;
4440
4441 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004442 if (!(sv->flags & SRV_F_COOKIESET)
4443 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4444 sv->cookie)
4445 srv_check_for_dup_dyncookie(sv);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004446 return 1;
4447}
4448
William Lallemand222baf22016-11-19 02:00:33 +01004449/* register cli keywords */
4450static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004451 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004452 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004453 { { "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 +01004454 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004455 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004456 { { "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 +01004457 { { "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 +01004458 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004459 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4460 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4461
William Lallemand222baf22016-11-19 02:00:33 +01004462 {{},}
4463}};
4464
4465__attribute__((constructor))
4466static void __server_init(void)
4467{
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004468 HA_SPIN_INIT(&updated_servers_lock);
William Lallemand222baf22016-11-19 02:00:33 +01004469 cli_register_kw(&cli_kws);
4470}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004471
Emeric Brun64cc49c2017-10-03 14:46:45 +02004472/*
4473 * This function applies server's status changes, it is
4474 * is designed to be called asynchronously.
4475 *
4476 */
4477void srv_update_status(struct server *s)
4478{
4479 struct check *check = &s->check;
4480 int xferred;
4481 struct proxy *px = s->proxy;
4482 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4483 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4484 int log_level;
4485 struct chunk *tmptrash = NULL;
4486
4487
4488 /* If currently main is not set we try to apply pending state changes */
4489 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4490 int next_admin;
4491
4492 /* Backup next admin */
4493 next_admin = s->next_admin;
4494
4495 /* restore current admin state */
4496 s->next_admin = s->cur_admin;
4497
4498 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4499 s->last_change = now.tv_sec;
4500 if (s->proxy->lbprm.set_server_status_down)
4501 s->proxy->lbprm.set_server_status_down(s);
4502
4503 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4504 srv_shutdown_streams(s, SF_ERR_DOWN);
4505
4506 /* we might have streams queued on this server and waiting for
4507 * a connection. Those which are redispatchable will be queued
4508 * to another server or to the proxy itself.
4509 */
4510 xferred = pendconn_redistribute(s);
4511
4512 tmptrash = alloc_trash_chunk();
4513 if (tmptrash) {
4514 chunk_printf(tmptrash,
4515 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4516 s->proxy->id, s->id);
4517
Emeric Brun5a133512017-10-19 14:42:30 +02004518 srv_append_status(tmptrash, s, NULL, xferred, 0);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004519 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004520
4521 /* we don't send an alert if the server was previously paused */
4522 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
4523 send_log(s->proxy, log_level, "%s.\n", tmptrash->str);
4524 send_email_alert(s, log_level, "%s", tmptrash->str);
4525 free_trash_chunk(tmptrash);
4526 tmptrash = NULL;
4527 }
4528 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4529 set_backend_down(s->proxy);
4530
4531 s->counters.down_trans++;
4532 }
4533 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4534 s->last_change = now.tv_sec;
4535 if (s->proxy->lbprm.set_server_status_down)
4536 s->proxy->lbprm.set_server_status_down(s);
4537
4538 /* we might have streams queued on this server and waiting for
4539 * a connection. Those which are redispatchable will be queued
4540 * to another server or to the proxy itself.
4541 */
4542 xferred = pendconn_redistribute(s);
4543
4544 tmptrash = alloc_trash_chunk();
4545 if (tmptrash) {
4546 chunk_printf(tmptrash,
4547 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4548 s->proxy->id, s->id);
4549
Emeric Brun5a133512017-10-19 14:42:30 +02004550 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004551
Christopher Faulet767a84b2017-11-24 16:50:31 +01004552 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004553 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4554 free_trash_chunk(tmptrash);
4555 tmptrash = NULL;
4556 }
4557
4558 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4559 set_backend_down(s->proxy);
4560 }
4561 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4562 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4563 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4564 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4565 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4566 s->proxy->last_change = now.tv_sec;
4567 }
4568
4569 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4570 s->down_time += now.tv_sec - s->last_change;
4571
4572 s->last_change = now.tv_sec;
4573 if (s->next_state == SRV_ST_STARTING)
4574 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4575
4576 server_recalc_eweight(s);
4577 /* now propagate the status change to any LB algorithms */
4578 if (px->lbprm.update_server_eweight)
4579 px->lbprm.update_server_eweight(s);
4580 else if (srv_willbe_usable(s)) {
4581 if (px->lbprm.set_server_status_up)
4582 px->lbprm.set_server_status_up(s);
4583 }
4584 else {
4585 if (px->lbprm.set_server_status_down)
4586 px->lbprm.set_server_status_down(s);
4587 }
4588
4589 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4590 * and it's not a backup server and its effective weight is > 0,
4591 * then it can accept new connections, so we shut down all streams
4592 * on all backup servers.
4593 */
4594 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4595 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4596 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4597
4598 /* check if we can handle some connections queued at the proxy. We
4599 * will take as many as we can handle.
4600 */
4601 xferred = pendconn_grab_from_px(s);
4602
4603 tmptrash = alloc_trash_chunk();
4604 if (tmptrash) {
4605 chunk_printf(tmptrash,
4606 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4607 s->proxy->id, s->id);
4608
Emeric Brun5a133512017-10-19 14:42:30 +02004609 srv_append_status(tmptrash, s, NULL, xferred, 0);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004610 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004611 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4612 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4613 free_trash_chunk(tmptrash);
4614 tmptrash = NULL;
4615 }
4616
4617 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4618 set_backend_down(s->proxy);
4619 }
4620 else if (s->cur_eweight != s->next_eweight) {
4621 /* now propagate the status change to any LB algorithms */
4622 if (px->lbprm.update_server_eweight)
4623 px->lbprm.update_server_eweight(s);
4624 else if (srv_willbe_usable(s)) {
4625 if (px->lbprm.set_server_status_up)
4626 px->lbprm.set_server_status_up(s);
4627 }
4628 else {
4629 if (px->lbprm.set_server_status_down)
4630 px->lbprm.set_server_status_down(s);
4631 }
4632
4633 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4634 set_backend_down(s->proxy);
4635 }
4636
4637 s->next_admin = next_admin;
4638 }
4639
Emeric Brun5a133512017-10-19 14:42:30 +02004640 /* reset operational state change */
4641 *s->op_st_chg.reason = 0;
4642 s->op_st_chg.status = s->op_st_chg.code = -1;
4643 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004644
4645 /* Now we try to apply pending admin changes */
4646
4647 /* Maintenance must also disable health checks */
4648 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4649 if (s->check.state & CHK_ST_ENABLED) {
4650 s->check.state |= CHK_ST_PAUSED;
4651 check->health = 0;
4652 }
4653
4654 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004655 tmptrash = alloc_trash_chunk();
4656 if (tmptrash) {
4657 chunk_printf(tmptrash,
4658 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4659 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4660 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004661
Olivier Houchard796a2b32017-10-24 17:42:47 +02004662 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004663
Olivier Houchard796a2b32017-10-24 17:42:47 +02004664 if (!(global.mode & MODE_STARTING)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004665 ha_warning("%s.\n", tmptrash->str);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004666 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4667 }
4668 free_trash_chunk(tmptrash);
4669 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004670 }
Emeric Brun8f298292017-12-06 16:47:17 +01004671 /* commit new admin status */
4672
4673 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004674 }
4675 else { /* server was still running */
4676 check->health = 0; /* failure */
4677 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004678
4679 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004680 if (s->proxy->lbprm.set_server_status_down)
4681 s->proxy->lbprm.set_server_status_down(s);
4682
Emeric Brun64cc49c2017-10-03 14:46:45 +02004683 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4684 srv_shutdown_streams(s, SF_ERR_DOWN);
4685
4686 /* we might have streams queued on this server and waiting for
4687 * a connection. Those which are redispatchable will be queued
4688 * to another server or to the proxy itself.
4689 */
4690 xferred = pendconn_redistribute(s);
4691
4692 tmptrash = alloc_trash_chunk();
4693 if (tmptrash) {
4694 chunk_printf(tmptrash,
4695 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4696 s->flags & SRV_F_BACKUP ? "Backup " : "",
4697 s->proxy->id, s->id,
4698 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4699
4700 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4701
4702 if (!(global.mode & MODE_STARTING)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004703 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004704 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", tmptrash->str);
4705 }
4706 free_trash_chunk(tmptrash);
4707 tmptrash = NULL;
4708 }
4709 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4710 set_backend_down(s->proxy);
4711
4712 s->counters.down_trans++;
4713 }
4714 }
4715 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4716 /* OK here we're leaving maintenance, we have many things to check,
4717 * because the server might possibly be coming back up depending on
4718 * its state. In practice, leaving maintenance means that we should
4719 * immediately turn to UP (more or less the slowstart) under the
4720 * following conditions :
4721 * - server is neither checked nor tracked
4722 * - server tracks another server which is not checked
4723 * - server tracks another server which is already up
4724 * Which sums up as something simpler :
4725 * "either the tracking server is up or the server's checks are disabled
4726 * or up". Otherwise we only re-enable health checks. There's a special
4727 * case associated to the stopping state which can be inherited. Note
4728 * that the server might still be in drain mode, which is naturally dealt
4729 * with by the lower level functions.
4730 */
4731
4732 if (s->check.state & CHK_ST_ENABLED) {
4733 s->check.state &= ~CHK_ST_PAUSED;
4734 check->health = check->rise; /* start OK but check immediately */
4735 }
4736
4737 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4738 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4739 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4740 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4741 s->next_state = SRV_ST_STOPPING;
4742 }
4743 else {
4744 s->next_state = SRV_ST_STARTING;
4745 if (s->slowstart > 0)
4746 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4747 else
4748 s->next_state = SRV_ST_RUNNING;
4749 }
4750
4751 }
4752
4753 tmptrash = alloc_trash_chunk();
4754 if (tmptrash) {
4755 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4756 chunk_printf(tmptrash,
4757 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4758 s->flags & SRV_F_BACKUP ? "Backup " : "",
4759 s->proxy->id, s->id,
4760 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4761 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4762 }
4763 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4764 chunk_printf(tmptrash,
4765 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4766 s->flags & SRV_F_BACKUP ? "Backup " : "",
4767 s->proxy->id, s->id, s->hostname,
4768 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4769 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4770 }
4771 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4772 chunk_printf(tmptrash,
4773 "%sServer %s/%s is %s/%s (leaving maintenance)",
4774 s->flags & SRV_F_BACKUP ? "Backup " : "",
4775 s->proxy->id, s->id,
4776 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4777 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4778 }
Christopher Faulet767a84b2017-11-24 16:50:31 +01004779 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004780 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4781 free_trash_chunk(tmptrash);
4782 tmptrash = NULL;
4783 }
4784
4785 server_recalc_eweight(s);
4786 /* now propagate the status change to any LB algorithms */
4787 if (px->lbprm.update_server_eweight)
4788 px->lbprm.update_server_eweight(s);
4789 else if (srv_willbe_usable(s)) {
4790 if (px->lbprm.set_server_status_up)
4791 px->lbprm.set_server_status_up(s);
4792 }
4793 else {
4794 if (px->lbprm.set_server_status_down)
4795 px->lbprm.set_server_status_down(s);
4796 }
4797
4798 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4799 set_backend_down(s->proxy);
4800
4801 }
4802 else if (s->next_admin & SRV_ADMF_MAINT) {
4803 /* remaining in maintenance mode, let's inform precisely about the
4804 * situation.
4805 */
4806 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4807 tmptrash = alloc_trash_chunk();
4808 if (tmptrash) {
4809 chunk_printf(tmptrash,
4810 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4811 s->flags & SRV_F_BACKUP ? "Backup " : "",
4812 s->proxy->id, s->id);
4813
4814 if (s->track) /* normally it's mandatory here */
4815 chunk_appendf(tmptrash, " via %s/%s",
4816 s->track->proxy->id, s->track->id);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004817 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004818 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4819 free_trash_chunk(tmptrash);
4820 tmptrash = NULL;
4821 }
4822 }
4823 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4824 tmptrash = alloc_trash_chunk();
4825 if (tmptrash) {
4826 chunk_printf(tmptrash,
4827 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4828 s->flags & SRV_F_BACKUP ? "Backup " : "",
4829 s->proxy->id, s->id, s->hostname);
4830
4831 if (s->track) /* normally it's mandatory here */
4832 chunk_appendf(tmptrash, " via %s/%s",
4833 s->track->proxy->id, s->track->id);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004834 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004835 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4836 free_trash_chunk(tmptrash);
4837 tmptrash = NULL;
4838 }
4839 }
4840 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4841 tmptrash = alloc_trash_chunk();
4842 if (tmptrash) {
4843 chunk_printf(tmptrash,
4844 "%sServer %s/%s remains in forced maintenance",
4845 s->flags & SRV_F_BACKUP ? "Backup " : "",
4846 s->proxy->id, s->id);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004847 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004848 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4849 free_trash_chunk(tmptrash);
4850 tmptrash = NULL;
4851 }
4852 }
4853 /* don't report anything when leaving drain mode and remaining in maintenance */
4854
4855 s->cur_admin = s->next_admin;
4856 }
4857
4858 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4859 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4860 /* drain state is applied only if not yet in maint */
4861
4862 s->last_change = now.tv_sec;
4863 if (px->lbprm.set_server_status_down)
4864 px->lbprm.set_server_status_down(s);
4865
4866 /* we might have streams queued on this server and waiting for
4867 * a connection. Those which are redispatchable will be queued
4868 * to another server or to the proxy itself.
4869 */
4870 xferred = pendconn_redistribute(s);
4871
4872 tmptrash = alloc_trash_chunk();
4873 if (tmptrash) {
4874 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4875 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4876 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4877
4878 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4879
4880 if (!(global.mode & MODE_STARTING)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004881 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004882 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4883 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4884 }
4885 free_trash_chunk(tmptrash);
4886 tmptrash = NULL;
4887 }
4888
4889 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4890 set_backend_down(s->proxy);
4891 }
4892 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4893 /* OK completely leaving drain mode */
4894 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4895 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4896 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4897 s->proxy->last_change = now.tv_sec;
4898 }
4899
4900 if (s->last_change < now.tv_sec) // ignore negative times
4901 s->down_time += now.tv_sec - s->last_change;
4902 s->last_change = now.tv_sec;
4903 server_recalc_eweight(s);
4904
4905 tmptrash = alloc_trash_chunk();
4906 if (tmptrash) {
4907 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4908 chunk_printf(tmptrash,
4909 "%sServer %s/%s is %s (leaving forced drain)",
4910 s->flags & SRV_F_BACKUP ? "Backup " : "",
4911 s->proxy->id, s->id,
4912 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4913 }
4914 else {
4915 chunk_printf(tmptrash,
4916 "%sServer %s/%s is %s (leaving drain)",
4917 s->flags & SRV_F_BACKUP ? "Backup " : "",
4918 s->proxy->id, s->id,
4919 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4920 if (s->track) /* normally it's mandatory here */
4921 chunk_appendf(tmptrash, " via %s/%s",
4922 s->track->proxy->id, s->track->id);
4923 }
4924
Christopher Faulet767a84b2017-11-24 16:50:31 +01004925 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004926 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4927 free_trash_chunk(tmptrash);
4928 tmptrash = NULL;
4929 }
4930
4931 /* now propagate the status change to any LB algorithms */
4932 if (px->lbprm.update_server_eweight)
4933 px->lbprm.update_server_eweight(s);
4934 else if (srv_willbe_usable(s)) {
4935 if (px->lbprm.set_server_status_up)
4936 px->lbprm.set_server_status_up(s);
4937 }
4938 else {
4939 if (px->lbprm.set_server_status_down)
4940 px->lbprm.set_server_status_down(s);
4941 }
4942 }
4943 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
4944 /* remaining in drain mode after removing one of its flags */
4945
4946 tmptrash = alloc_trash_chunk();
4947 if (tmptrash) {
4948 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4949 chunk_printf(tmptrash,
4950 "%sServer %s/%s is leaving forced drain but remains in drain mode",
4951 s->flags & SRV_F_BACKUP ? "Backup " : "",
4952 s->proxy->id, s->id);
4953
4954 if (s->track) /* normally it's mandatory here */
4955 chunk_appendf(tmptrash, " via %s/%s",
4956 s->track->proxy->id, s->track->id);
4957 }
4958 else {
4959 chunk_printf(tmptrash,
4960 "%sServer %s/%s remains in forced drain mode",
4961 s->flags & SRV_F_BACKUP ? "Backup " : "",
4962 s->proxy->id, s->id);
4963 }
Christopher Faulet767a84b2017-11-24 16:50:31 +01004964 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004965 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4966 free_trash_chunk(tmptrash);
4967 tmptrash = NULL;
4968 }
4969
4970 /* commit new admin status */
4971
4972 s->cur_admin = s->next_admin;
4973 }
4974 }
4975
4976 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02004977 *s->adm_st_chg_cause = 0;
4978}
4979/*
4980 * This function loops on servers registered for asynchronous
4981 * status changes
Christopher Faulet5d42e092017-10-16 12:00:40 +02004982 *
4983 * NOTE: No needs to lock <updated_servers> list because it is called inside the
4984 * sync point.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004985 */
4986void servers_update_status(void) {
4987 struct server *s, *stmp;
4988
4989 list_for_each_entry_safe(s, stmp, &updated_servers, update_status) {
4990 srv_update_status(s);
4991 LIST_DEL(&s->update_status);
4992 LIST_INIT(&s->update_status);
4993 }
4994}
4995
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004996/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004997 * Local variables:
4998 * c-indent-level: 8
4999 * c-basic-offset: 8
5000 * End:
5001 */