blob: f4cf45cd3221c634b3338f68d75e6d9b93778ca6 [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;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100520 } else if (!strcmp(p, "cert-key")) {
521 newsrv->pp_opts |= SRV_PP_V2_SSL;
522 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
523 } else if (!strcmp(p, "cert-sig")) {
524 newsrv->pp_opts |= SRV_PP_V2_SSL;
525 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
526 } else if (!strcmp(p, "ssl-cipher")) {
527 newsrv->pp_opts |= SRV_PP_V2_SSL;
528 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100529 } else if (!strcmp(p, "authority")) {
530 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100531 } else
532 goto fail;
533 }
534 return 0;
535 fail:
536 if (err)
537 memprintf(err, "'%s' : proxy v2 option not implemented", p);
538 return ERR_ALERT | ERR_FATAL;
539}
540
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100541/* Parse the "observe" server keyword */
542static int srv_parse_observe(char **args, int *cur_arg,
543 struct proxy *curproxy, struct server *newsrv, char **err)
544{
545 char *arg;
546
547 arg = args[*cur_arg + 1];
548 if (!*arg) {
549 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
550 return ERR_ALERT | ERR_FATAL;
551 }
552
553 if (!strcmp(arg, "none")) {
554 newsrv->observe = HANA_OBS_NONE;
555 }
556 else if (!strcmp(arg, "layer4")) {
557 newsrv->observe = HANA_OBS_LAYER4;
558 }
559 else if (!strcmp(arg, "layer7")) {
560 if (curproxy->mode != PR_MODE_HTTP) {
561 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
562 return ERR_ALERT;
563 }
564 newsrv->observe = HANA_OBS_LAYER7;
565 }
566 else {
567 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
568 "but got '%s'\n", args[*cur_arg], arg);
569 return ERR_ALERT | ERR_FATAL;
570 }
571
572 return 0;
573}
574
Frédéric Lécaille16186232017-03-14 16:42:49 +0100575/* Parse the "redir" server keyword */
576static int srv_parse_redir(char **args, int *cur_arg,
577 struct proxy *curproxy, struct server *newsrv, char **err)
578{
579 char *arg;
580
581 arg = args[*cur_arg + 1];
582 if (!*arg) {
583 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
584 return ERR_ALERT | ERR_FATAL;
585 }
586
587 free(newsrv->rdr_pfx);
588 newsrv->rdr_pfx = strdup(arg);
589 newsrv->rdr_len = strlen(arg);
590
591 return 0;
592}
593
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100594/* Parse the "send-proxy" server keyword */
595static int srv_parse_send_proxy(char **args, int *cur_arg,
596 struct proxy *curproxy, struct server *newsrv, char **err)
597{
598 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
599}
600
601/* Parse the "send-proxy-v2" server keyword */
602static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
603 struct proxy *curproxy, struct server *newsrv, char **err)
604{
605 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
606}
607
Frédéric Lécailledba97072017-03-17 15:33:50 +0100608
609/* Parse the "source" server keyword */
610static int srv_parse_source(char **args, int *cur_arg,
611 struct proxy *curproxy, struct server *newsrv, char **err)
612{
613 char *errmsg;
614 int port_low, port_high;
615 struct sockaddr_storage *sk;
616 struct protocol *proto;
617
618 errmsg = NULL;
619
620 if (!*args[*cur_arg + 1]) {
621 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
622 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
623 goto err;
624 }
625
626 /* 'sk' is statically allocated (no need to be freed). */
627 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
628 if (!sk) {
629 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
630 goto err;
631 }
632
633 proto = protocol_by_family(sk->ss_family);
634 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100635 ha_alert("'%s %s' : connect() not supported for this address family.\n",
636 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100637 goto err;
638 }
639
640 newsrv->conn_src.opts |= CO_SRC_BIND;
641 newsrv->conn_src.source_addr = *sk;
642
643 if (port_low != port_high) {
644 int i;
645
646 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100647 ha_alert("'%s' does not support port offsets (found '%s').\n",
648 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100649 goto err;
650 }
651
652 if (port_low <= 0 || port_low > 65535 ||
653 port_high <= 0 || port_high > 65535 ||
654 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100655 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 +0100656 goto err;
657 }
658 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
659 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
660 newsrv->conn_src.sport_range->ports[i] = port_low + i;
661 }
662
663 *cur_arg += 2;
664 while (*(args[*cur_arg])) {
665 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
666#if defined(CONFIG_HAP_TRANSPARENT)
667 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100668 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
669 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100670 goto err;
671 }
672 if (!strcmp(args[*cur_arg + 1], "client")) {
673 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
674 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
675 }
676 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
677 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
678 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
679 }
680 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
681 char *name, *end;
682
683 name = args[*cur_arg + 1] + 7;
684 while (isspace(*name))
685 name++;
686
687 end = name;
688 while (*end && !isspace(*end) && *end != ',' && *end != ')')
689 end++;
690
691 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
692 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
693 free(newsrv->conn_src.bind_hdr_name);
694 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
695 newsrv->conn_src.bind_hdr_len = end - name;
696 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
697 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
698 newsrv->conn_src.bind_hdr_occ = -1;
699
700 /* now look for an occurrence number */
701 while (isspace(*end))
702 end++;
703 if (*end == ',') {
704 end++;
705 name = end;
706 if (*end == '-')
707 end++;
708 while (isdigit((int)*end))
709 end++;
710 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
711 }
712
713 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100714 ha_alert("usesrc hdr_ip(name,num) does not support negative"
715 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100716 goto err;
717 }
718 }
719 else {
720 struct sockaddr_storage *sk;
721 int port1, port2;
722
723 /* 'sk' is statically allocated (no need to be freed). */
724 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
725 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100726 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100727 goto err;
728 }
729
730 proto = protocol_by_family(sk->ss_family);
731 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100732 ha_alert("'%s %s' : connect() not supported for this address family.\n",
733 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100734 goto err;
735 }
736
737 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100738 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
739 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100740 goto err;
741 }
742 newsrv->conn_src.tproxy_addr = *sk;
743 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
744 }
745 global.last_checks |= LSTCHK_NETADM;
746 *cur_arg += 2;
747 continue;
748#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100749 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 +0100750 goto err;
751#endif /* defined(CONFIG_HAP_TRANSPARENT) */
752 } /* "usesrc" */
753
754 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
755#ifdef SO_BINDTODEVICE
756 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100757 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100758 goto err;
759 }
760 free(newsrv->conn_src.iface_name);
761 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
762 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
763 global.last_checks |= LSTCHK_NETADM;
764#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100765 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100766 goto err;
767#endif
768 *cur_arg += 2;
769 continue;
770 }
771 /* this keyword in not an option of "source" */
772 break;
773 } /* while */
774
775 return 0;
776
777 err:
778 free(errmsg);
779 return ERR_ALERT | ERR_FATAL;
780}
781
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100782/* Parse the "stick" server keyword */
783static int srv_parse_stick(char **args, int *cur_arg,
784 struct proxy *curproxy, struct server *newsrv, char **err)
785{
786 newsrv->flags &= ~SRV_F_NON_STICK;
787 return 0;
788}
789
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100790/* Parse the "track" server keyword */
791static int srv_parse_track(char **args, int *cur_arg,
792 struct proxy *curproxy, struct server *newsrv, char **err)
793{
794 char *arg;
795
796 arg = args[*cur_arg + 1];
797 if (!*arg) {
798 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
799 return ERR_ALERT | ERR_FATAL;
800 }
801
802 free(newsrv->trackit);
803 newsrv->trackit = strdup(arg);
804
805 return 0;
806}
807
Frédéric Lécailledba97072017-03-17 15:33:50 +0100808
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200809/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200810 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200811 * shutdown.
812 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200813void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200814{
Willy Tarreau87b09662015-04-03 00:22:06 +0200815 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200816
Willy Tarreau87b09662015-04-03 00:22:06 +0200817 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
818 if (stream->srv_conn == srv)
819 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200820}
821
822/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200823 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200824 * the reason for the shutdown.
825 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200826void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200827{
828 struct server *srv;
829
830 for (srv = px->srv; srv != NULL; srv = srv->next)
831 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200832 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200833}
834
Willy Tarreaubda92272014-05-20 21:55:30 +0200835/* Appends some information to a message string related to a server going UP or
836 * DOWN. If both <forced> and <reason> are null and the server tracks another
837 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200838 * If <check> is non-null, an entire string describing the check result will be
839 * appended after a comma and a space (eg: to report some information from the
840 * check that changed the state). In the other case, the string will be built
841 * using the check results stored into the struct server if present.
842 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200843 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200844 */
Emeric Brun5a133512017-10-19 14:42:30 +0200845void srv_append_status(struct chunk *msg, struct server *s, struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200846{
Emeric Brun5a133512017-10-19 14:42:30 +0200847 short status = s->op_st_chg.status;
848 short code = s->op_st_chg.code;
849 long duration = s->op_st_chg.duration;
850 char *desc = s->op_st_chg.reason;
851
852 if (check) {
853 status = check->status;
854 code = check->code;
855 duration = check->duration;
856 desc = check->desc;
857 }
858
859 if (status != -1) {
860 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
861
862 if (status >= HCHK_STATUS_L57DATA)
863 chunk_appendf(msg, ", code: %d", code);
864
865 if (desc && *desc) {
866 struct chunk src;
867
868 chunk_appendf(msg, ", info: \"");
869
870 chunk_initlen(&src, desc, 0, strlen(desc));
871 chunk_asciiencode(msg, &src, '"');
872
873 chunk_appendf(msg, "\"");
874 }
875
876 if (duration >= 0)
877 chunk_appendf(msg, ", check duration: %ldms", duration);
878 }
879 else if (desc && *desc) {
880 chunk_appendf(msg, ", %s", desc);
881 }
882 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200883 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200884 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200885
886 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200887 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200888 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
889 " %d sessions active, %d requeued, %d remaining in queue",
890 s->proxy->srv_act, s->proxy->srv_bck,
891 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
892 s->cur_sess, xferred, s->nbpend);
893 else
894 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
895 " %d sessions requeued, %d total in queue",
896 s->proxy->srv_act, s->proxy->srv_bck,
897 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
898 xferred, s->nbpend);
899 }
900}
901
Emeric Brun5a133512017-10-19 14:42:30 +0200902/* Marks server <s> down, regardless of its checks' statuses. The server is
903 * registered in a list to postpone the counting of the remaining servers on
904 * the proxy and transfers queued streams whenever possible to other servers at
905 * a sync point. Maintenance servers are ignored. It stores the <reason> if
906 * non-null as the reason for going down or the available data from the check
907 * struct to recompute this reason later.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200908 */
Emeric Brun5a133512017-10-19 14:42:30 +0200909void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200910{
911 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200912
Emeric Brun64cc49c2017-10-03 14:46:45 +0200913 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200914 return;
915
Emeric Brun52a91d32017-08-31 14:41:55 +0200916 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200917 *s->op_st_chg.reason = 0;
918 s->op_st_chg.status = -1;
919 if (reason) {
920 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
921 }
922 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100923 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200924 s->op_st_chg.code = check->code;
925 s->op_st_chg.status = check->status;
926 s->op_st_chg.duration = check->duration;
927 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200928
Christopher Faulet5d42e092017-10-16 12:00:40 +0200929 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200930 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100931 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200932 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100933 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200934 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200935}
936
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200937/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200938 * in maintenance. The server is registered in a list to postpone the counting
939 * of the remaining servers on the proxy and tries to grab requests from the
940 * proxy at a sync point. Maintenance servers are ignored. It stores the
941 * <reason> if non-null as the reason for going down or the available data
942 * from the check struct to recompute this reason later.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200943 */
Emeric Brun5a133512017-10-19 14:42:30 +0200944void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200945{
946 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200947
Emeric Brun64cc49c2017-10-03 14:46:45 +0200948 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200949 return;
950
Emeric Brun52a91d32017-08-31 14:41:55 +0200951 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200952 return;
953
Emeric Brun52a91d32017-08-31 14:41:55 +0200954 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200955 *s->op_st_chg.reason = 0;
956 s->op_st_chg.status = -1;
957 if (reason) {
958 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
959 }
960 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100961 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200962 s->op_st_chg.code = check->code;
963 s->op_st_chg.status = check->status;
964 s->op_st_chg.duration = check->duration;
965 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200966
Emeric Brun64cc49c2017-10-03 14:46:45 +0200967 if (s->slowstart <= 0)
968 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200969
Christopher Faulet5d42e092017-10-16 12:00:40 +0200970 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200971 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100972 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200973 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100974 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200975 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200976}
977
Willy Tarreau8eb77842014-05-21 13:54:57 +0200978/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +0200979 * isn't in maintenance. The server is registered in a list to postpone the
980 * counting of the remaining servers on the proxy and tries to grab requests
981 * from the proxy. Maintenance servers are ignored. It stores the
982 * <reason> if non-null as the reason for going down or the available data
983 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +0200984 * up. Note that it makes use of the trash to build the log strings, so <reason>
985 * must not be placed there.
986 */
Emeric Brun5a133512017-10-19 14:42:30 +0200987void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200988{
989 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200990
Emeric Brun64cc49c2017-10-03 14:46:45 +0200991 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200992 return;
993
Emeric Brun52a91d32017-08-31 14:41:55 +0200994 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200995 return;
996
Emeric Brun52a91d32017-08-31 14:41:55 +0200997 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +0200998 *s->op_st_chg.reason = 0;
999 s->op_st_chg.status = -1;
1000 if (reason) {
1001 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1002 }
1003 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001004 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001005 s->op_st_chg.code = check->code;
1006 s->op_st_chg.status = check->status;
1007 s->op_st_chg.duration = check->duration;
1008 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001009
Christopher Faulet5d42e092017-10-16 12:00:40 +02001010 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001011 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001012 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001013 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001014 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001015 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001016}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001017
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001018/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1019 * enforce either maint mode or drain mode. It is not allowed to set more than
1020 * one flag at once. The equivalent "inherited" flag is propagated to all
1021 * tracking servers. Maintenance mode disables health checks (but not agent
1022 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001023 * is done. If <cause> is non-null, it will be displayed at the end of the log
1024 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001025 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001026void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001027{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001028 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001029
1030 if (!mode)
1031 return;
1032
1033 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001034 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001035 return;
1036
Emeric Brun52a91d32017-08-31 14:41:55 +02001037 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001038 if (cause)
1039 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1040
Christopher Faulet5d42e092017-10-16 12:00:40 +02001041 srv_register_update(s);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001042
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001043 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001044 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1045 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001046 return;
1047
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001048 /* compute the inherited flag to propagate */
1049 if (mode & SRV_ADMF_MAINT)
1050 mode = SRV_ADMF_IMAINT;
1051 else if (mode & SRV_ADMF_DRAIN)
1052 mode = SRV_ADMF_IDRAIN;
1053
Emeric Brun9f0b4582017-10-23 14:39:51 +02001054 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001055 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001056 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001057 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001058 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001059}
1060
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001061/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1062 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1063 * than one flag at once. The equivalent "inherited" flag is propagated to all
1064 * tracking servers. Leaving maintenance mode re-enables health checks. When
1065 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001066 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001067void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001068{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001069 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001070
1071 if (!mode)
1072 return;
1073
1074 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001075 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001076 return;
1077
Emeric Brun52a91d32017-08-31 14:41:55 +02001078 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001079
Christopher Faulet5d42e092017-10-16 12:00:40 +02001080 srv_register_update(s);
1081
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001082 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001083 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1084 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001085 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001086
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001087 if (mode & SRV_ADMF_MAINT)
1088 mode = SRV_ADMF_IMAINT;
1089 else if (mode & SRV_ADMF_DRAIN)
1090 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001091
Emeric Brun9f0b4582017-10-23 14:39:51 +02001092 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001093 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001094 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001095 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001096 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001097}
1098
Willy Tarreau757478e2016-11-03 19:22:19 +01001099/* principle: propagate maint and drain to tracking servers. This is useful
1100 * upon startup so that inherited states are correct.
1101 */
1102static void srv_propagate_admin_state(struct server *srv)
1103{
1104 struct server *srv2;
1105
1106 if (!srv->trackers)
1107 return;
1108
1109 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001110 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001111 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001112 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001113
Emeric Brun52a91d32017-08-31 14:41:55 +02001114 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001115 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001116 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001117 }
1118}
1119
1120/* Compute and propagate the admin states for all servers in proxy <px>.
1121 * Only servers *not* tracking another one are considered, because other
1122 * ones will be handled when the server they track is visited.
1123 */
1124void srv_compute_all_admin_states(struct proxy *px)
1125{
1126 struct server *srv;
1127
1128 for (srv = px->srv; srv; srv = srv->next) {
1129 if (srv->track)
1130 continue;
1131 srv_propagate_admin_state(srv);
1132 }
1133}
1134
Willy Tarreaudff55432012-10-10 17:51:05 +02001135/* Note: must not be declared <const> as its list will be overwritten.
1136 * Please take care of keeping this list alphabetically sorted, doing so helps
1137 * all code contributors.
1138 * Optional keywords are also declared with a NULL ->parse() function so that
1139 * the config parser can report an appropriate error when a known keyword was
1140 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001141 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001142 */
1143static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001144 { "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 +01001145 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001146 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001147 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001148 { "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 +01001149 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001150 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1151 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001152 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001153 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001154 { "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 +01001155 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001156 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001157 { "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 +01001158 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1159 { "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 +01001160 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001161 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001162 { "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 +01001163 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001164 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1165 { "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 +02001166 { "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 +01001167 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001168 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001169 { NULL, NULL, 0 },
1170}};
1171
1172__attribute__((constructor))
1173static void __listener_init(void)
1174{
1175 srv_register_keywords(&srv_kws);
1176}
1177
Willy Tarreau004e0452013-11-21 11:22:01 +01001178/* Recomputes the server's eweight based on its state, uweight, the current time,
1179 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1180 * state is automatically disabled if the time is elapsed.
1181 */
1182void server_recalc_eweight(struct server *sv)
1183{
1184 struct proxy *px = sv->proxy;
1185 unsigned w;
1186
1187 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1188 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001189 if (sv->next_state == SRV_ST_STARTING)
1190 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001191 }
1192
1193 /* We must take care of not pushing the server to full throttle during slow starts.
1194 * It must also start immediately, at least at the minimal step when leaving maintenance.
1195 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001196 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001197 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1198 else
1199 w = px->lbprm.wdiv;
1200
Emeric Brun52a91d32017-08-31 14:41:55 +02001201 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001202
Christopher Faulet5d42e092017-10-16 12:00:40 +02001203 srv_register_update(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001204}
1205
Willy Tarreaubaaee002006-06-26 02:48:02 +02001206/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001207 * Parses weight_str and configures sv accordingly.
1208 * Returns NULL on success, error message string otherwise.
1209 */
1210const char *server_parse_weight_change_request(struct server *sv,
1211 const char *weight_str)
1212{
1213 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001214 long int w;
1215 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001216
1217 px = sv->proxy;
1218
1219 /* if the weight is terminated with '%', it is set relative to
1220 * the initial weight, otherwise it is absolute.
1221 */
1222 if (!*weight_str)
1223 return "Require <weight> or <weight%>.\n";
1224
Simon Hormanb796afa2013-02-12 10:45:53 +09001225 w = strtol(weight_str, &end, 10);
1226 if (end == weight_str)
1227 return "Empty weight string empty or preceded by garbage";
1228 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001229 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001230 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001231 /* Avoid integer overflow */
1232 if (w > 25600)
1233 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001234 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001235 if (w > 256)
1236 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001237 }
1238 else if (w < 0 || w > 256)
1239 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001240 else if (end[0] != '\0')
1241 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001242
1243 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1244 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1245
1246 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001247 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001248
1249 return NULL;
1250}
1251
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001252/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001253 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1254 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001255 * Returns:
1256 * - error string on error
1257 * - NULL on success
1258 */
1259const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001260 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001261{
1262 unsigned char ip[INET6_ADDRSTRLEN];
1263
1264 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001265 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001266 return NULL;
1267 }
1268 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001269 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001270 return NULL;
1271 }
1272
1273 return "Could not understand IP address format.\n";
1274}
1275
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001276const char *server_parse_maxconn_change_request(struct server *sv,
1277 const char *maxconn_str)
1278{
1279 long int v;
1280 char *end;
1281
1282 if (!*maxconn_str)
1283 return "Require <maxconn>.\n";
1284
1285 v = strtol(maxconn_str, &end, 10);
1286 if (end == maxconn_str)
1287 return "maxconn string empty or preceded by garbage";
1288 else if (end[0] != '\0')
1289 return "Trailing garbage in maxconn string";
1290
1291 if (sv->maxconn == sv->minconn) { // static maxconn
1292 sv->maxconn = sv->minconn = v;
1293 } else { // dynamic maxconn
1294 sv->maxconn = v;
1295 }
1296
1297 if (may_dequeue_tasks(sv, sv->proxy))
1298 process_srv_queue(sv);
1299
1300 return NULL;
1301}
1302
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001303#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001304static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1305 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001306{
1307 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001308 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001309 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001310 NULL,
1311 };
1312
1313 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001314 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001315
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001316 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1317}
1318
1319static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1320{
1321 struct sample_expr *expr;
1322
1323 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 +01001324 if (!expr) {
1325 memprintf(err, "error detected while parsing sni expression : %s", *err);
1326 return ERR_ALERT | ERR_FATAL;
1327 }
1328
1329 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1330 memprintf(err, "error detected while parsing sni expression : "
1331 " fetch method '%s' extracts information from '%s', "
1332 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001333 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001334 return ERR_ALERT | ERR_FATAL;
1335 }
1336
1337 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1338 release_sample_expr(newsrv->ssl_ctx.sni);
1339 newsrv->ssl_ctx.sni = expr;
1340
1341 return 0;
1342}
1343#endif
1344
1345static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1346{
1347 if (err && *err) {
1348 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001349 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 +01001350 }
1351 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001352 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1353 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001354}
1355
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001356static void srv_conn_src_sport_range_cpy(struct server *srv,
1357 struct server *src)
1358{
1359 int range_sz;
1360
1361 range_sz = src->conn_src.sport_range->size;
1362 if (range_sz > 0) {
1363 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1364 if (srv->conn_src.sport_range != NULL) {
1365 int i;
1366
1367 for (i = 0; i < range_sz; i++) {
1368 srv->conn_src.sport_range->ports[i] =
1369 src->conn_src.sport_range->ports[i];
1370 }
1371 }
1372 }
1373}
1374
1375/*
1376 * Copy <src> server connection source settings to <srv> server everything needed.
1377 */
1378static void srv_conn_src_cpy(struct server *srv, struct server *src)
1379{
1380 srv->conn_src.opts = src->conn_src.opts;
1381 srv->conn_src.source_addr = src->conn_src.source_addr;
1382
1383 /* Source port range copy. */
1384 if (src->conn_src.sport_range != NULL)
1385 srv_conn_src_sport_range_cpy(srv, src);
1386
1387#ifdef CONFIG_HAP_TRANSPARENT
1388 if (src->conn_src.bind_hdr_name != NULL) {
1389 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1390 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1391 }
1392 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1393 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1394#endif
1395 if (src->conn_src.iface_name != NULL)
1396 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1397}
1398
1399/*
1400 * Copy <src> server SSL settings to <srv> server allocating
1401 * everything needed.
1402 */
1403#if defined(USE_OPENSSL)
1404static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1405{
1406 if (src->ssl_ctx.ca_file != NULL)
1407 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1408 if (src->ssl_ctx.crl_file != NULL)
1409 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1410 if (src->ssl_ctx.client_crt != NULL)
1411 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1412
1413 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1414
1415 if (src->ssl_ctx.verify_host != NULL)
1416 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1417 if (src->ssl_ctx.ciphers != NULL)
1418 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1419 if (src->sni_expr != NULL)
1420 srv->sni_expr = strdup(src->sni_expr);
1421}
1422#endif
1423
1424/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001425 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001426 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001427 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001428 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001429static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001430{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001431 char *hostname_dn;
1432 int hostname_len, hostname_dn_len;
1433
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001434 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001435 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001436
Christopher Faulet67957bd2017-09-27 11:00:59 +02001437 hostname_len = strlen(hostname);
1438 hostname_dn = trash.str;
1439 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1440 hostname_dn, trash.size);
1441 if (hostname_dn_len == -1)
1442 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001443
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001444
Christopher Faulet67957bd2017-09-27 11:00:59 +02001445 free(srv->hostname);
1446 free(srv->hostname_dn);
1447 srv->hostname = strdup(hostname);
1448 srv->hostname_dn = strdup(hostname_dn);
1449 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001450 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001451 goto err;
1452
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001453 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001454
1455 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001456 free(srv->hostname); srv->hostname = NULL;
1457 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001458 return -1;
1459}
1460
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001461/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001462 * Copy <src> server settings to <srv> server allocating
1463 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001464 * This function is not supposed to be called at any time, but only
1465 * during server settings parsing or during server allocations from
1466 * a server template, and just after having calloc()'ed a new server.
1467 * So, <src> may only be a default server (when parsing server settings)
1468 * or a server template (during server allocations from a server template).
1469 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1470 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001471 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001472static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001473{
1474 /* Connection source settings copy */
1475 srv_conn_src_cpy(srv, src);
1476
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001477 if (srv_tmpl) {
1478 srv->addr = src->addr;
1479 srv->svc_port = src->svc_port;
1480 }
1481
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001482 srv->pp_opts = src->pp_opts;
1483 if (src->rdr_pfx != NULL) {
1484 srv->rdr_pfx = strdup(src->rdr_pfx);
1485 srv->rdr_len = src->rdr_len;
1486 }
1487 if (src->cookie != NULL) {
1488 srv->cookie = strdup(src->cookie);
1489 srv->cklen = src->cklen;
1490 }
1491 srv->use_ssl = src->use_ssl;
1492 srv->check.addr = srv->agent.addr = src->check.addr;
1493 srv->check.use_ssl = src->check.use_ssl;
1494 srv->check.port = src->check.port;
1495 /* Note: 'flags' field has potentially been already initialized. */
1496 srv->flags |= src->flags;
1497 srv->do_check = src->do_check;
1498 srv->do_agent = src->do_agent;
1499 if (srv->check.port)
1500 srv->flags |= SRV_F_CHECKPORT;
1501 srv->check.inter = src->check.inter;
1502 srv->check.fastinter = src->check.fastinter;
1503 srv->check.downinter = src->check.downinter;
1504 srv->agent.use_ssl = src->agent.use_ssl;
1505 srv->agent.port = src->agent.port;
1506 if (src->agent.send_string != NULL)
1507 srv->agent.send_string = strdup(src->agent.send_string);
1508 srv->agent.send_string_len = src->agent.send_string_len;
1509 srv->agent.inter = src->agent.inter;
1510 srv->agent.fastinter = src->agent.fastinter;
1511 srv->agent.downinter = src->agent.downinter;
1512 srv->maxqueue = src->maxqueue;
1513 srv->minconn = src->minconn;
1514 srv->maxconn = src->maxconn;
1515 srv->slowstart = src->slowstart;
1516 srv->observe = src->observe;
1517 srv->onerror = src->onerror;
1518 srv->onmarkeddown = src->onmarkeddown;
1519 srv->onmarkedup = src->onmarkedup;
1520 if (src->trackit != NULL)
1521 srv->trackit = strdup(src->trackit);
1522 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1523 srv->uweight = srv->iweight = src->iweight;
1524
1525 srv->check.send_proxy = src->check.send_proxy;
1526 /* health: up, but will fall down at first failure */
1527 srv->check.rise = srv->check.health = src->check.rise;
1528 srv->check.fall = src->check.fall;
1529
1530 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001531 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1532 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1533 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001534 srv->check.state |= CHK_ST_PAUSED;
1535 srv->check.health = 0;
1536 }
1537
1538 /* health: up but will fall down at first failure */
1539 srv->agent.rise = srv->agent.health = src->agent.rise;
1540 srv->agent.fall = src->agent.fall;
1541
1542 if (src->resolvers_id != NULL)
1543 srv->resolvers_id = strdup(src->resolvers_id);
1544 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1545 if (srv->dns_opts.family_prio == AF_UNSPEC)
1546 srv->dns_opts.family_prio = AF_INET6;
1547 memcpy(srv->dns_opts.pref_net,
1548 src->dns_opts.pref_net,
1549 sizeof srv->dns_opts.pref_net);
1550 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1551
1552 srv->init_addr_methods = src->init_addr_methods;
1553 srv->init_addr = src->init_addr;
1554#if defined(USE_OPENSSL)
1555 srv_ssl_settings_cpy(srv, src);
1556#endif
1557#ifdef TCP_USER_TIMEOUT
1558 srv->tcp_ut = src->tcp_ut;
1559#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001560 if (srv_tmpl)
1561 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001562}
1563
1564static struct server *new_server(struct proxy *proxy)
1565{
1566 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001567 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001568
1569 srv = calloc(1, sizeof *srv);
1570 if (!srv)
1571 return NULL;
1572
1573 srv->obj_type = OBJ_TYPE_SERVER;
1574 srv->proxy = proxy;
1575 LIST_INIT(&srv->actconns);
1576 LIST_INIT(&srv->pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02001577
1578 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1579 goto free_srv;
1580 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1581 goto free_priv_conns;
1582 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1583 goto free_idle_conns;
1584
1585 for (i = 0; i < global.nbthread; i++) {
1586 LIST_INIT(&srv->priv_conns[i]);
1587 LIST_INIT(&srv->idle_conns[i]);
1588 LIST_INIT(&srv->safe_conns[i]);
1589 }
1590
Emeric Brun64cc49c2017-10-03 14:46:45 +02001591 LIST_INIT(&srv->update_status);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001592
Emeric Brun52a91d32017-08-31 14:41:55 +02001593 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001594 srv->last_change = now.tv_sec;
1595
1596 srv->check.status = HCHK_STATUS_INI;
1597 srv->check.server = srv;
1598 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1599
1600 srv->agent.status = HCHK_STATUS_INI;
1601 srv->agent.server = srv;
1602 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1603
1604 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001605
1606 free_idle_conns:
1607 free(srv->idle_conns);
1608 free_priv_conns:
1609 free(srv->priv_conns);
1610 free_srv:
1611 free(srv);
1612 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001613}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001614
1615/*
1616 * Validate <srv> server health-check settings.
1617 * Returns 0 if everything is OK, -1 if not.
1618 */
1619static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1620{
1621 struct tcpcheck_rule *r = NULL;
1622 struct list *l;
1623
1624 /*
1625 * We need at least a service port, a check port or the first tcp-check rule must
1626 * be a 'connect' one when checking an IPv4/IPv6 server.
1627 */
1628 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1629 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1630 return 0;
1631
1632 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1633 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001634 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1635 "Check has been disabled.\n",
1636 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001637 return -1;
1638 }
1639
1640 /* search the first action (connect / send / expect) in the list */
1641 l = &srv->proxy->tcpcheck_rules;
1642 list_for_each_entry(r, l, list) {
1643 if (r->action != TCPCHK_ACT_COMMENT)
1644 break;
1645 }
1646
1647 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001648 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1649 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1650 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001651 return -1;
1652 }
1653
1654 /* scan the tcp-check ruleset to ensure a port has been configured */
1655 l = &srv->proxy->tcpcheck_rules;
1656 list_for_each_entry(r, l, list) {
1657 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001658 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1659 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1660 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001661 return -1;
1662 }
1663 }
1664
1665 return 0;
1666}
1667
1668/*
1669 * Initialize <srv> health-check structure.
1670 * Returns the error string in case of memory allocation failure, NULL if not.
1671 */
1672static const char *do_health_check_init(struct server *srv, int check_type, int state)
1673{
1674 const char *ret;
1675
1676 if (!srv->do_check)
1677 return NULL;
1678
1679 ret = init_check(&srv->check, check_type);
1680 if (ret)
1681 return ret;
1682
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001683 srv->check.state |= state;
1684 global.maxsock++;
1685
1686 return NULL;
1687}
1688
1689static int server_health_check_init(const char *file, int linenum,
1690 struct server *srv, struct proxy *curproxy)
1691{
1692 const char *ret;
1693
1694 if (!srv->do_check)
1695 return 0;
1696
1697 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001698 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1699 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001700 return ERR_ALERT | ERR_FATAL;
1701 }
1702
1703 if (server_healthcheck_validate(file, linenum, srv) < 0)
1704 return ERR_ALERT | ERR_ABORT;
1705
1706 /* note: check type will be set during the config review phase */
1707 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1708 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001709 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001710 return ERR_ALERT | ERR_ABORT;
1711 }
1712
1713 return 0;
1714}
1715
1716/*
1717 * Initialize <srv> agent check structure.
1718 * Returns the error string in case of memory allocation failure, NULL if not.
1719 */
1720static const char *do_server_agent_check_init(struct server *srv, int state)
1721{
1722 const char *ret;
1723
1724 if (!srv->do_agent)
1725 return NULL;
1726
1727 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1728 if (ret)
1729 return ret;
1730
1731 if (!srv->agent.inter)
1732 srv->agent.inter = srv->check.inter;
1733
1734 srv->agent.state |= state;
1735 global.maxsock++;
1736
1737 return NULL;
1738}
1739
1740static int server_agent_check_init(const char *file, int linenum,
1741 struct server *srv, struct proxy *curproxy)
1742{
1743 const char *ret;
1744
1745 if (!srv->do_agent)
1746 return 0;
1747
1748 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001749 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 +02001750 file, linenum, srv->id);
1751 return ERR_ALERT | ERR_FATAL;
1752 }
1753
1754 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1755 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001756 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001757 return ERR_ALERT | ERR_ABORT;
1758 }
1759
1760 return 0;
1761}
1762
1763#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1764static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1765 struct server *srv, struct proxy *proxy)
1766{
1767 int ret;
1768 char *err = NULL;
1769
1770 if (!srv->sni_expr)
1771 return 0;
1772
1773 ret = server_parse_sni_expr(srv, proxy, &err);
1774 if (!ret)
1775 return 0;
1776
1777 display_parser_err(file, linenum, args, cur_arg, &err);
1778 free(err);
1779
1780 return ret;
1781}
1782#endif
1783
1784/*
1785 * Server initializations finalization.
1786 * Initialize health check, agent check and SNI expression if enabled.
1787 * Must not be called for a default server instance.
1788 */
1789static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1790 struct server *srv, struct proxy *px)
1791{
1792 int ret;
1793
1794 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1795 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1796 return ret;
1797 }
1798
1799#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1800 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1801 return ret;
1802#endif
1803
1804 if (srv->flags & SRV_F_BACKUP)
1805 px->srv_bck++;
1806 else
1807 px->srv_act++;
1808 srv_lb_commit_status(srv);
1809
1810 return 0;
1811}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001812
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001813/*
1814 * Parse as much as possible such a range string argument: low[-high]
1815 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1816 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1817 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1818 * Returns 0 if succeeded, -1 if not.
1819 */
1820static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1821{
1822 char *nb_high_arg;
1823
1824 *nb_high = 0;
1825 chunk_printf(&trash, "%s", arg);
1826 *nb_low = atoi(trash.str);
1827
1828 if ((nb_high_arg = strchr(trash.str, '-'))) {
1829 *nb_high_arg++ = '\0';
1830 *nb_high = atoi(nb_high_arg);
1831 }
1832 else {
1833 *nb_high += *nb_low;
1834 *nb_low = 1;
1835 }
1836
1837 if (*nb_low < 0 || *nb_high < *nb_low)
1838 return -1;
1839
1840 return 0;
1841}
1842
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001843static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1844{
1845 chunk_printf(&trash, "%s%d", prefix, nb);
1846 free(srv->id);
1847 srv->id = strdup(trash.str);
1848}
1849
1850/*
1851 * Initialize as much as possible servers from <srv> server template.
1852 * Note that a server template is a special server with
1853 * a few different parameters than a server which has
1854 * been parsed mostly the same way as a server.
1855 * Returns the number of servers succesfully allocated,
1856 * 'srv' template included.
1857 */
1858static int server_template_init(struct server *srv, struct proxy *px)
1859{
1860 int i;
1861 struct server *newsrv;
1862
1863 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1864 int check_init_state;
1865 int agent_init_state;
1866
1867 newsrv = new_server(px);
1868 if (!newsrv)
1869 goto err;
1870
1871 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001872 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001873#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1874 if (newsrv->sni_expr) {
1875 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1876 if (!newsrv->ssl_ctx.sni)
1877 goto err;
1878 }
1879#endif
1880 /* Set this new server ID. */
1881 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1882
1883 /* Initial checks states. */
1884 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1885 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1886
1887 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1888 do_server_agent_check_init(newsrv, agent_init_state))
1889 goto err;
1890
1891 /* Linked backwards first. This will be restablished after parsing. */
1892 newsrv->next = px->srv;
1893 px->srv = newsrv;
1894 }
1895 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1896
1897 return i - srv->tmpl_info.nb_low;
1898
1899 err:
1900 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1901 if (newsrv) {
1902#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1903 release_sample_expr(newsrv->ssl_ctx.sni);
1904#endif
1905 free_check(&newsrv->agent);
1906 free_check(&newsrv->check);
1907 }
1908 free(newsrv);
1909 return i - srv->tmpl_info.nb_low;
1910}
1911
Willy Tarreau272adea2014-03-31 10:39:59 +02001912int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1913{
1914 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001915 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001916 char *errmsg = NULL;
1917 int err_code = 0;
1918 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001919 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001920
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001921 if (!strcmp(args[0], "server") ||
1922 !strcmp(args[0], "default-server") ||
1923 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001924 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001925 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001926 int srv = !defsrv && !strcmp(args[0], "server");
1927 int srv_tmpl = !defsrv && !srv;
1928 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001929
1930 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001931 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001932 err_code |= ERR_ALERT | ERR_FATAL;
1933 goto out;
1934 }
1935 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1936 err_code |= ERR_ALERT | ERR_FATAL;
1937
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001938 /* There is no mandatory first arguments for default server. */
1939 if (srv) {
1940 if (!*args[2]) {
1941 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001942 ha_alert("parsing [%s:%d] : '%s' expects <name> and <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_char(args[1]);
1949 }
1950 else if (srv_tmpl) {
1951 if (!*args[3]) {
1952 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001953 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 +02001954 file, linenum, args[0]);
1955 err_code |= ERR_ALERT | ERR_FATAL;
1956 goto out;
1957 }
1958
1959 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001960 }
1961
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001962 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001963 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 +02001964 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001965 err_code |= ERR_ALERT | ERR_FATAL;
1966 goto out;
1967 }
1968
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001969 cur_arg = 2;
1970 if (srv_tmpl) {
1971 /* Parse server-template <nb | range> arg. */
1972 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001973 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001974 file, linenum, args[0], args[cur_arg]);
1975 err_code |= ERR_ALERT | ERR_FATAL;
1976 goto out;
1977 }
1978 cur_arg++;
1979 }
1980
Willy Tarreau272adea2014-03-31 10:39:59 +02001981 if (!defsrv) {
1982 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001983 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001984 struct protocol *proto;
1985
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001986 newsrv = new_server(curproxy);
1987 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001988 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02001989 err_code |= ERR_ALERT | ERR_ABORT;
1990 goto out;
1991 }
1992
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001993 if (srv_tmpl) {
1994 newsrv->tmpl_info.nb_low = tmpl_range_low;
1995 newsrv->tmpl_info.nb_high = tmpl_range_high;
1996 }
1997
Willy Tarreau272adea2014-03-31 10:39:59 +02001998 /* the servers are linked backwards first */
1999 newsrv->next = curproxy->srv;
2000 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002001 newsrv->conf.file = strdup(file);
2002 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002003 /* Note: for a server template, its id is its prefix.
2004 * This is a temporary id which will be used for server allocations to come
2005 * after parsing.
2006 */
2007 if (srv)
2008 newsrv->id = strdup(args[1]);
2009 else
2010 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002011
2012 /* several ways to check the port component :
2013 * - IP => port=+0, relative (IPv4 only)
2014 * - IP: => port=+0, relative
2015 * - IP:N => port=N, absolute
2016 * - IP:+N => port=+N, relative
2017 * - IP:-N => port=-N, relative
2018 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002019 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002020 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002021 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002022 err_code |= ERR_ALERT | ERR_FATAL;
2023 goto out;
2024 }
2025
2026 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002027 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002028 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002029 file, linenum, args[0], args[1]);
2030 err_code |= ERR_ALERT | ERR_FATAL;
2031 goto out;
2032 }
2033
2034 if (!port1 || !port2) {
2035 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002036 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002037 }
2038 else if (port1 != port2) {
2039 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002040 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002041 file, linenum, args[0], args[1], args[2]);
2042 err_code |= ERR_ALERT | ERR_FATAL;
2043 goto out;
2044 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002045
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002046 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002047 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002048 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002049 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002050 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2051 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2052 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002053 err_code |= ERR_ALERT | ERR_FATAL;
2054 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002055 }
2056 }
2057 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002058 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002059 file, linenum, newsrv->id);
2060 err_code |= ERR_ALERT | ERR_FATAL;
2061 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002062 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002063 }
2064
Willy Tarreau272adea2014-03-31 10:39:59 +02002065 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002066 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002067
Olivier Houchard8da5f982017-08-04 18:35:36 +02002068 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002069 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002070 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002071 err_code |= ERR_ALERT | ERR_FATAL;
2072 goto out;
2073 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002074
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002075 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002076 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002077 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002078 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002079 } else {
2080 newsrv = &curproxy->defsrv;
2081 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002082 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002083 }
2084
2085 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002086 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002087 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2088 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002089 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002090 file, linenum, *err, newsrv->id);
2091 err_code |= ERR_ALERT | ERR_FATAL;
2092 goto out;
2093 }
2094 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002095 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002096 file, linenum, val, args[cur_arg], newsrv->id);
2097 err_code |= ERR_ALERT | ERR_FATAL;
2098 goto out;
2099 }
2100 newsrv->agent.inter = val;
2101 cur_arg += 2;
2102 }
Misiekea849332017-01-09 09:39:51 +01002103 else if (!strcmp(args[cur_arg], "agent-addr")) {
2104 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002105 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002106 goto out;
2107 }
2108
2109 cur_arg += 2;
2110 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002111 else if (!strcmp(args[cur_arg], "agent-port")) {
2112 global.maxsock++;
2113 newsrv->agent.port = atol(args[cur_arg + 1]);
2114 cur_arg += 2;
2115 }
James Brown55f9ff12015-10-21 18:19:05 -07002116 else if (!strcmp(args[cur_arg], "agent-send")) {
2117 global.maxsock++;
2118 free(newsrv->agent.send_string);
2119 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2120 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2121 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2122 cur_arg += 2;
2123 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002124 else if (!strcmp(args[cur_arg], "init-addr")) {
2125 char *p, *end;
2126 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002127 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002128
2129 newsrv->init_addr_methods = 0;
2130 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2131
2132 for (p = args[cur_arg + 1]; *p; p = end) {
2133 /* cut on next comma */
2134 for (end = p; *end && *end != ','; end++);
2135 if (*end)
2136 *(end++) = 0;
2137
Willy Tarreau4310d362016-11-02 15:05:56 +01002138 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002139 if (!strcmp(p, "libc")) {
2140 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2141 }
2142 else if (!strcmp(p, "last")) {
2143 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2144 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002145 else if (!strcmp(p, "none")) {
2146 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2147 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002148 else if (str2ip2(p, &sa, 0)) {
2149 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002150 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002151 file, linenum, args[cur_arg], p);
2152 err_code |= ERR_ALERT | ERR_FATAL;
2153 goto out;
2154 }
2155 newsrv->init_addr = sa;
2156 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2157 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002158 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002159 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 +02002160 file, linenum, args[cur_arg], p);
2161 err_code |= ERR_ALERT | ERR_FATAL;
2162 goto out;
2163 }
2164 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002165 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002166 file, linenum, args[cur_arg], p);
2167 err_code |= ERR_ALERT | ERR_FATAL;
2168 goto out;
2169 }
2170 }
2171 cur_arg += 2;
2172 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002173 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002174 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002175 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2176 cur_arg += 2;
2177 }
2178 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2179 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002180 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002181 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002182 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002183 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002184 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002185 file, linenum, args[cur_arg]);
2186 err_code |= ERR_ALERT | ERR_FATAL;
2187 goto out;
2188 }
2189 cur_arg += 2;
2190 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002191 else if (!strcmp(args[cur_arg], "resolve-net")) {
2192 char *p, *e;
2193 unsigned char mask;
2194 struct dns_options *opt;
2195
2196 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002197 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002198 file, linenum, args[cur_arg]);
2199 err_code |= ERR_ALERT | ERR_FATAL;
2200 goto out;
2201 }
2202
2203 opt = &newsrv->dns_opts;
2204
2205 /* Split arguments by comma, and convert it from ipv4 or ipv6
2206 * string network in in_addr or in6_addr.
2207 */
2208 p = args[cur_arg + 1];
2209 e = p;
2210 while (*p != '\0') {
2211 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002212 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002213 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002214 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2215 err_code |= ERR_ALERT | ERR_FATAL;
2216 goto out;
2217 }
2218 /* look for end or comma. */
2219 while (*e != ',' && *e != '\0')
2220 e++;
2221 if (*e == ',') {
2222 *e = '\0';
2223 e++;
2224 }
2225 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2226 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2227 /* Try to convert input string from ipv4 or ipv6 network. */
2228 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2229 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2230 &mask)) {
2231 /* Try to convert input string from ipv6 network. */
2232 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2233 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2234 } else {
2235 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002236 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002237 file, linenum, args[cur_arg], p);
2238 err_code |= ERR_ALERT | ERR_FATAL;
2239 goto out;
2240 }
2241 opt->pref_net_nb++;
2242 p = e;
2243 }
2244
2245 cur_arg += 2;
2246 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002247 else if (!strcmp(args[cur_arg], "rise")) {
2248 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002249 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002250 file, linenum, args[cur_arg]);
2251 err_code |= ERR_ALERT | ERR_FATAL;
2252 goto out;
2253 }
2254
2255 newsrv->check.rise = atol(args[cur_arg + 1]);
2256 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002257 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002258 file, linenum, args[cur_arg]);
2259 err_code |= ERR_ALERT | ERR_FATAL;
2260 goto out;
2261 }
2262
2263 if (newsrv->check.health)
2264 newsrv->check.health = newsrv->check.rise;
2265 cur_arg += 2;
2266 }
2267 else if (!strcmp(args[cur_arg], "fall")) {
2268 newsrv->check.fall = atol(args[cur_arg + 1]);
2269
2270 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002271 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002272 file, linenum, args[cur_arg]);
2273 err_code |= ERR_ALERT | ERR_FATAL;
2274 goto out;
2275 }
2276
2277 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002278 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002279 file, linenum, args[cur_arg]);
2280 err_code |= ERR_ALERT | ERR_FATAL;
2281 goto out;
2282 }
2283
2284 cur_arg += 2;
2285 }
2286 else if (!strcmp(args[cur_arg], "inter")) {
2287 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2288 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002289 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002290 file, linenum, *err, newsrv->id);
2291 err_code |= ERR_ALERT | ERR_FATAL;
2292 goto out;
2293 }
2294 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002295 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002296 file, linenum, val, args[cur_arg], newsrv->id);
2297 err_code |= ERR_ALERT | ERR_FATAL;
2298 goto out;
2299 }
2300 newsrv->check.inter = val;
2301 cur_arg += 2;
2302 }
2303 else if (!strcmp(args[cur_arg], "fastinter")) {
2304 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2305 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002306 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002307 file, linenum, *err, newsrv->id);
2308 err_code |= ERR_ALERT | ERR_FATAL;
2309 goto out;
2310 }
2311 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002312 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002313 file, linenum, val, args[cur_arg], newsrv->id);
2314 err_code |= ERR_ALERT | ERR_FATAL;
2315 goto out;
2316 }
2317 newsrv->check.fastinter = val;
2318 cur_arg += 2;
2319 }
2320 else if (!strcmp(args[cur_arg], "downinter")) {
2321 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2322 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002323 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002324 file, linenum, *err, newsrv->id);
2325 err_code |= ERR_ALERT | ERR_FATAL;
2326 goto out;
2327 }
2328 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002329 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002330 file, linenum, val, args[cur_arg], newsrv->id);
2331 err_code |= ERR_ALERT | ERR_FATAL;
2332 goto out;
2333 }
2334 newsrv->check.downinter = val;
2335 cur_arg += 2;
2336 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002337 else if (!strcmp(args[cur_arg], "port")) {
2338 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002339 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002340 cur_arg += 2;
2341 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002342 else if (!strcmp(args[cur_arg], "weight")) {
2343 int w;
2344 w = atol(args[cur_arg + 1]);
2345 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002346 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002347 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2348 err_code |= ERR_ALERT | ERR_FATAL;
2349 goto out;
2350 }
2351 newsrv->uweight = newsrv->iweight = w;
2352 cur_arg += 2;
2353 }
2354 else if (!strcmp(args[cur_arg], "minconn")) {
2355 newsrv->minconn = atol(args[cur_arg + 1]);
2356 cur_arg += 2;
2357 }
2358 else if (!strcmp(args[cur_arg], "maxconn")) {
2359 newsrv->maxconn = atol(args[cur_arg + 1]);
2360 cur_arg += 2;
2361 }
2362 else if (!strcmp(args[cur_arg], "maxqueue")) {
2363 newsrv->maxqueue = atol(args[cur_arg + 1]);
2364 cur_arg += 2;
2365 }
2366 else if (!strcmp(args[cur_arg], "slowstart")) {
2367 /* slowstart is stored in seconds */
2368 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2369 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002370 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002371 file, linenum, *err, newsrv->id);
2372 err_code |= ERR_ALERT | ERR_FATAL;
2373 goto out;
2374 }
2375 newsrv->slowstart = (val + 999) / 1000;
2376 cur_arg += 2;
2377 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002378 else if (!strcmp(args[cur_arg], "on-error")) {
2379 if (!strcmp(args[cur_arg + 1], "fastinter"))
2380 newsrv->onerror = HANA_ONERR_FASTINTER;
2381 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2382 newsrv->onerror = HANA_ONERR_FAILCHK;
2383 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2384 newsrv->onerror = HANA_ONERR_SUDDTH;
2385 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2386 newsrv->onerror = HANA_ONERR_MARKDWN;
2387 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002388 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002389 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2390 file, linenum, args[cur_arg], args[cur_arg + 1]);
2391 err_code |= ERR_ALERT | ERR_FATAL;
2392 goto out;
2393 }
2394
2395 cur_arg += 2;
2396 }
2397 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2398 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2399 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2400 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002401 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002402 file, linenum, args[cur_arg], args[cur_arg + 1]);
2403 err_code |= ERR_ALERT | ERR_FATAL;
2404 goto out;
2405 }
2406
2407 cur_arg += 2;
2408 }
2409 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2410 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2411 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2412 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002413 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002414 file, linenum, args[cur_arg], args[cur_arg + 1]);
2415 err_code |= ERR_ALERT | ERR_FATAL;
2416 goto out;
2417 }
2418
2419 cur_arg += 2;
2420 }
2421 else if (!strcmp(args[cur_arg], "error-limit")) {
2422 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002423 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002424 file, linenum, args[cur_arg]);
2425 err_code |= ERR_ALERT | ERR_FATAL;
2426 goto out;
2427 }
2428
2429 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2430
2431 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002432 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002433 file, linenum, args[cur_arg]);
2434 err_code |= ERR_ALERT | ERR_FATAL;
2435 goto out;
2436 }
2437 cur_arg += 2;
2438 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002439 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002440 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002441 file, linenum, "usesrc", "source");
2442 err_code |= ERR_ALERT | ERR_FATAL;
2443 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002444 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002445 else {
2446 static int srv_dumped;
2447 struct srv_kw *kw;
2448 char *err;
2449
2450 kw = srv_find_kw(args[cur_arg]);
2451 if (kw) {
2452 char *err = NULL;
2453 int code;
2454
2455 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002456 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 +02002457 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002458 if (kw->skip != -1)
2459 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002460 err_code |= ERR_ALERT | ERR_FATAL;
2461 goto out;
2462 }
2463
2464 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002465 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002466 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002467 if (kw->skip != -1)
2468 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002469 err_code |= ERR_ALERT;
2470 continue;
2471 }
2472
2473 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2474 err_code |= code;
2475
2476 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002477 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002478 if (code & ERR_FATAL) {
2479 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002480 if (kw->skip != -1)
2481 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002482 goto out;
2483 }
2484 }
2485 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002486 if (kw->skip != -1)
2487 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002488 continue;
2489 }
2490
2491 err = NULL;
2492 if (!srv_dumped) {
2493 srv_dump_kws(&err);
2494 indent_msg(&err, 4);
2495 srv_dumped = 1;
2496 }
2497
Christopher Faulet767a84b2017-11-24 16:50:31 +01002498 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002499 file, linenum, args[0], args[1], args[cur_arg],
2500 err ? " Registered keywords :" : "", err ? err : "");
2501 free(err);
2502
2503 err_code |= ERR_ALERT | ERR_FATAL;
2504 goto out;
2505 }
2506 }
2507
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002508 if (!defsrv)
2509 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2510 if (err_code & ERR_FATAL)
2511 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002512 if (srv_tmpl)
2513 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002514 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002515 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002516 return 0;
2517
2518 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002519 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002520 free(errmsg);
2521 return err_code;
2522}
2523
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002524/* Returns a pointer to the first server matching either id <id>.
2525 * NULL is returned if no match is found.
2526 * the lookup is performed in the backend <bk>
2527 */
2528struct server *server_find_by_id(struct proxy *bk, int id)
2529{
2530 struct eb32_node *eb32;
2531 struct server *curserver;
2532
2533 if (!bk || (id ==0))
2534 return NULL;
2535
2536 /* <bk> has no backend capabilities, so it can't have a server */
2537 if (!(bk->cap & PR_CAP_BE))
2538 return NULL;
2539
2540 curserver = NULL;
2541
2542 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2543 if (eb32)
2544 curserver = container_of(eb32, struct server, conf.id);
2545
2546 return curserver;
2547}
2548
2549/* Returns a pointer to the first server matching either name <name>, or id
2550 * if <name> starts with a '#'. NULL is returned if no match is found.
2551 * the lookup is performed in the backend <bk>
2552 */
2553struct server *server_find_by_name(struct proxy *bk, const char *name)
2554{
2555 struct server *curserver;
2556
2557 if (!bk || !name)
2558 return NULL;
2559
2560 /* <bk> has no backend capabilities, so it can't have a server */
2561 if (!(bk->cap & PR_CAP_BE))
2562 return NULL;
2563
2564 curserver = NULL;
2565 if (*name == '#') {
2566 curserver = server_find_by_id(bk, atoi(name + 1));
2567 if (curserver)
2568 return curserver;
2569 }
2570 else {
2571 curserver = bk->srv;
2572
2573 while (curserver && (strcmp(curserver->id, name) != 0))
2574 curserver = curserver->next;
2575
2576 if (curserver)
2577 return curserver;
2578 }
2579
2580 return NULL;
2581}
2582
2583struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2584{
2585 struct server *byname;
2586 struct server *byid;
2587
2588 if (!name && !id)
2589 return NULL;
2590
2591 if (diff)
2592 *diff = 0;
2593
2594 byname = byid = NULL;
2595
2596 if (name) {
2597 byname = server_find_by_name(bk, name);
2598 if (byname && (!id || byname->puid == id))
2599 return byname;
2600 }
2601
2602 /* remaining possibilities :
2603 * - name not set
2604 * - name set but not found
2605 * - name found but ID doesn't match
2606 */
2607 if (id) {
2608 byid = server_find_by_id(bk, id);
2609 if (byid) {
2610 if (byname) {
2611 /* use id only if forced by configuration */
2612 if (byid->flags & SRV_F_FORCED_ID) {
2613 if (diff)
2614 *diff |= 2;
2615 return byid;
2616 }
2617 else {
2618 if (diff)
2619 *diff |= 1;
2620 return byname;
2621 }
2622 }
2623
2624 /* remaining possibilities:
2625 * - name not set
2626 * - name set but not found
2627 */
2628 if (name && diff)
2629 *diff |= 2;
2630 return byid;
2631 }
2632
2633 /* id bot found */
2634 if (byname) {
2635 if (diff)
2636 *diff |= 1;
2637 return byname;
2638 }
2639 }
2640
2641 return NULL;
2642}
2643
Christopher Faulet5d42e092017-10-16 12:00:40 +02002644/* Registers changes to be applied asynchronously */
2645static void srv_register_update(struct server *srv)
2646{
2647 if (LIST_ISEMPTY(&srv->update_status)) {
2648 THREAD_WANT_SYNC();
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002649 HA_SPIN_LOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
Christopher Faulet5d42e092017-10-16 12:00:40 +02002650 if (LIST_ISEMPTY(&srv->update_status))
2651 LIST_ADDQ(&updated_servers, &srv->update_status);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002652 HA_SPIN_UNLOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
Christopher Faulet5d42e092017-10-16 12:00:40 +02002653 }
2654}
2655
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002656/* Update a server state using the parameters available in the params list */
2657static void srv_update_state(struct server *srv, int version, char **params)
2658{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002659 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002660 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002661
2662 /* fields since version 1
2663 * and common to all other upcoming versions
2664 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002665 enum srv_state srv_op_state;
2666 enum srv_admin srv_admin_state;
2667 unsigned srv_uweight, srv_iweight;
2668 unsigned long srv_last_time_change;
2669 short srv_check_status;
2670 enum chk_result srv_check_result;
2671 int srv_check_health;
2672 int srv_check_state, srv_agent_state;
2673 int bk_f_forced_id;
2674 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002675 int fqdn_set_by_cli;
2676 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002677 const char *port_str;
2678 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002679
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002680 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002681 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002682 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002683 switch (version) {
2684 case 1:
2685 /*
2686 * now we can proceed with server's state update:
2687 * srv_addr: params[0]
2688 * srv_op_state: params[1]
2689 * srv_admin_state: params[2]
2690 * srv_uweight: params[3]
2691 * srv_iweight: params[4]
2692 * srv_last_time_change: params[5]
2693 * srv_check_status: params[6]
2694 * srv_check_result: params[7]
2695 * srv_check_health: params[8]
2696 * srv_check_state: params[9]
2697 * srv_agent_state: params[10]
2698 * bk_f_forced_id: params[11]
2699 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002700 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002701 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002702 */
2703
2704 /* validating srv_op_state */
2705 p = NULL;
2706 errno = 0;
2707 srv_op_state = strtol(params[1], &p, 10);
2708 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2709 (srv_op_state != SRV_ST_STOPPED &&
2710 srv_op_state != SRV_ST_STARTING &&
2711 srv_op_state != SRV_ST_RUNNING &&
2712 srv_op_state != SRV_ST_STOPPING)) {
2713 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2714 }
2715
2716 /* validating srv_admin_state */
2717 p = NULL;
2718 errno = 0;
2719 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002720 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002721
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002722 /* inherited statuses will be recomputed later.
2723 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2724 */
2725 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002726
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002727 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2728 (srv_admin_state != 0 &&
2729 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002730 srv_admin_state != SRV_ADMF_CMAINT &&
2731 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002732 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002733 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002734 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2735 }
2736
2737 /* validating srv_uweight */
2738 p = NULL;
2739 errno = 0;
2740 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002741 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002742 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2743
2744 /* validating srv_iweight */
2745 p = NULL;
2746 errno = 0;
2747 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002748 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002749 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2750
2751 /* validating srv_last_time_change */
2752 p = NULL;
2753 errno = 0;
2754 srv_last_time_change = strtol(params[5], &p, 10);
2755 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2756 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2757
2758 /* validating srv_check_status */
2759 p = NULL;
2760 errno = 0;
2761 srv_check_status = strtol(params[6], &p, 10);
2762 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2763 (srv_check_status >= HCHK_STATUS_SIZE))
2764 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2765
2766 /* validating srv_check_result */
2767 p = NULL;
2768 errno = 0;
2769 srv_check_result = strtol(params[7], &p, 10);
2770 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2771 (srv_check_result != CHK_RES_UNKNOWN &&
2772 srv_check_result != CHK_RES_NEUTRAL &&
2773 srv_check_result != CHK_RES_FAILED &&
2774 srv_check_result != CHK_RES_PASSED &&
2775 srv_check_result != CHK_RES_CONDPASS)) {
2776 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2777 }
2778
2779 /* validating srv_check_health */
2780 p = NULL;
2781 errno = 0;
2782 srv_check_health = strtol(params[8], &p, 10);
2783 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2784 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2785
2786 /* validating srv_check_state */
2787 p = NULL;
2788 errno = 0;
2789 srv_check_state = strtol(params[9], &p, 10);
2790 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2791 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2792 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2793
2794 /* validating srv_agent_state */
2795 p = NULL;
2796 errno = 0;
2797 srv_agent_state = strtol(params[10], &p, 10);
2798 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2799 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2800 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2801
2802 /* validating bk_f_forced_id */
2803 p = NULL;
2804 errno = 0;
2805 bk_f_forced_id = strtol(params[11], &p, 10);
2806 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2807 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2808
2809 /* validating srv_f_forced_id */
2810 p = NULL;
2811 errno = 0;
2812 srv_f_forced_id = strtol(params[12], &p, 10);
2813 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2814 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2815
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002816 /* validating srv_fqdn */
2817 fqdn = params[13];
2818 if (fqdn && *fqdn == '-')
2819 fqdn = NULL;
2820 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2821 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2822 fqdn = NULL;
2823 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002824
Frédéric Lécaille31694712017-08-01 08:47:19 +02002825 port_str = params[14];
2826 if (port_str) {
2827 port = strl2uic(port_str, strlen(port_str));
2828 if (port > USHRT_MAX) {
2829 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2830 port_str = NULL;
2831 }
2832 }
2833
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002834 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002835 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002836 goto out;
2837
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002838 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002839 /* recover operational state and apply it to this server
2840 * and all servers tracking this one */
2841 switch (srv_op_state) {
2842 case SRV_ST_STOPPED:
2843 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002844 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002845 break;
2846 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002847 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002848 break;
2849 case SRV_ST_STOPPING:
2850 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002851 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002852 break;
2853 case SRV_ST_RUNNING:
2854 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002855 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002856 break;
2857 }
2858
2859 /* When applying server state, the following rules apply:
2860 * - in case of a configuration change, we apply the setting from the new
2861 * configuration, regardless of old running state
2862 * - if no configuration change, we apply old running state only if old running
2863 * state is different from new configuration state
2864 */
2865 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002866 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2867 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002868 srv_adm_set_maint(srv);
2869 else
2870 srv_adm_set_ready(srv);
2871 }
2872 /* configuration is the same, let's compate old running state and new conf state */
2873 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002874 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002875 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002876 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002877 srv_adm_set_ready(srv);
2878 }
2879 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002880 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002881 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002882 * (srv->iweight is the weight set up in configuration).
2883 * There are two possible reasons for FDRAIN to have been present :
2884 * - previous config weight was zero
2885 * - "set server b/s drain" was sent to the CLI
2886 *
2887 * In the first case, we simply want to drop this drain state
2888 * if the new weight is not zero anymore, meaning the administrator
2889 * has intentionally turned the weight back to a positive value to
2890 * enable the server again after an operation. In the second case,
2891 * the drain state was forced on the CLI regardless of the config's
2892 * weight so we don't want a change to the config weight to lose this
2893 * status. What this means is :
2894 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2895 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002896 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002897 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002898 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002899 }
2900
2901 srv->last_change = date.tv_sec - srv_last_time_change;
2902 srv->check.status = srv_check_status;
2903 srv->check.result = srv_check_result;
2904 srv->check.health = srv_check_health;
2905
2906 /* Only case we want to apply is removing ENABLED flag which could have been
2907 * done by the "disable health" command over the stats socket
2908 */
2909 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2910 (srv_check_state & CHK_ST_CONFIGURED) &&
2911 !(srv_check_state & CHK_ST_ENABLED))
2912 srv->check.state &= ~CHK_ST_ENABLED;
2913
2914 /* Only case we want to apply is removing ENABLED flag which could have been
2915 * done by the "disable agent" command over the stats socket
2916 */
2917 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2918 (srv_agent_state & CHK_ST_CONFIGURED) &&
2919 !(srv_agent_state & CHK_ST_ENABLED))
2920 srv->agent.state &= ~CHK_ST_ENABLED;
2921
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002922 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2923 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002924 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002925 * It means that a configuration file change has precedence over a unix socket change
2926 * for server's weight
2927 *
2928 * by default, HAProxy applies the following weight when parsing the configuration
2929 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002930 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002931 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002932 srv->uweight = srv_uweight;
2933 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002934 server_recalc_eweight(srv);
2935
Willy Tarreaue5a60682016-11-09 14:54:53 +01002936 /* load server IP address */
2937 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002938
2939 if (fqdn && srv->hostname) {
2940 if (!strcmp(srv->hostname, fqdn)) {
2941 /* Here we reset the 'set from stats socket FQDN' flag
2942 * to support such transitions:
2943 * Let's say initial FQDN value is foo1 (in configuration file).
2944 * - FQDN changed from stats socket, from foo1 to foo2 value,
2945 * - FQDN changed again from file configuration (with the same previous value
2946 set from stats socket, from foo1 to foo2 value),
2947 * - reload for any other reason than a FQDN modification,
2948 * the configuration file FQDN matches the fqdn server state file value.
2949 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
2950 * any futher FQDN modification.
2951 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002952 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002953 }
2954 else {
2955 /* If the FDQN has been changed from stats socket,
2956 * apply fqdn state file value (which is the value set
2957 * from stats socket).
2958 */
2959 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002960 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02002961 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002962 }
2963 }
2964 }
2965
Frédéric Lécaille31694712017-08-01 08:47:19 +02002966 if (port_str)
2967 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002968 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002969
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002970 break;
2971 default:
2972 chunk_appendf(msg, ", version '%d' not supported", version);
2973 }
2974
2975 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002976 if (msg->len) {
2977 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01002978 ha_warning("server-state application failed for server '%s/%s'%s",
2979 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002980 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002981}
2982
2983/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2984 * For each proxy, it does the following:
2985 * - opens its server state file (either one or local one)
2986 * - read whole file, line by line
2987 * - analyse each line to check if it matches our current backend:
2988 * - backend name matches
2989 * - backend id matches if id is forced and name doesn't match
2990 * - if the server pointed by the line is found, then state is applied
2991 *
2992 * If the running backend uuid or id differs from the state file, then HAProxy reports
2993 * a warning.
2994 */
2995void apply_server_state(void)
2996{
2997 char *cur, *end;
2998 char mybuf[SRV_STATE_LINE_MAXLEN];
2999 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003000 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3001 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003002 int arg, srv_arg, version, diff;
3003 FILE *f;
3004 char *filepath;
3005 char globalfilepath[MAXPATHLEN + 1];
3006 char localfilepath[MAXPATHLEN + 1];
3007 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003008 struct proxy *curproxy, *bk;
3009 struct server *srv;
3010
3011 globalfilepathlen = 0;
3012 /* create the globalfilepath variable */
3013 if (global.server_state_file) {
3014 /* absolute path or no base directory provided */
3015 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3016 len = strlen(global.server_state_file);
3017 if (len > MAXPATHLEN) {
3018 globalfilepathlen = 0;
3019 goto globalfileerror;
3020 }
3021 memcpy(globalfilepath, global.server_state_file, len);
3022 globalfilepath[len] = '\0';
3023 globalfilepathlen = len;
3024 }
3025 else if (global.server_state_base) {
3026 len = strlen(global.server_state_base);
3027 globalfilepathlen += len;
3028
3029 if (globalfilepathlen > MAXPATHLEN) {
3030 globalfilepathlen = 0;
3031 goto globalfileerror;
3032 }
3033 strncpy(globalfilepath, global.server_state_base, len);
3034 globalfilepath[globalfilepathlen] = 0;
3035
3036 /* append a slash if needed */
3037 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3038 if (globalfilepathlen + 1 > MAXPATHLEN) {
3039 globalfilepathlen = 0;
3040 goto globalfileerror;
3041 }
3042 globalfilepath[globalfilepathlen++] = '/';
3043 }
3044
3045 len = strlen(global.server_state_file);
3046 if (globalfilepathlen + len > MAXPATHLEN) {
3047 globalfilepathlen = 0;
3048 goto globalfileerror;
3049 }
3050 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3051 globalfilepathlen += len;
3052 globalfilepath[globalfilepathlen++] = 0;
3053 }
3054 }
3055 globalfileerror:
3056 if (globalfilepathlen == 0)
3057 globalfilepath[0] = '\0';
3058
3059 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003060 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003061 /* servers are only in backends */
3062 if (!(curproxy->cap & PR_CAP_BE))
3063 continue;
3064 fileopenerr = 0;
3065 filepath = NULL;
3066
3067 /* search server state file path and name */
3068 switch (curproxy->load_server_state_from_file) {
3069 /* read servers state from global file */
3070 case PR_SRV_STATE_FILE_GLOBAL:
3071 /* there was an error while generating global server state file path */
3072 if (globalfilepathlen == 0)
3073 continue;
3074 filepath = globalfilepath;
3075 fileopenerr = 1;
3076 break;
3077 /* this backend has its own file */
3078 case PR_SRV_STATE_FILE_LOCAL:
3079 localfilepathlen = 0;
3080 localfilepath[0] = '\0';
3081 len = 0;
3082 /* create the localfilepath variable */
3083 /* absolute path or no base directory provided */
3084 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3085 len = strlen(curproxy->server_state_file_name);
3086 if (len > MAXPATHLEN) {
3087 localfilepathlen = 0;
3088 goto localfileerror;
3089 }
3090 memcpy(localfilepath, curproxy->server_state_file_name, len);
3091 localfilepath[len] = '\0';
3092 localfilepathlen = len;
3093 }
3094 else if (global.server_state_base) {
3095 len = strlen(global.server_state_base);
3096 localfilepathlen += len;
3097
3098 if (localfilepathlen > MAXPATHLEN) {
3099 localfilepathlen = 0;
3100 goto localfileerror;
3101 }
3102 strncpy(localfilepath, global.server_state_base, len);
3103 localfilepath[localfilepathlen] = 0;
3104
3105 /* append a slash if needed */
3106 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3107 if (localfilepathlen + 1 > MAXPATHLEN) {
3108 localfilepathlen = 0;
3109 goto localfileerror;
3110 }
3111 localfilepath[localfilepathlen++] = '/';
3112 }
3113
3114 len = strlen(curproxy->server_state_file_name);
3115 if (localfilepathlen + len > MAXPATHLEN) {
3116 localfilepathlen = 0;
3117 goto localfileerror;
3118 }
3119 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3120 localfilepathlen += len;
3121 localfilepath[localfilepathlen++] = 0;
3122 }
3123 filepath = localfilepath;
3124 localfileerror:
3125 if (localfilepathlen == 0)
3126 localfilepath[0] = '\0';
3127
3128 break;
3129 case PR_SRV_STATE_FILE_NONE:
3130 default:
3131 continue;
3132 }
3133
3134 /* preload global state file */
3135 errno = 0;
3136 f = fopen(filepath, "r");
3137 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003138 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003139 if (!f)
3140 continue;
3141
3142 mybuf[0] = '\0';
3143 mybuflen = 0;
3144 version = 0;
3145
3146 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003147 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003148 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003149 goto fileclose;
3150 }
3151
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003152 cur = mybuf;
3153 version = atoi(cur);
3154 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3155 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003156 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003157
3158 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3159 int bk_f_forced_id = 0;
3160 int check_id = 0;
3161 int check_name = 0;
3162
3163 mybuflen = strlen(mybuf);
3164 cur = mybuf;
3165 end = cur + mybuflen;
3166
3167 bk = NULL;
3168 srv = NULL;
3169
3170 /* we need at least one character */
3171 if (mybuflen == 0)
3172 continue;
3173
3174 /* ignore blank characters at the beginning of the line */
3175 while (isspace(*cur))
3176 ++cur;
3177
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003178 /* Ignore empty or commented lines */
3179 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003180 continue;
3181
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003182 /* truncated lines */
3183 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003184 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003185 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003186 }
3187
3188 /* Removes trailing '\n' */
3189 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003190
3191 /* we're now ready to move the line into *srv_params[] */
3192 params[0] = cur;
3193 arg = 1;
3194 srv_arg = 0;
3195 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3196 if (isspace(*cur)) {
3197 *cur = '\0';
3198 ++cur;
3199 while (isspace(*cur))
3200 ++cur;
3201 switch (version) {
3202 case 1:
3203 /*
3204 * srv_addr: params[4] => srv_params[0]
3205 * srv_op_state: params[5] => srv_params[1]
3206 * srv_admin_state: params[6] => srv_params[2]
3207 * srv_uweight: params[7] => srv_params[3]
3208 * srv_iweight: params[8] => srv_params[4]
3209 * srv_last_time_change: params[9] => srv_params[5]
3210 * srv_check_status: params[10] => srv_params[6]
3211 * srv_check_result: params[11] => srv_params[7]
3212 * srv_check_health: params[12] => srv_params[8]
3213 * srv_check_state: params[13] => srv_params[9]
3214 * srv_agent_state: params[14] => srv_params[10]
3215 * bk_f_forced_id: params[15] => srv_params[11]
3216 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003217 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003218 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003219 */
3220 if (arg >= 4) {
3221 srv_params[srv_arg] = cur;
3222 ++srv_arg;
3223 }
3224 break;
3225 }
3226
3227 params[arg] = cur;
3228 ++arg;
3229 }
3230 else {
3231 ++cur;
3232 }
3233 }
3234
3235 /* if line is incomplete line, then ignore it.
3236 * otherwise, update useful flags */
3237 switch (version) {
3238 case 1:
3239 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3240 continue;
3241 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3242 check_id = (atoi(params[0]) == curproxy->uuid);
3243 check_name = (strcmp(curproxy->id, params[1]) == 0);
3244 break;
3245 }
3246
3247 diff = 0;
3248 bk = curproxy;
3249
3250 /* if backend can't be found, let's continue */
3251 if (!check_id && !check_name)
3252 continue;
3253 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003254 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 +02003255 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3256 }
3257 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003258 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 +02003259 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3260 /* if name doesn't match, we still want to update curproxy if the backend id
3261 * was forced in previous the previous configuration */
3262 if (!bk_f_forced_id)
3263 continue;
3264 }
3265
3266 /* look for the server by its id: param[2] */
3267 /* else look for the server by its name: param[3] */
3268 diff = 0;
3269 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3270
3271 if (!srv) {
3272 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003273 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3274 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003275 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3276 params[3], params[2], params[0], params[1]);
3277 continue;
3278 }
3279 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003280 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 +02003281 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 +02003282 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003283 }
3284 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003285 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 +02003286 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 +02003287 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003288 }
3289
3290 /* now we can proceed with server's state update */
3291 srv_update_state(srv, version, srv_params);
3292 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003293fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003294 fclose(f);
3295 }
3296}
3297
Simon Horman7d09b9a2013-02-12 10:45:51 +09003298/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003299 * update a server's current IP address.
3300 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3301 * ip is in network format.
3302 * updater is a string which contains an information about the requester of the update.
3303 * updater is used if not NULL.
3304 *
3305 * A log line and a stderr warning message is generated based on server's backend options.
3306 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003307int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003308{
3309 /* generates a log line and a warning on stderr */
3310 if (1) {
3311 /* book enough space for both IPv4 and IPv6 */
3312 char oldip[INET6_ADDRSTRLEN];
3313 char newip[INET6_ADDRSTRLEN];
3314
3315 memset(oldip, '\0', INET6_ADDRSTRLEN);
3316 memset(newip, '\0', INET6_ADDRSTRLEN);
3317
3318 /* copy old IP address in a string */
3319 switch (s->addr.ss_family) {
3320 case AF_INET:
3321 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3322 break;
3323 case AF_INET6:
3324 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3325 break;
3326 };
3327
3328 /* copy new IP address in a string */
3329 switch (ip_sin_family) {
3330 case AF_INET:
3331 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3332 break;
3333 case AF_INET6:
3334 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3335 break;
3336 };
3337
3338 /* save log line into a buffer */
3339 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3340 s->proxy->id, s->id, oldip, newip, updater);
3341
3342 /* write the buffer on stderr */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003343 ha_warning("%s.\n", trash.str);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003344
3345 /* send a log */
3346 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3347 }
3348
3349 /* save the new IP family */
3350 s->addr.ss_family = ip_sin_family;
3351 /* save the new IP address */
3352 switch (ip_sin_family) {
3353 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003354 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003355 break;
3356 case AF_INET6:
3357 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3358 break;
3359 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003360 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003361
3362 return 0;
3363}
3364
3365/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003366 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3367 *
3368 * Caller can pass its name through <updater> to get it integrated in the response message
3369 * returned by the function.
3370 *
3371 * The function first does the following, in that order:
3372 * - validates the new addr and/or port
3373 * - checks if an update is required (new IP or port is different than current ones)
3374 * - checks the update is allowed:
3375 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3376 * - allow all changes if no CHECKS are configured
3377 * - if CHECK is configured:
3378 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3379 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3380 * conditions are met
3381 */
3382const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3383{
3384 struct sockaddr_storage sa;
3385 int ret, port_change_required;
3386 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003387 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003388 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003389 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003390
3391 msg = get_trash_chunk();
3392 chunk_reset(msg);
3393
3394 if (addr) {
3395 memset(&sa, 0, sizeof(struct sockaddr_storage));
3396 if (str2ip2(addr, &sa, 0) == NULL) {
3397 chunk_printf(msg, "Invalid addr '%s'", addr);
3398 goto out;
3399 }
3400
3401 /* changes are allowed on AF_INET* families only */
3402 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3403 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3404 goto out;
3405 }
3406
3407 /* collecting data currently setup */
3408 memset(current_addr, '\0', sizeof(current_addr));
3409 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3410 /* changes are allowed on AF_INET* families only */
3411 if ((ret != AF_INET) && (ret != AF_INET6)) {
3412 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3413 goto out;
3414 }
3415
3416 /* applying ADDR changes if required and allowed
3417 * ipcmp returns 0 when both ADDR are the same
3418 */
3419 if (ipcmp(&s->addr, &sa) == 0) {
3420 chunk_appendf(msg, "no need to change the addr");
3421 goto port;
3422 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003423 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003424 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003425
3426 /* we also need to update check's ADDR only if it uses the server's one */
3427 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003428 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003429 }
3430
3431 /* we also need to update agent ADDR only if it use the server's one */
3432 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003433 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003434 }
3435
3436 /* update report for caller */
3437 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3438 }
3439
3440 port:
3441 if (port) {
3442 char sign = '\0';
3443 char *endptr;
3444
3445 if (addr)
3446 chunk_appendf(msg, ", ");
3447
3448 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003449 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003450
3451 /* check if PORT change is required */
3452 port_change_required = 0;
3453
3454 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003455 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003456 new_port = strtol(port, &endptr, 10);
3457 if ((errno != 0) || (port == endptr)) {
3458 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3459 goto out;
3460 }
3461
3462 /* check if caller triggers a port mapped or offset */
3463 if (sign == '-' || (sign == '+')) {
3464 /* check if server currently uses port map */
3465 if (!(s->flags & SRV_F_MAPPORTS)) {
3466 /* switch from fixed port to port map mandatorily triggers
3467 * a port change */
3468 port_change_required = 1;
3469 /* check is configured
3470 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3471 * prevent PORT change if check doesn't have it's dedicated port while switching
3472 * to port mapping */
3473 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3474 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.");
3475 goto out;
3476 }
3477 }
3478 /* we're already using port maps */
3479 else {
3480 port_change_required = current_port != new_port;
3481 }
3482 }
3483 /* fixed port */
3484 else {
3485 port_change_required = current_port != new_port;
3486 }
3487
3488 /* applying PORT changes if required and update response message */
3489 if (port_change_required) {
3490 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003491 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003492 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003493
3494 /* prepare message */
3495 chunk_appendf(msg, "port changed from '");
3496 if (s->flags & SRV_F_MAPPORTS)
3497 chunk_appendf(msg, "+");
3498 chunk_appendf(msg, "%d' to '", current_port);
3499
3500 if (sign == '-') {
3501 s->flags |= SRV_F_MAPPORTS;
3502 chunk_appendf(msg, "%c", sign);
3503 /* just use for result output */
3504 new_port = -new_port;
3505 }
3506 else if (sign == '+') {
3507 s->flags |= SRV_F_MAPPORTS;
3508 chunk_appendf(msg, "%c", sign);
3509 }
3510 else {
3511 s->flags &= ~SRV_F_MAPPORTS;
3512 }
3513
3514 chunk_appendf(msg, "%d'", new_port);
3515
3516 /* we also need to update health checks port only if it uses server's realport */
3517 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3518 s->check.port = new_port;
3519 }
3520 }
3521 else {
3522 chunk_appendf(msg, "no need to change the port");
3523 }
3524 }
3525
3526out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003527 if (changed)
3528 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003529 if (updater)
3530 chunk_appendf(msg, " by '%s'", updater);
3531 chunk_appendf(msg, "\n");
3532 return msg->str;
3533}
3534
3535
3536/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003537 * update server status based on result of name resolution
3538 * returns:
3539 * 0 if server status is updated
3540 * 1 if server status has not changed
3541 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003542int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003543{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003544 struct dns_resolvers *resolvers = s->resolvers;
3545 struct dns_resolution *resolution = s->dns_requester->resolution;
3546 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003547
3548 switch (resolution->status) {
3549 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003550 /* status when HAProxy has just (re)started.
3551 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003552 break;
3553
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003554 case RSLV_STATUS_VALID:
3555 /*
3556 * resume health checks
3557 * server will be turned back on if health check is safe
3558 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003559 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003560 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003561 return 1;
3562 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3563 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003564 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003565 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003566
Emeric Brun52a91d32017-08-31 14:41:55 +02003567 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003568 return 1;
3569 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3570 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3571 s->proxy->id, s->id);
3572
Christopher Faulet767a84b2017-11-24 16:50:31 +01003573 ha_warning("%s.\n", trash.str);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003574 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3575 return 0;
3576
3577 case RSLV_STATUS_NX:
3578 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003579 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
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 NX status");
3586 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003587
3588 case RSLV_STATUS_TIMEOUT:
3589 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003590 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
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 timeout status");
3597 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003598
3599 case RSLV_STATUS_REFUSED:
3600 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003601 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
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, "DNS refused status");
3608 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003609
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003610 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003611 /* stop server if resolution failed for a long enough period */
3612 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3613 if (!tick_is_expired(exp, now_ms))
3614 break;
3615
3616 if (s->next_admin & SRV_ADMF_RMAINT)
3617 return 1;
3618 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3619 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003620 }
3621
3622 return 1;
3623}
3624
3625/*
3626 * Server Name Resolution valid response callback
3627 * It expects:
3628 * - <nameserver>: the name server which answered the valid response
3629 * - <response>: buffer containing a valid DNS response
3630 * - <response_len>: size of <response>
3631 * It performs the following actions:
3632 * - ignore response if current ip found and server family not met
3633 * - update with first new ip found if family is met and current IP is not found
3634 * returns:
3635 * 0 on error
3636 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003637 *
3638 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003639 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003640int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003641{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003642 struct server *s = NULL;
3643 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003644 void *serverip, *firstip;
3645 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003646 int ret;
3647 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003648 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003649
Christopher Faulet67957bd2017-09-27 11:00:59 +02003650 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003651 if (!s)
3652 return 1;
3653
Christopher Faulet67957bd2017-09-27 11:00:59 +02003654 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003655
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003656 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003657 firstip = NULL; /* pointer to the first valid response found */
3658 /* it will be used as the new IP if a change is required */
3659 firstip_sin_family = AF_UNSPEC;
3660 serverip = NULL; /* current server IP address */
3661
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003662 /* initializing server IP pointer */
3663 server_sin_family = s->addr.ss_family;
3664 switch (server_sin_family) {
3665 case AF_INET:
3666 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3667 break;
3668
3669 case AF_INET6:
3670 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3671 break;
3672
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003673 case AF_UNSPEC:
3674 break;
3675
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003676 default:
3677 goto invalid;
3678 }
3679
Baptiste Assmann729c9012017-05-22 15:13:10 +02003680 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003681 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003682 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003683
3684 switch (ret) {
3685 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003686 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003687
3688 case DNS_UPD_SRVIP_NOT_FOUND:
3689 goto save_ip;
3690
3691 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003692 goto invalid;
3693
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003694 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003695 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003696 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003697
Baptiste Assmannfad03182015-10-28 02:03:32 +01003698 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003699 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003700 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003701 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003702
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003703 default:
3704 goto invalid;
3705
3706 }
3707
3708 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003709 if (nameserver) {
3710 nameserver->counters.update++;
3711 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003712 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003713 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003714 else
3715 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003716 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3717
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003718 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003719 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003720 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003721
3722 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003723 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003724 nameserver->counters.invalid++;
3725 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003726 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003727 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003728 return 0;
3729}
3730
3731/*
3732 * Server Name Resolution error management callback
3733 * returns:
3734 * 0 on error
3735 * 1 when no error or safe ignore
3736 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003737int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003738{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003739 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003740
Christopher Faulet67957bd2017-09-27 11:00:59 +02003741 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003742 if (!s)
3743 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003744 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003745 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003746 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003747 return 1;
3748}
3749
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003750/*
3751 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003752 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003753 * It returns a pointer to the first server found or NULL if <ip> is not yet
3754 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003755 *
3756 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003757 */
3758struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3759{
3760 struct server *tmpsrv;
3761 struct proxy *be;
3762
3763 if (!srv)
3764 return NULL;
3765
3766 be = srv->proxy;
3767 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003768 /* we found the current server is the same, ignore it */
3769 if (srv == tmpsrv)
3770 continue;
3771
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003772 /* We want to compare the IP in the record with the IP of the servers in the
3773 * same backend, only if:
3774 * * DNS resolution is enabled on the server
3775 * * the hostname used for the resolution by our server is the same than the
3776 * one used for the server found in the backend
3777 * * the server found in the backend is not our current server
3778 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003779 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003780 if ((tmpsrv->hostname_dn == NULL) ||
3781 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3782 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003783 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003784 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003785 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003786 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003787
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003788 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003789 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003790 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003791 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003792 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003793
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003794 /* At this point, we have 2 different servers using the same DNS hostname
3795 * for their respective resolution.
3796 */
3797 if (*ip_family == tmpsrv->addr.ss_family &&
3798 ((tmpsrv->addr.ss_family == AF_INET &&
3799 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3800 (tmpsrv->addr.ss_family == AF_INET6 &&
3801 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003802 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003803 return tmpsrv;
3804 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003805 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003806 }
3807
Emeric Brune9fd6b52017-11-02 17:20:39 +01003808
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003809 return NULL;
3810}
3811
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003812/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3813 * resolver. This is suited for initial address configuration. Returns 0 on
3814 * success otherwise a non-zero error code. In case of error, *err_code, if
3815 * not NULL, is filled up.
3816 */
3817int srv_set_addr_via_libc(struct server *srv, int *err_code)
3818{
3819 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003820 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003821 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003822 return 1;
3823 }
3824 return 0;
3825}
3826
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003827/* Set the server's FDQN (->hostname) from <hostname>.
3828 * Returns -1 if failed, 0 if not.
3829 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003830int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003831{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003832 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003833 char *hostname_dn;
3834 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003835
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003836 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003837 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003838 /* run time DNS resolution was not active for this server
3839 * and we can't enable it at run time for now.
3840 */
3841 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003842 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003843
3844 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003845 hostname_len = strlen(hostname);
3846 hostname_dn = trash.str;
3847 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3848 hostname_dn, trash.size);
3849 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003850 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003851
Christopher Faulet67957bd2017-09-27 11:00:59 +02003852 resolution = srv->dns_requester->resolution;
3853 if (resolution &&
3854 resolution->hostname_dn &&
3855 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02003856 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003857
Christopher Faulet67957bd2017-09-27 11:00:59 +02003858 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003859
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003860 free(srv->hostname);
3861 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003862 srv->hostname = strdup(hostname);
3863 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003864 srv->hostname_dn_len = hostname_dn_len;
3865 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003866 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003867
Olivier Houchard55dcdf42017-11-06 15:15:04 +01003868 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003869 goto err;
3870
3871 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003872 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003873 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003874 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003875
3876 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003877 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003878 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003879 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003880}
3881
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003882/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3883 * from the state file. This is suited for initial address configuration.
3884 * Returns 0 on success otherwise a non-zero error code. In case of error,
3885 * *err_code, if not NULL, is filled up.
3886 */
3887static int srv_apply_lastaddr(struct server *srv, int *err_code)
3888{
3889 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3890 if (err_code)
3891 *err_code |= ERR_WARN;
3892 return 1;
3893 }
3894 return 0;
3895}
3896
Willy Tarreau25e51522016-11-04 15:10:17 +01003897/* returns 0 if no error, otherwise a combination of ERR_* flags */
3898static int srv_iterate_initaddr(struct server *srv)
3899{
3900 int return_code = 0;
3901 int err_code;
3902 unsigned int methods;
3903
3904 methods = srv->init_addr_methods;
3905 if (!methods) { // default to "last,libc"
3906 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3907 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3908 }
3909
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003910 /* "-dr" : always append "none" so that server addresses resolution
3911 * failures are silently ignored, this is convenient to validate some
3912 * configs out of their environment.
3913 */
3914 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3915 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3916
Willy Tarreau25e51522016-11-04 15:10:17 +01003917 while (methods) {
3918 err_code = 0;
3919 switch (srv_get_next_initaddr(&methods)) {
3920 case SRV_IADDR_LAST:
3921 if (!srv->lastaddr)
3922 continue;
3923 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003924 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003925 return_code |= err_code;
3926 break;
3927
3928 case SRV_IADDR_LIBC:
3929 if (!srv->hostname)
3930 continue;
3931 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003932 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003933 return_code |= err_code;
3934 break;
3935
Willy Tarreau37ebe122016-11-04 15:17:58 +01003936 case SRV_IADDR_NONE:
3937 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003938 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003939 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3940 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003941 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003942 return return_code;
3943
Willy Tarreau4310d362016-11-02 15:05:56 +01003944 case SRV_IADDR_IP:
3945 ipcpy(&srv->init_addr, &srv->addr);
3946 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003947 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3948 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01003949 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003950 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003951
Willy Tarreau25e51522016-11-04 15:10:17 +01003952 default: /* unhandled method */
3953 break;
3954 }
3955 }
3956
3957 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003958 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01003959 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3960 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003961 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003962 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01003963 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3964 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003965
3966 return_code |= ERR_ALERT | ERR_FATAL;
3967 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003968out:
3969 srv_set_dyncookie(srv);
3970 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003971}
3972
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003973/*
3974 * This function parses all backends and all servers within each backend
3975 * and performs servers' addr resolution based on information provided by:
3976 * - configuration file
3977 * - server-state file (states provided by an 'old' haproxy process)
3978 *
3979 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3980 */
3981int srv_init_addr(void)
3982{
3983 struct proxy *curproxy;
3984 int return_code = 0;
3985
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003986 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003987 while (curproxy) {
3988 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003989
3990 /* servers are in backend only */
3991 if (!(curproxy->cap & PR_CAP_BE))
3992 goto srv_init_addr_next;
3993
Willy Tarreau25e51522016-11-04 15:10:17 +01003994 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003995 if (srv->hostname)
3996 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003997
3998 srv_init_addr_next:
3999 curproxy = curproxy->next;
4000 }
4001
4002 return return_code;
4003}
4004
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004005const 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 +02004006{
4007
4008 struct chunk *msg;
4009
4010 msg = get_trash_chunk();
4011 chunk_reset(msg);
4012
Olivier Houchard8da5f982017-08-04 18:35:36 +02004013 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004014 chunk_appendf(msg, "no need to change the FDQN");
4015 goto out;
4016 }
4017
4018 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4019 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4020 goto out;
4021 }
4022
4023 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4024 server->proxy->id, server->id, server->hostname, fqdn);
4025
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004026 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004027 chunk_reset(msg);
4028 chunk_appendf(msg, "could not update %s/%s FQDN",
4029 server->proxy->id, server->id);
4030 goto out;
4031 }
4032
4033 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004034 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004035
4036 out:
4037 if (updater)
4038 chunk_appendf(msg, " by '%s'", updater);
4039 chunk_appendf(msg, "\n");
4040
4041 return msg->str;
4042}
4043
4044
Willy Tarreau21b069d2016-11-23 17:15:08 +01004045/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4046 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004047 * 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 +01004048 * used for CLI commands requiring a server name.
4049 * Important: the <arg> is modified to remove the '/'.
4050 */
4051struct server *cli_find_server(struct appctx *appctx, char *arg)
4052{
4053 struct proxy *px;
4054 struct server *sv;
4055 char *line;
4056
4057 /* split "backend/server" and make <line> point to server */
4058 for (line = arg; *line; line++)
4059 if (*line == '/') {
4060 *line++ = '\0';
4061 break;
4062 }
4063
4064 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004065 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004066 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004067 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004068 return NULL;
4069 }
4070
4071 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004072 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004073 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004074 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004075 return NULL;
4076 }
4077
4078 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004079 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004080 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004081 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004082 return NULL;
4083 }
4084
4085 return sv;
4086}
4087
William Lallemand222baf22016-11-19 02:00:33 +01004088
4089static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4090{
4091 struct server *sv;
4092 const char *warning;
4093
4094 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4095 return 1;
4096
4097 sv = cli_find_server(appctx, args[2]);
4098 if (!sv)
4099 return 1;
4100
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004101 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004102
William Lallemand222baf22016-11-19 02:00:33 +01004103 if (strcmp(args[3], "weight") == 0) {
4104 warning = server_parse_weight_change_request(sv, args[4]);
4105 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004106 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004107 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004108 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004109 }
4110 }
4111 else if (strcmp(args[3], "state") == 0) {
4112 if (strcmp(args[4], "ready") == 0)
4113 srv_adm_set_ready(sv);
4114 else if (strcmp(args[4], "drain") == 0)
4115 srv_adm_set_drain(sv);
4116 else if (strcmp(args[4], "maint") == 0)
4117 srv_adm_set_maint(sv);
4118 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004119 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004120 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004121 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004122 }
4123 }
4124 else if (strcmp(args[3], "health") == 0) {
4125 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004126 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004127 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004128 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004129 }
4130 else if (strcmp(args[4], "up") == 0) {
4131 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004132 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004133 }
4134 else if (strcmp(args[4], "stopping") == 0) {
4135 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004136 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004137 }
4138 else if (strcmp(args[4], "down") == 0) {
4139 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004140 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004141 }
4142 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004143 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004144 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004145 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004146 }
4147 }
4148 else if (strcmp(args[3], "agent") == 0) {
4149 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004150 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004151 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004152 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004153 }
4154 else if (strcmp(args[4], "up") == 0) {
4155 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004156 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004157 }
4158 else if (strcmp(args[4], "down") == 0) {
4159 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004160 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004161 }
4162 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004163 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004164 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004165 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004166 }
4167 }
Misiek2da082d2017-01-09 09:40:42 +01004168 else if (strcmp(args[3], "agent-addr") == 0) {
4169 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004170 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004171 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4172 appctx->st0 = CLI_ST_PRINT;
4173 } else {
4174 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004175 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004176 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4177 appctx->st0 = CLI_ST_PRINT;
4178 }
4179 }
4180 }
4181 else if (strcmp(args[3], "agent-send") == 0) {
4182 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004183 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004184 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4185 appctx->st0 = CLI_ST_PRINT;
4186 } else {
4187 char *nss = strdup(args[4]);
4188 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004189 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004190 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4191 appctx->st0 = CLI_ST_PRINT;
4192 } else {
4193 free(sv->agent.send_string);
4194 sv->agent.send_string = nss;
4195 sv->agent.send_string_len = strlen(args[4]);
4196 }
4197 }
4198 }
William Lallemand222baf22016-11-19 02:00:33 +01004199 else if (strcmp(args[3], "check-port") == 0) {
4200 int i = 0;
4201 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004202 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004203 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004204 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004205 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004206 }
4207 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004208 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004209 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004210 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004211 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004212 }
4213 /* prevent the update of port to 0 if MAPPORTS are in use */
4214 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004215 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004216 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004217 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004218 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004219 }
4220 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004221 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004222 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004223 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004224 }
4225 else if (strcmp(args[3], "addr") == 0) {
4226 char *addr = NULL;
4227 char *port = NULL;
4228 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004229 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004230 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004231 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004232 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004233 }
4234 else {
4235 addr = args[4];
4236 }
4237 if (strcmp(args[5], "port") == 0) {
4238 port = args[6];
4239 }
4240 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4241 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004242 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004243 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004244 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004245 }
4246 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4247 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004248 else if (strcmp(args[3], "fqdn") == 0) {
4249 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004250 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004251 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4252 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004253 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004254 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004255 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004256 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004257 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004258 appctx->ctx.cli.msg = warning;
4259 appctx->st0 = CLI_ST_PRINT;
4260 }
4261 }
William Lallemand222baf22016-11-19 02:00:33 +01004262 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004263 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004264 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 +01004265 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004266 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004267 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004268 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004269 return 1;
4270}
4271
William Lallemand6b160942016-11-22 12:34:35 +01004272static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4273{
4274 struct stream_interface *si = appctx->owner;
4275 struct proxy *px;
4276 struct server *sv;
4277 char *line;
4278
4279
4280 /* split "backend/server" and make <line> point to server */
4281 for (line = args[2]; *line; line++)
4282 if (*line == '/') {
4283 *line++ = '\0';
4284 break;
4285 }
4286
4287 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004288 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004289 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004290 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004291 return 1;
4292 }
4293
4294 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004295 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004296 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004297 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004298 return 1;
4299 }
4300
4301 /* return server's effective weight at the moment */
4302 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004303 if (ci_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004304 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004305 return 0;
4306 }
William Lallemand6b160942016-11-22 12:34:35 +01004307 return 1;
4308}
4309
4310static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4311{
4312 struct server *sv;
4313 const char *warning;
4314
4315 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4316 return 1;
4317
4318 sv = cli_find_server(appctx, args[2]);
4319 if (!sv)
4320 return 1;
4321
4322 warning = server_parse_weight_change_request(sv, args[3]);
4323 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004324 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004325 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004326 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004327 }
4328 return 1;
4329}
4330
Willy Tarreaub8026272016-11-23 11:26:56 +01004331/* parse a "set maxconn server" command. It always returns 1. */
4332static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4333{
4334 struct server *sv;
4335 const char *warning;
4336
4337 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4338 return 1;
4339
4340 sv = cli_find_server(appctx, args[3]);
4341 if (!sv)
4342 return 1;
4343
4344 warning = server_parse_maxconn_change_request(sv, args[4]);
4345 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004346 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004347 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004348 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004349 }
4350 return 1;
4351}
William Lallemand6b160942016-11-22 12:34:35 +01004352
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004353/* parse a "disable agent" command. It always returns 1. */
4354static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4355{
4356 struct server *sv;
4357
4358 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4359 return 1;
4360
4361 sv = cli_find_server(appctx, args[2]);
4362 if (!sv)
4363 return 1;
4364
4365 sv->agent.state &= ~CHK_ST_ENABLED;
4366 return 1;
4367}
4368
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004369/* parse a "disable health" command. It always returns 1. */
4370static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4371{
4372 struct server *sv;
4373
4374 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4375 return 1;
4376
4377 sv = cli_find_server(appctx, args[2]);
4378 if (!sv)
4379 return 1;
4380
4381 sv->check.state &= ~CHK_ST_ENABLED;
4382 return 1;
4383}
4384
Willy Tarreauffb4d582016-11-24 12:47:00 +01004385/* parse a "disable server" command. It always returns 1. */
4386static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4387{
4388 struct server *sv;
4389
4390 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4391 return 1;
4392
4393 sv = cli_find_server(appctx, args[2]);
4394 if (!sv)
4395 return 1;
4396
4397 srv_adm_set_maint(sv);
4398 return 1;
4399}
4400
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004401/* parse a "enable agent" command. It always returns 1. */
4402static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4403{
4404 struct server *sv;
4405
4406 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4407 return 1;
4408
4409 sv = cli_find_server(appctx, args[2]);
4410 if (!sv)
4411 return 1;
4412
4413 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004414 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004415 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004416 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004417 return 1;
4418 }
4419
4420 sv->agent.state |= CHK_ST_ENABLED;
4421 return 1;
4422}
4423
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004424/* parse a "enable health" command. It always returns 1. */
4425static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4426{
4427 struct server *sv;
4428
4429 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4430 return 1;
4431
4432 sv = cli_find_server(appctx, args[2]);
4433 if (!sv)
4434 return 1;
4435
4436 sv->check.state |= CHK_ST_ENABLED;
4437 return 1;
4438}
4439
Willy Tarreauffb4d582016-11-24 12:47:00 +01004440/* parse a "enable server" command. It always returns 1. */
4441static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4442{
4443 struct server *sv;
4444
4445 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4446 return 1;
4447
4448 sv = cli_find_server(appctx, args[2]);
4449 if (!sv)
4450 return 1;
4451
4452 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004453 if (!(sv->flags & SRV_F_COOKIESET)
4454 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4455 sv->cookie)
4456 srv_check_for_dup_dyncookie(sv);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004457 return 1;
4458}
4459
William Lallemand222baf22016-11-19 02:00:33 +01004460/* register cli keywords */
4461static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004462 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004463 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004464 { { "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 +01004465 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004466 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004467 { { "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 +01004468 { { "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 +01004469 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004470 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4471 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4472
William Lallemand222baf22016-11-19 02:00:33 +01004473 {{},}
4474}};
4475
4476__attribute__((constructor))
4477static void __server_init(void)
4478{
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004479 HA_SPIN_INIT(&updated_servers_lock);
William Lallemand222baf22016-11-19 02:00:33 +01004480 cli_register_kw(&cli_kws);
4481}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004482
Emeric Brun64cc49c2017-10-03 14:46:45 +02004483/*
4484 * This function applies server's status changes, it is
4485 * is designed to be called asynchronously.
4486 *
4487 */
4488void srv_update_status(struct server *s)
4489{
4490 struct check *check = &s->check;
4491 int xferred;
4492 struct proxy *px = s->proxy;
4493 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4494 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4495 int log_level;
4496 struct chunk *tmptrash = NULL;
4497
4498
4499 /* If currently main is not set we try to apply pending state changes */
4500 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4501 int next_admin;
4502
4503 /* Backup next admin */
4504 next_admin = s->next_admin;
4505
4506 /* restore current admin state */
4507 s->next_admin = s->cur_admin;
4508
4509 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4510 s->last_change = now.tv_sec;
4511 if (s->proxy->lbprm.set_server_status_down)
4512 s->proxy->lbprm.set_server_status_down(s);
4513
4514 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4515 srv_shutdown_streams(s, SF_ERR_DOWN);
4516
4517 /* we might have streams queued on this server and waiting for
4518 * a connection. Those which are redispatchable will be queued
4519 * to another server or to the proxy itself.
4520 */
4521 xferred = pendconn_redistribute(s);
4522
4523 tmptrash = alloc_trash_chunk();
4524 if (tmptrash) {
4525 chunk_printf(tmptrash,
4526 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4527 s->proxy->id, s->id);
4528
Emeric Brun5a133512017-10-19 14:42:30 +02004529 srv_append_status(tmptrash, s, NULL, xferred, 0);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004530 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004531
4532 /* we don't send an alert if the server was previously paused */
4533 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
4534 send_log(s->proxy, log_level, "%s.\n", tmptrash->str);
4535 send_email_alert(s, log_level, "%s", tmptrash->str);
4536 free_trash_chunk(tmptrash);
4537 tmptrash = NULL;
4538 }
4539 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4540 set_backend_down(s->proxy);
4541
4542 s->counters.down_trans++;
4543 }
4544 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4545 s->last_change = now.tv_sec;
4546 if (s->proxy->lbprm.set_server_status_down)
4547 s->proxy->lbprm.set_server_status_down(s);
4548
4549 /* we might have streams queued on this server and waiting for
4550 * a connection. Those which are redispatchable will be queued
4551 * to another server or to the proxy itself.
4552 */
4553 xferred = pendconn_redistribute(s);
4554
4555 tmptrash = alloc_trash_chunk();
4556 if (tmptrash) {
4557 chunk_printf(tmptrash,
4558 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4559 s->proxy->id, s->id);
4560
Emeric Brun5a133512017-10-19 14:42:30 +02004561 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004562
Christopher Faulet767a84b2017-11-24 16:50:31 +01004563 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004564 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4565 free_trash_chunk(tmptrash);
4566 tmptrash = NULL;
4567 }
4568
4569 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4570 set_backend_down(s->proxy);
4571 }
4572 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4573 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4574 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4575 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4576 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4577 s->proxy->last_change = now.tv_sec;
4578 }
4579
4580 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4581 s->down_time += now.tv_sec - s->last_change;
4582
4583 s->last_change = now.tv_sec;
4584 if (s->next_state == SRV_ST_STARTING)
4585 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4586
4587 server_recalc_eweight(s);
4588 /* now propagate the status change to any LB algorithms */
4589 if (px->lbprm.update_server_eweight)
4590 px->lbprm.update_server_eweight(s);
4591 else if (srv_willbe_usable(s)) {
4592 if (px->lbprm.set_server_status_up)
4593 px->lbprm.set_server_status_up(s);
4594 }
4595 else {
4596 if (px->lbprm.set_server_status_down)
4597 px->lbprm.set_server_status_down(s);
4598 }
4599
4600 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4601 * and it's not a backup server and its effective weight is > 0,
4602 * then it can accept new connections, so we shut down all streams
4603 * on all backup servers.
4604 */
4605 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4606 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4607 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4608
4609 /* check if we can handle some connections queued at the proxy. We
4610 * will take as many as we can handle.
4611 */
4612 xferred = pendconn_grab_from_px(s);
4613
4614 tmptrash = alloc_trash_chunk();
4615 if (tmptrash) {
4616 chunk_printf(tmptrash,
4617 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4618 s->proxy->id, s->id);
4619
Emeric Brun5a133512017-10-19 14:42:30 +02004620 srv_append_status(tmptrash, s, NULL, xferred, 0);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004621 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004622 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4623 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4624 free_trash_chunk(tmptrash);
4625 tmptrash = NULL;
4626 }
4627
4628 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4629 set_backend_down(s->proxy);
4630 }
4631 else if (s->cur_eweight != s->next_eweight) {
4632 /* now propagate the status change to any LB algorithms */
4633 if (px->lbprm.update_server_eweight)
4634 px->lbprm.update_server_eweight(s);
4635 else if (srv_willbe_usable(s)) {
4636 if (px->lbprm.set_server_status_up)
4637 px->lbprm.set_server_status_up(s);
4638 }
4639 else {
4640 if (px->lbprm.set_server_status_down)
4641 px->lbprm.set_server_status_down(s);
4642 }
4643
4644 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4645 set_backend_down(s->proxy);
4646 }
4647
4648 s->next_admin = next_admin;
4649 }
4650
Emeric Brun5a133512017-10-19 14:42:30 +02004651 /* reset operational state change */
4652 *s->op_st_chg.reason = 0;
4653 s->op_st_chg.status = s->op_st_chg.code = -1;
4654 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004655
4656 /* Now we try to apply pending admin changes */
4657
4658 /* Maintenance must also disable health checks */
4659 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4660 if (s->check.state & CHK_ST_ENABLED) {
4661 s->check.state |= CHK_ST_PAUSED;
4662 check->health = 0;
4663 }
4664
4665 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004666 tmptrash = alloc_trash_chunk();
4667 if (tmptrash) {
4668 chunk_printf(tmptrash,
4669 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4670 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4671 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004672
Olivier Houchard796a2b32017-10-24 17:42:47 +02004673 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004674
Olivier Houchard796a2b32017-10-24 17:42:47 +02004675 if (!(global.mode & MODE_STARTING)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004676 ha_warning("%s.\n", tmptrash->str);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004677 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4678 }
4679 free_trash_chunk(tmptrash);
4680 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004681 }
Emeric Brun8f298292017-12-06 16:47:17 +01004682 /* commit new admin status */
4683
4684 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004685 }
4686 else { /* server was still running */
4687 check->health = 0; /* failure */
4688 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004689
4690 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004691 if (s->proxy->lbprm.set_server_status_down)
4692 s->proxy->lbprm.set_server_status_down(s);
4693
Emeric Brun64cc49c2017-10-03 14:46:45 +02004694 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4695 srv_shutdown_streams(s, SF_ERR_DOWN);
4696
4697 /* we might have streams queued on this server and waiting for
4698 * a connection. Those which are redispatchable will be queued
4699 * to another server or to the proxy itself.
4700 */
4701 xferred = pendconn_redistribute(s);
4702
4703 tmptrash = alloc_trash_chunk();
4704 if (tmptrash) {
4705 chunk_printf(tmptrash,
4706 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4707 s->flags & SRV_F_BACKUP ? "Backup " : "",
4708 s->proxy->id, s->id,
4709 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4710
4711 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4712
4713 if (!(global.mode & MODE_STARTING)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004714 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004715 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", tmptrash->str);
4716 }
4717 free_trash_chunk(tmptrash);
4718 tmptrash = NULL;
4719 }
4720 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4721 set_backend_down(s->proxy);
4722
4723 s->counters.down_trans++;
4724 }
4725 }
4726 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4727 /* OK here we're leaving maintenance, we have many things to check,
4728 * because the server might possibly be coming back up depending on
4729 * its state. In practice, leaving maintenance means that we should
4730 * immediately turn to UP (more or less the slowstart) under the
4731 * following conditions :
4732 * - server is neither checked nor tracked
4733 * - server tracks another server which is not checked
4734 * - server tracks another server which is already up
4735 * Which sums up as something simpler :
4736 * "either the tracking server is up or the server's checks are disabled
4737 * or up". Otherwise we only re-enable health checks. There's a special
4738 * case associated to the stopping state which can be inherited. Note
4739 * that the server might still be in drain mode, which is naturally dealt
4740 * with by the lower level functions.
4741 */
4742
4743 if (s->check.state & CHK_ST_ENABLED) {
4744 s->check.state &= ~CHK_ST_PAUSED;
4745 check->health = check->rise; /* start OK but check immediately */
4746 }
4747
4748 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4749 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4750 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4751 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4752 s->next_state = SRV_ST_STOPPING;
4753 }
4754 else {
4755 s->next_state = SRV_ST_STARTING;
4756 if (s->slowstart > 0)
4757 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4758 else
4759 s->next_state = SRV_ST_RUNNING;
4760 }
4761
4762 }
4763
4764 tmptrash = alloc_trash_chunk();
4765 if (tmptrash) {
4766 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4767 chunk_printf(tmptrash,
4768 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4769 s->flags & SRV_F_BACKUP ? "Backup " : "",
4770 s->proxy->id, s->id,
4771 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4772 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4773 }
4774 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4775 chunk_printf(tmptrash,
4776 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4777 s->flags & SRV_F_BACKUP ? "Backup " : "",
4778 s->proxy->id, s->id, s->hostname,
4779 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4780 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4781 }
4782 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4783 chunk_printf(tmptrash,
4784 "%sServer %s/%s is %s/%s (leaving maintenance)",
4785 s->flags & SRV_F_BACKUP ? "Backup " : "",
4786 s->proxy->id, s->id,
4787 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4788 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4789 }
Christopher Faulet767a84b2017-11-24 16:50:31 +01004790 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004791 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4792 free_trash_chunk(tmptrash);
4793 tmptrash = NULL;
4794 }
4795
4796 server_recalc_eweight(s);
4797 /* now propagate the status change to any LB algorithms */
4798 if (px->lbprm.update_server_eweight)
4799 px->lbprm.update_server_eweight(s);
4800 else if (srv_willbe_usable(s)) {
4801 if (px->lbprm.set_server_status_up)
4802 px->lbprm.set_server_status_up(s);
4803 }
4804 else {
4805 if (px->lbprm.set_server_status_down)
4806 px->lbprm.set_server_status_down(s);
4807 }
4808
4809 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4810 set_backend_down(s->proxy);
4811
4812 }
4813 else if (s->next_admin & SRV_ADMF_MAINT) {
4814 /* remaining in maintenance mode, let's inform precisely about the
4815 * situation.
4816 */
4817 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4818 tmptrash = alloc_trash_chunk();
4819 if (tmptrash) {
4820 chunk_printf(tmptrash,
4821 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4822 s->flags & SRV_F_BACKUP ? "Backup " : "",
4823 s->proxy->id, s->id);
4824
4825 if (s->track) /* normally it's mandatory here */
4826 chunk_appendf(tmptrash, " via %s/%s",
4827 s->track->proxy->id, s->track->id);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004828 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004829 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4830 free_trash_chunk(tmptrash);
4831 tmptrash = NULL;
4832 }
4833 }
4834 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4835 tmptrash = alloc_trash_chunk();
4836 if (tmptrash) {
4837 chunk_printf(tmptrash,
4838 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4839 s->flags & SRV_F_BACKUP ? "Backup " : "",
4840 s->proxy->id, s->id, s->hostname);
4841
4842 if (s->track) /* normally it's mandatory here */
4843 chunk_appendf(tmptrash, " via %s/%s",
4844 s->track->proxy->id, s->track->id);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004845 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004846 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4847 free_trash_chunk(tmptrash);
4848 tmptrash = NULL;
4849 }
4850 }
4851 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4852 tmptrash = alloc_trash_chunk();
4853 if (tmptrash) {
4854 chunk_printf(tmptrash,
4855 "%sServer %s/%s remains in forced maintenance",
4856 s->flags & SRV_F_BACKUP ? "Backup " : "",
4857 s->proxy->id, s->id);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004858 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004859 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4860 free_trash_chunk(tmptrash);
4861 tmptrash = NULL;
4862 }
4863 }
4864 /* don't report anything when leaving drain mode and remaining in maintenance */
4865
4866 s->cur_admin = s->next_admin;
4867 }
4868
4869 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4870 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4871 /* drain state is applied only if not yet in maint */
4872
4873 s->last_change = now.tv_sec;
4874 if (px->lbprm.set_server_status_down)
4875 px->lbprm.set_server_status_down(s);
4876
4877 /* we might have streams queued on this server and waiting for
4878 * a connection. Those which are redispatchable will be queued
4879 * to another server or to the proxy itself.
4880 */
4881 xferred = pendconn_redistribute(s);
4882
4883 tmptrash = alloc_trash_chunk();
4884 if (tmptrash) {
4885 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4886 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4887 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4888
4889 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4890
4891 if (!(global.mode & MODE_STARTING)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004892 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004893 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4894 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4895 }
4896 free_trash_chunk(tmptrash);
4897 tmptrash = NULL;
4898 }
4899
4900 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4901 set_backend_down(s->proxy);
4902 }
4903 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4904 /* OK completely leaving drain mode */
4905 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4906 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4907 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4908 s->proxy->last_change = now.tv_sec;
4909 }
4910
4911 if (s->last_change < now.tv_sec) // ignore negative times
4912 s->down_time += now.tv_sec - s->last_change;
4913 s->last_change = now.tv_sec;
4914 server_recalc_eweight(s);
4915
4916 tmptrash = alloc_trash_chunk();
4917 if (tmptrash) {
4918 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4919 chunk_printf(tmptrash,
4920 "%sServer %s/%s is %s (leaving forced drain)",
4921 s->flags & SRV_F_BACKUP ? "Backup " : "",
4922 s->proxy->id, s->id,
4923 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4924 }
4925 else {
4926 chunk_printf(tmptrash,
4927 "%sServer %s/%s is %s (leaving drain)",
4928 s->flags & SRV_F_BACKUP ? "Backup " : "",
4929 s->proxy->id, s->id,
4930 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4931 if (s->track) /* normally it's mandatory here */
4932 chunk_appendf(tmptrash, " via %s/%s",
4933 s->track->proxy->id, s->track->id);
4934 }
4935
Christopher Faulet767a84b2017-11-24 16:50:31 +01004936 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004937 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4938 free_trash_chunk(tmptrash);
4939 tmptrash = NULL;
4940 }
4941
4942 /* now propagate the status change to any LB algorithms */
4943 if (px->lbprm.update_server_eweight)
4944 px->lbprm.update_server_eweight(s);
4945 else if (srv_willbe_usable(s)) {
4946 if (px->lbprm.set_server_status_up)
4947 px->lbprm.set_server_status_up(s);
4948 }
4949 else {
4950 if (px->lbprm.set_server_status_down)
4951 px->lbprm.set_server_status_down(s);
4952 }
4953 }
4954 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
4955 /* remaining in drain mode after removing one of its flags */
4956
4957 tmptrash = alloc_trash_chunk();
4958 if (tmptrash) {
4959 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4960 chunk_printf(tmptrash,
4961 "%sServer %s/%s is leaving forced drain but remains in drain mode",
4962 s->flags & SRV_F_BACKUP ? "Backup " : "",
4963 s->proxy->id, s->id);
4964
4965 if (s->track) /* normally it's mandatory here */
4966 chunk_appendf(tmptrash, " via %s/%s",
4967 s->track->proxy->id, s->track->id);
4968 }
4969 else {
4970 chunk_printf(tmptrash,
4971 "%sServer %s/%s remains in forced drain mode",
4972 s->flags & SRV_F_BACKUP ? "Backup " : "",
4973 s->proxy->id, s->id);
4974 }
Christopher Faulet767a84b2017-11-24 16:50:31 +01004975 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004976 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4977 free_trash_chunk(tmptrash);
4978 tmptrash = NULL;
4979 }
4980
4981 /* commit new admin status */
4982
4983 s->cur_admin = s->next_admin;
4984 }
4985 }
4986
4987 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02004988 *s->adm_st_chg_cause = 0;
4989}
4990/*
4991 * This function loops on servers registered for asynchronous
4992 * status changes
Christopher Faulet5d42e092017-10-16 12:00:40 +02004993 *
4994 * NOTE: No needs to lock <updated_servers> list because it is called inside the
4995 * sync point.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004996 */
4997void servers_update_status(void) {
4998 struct server *s, *stmp;
4999
5000 list_for_each_entry_safe(s, stmp, &updated_servers, update_status) {
5001 srv_update_status(s);
5002 LIST_DEL(&s->update_status);
5003 LIST_INIT(&s->update_status);
5004 }
5005}
5006
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005007/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005008 * Local variables:
5009 * c-indent-level: 8
5010 * c-basic-offset: 8
5011 * End:
5012 */