blob: 23e4cc98e0853706e826455c969ab400494c3413 [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 Houchard4e694042017-03-14 20:01:29 +010089void srv_set_dyncookie(struct server *s)
90{
91 struct proxy *p = s->proxy;
92 struct server *tmpserv;
93 char *tmpbuf;
94 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010095 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +010096 size_t buffer_len;
97 int addr_len;
98 int port;
99
100 if ((s->flags & SRV_F_COOKIESET) ||
101 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
102 s->proxy->dyncookie_key == NULL)
103 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100104 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100105
106 if (s->addr.ss_family != AF_INET &&
107 s->addr.ss_family != AF_INET6)
108 return;
109 /*
110 * Buffer to calculate the cookie value.
111 * The buffer contains the secret key + the server IP address
112 * + the TCP port.
113 */
114 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
115 /*
116 * The TCP port should use only 2 bytes, but is stored in
117 * an unsigned int in struct server, so let's use 4, to be
118 * on the safe side.
119 */
120 buffer_len = key_len + addr_len + 4;
121 tmpbuf = trash.str;
122 memcpy(tmpbuf, p->dyncookie_key, key_len);
123 memcpy(&(tmpbuf[key_len]),
124 s->addr.ss_family == AF_INET ?
125 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
126 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
127 addr_len);
128 /*
129 * Make sure it's the same across all the load balancers,
130 * no matter their endianness.
131 */
132 port = htonl(s->svc_port);
133 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
134 hash_value = XXH64(tmpbuf, buffer_len, 0);
135 memprintf(&s->cookie, "%016llx", hash_value);
136 if (!s->cookie)
137 return;
138 s->cklen = 16;
139 /*
140 * Check that we did not get a hash collision.
141 * Unlikely, but it can happen.
142 */
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200143 for (tmpserv = p->srv; tmpserv != NULL;
144 tmpserv = tmpserv->next) {
145 if (tmpserv == s)
146 continue;
147 if (tmpserv->cookie &&
148 strcmp(tmpserv->cookie, s->cookie) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100149 ha_warning("We generated two equal cookies for two different servers.\n"
150 "Please change the secret key for '%s'.\n",
151 s->proxy->id);
Olivier Houchard4e694042017-03-14 20:01:29 +0100152 }
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200153 }
Olivier Houchard4e694042017-03-14 20:01:29 +0100154}
155
Willy Tarreau21faa912012-10-10 08:27:36 +0200156/*
157 * Registers the server keyword list <kwl> as a list of valid keywords for next
158 * parsing sessions.
159 */
160void srv_register_keywords(struct srv_kw_list *kwl)
161{
162 LIST_ADDQ(&srv_keywords.list, &kwl->list);
163}
164
165/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
166 * keyword is found with a NULL ->parse() function, then an attempt is made to
167 * find one with a valid ->parse() function. This way it is possible to declare
168 * platform-dependant, known keywords as NULL, then only declare them as valid
169 * if some options are met. Note that if the requested keyword contains an
170 * opening parenthesis, everything from this point is ignored.
171 */
172struct srv_kw *srv_find_kw(const char *kw)
173{
174 int index;
175 const char *kwend;
176 struct srv_kw_list *kwl;
177 struct srv_kw *ret = NULL;
178
179 kwend = strchr(kw, '(');
180 if (!kwend)
181 kwend = kw + strlen(kw);
182
183 list_for_each_entry(kwl, &srv_keywords.list, list) {
184 for (index = 0; kwl->kw[index].kw != NULL; index++) {
185 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
186 kwl->kw[index].kw[kwend-kw] == 0) {
187 if (kwl->kw[index].parse)
188 return &kwl->kw[index]; /* found it !*/
189 else
190 ret = &kwl->kw[index]; /* may be OK */
191 }
192 }
193 }
194 return ret;
195}
196
197/* Dumps all registered "server" keywords to the <out> string pointer. The
198 * unsupported keywords are only dumped if their supported form was not
199 * found.
200 */
201void srv_dump_kws(char **out)
202{
203 struct srv_kw_list *kwl;
204 int index;
205
206 *out = NULL;
207 list_for_each_entry(kwl, &srv_keywords.list, list) {
208 for (index = 0; kwl->kw[index].kw != NULL; index++) {
209 if (kwl->kw[index].parse ||
210 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
211 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
212 kwl->scope,
213 kwl->kw[index].kw,
214 kwl->kw[index].skip ? " <arg>" : "",
215 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
216 kwl->kw[index].parse ? "" : " (not supported)");
217 }
218 }
219 }
220}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100221
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100222/* Parse the "addr" server keyword */
223static int srv_parse_addr(char **args, int *cur_arg,
224 struct proxy *curproxy, struct server *newsrv, char **err)
225{
226 char *errmsg, *arg;
227 struct sockaddr_storage *sk;
228 int port1, port2;
229 struct protocol *proto;
230
231 errmsg = NULL;
232 arg = args[*cur_arg + 1];
233
234 if (!*arg) {
235 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
236 goto err;
237 }
238
239 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
240 if (!sk) {
241 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
242 goto err;
243 }
244
245 proto = protocol_by_family(sk->ss_family);
246 if (!proto || !proto->connect) {
247 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
248 args[*cur_arg], arg);
249 goto err;
250 }
251
252 if (port1 != port2) {
253 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
254 args[*cur_arg], arg);
255 goto err;
256 }
257
258 newsrv->check.addr = newsrv->agent.addr = *sk;
259 newsrv->flags |= SRV_F_CHECKADDR;
260 newsrv->flags |= SRV_F_AGENTADDR;
261
262 return 0;
263
264 err:
265 free(errmsg);
266 return ERR_ALERT | ERR_FATAL;
267}
268
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100269/* Parse the "agent-check" server keyword */
270static int srv_parse_agent_check(char **args, int *cur_arg,
271 struct proxy *curproxy, struct server *newsrv, char **err)
272{
273 newsrv->do_agent = 1;
274 return 0;
275}
276
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100277/* Parse the "backup" server keyword */
278static int srv_parse_backup(char **args, int *cur_arg,
279 struct proxy *curproxy, struct server *newsrv, char **err)
280{
281 newsrv->flags |= SRV_F_BACKUP;
282 return 0;
283}
284
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100285/* Parse the "check" server keyword */
286static int srv_parse_check(char **args, int *cur_arg,
287 struct proxy *curproxy, struct server *newsrv, char **err)
288{
289 newsrv->do_check = 1;
290 return 0;
291}
292
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100293/* Parse the "check-send-proxy" server keyword */
294static int srv_parse_check_send_proxy(char **args, int *cur_arg,
295 struct proxy *curproxy, struct server *newsrv, char **err)
296{
297 newsrv->check.send_proxy = 1;
298 return 0;
299}
300
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100301/* Parse the "cookie" server keyword */
302static int srv_parse_cookie(char **args, int *cur_arg,
303 struct proxy *curproxy, struct server *newsrv, char **err)
304{
305 char *arg;
306
307 arg = args[*cur_arg + 1];
308 if (!*arg) {
309 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
310 return ERR_ALERT | ERR_FATAL;
311 }
312
313 free(newsrv->cookie);
314 newsrv->cookie = strdup(arg);
315 newsrv->cklen = strlen(arg);
316 newsrv->flags |= SRV_F_COOKIESET;
317 return 0;
318}
319
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100320/* Parse the "disabled" server keyword */
321static int srv_parse_disabled(char **args, int *cur_arg,
322 struct proxy *curproxy, struct server *newsrv, char **err)
323{
Emeric Brun52a91d32017-08-31 14:41:55 +0200324 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
325 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100326 newsrv->check.state |= CHK_ST_PAUSED;
327 newsrv->check.health = 0;
328 return 0;
329}
330
331/* Parse the "enabled" server keyword */
332static int srv_parse_enabled(char **args, int *cur_arg,
333 struct proxy *curproxy, struct server *newsrv, char **err)
334{
Emeric Brun52a91d32017-08-31 14:41:55 +0200335 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
336 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100337 newsrv->check.state &= ~CHK_ST_PAUSED;
338 newsrv->check.health = newsrv->check.rise;
339 return 0;
340}
341
Willy Tarreaudff55432012-10-10 17:51:05 +0200342/* parse the "id" server keyword */
343static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
344{
345 struct eb32_node *node;
346
347 if (!*args[*cur_arg + 1]) {
348 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
349 return ERR_ALERT | ERR_FATAL;
350 }
351
352 newsrv->puid = atol(args[*cur_arg + 1]);
353 newsrv->conf.id.key = newsrv->puid;
354
355 if (newsrv->puid <= 0) {
356 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
357 return ERR_ALERT | ERR_FATAL;
358 }
359
360 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
361 if (node) {
362 struct server *target = container_of(node, struct server, conf.id);
363 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
364 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
365 target->id);
366 return ERR_ALERT | ERR_FATAL;
367 }
368
369 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200370 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200371 return 0;
372}
373
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100374/* Parse the "namespace" server keyword */
375static int srv_parse_namespace(char **args, int *cur_arg,
376 struct proxy *curproxy, struct server *newsrv, char **err)
377{
378#ifdef CONFIG_HAP_NS
379 char *arg;
380
381 arg = args[*cur_arg + 1];
382 if (!*arg) {
383 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
384 return ERR_ALERT | ERR_FATAL;
385 }
386
387 if (!strcmp(arg, "*")) {
388 /* Use the namespace associated with the connection (if present). */
389 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
390 return 0;
391 }
392
393 /*
394 * As this parser may be called several times for the same 'default-server'
395 * object, or for a new 'server' instance deriving from a 'default-server'
396 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
397 */
398 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
399
400 newsrv->netns = netns_store_lookup(arg, strlen(arg));
401 if (!newsrv->netns)
402 newsrv->netns = netns_store_insert(arg);
403
404 if (!newsrv->netns) {
405 memprintf(err, "Cannot open namespace '%s'", arg);
406 return ERR_ALERT | ERR_FATAL;
407 }
408
409 return 0;
410#else
411 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
412 return ERR_ALERT | ERR_FATAL;
413#endif
414}
415
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100416/* Parse the "no-agent-check" server keyword */
417static int srv_parse_no_agent_check(char **args, int *cur_arg,
418 struct proxy *curproxy, struct server *newsrv, char **err)
419{
420 free_check(&newsrv->agent);
421 newsrv->agent.inter = 0;
422 newsrv->agent.port = 0;
423 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
424 newsrv->do_agent = 0;
425 return 0;
426}
427
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100428/* Parse the "no-backup" server keyword */
429static int srv_parse_no_backup(char **args, int *cur_arg,
430 struct proxy *curproxy, struct server *newsrv, char **err)
431{
432 newsrv->flags &= ~SRV_F_BACKUP;
433 return 0;
434}
435
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100436/* Parse the "no-check" server keyword */
437static int srv_parse_no_check(char **args, int *cur_arg,
438 struct proxy *curproxy, struct server *newsrv, char **err)
439{
440 free_check(&newsrv->check);
441 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
442 newsrv->do_check = 0;
443 return 0;
444}
445
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100446/* Parse the "no-check-send-proxy" server keyword */
447static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
448 struct proxy *curproxy, struct server *newsrv, char **err)
449{
450 newsrv->check.send_proxy = 0;
451 return 0;
452}
453
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100454/* Disable server PROXY protocol flags. */
455static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
456{
457 srv->pp_opts &= ~flags;
458 return 0;
459}
460
461/* Parse the "no-send-proxy" server keyword */
462static int srv_parse_no_send_proxy(char **args, int *cur_arg,
463 struct proxy *curproxy, struct server *newsrv, char **err)
464{
465 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
466}
467
468/* Parse the "no-send-proxy-v2" server keyword */
469static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
470 struct proxy *curproxy, struct server *newsrv, char **err)
471{
472 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
473}
474
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100475/* Parse the "non-stick" server keyword */
476static int srv_parse_non_stick(char **args, int *cur_arg,
477 struct proxy *curproxy, struct server *newsrv, char **err)
478{
479 newsrv->flags |= SRV_F_NON_STICK;
480 return 0;
481}
482
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100483/* Enable server PROXY protocol flags. */
484static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
485{
486 srv->pp_opts |= flags;
487 return 0;
488}
489
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100490/* Parse the "observe" server keyword */
491static int srv_parse_observe(char **args, int *cur_arg,
492 struct proxy *curproxy, struct server *newsrv, char **err)
493{
494 char *arg;
495
496 arg = args[*cur_arg + 1];
497 if (!*arg) {
498 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
499 return ERR_ALERT | ERR_FATAL;
500 }
501
502 if (!strcmp(arg, "none")) {
503 newsrv->observe = HANA_OBS_NONE;
504 }
505 else if (!strcmp(arg, "layer4")) {
506 newsrv->observe = HANA_OBS_LAYER4;
507 }
508 else if (!strcmp(arg, "layer7")) {
509 if (curproxy->mode != PR_MODE_HTTP) {
510 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
511 return ERR_ALERT;
512 }
513 newsrv->observe = HANA_OBS_LAYER7;
514 }
515 else {
516 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
517 "but got '%s'\n", args[*cur_arg], arg);
518 return ERR_ALERT | ERR_FATAL;
519 }
520
521 return 0;
522}
523
Frédéric Lécaille16186232017-03-14 16:42:49 +0100524/* Parse the "redir" server keyword */
525static int srv_parse_redir(char **args, int *cur_arg,
526 struct proxy *curproxy, struct server *newsrv, char **err)
527{
528 char *arg;
529
530 arg = args[*cur_arg + 1];
531 if (!*arg) {
532 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
533 return ERR_ALERT | ERR_FATAL;
534 }
535
536 free(newsrv->rdr_pfx);
537 newsrv->rdr_pfx = strdup(arg);
538 newsrv->rdr_len = strlen(arg);
539
540 return 0;
541}
542
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100543/* Parse the "send-proxy" server keyword */
544static int srv_parse_send_proxy(char **args, int *cur_arg,
545 struct proxy *curproxy, struct server *newsrv, char **err)
546{
547 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
548}
549
550/* Parse the "send-proxy-v2" server keyword */
551static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
552 struct proxy *curproxy, struct server *newsrv, char **err)
553{
554 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
555}
556
Frédéric Lécailledba97072017-03-17 15:33:50 +0100557
558/* Parse the "source" server keyword */
559static int srv_parse_source(char **args, int *cur_arg,
560 struct proxy *curproxy, struct server *newsrv, char **err)
561{
562 char *errmsg;
563 int port_low, port_high;
564 struct sockaddr_storage *sk;
565 struct protocol *proto;
566
567 errmsg = NULL;
568
569 if (!*args[*cur_arg + 1]) {
570 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
571 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
572 goto err;
573 }
574
575 /* 'sk' is statically allocated (no need to be freed). */
576 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
577 if (!sk) {
578 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
579 goto err;
580 }
581
582 proto = protocol_by_family(sk->ss_family);
583 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100584 ha_alert("'%s %s' : connect() not supported for this address family.\n",
585 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100586 goto err;
587 }
588
589 newsrv->conn_src.opts |= CO_SRC_BIND;
590 newsrv->conn_src.source_addr = *sk;
591
592 if (port_low != port_high) {
593 int i;
594
595 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100596 ha_alert("'%s' does not support port offsets (found '%s').\n",
597 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100598 goto err;
599 }
600
601 if (port_low <= 0 || port_low > 65535 ||
602 port_high <= 0 || port_high > 65535 ||
603 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100604 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 +0100605 goto err;
606 }
607 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
608 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
609 newsrv->conn_src.sport_range->ports[i] = port_low + i;
610 }
611
612 *cur_arg += 2;
613 while (*(args[*cur_arg])) {
614 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
615#if defined(CONFIG_HAP_TRANSPARENT)
616 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100617 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
618 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100619 goto err;
620 }
621 if (!strcmp(args[*cur_arg + 1], "client")) {
622 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
623 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
624 }
625 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
626 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
627 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
628 }
629 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
630 char *name, *end;
631
632 name = args[*cur_arg + 1] + 7;
633 while (isspace(*name))
634 name++;
635
636 end = name;
637 while (*end && !isspace(*end) && *end != ',' && *end != ')')
638 end++;
639
640 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
641 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
642 free(newsrv->conn_src.bind_hdr_name);
643 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
644 newsrv->conn_src.bind_hdr_len = end - name;
645 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
646 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
647 newsrv->conn_src.bind_hdr_occ = -1;
648
649 /* now look for an occurrence number */
650 while (isspace(*end))
651 end++;
652 if (*end == ',') {
653 end++;
654 name = end;
655 if (*end == '-')
656 end++;
657 while (isdigit((int)*end))
658 end++;
659 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
660 }
661
662 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100663 ha_alert("usesrc hdr_ip(name,num) does not support negative"
664 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100665 goto err;
666 }
667 }
668 else {
669 struct sockaddr_storage *sk;
670 int port1, port2;
671
672 /* 'sk' is statically allocated (no need to be freed). */
673 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
674 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100675 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100676 goto err;
677 }
678
679 proto = protocol_by_family(sk->ss_family);
680 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100681 ha_alert("'%s %s' : connect() not supported for this address family.\n",
682 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100683 goto err;
684 }
685
686 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100687 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
688 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100689 goto err;
690 }
691 newsrv->conn_src.tproxy_addr = *sk;
692 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
693 }
694 global.last_checks |= LSTCHK_NETADM;
695 *cur_arg += 2;
696 continue;
697#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100698 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 +0100699 goto err;
700#endif /* defined(CONFIG_HAP_TRANSPARENT) */
701 } /* "usesrc" */
702
703 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
704#ifdef SO_BINDTODEVICE
705 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100706 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100707 goto err;
708 }
709 free(newsrv->conn_src.iface_name);
710 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
711 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
712 global.last_checks |= LSTCHK_NETADM;
713#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100714 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100715 goto err;
716#endif
717 *cur_arg += 2;
718 continue;
719 }
720 /* this keyword in not an option of "source" */
721 break;
722 } /* while */
723
724 return 0;
725
726 err:
727 free(errmsg);
728 return ERR_ALERT | ERR_FATAL;
729}
730
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100731/* Parse the "stick" server keyword */
732static int srv_parse_stick(char **args, int *cur_arg,
733 struct proxy *curproxy, struct server *newsrv, char **err)
734{
735 newsrv->flags &= ~SRV_F_NON_STICK;
736 return 0;
737}
738
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100739/* Parse the "track" server keyword */
740static int srv_parse_track(char **args, int *cur_arg,
741 struct proxy *curproxy, struct server *newsrv, char **err)
742{
743 char *arg;
744
745 arg = args[*cur_arg + 1];
746 if (!*arg) {
747 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
748 return ERR_ALERT | ERR_FATAL;
749 }
750
751 free(newsrv->trackit);
752 newsrv->trackit = strdup(arg);
753
754 return 0;
755}
756
Frédéric Lécailledba97072017-03-17 15:33:50 +0100757
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200758/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200759 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200760 * shutdown.
761 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200762void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200763{
Willy Tarreau87b09662015-04-03 00:22:06 +0200764 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200765
Willy Tarreau87b09662015-04-03 00:22:06 +0200766 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
767 if (stream->srv_conn == srv)
768 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200769}
770
771/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200772 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200773 * the reason for the shutdown.
774 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200775void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200776{
777 struct server *srv;
778
779 for (srv = px->srv; srv != NULL; srv = srv->next)
780 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200781 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200782}
783
Willy Tarreaubda92272014-05-20 21:55:30 +0200784/* Appends some information to a message string related to a server going UP or
785 * DOWN. If both <forced> and <reason> are null and the server tracks another
786 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200787 * If <check> is non-null, an entire string describing the check result will be
788 * appended after a comma and a space (eg: to report some information from the
789 * check that changed the state). In the other case, the string will be built
790 * using the check results stored into the struct server if present.
791 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200792 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200793 */
Emeric Brun5a133512017-10-19 14:42:30 +0200794void srv_append_status(struct chunk *msg, struct server *s, struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200795{
Emeric Brun5a133512017-10-19 14:42:30 +0200796 short status = s->op_st_chg.status;
797 short code = s->op_st_chg.code;
798 long duration = s->op_st_chg.duration;
799 char *desc = s->op_st_chg.reason;
800
801 if (check) {
802 status = check->status;
803 code = check->code;
804 duration = check->duration;
805 desc = check->desc;
806 }
807
808 if (status != -1) {
809 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
810
811 if (status >= HCHK_STATUS_L57DATA)
812 chunk_appendf(msg, ", code: %d", code);
813
814 if (desc && *desc) {
815 struct chunk src;
816
817 chunk_appendf(msg, ", info: \"");
818
819 chunk_initlen(&src, desc, 0, strlen(desc));
820 chunk_asciiencode(msg, &src, '"');
821
822 chunk_appendf(msg, "\"");
823 }
824
825 if (duration >= 0)
826 chunk_appendf(msg, ", check duration: %ldms", duration);
827 }
828 else if (desc && *desc) {
829 chunk_appendf(msg, ", %s", desc);
830 }
831 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200832 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200833 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200834
835 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200836 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200837 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
838 " %d sessions active, %d requeued, %d remaining in queue",
839 s->proxy->srv_act, s->proxy->srv_bck,
840 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
841 s->cur_sess, xferred, s->nbpend);
842 else
843 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
844 " %d sessions requeued, %d total in queue",
845 s->proxy->srv_act, s->proxy->srv_bck,
846 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
847 xferred, s->nbpend);
848 }
849}
850
Emeric Brun5a133512017-10-19 14:42:30 +0200851/* Marks server <s> down, regardless of its checks' statuses. The server is
852 * registered in a list to postpone the counting of the remaining servers on
853 * the proxy and transfers queued streams whenever possible to other servers at
854 * a sync point. Maintenance servers are ignored. It stores the <reason> if
855 * non-null as the reason for going down or the available data from the check
856 * struct to recompute this reason later.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200857 */
Emeric Brun5a133512017-10-19 14:42:30 +0200858void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200859{
860 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200861
Emeric Brun64cc49c2017-10-03 14:46:45 +0200862 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200863 return;
864
Emeric Brun52a91d32017-08-31 14:41:55 +0200865 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200866 *s->op_st_chg.reason = 0;
867 s->op_st_chg.status = -1;
868 if (reason) {
869 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
870 }
871 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100872 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200873 s->op_st_chg.code = check->code;
874 s->op_st_chg.status = check->status;
875 s->op_st_chg.duration = check->duration;
876 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200877
Christopher Faulet5d42e092017-10-16 12:00:40 +0200878 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200879 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100880 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200881 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100882 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200883 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200884}
885
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200886/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200887 * in maintenance. The server is registered in a list to postpone the counting
888 * of the remaining servers on the proxy and tries to grab requests from the
889 * proxy at a sync point. Maintenance servers are ignored. It stores the
890 * <reason> if non-null as the reason for going down or the available data
891 * from the check struct to recompute this reason later.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200892 */
Emeric Brun5a133512017-10-19 14:42:30 +0200893void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200894{
895 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200896
Emeric Brun64cc49c2017-10-03 14:46:45 +0200897 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200898 return;
899
Emeric Brun52a91d32017-08-31 14:41:55 +0200900 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200901 return;
902
Emeric Brun52a91d32017-08-31 14:41:55 +0200903 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200904 *s->op_st_chg.reason = 0;
905 s->op_st_chg.status = -1;
906 if (reason) {
907 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
908 }
909 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100910 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200911 s->op_st_chg.code = check->code;
912 s->op_st_chg.status = check->status;
913 s->op_st_chg.duration = check->duration;
914 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200915
Emeric Brun64cc49c2017-10-03 14:46:45 +0200916 if (s->slowstart <= 0)
917 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200918
Christopher Faulet5d42e092017-10-16 12:00:40 +0200919 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200920 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100921 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200922 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100923 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200924 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200925}
926
Willy Tarreau8eb77842014-05-21 13:54:57 +0200927/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +0200928 * isn't in maintenance. The server is registered in a list to postpone the
929 * counting of the remaining servers on the proxy and tries to grab requests
930 * from the proxy. Maintenance servers are ignored. It stores the
931 * <reason> if non-null as the reason for going down or the available data
932 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +0200933 * up. Note that it makes use of the trash to build the log strings, so <reason>
934 * must not be placed there.
935 */
Emeric Brun5a133512017-10-19 14:42:30 +0200936void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200937{
938 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200939
Emeric Brun64cc49c2017-10-03 14:46:45 +0200940 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200941 return;
942
Emeric Brun52a91d32017-08-31 14:41:55 +0200943 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200944 return;
945
Emeric Brun52a91d32017-08-31 14:41:55 +0200946 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +0200947 *s->op_st_chg.reason = 0;
948 s->op_st_chg.status = -1;
949 if (reason) {
950 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
951 }
952 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100953 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200954 s->op_st_chg.code = check->code;
955 s->op_st_chg.status = check->status;
956 s->op_st_chg.duration = check->duration;
957 }
Willy Tarreau8eb77842014-05-21 13:54:57 +0200958
Christopher Faulet5d42e092017-10-16 12:00:40 +0200959 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200960 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100961 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200962 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100963 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200964 }
Willy Tarreau8eb77842014-05-21 13:54:57 +0200965}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200966
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200967/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
968 * enforce either maint mode or drain mode. It is not allowed to set more than
969 * one flag at once. The equivalent "inherited" flag is propagated to all
970 * tracking servers. Maintenance mode disables health checks (but not agent
971 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100972 * is done. If <cause> is non-null, it will be displayed at the end of the log
973 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200974 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100975void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200976{
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200977 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200978
979 if (!mode)
980 return;
981
982 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +0200983 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200984 return;
985
Emeric Brun52a91d32017-08-31 14:41:55 +0200986 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +0200987 if (cause)
988 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
989
Christopher Faulet5d42e092017-10-16 12:00:40 +0200990 srv_register_update(s);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200991
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200992 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +0200993 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
994 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200995 return;
996
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200997 /* compute the inherited flag to propagate */
998 if (mode & SRV_ADMF_MAINT)
999 mode = SRV_ADMF_IMAINT;
1000 else if (mode & SRV_ADMF_DRAIN)
1001 mode = SRV_ADMF_IDRAIN;
1002
Emeric Brun9f0b4582017-10-23 14:39:51 +02001003 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001004 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001005 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001006 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001007 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001008}
1009
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001010/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1011 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1012 * than one flag at once. The equivalent "inherited" flag is propagated to all
1013 * tracking servers. Leaving maintenance mode re-enables health checks. When
1014 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001015 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001016void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001017{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001018 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001019
1020 if (!mode)
1021 return;
1022
1023 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001024 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001025 return;
1026
Emeric Brun52a91d32017-08-31 14:41:55 +02001027 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001028
Christopher Faulet5d42e092017-10-16 12:00:40 +02001029 srv_register_update(s);
1030
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001031 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001032 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1033 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001034 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001035
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001036 if (mode & SRV_ADMF_MAINT)
1037 mode = SRV_ADMF_IMAINT;
1038 else if (mode & SRV_ADMF_DRAIN)
1039 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001040
Emeric Brun9f0b4582017-10-23 14:39:51 +02001041 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001042 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001043 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001044 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001045 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001046}
1047
Willy Tarreau757478e2016-11-03 19:22:19 +01001048/* principle: propagate maint and drain to tracking servers. This is useful
1049 * upon startup so that inherited states are correct.
1050 */
1051static void srv_propagate_admin_state(struct server *srv)
1052{
1053 struct server *srv2;
1054
1055 if (!srv->trackers)
1056 return;
1057
1058 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001059 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001060 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001061 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001062
Emeric Brun52a91d32017-08-31 14:41:55 +02001063 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001064 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001065 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001066 }
1067}
1068
1069/* Compute and propagate the admin states for all servers in proxy <px>.
1070 * Only servers *not* tracking another one are considered, because other
1071 * ones will be handled when the server they track is visited.
1072 */
1073void srv_compute_all_admin_states(struct proxy *px)
1074{
1075 struct server *srv;
1076
1077 for (srv = px->srv; srv; srv = srv->next) {
1078 if (srv->track)
1079 continue;
1080 srv_propagate_admin_state(srv);
1081 }
1082}
1083
Willy Tarreaudff55432012-10-10 17:51:05 +02001084/* Note: must not be declared <const> as its list will be overwritten.
1085 * Please take care of keeping this list alphabetically sorted, doing so helps
1086 * all code contributors.
1087 * Optional keywords are also declared with a NULL ->parse() function so that
1088 * the config parser can report an appropriate error when a known keyword was
1089 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001090 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001091 */
1092static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001093 { "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 +01001094 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001095 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001096 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001097 { "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 +01001098 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001099 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1100 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001101 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001102 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001103 { "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 +01001104 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001105 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001106 { "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 +01001107 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1108 { "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 +01001109 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001110 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Frédéric Lécaille16186232017-03-14 16:42:49 +01001111 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001112 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1113 { "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 +02001114 { "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 +01001115 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001116 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001117 { NULL, NULL, 0 },
1118}};
1119
1120__attribute__((constructor))
1121static void __listener_init(void)
1122{
1123 srv_register_keywords(&srv_kws);
1124}
1125
Willy Tarreau004e0452013-11-21 11:22:01 +01001126/* Recomputes the server's eweight based on its state, uweight, the current time,
1127 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1128 * state is automatically disabled if the time is elapsed.
1129 */
1130void server_recalc_eweight(struct server *sv)
1131{
1132 struct proxy *px = sv->proxy;
1133 unsigned w;
1134
1135 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1136 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001137 if (sv->next_state == SRV_ST_STARTING)
1138 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001139 }
1140
1141 /* We must take care of not pushing the server to full throttle during slow starts.
1142 * It must also start immediately, at least at the minimal step when leaving maintenance.
1143 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001144 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001145 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1146 else
1147 w = px->lbprm.wdiv;
1148
Emeric Brun52a91d32017-08-31 14:41:55 +02001149 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001150
Christopher Faulet5d42e092017-10-16 12:00:40 +02001151 srv_register_update(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001152}
1153
Willy Tarreaubaaee002006-06-26 02:48:02 +02001154/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001155 * Parses weight_str and configures sv accordingly.
1156 * Returns NULL on success, error message string otherwise.
1157 */
1158const char *server_parse_weight_change_request(struct server *sv,
1159 const char *weight_str)
1160{
1161 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001162 long int w;
1163 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001164
1165 px = sv->proxy;
1166
1167 /* if the weight is terminated with '%', it is set relative to
1168 * the initial weight, otherwise it is absolute.
1169 */
1170 if (!*weight_str)
1171 return "Require <weight> or <weight%>.\n";
1172
Simon Hormanb796afa2013-02-12 10:45:53 +09001173 w = strtol(weight_str, &end, 10);
1174 if (end == weight_str)
1175 return "Empty weight string empty or preceded by garbage";
1176 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001177 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001178 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001179 /* Avoid integer overflow */
1180 if (w > 25600)
1181 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001182 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001183 if (w > 256)
1184 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001185 }
1186 else if (w < 0 || w > 256)
1187 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001188 else if (end[0] != '\0')
1189 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001190
1191 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1192 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1193
1194 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001195 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001196
1197 return NULL;
1198}
1199
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001200/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001201 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1202 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001203 * Returns:
1204 * - error string on error
1205 * - NULL on success
1206 */
1207const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001208 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001209{
1210 unsigned char ip[INET6_ADDRSTRLEN];
1211
1212 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001213 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001214 return NULL;
1215 }
1216 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001217 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001218 return NULL;
1219 }
1220
1221 return "Could not understand IP address format.\n";
1222}
1223
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001224const char *server_parse_maxconn_change_request(struct server *sv,
1225 const char *maxconn_str)
1226{
1227 long int v;
1228 char *end;
1229
1230 if (!*maxconn_str)
1231 return "Require <maxconn>.\n";
1232
1233 v = strtol(maxconn_str, &end, 10);
1234 if (end == maxconn_str)
1235 return "maxconn string empty or preceded by garbage";
1236 else if (end[0] != '\0')
1237 return "Trailing garbage in maxconn string";
1238
1239 if (sv->maxconn == sv->minconn) { // static maxconn
1240 sv->maxconn = sv->minconn = v;
1241 } else { // dynamic maxconn
1242 sv->maxconn = v;
1243 }
1244
1245 if (may_dequeue_tasks(sv, sv->proxy))
1246 process_srv_queue(sv);
1247
1248 return NULL;
1249}
1250
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001251#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001252static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1253 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001254{
1255 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001256 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001257 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001258 NULL,
1259 };
1260
1261 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001262 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001263
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001264 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1265}
1266
1267static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1268{
1269 struct sample_expr *expr;
1270
1271 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 +01001272 if (!expr) {
1273 memprintf(err, "error detected while parsing sni expression : %s", *err);
1274 return ERR_ALERT | ERR_FATAL;
1275 }
1276
1277 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1278 memprintf(err, "error detected while parsing sni expression : "
1279 " fetch method '%s' extracts information from '%s', "
1280 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001281 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001282 return ERR_ALERT | ERR_FATAL;
1283 }
1284
1285 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1286 release_sample_expr(newsrv->ssl_ctx.sni);
1287 newsrv->ssl_ctx.sni = expr;
1288
1289 return 0;
1290}
1291#endif
1292
1293static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1294{
1295 if (err && *err) {
1296 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001297 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 +01001298 }
1299 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001300 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1301 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001302}
1303
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001304static void srv_conn_src_sport_range_cpy(struct server *srv,
1305 struct server *src)
1306{
1307 int range_sz;
1308
1309 range_sz = src->conn_src.sport_range->size;
1310 if (range_sz > 0) {
1311 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1312 if (srv->conn_src.sport_range != NULL) {
1313 int i;
1314
1315 for (i = 0; i < range_sz; i++) {
1316 srv->conn_src.sport_range->ports[i] =
1317 src->conn_src.sport_range->ports[i];
1318 }
1319 }
1320 }
1321}
1322
1323/*
1324 * Copy <src> server connection source settings to <srv> server everything needed.
1325 */
1326static void srv_conn_src_cpy(struct server *srv, struct server *src)
1327{
1328 srv->conn_src.opts = src->conn_src.opts;
1329 srv->conn_src.source_addr = src->conn_src.source_addr;
1330
1331 /* Source port range copy. */
1332 if (src->conn_src.sport_range != NULL)
1333 srv_conn_src_sport_range_cpy(srv, src);
1334
1335#ifdef CONFIG_HAP_TRANSPARENT
1336 if (src->conn_src.bind_hdr_name != NULL) {
1337 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1338 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1339 }
1340 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1341 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1342#endif
1343 if (src->conn_src.iface_name != NULL)
1344 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1345}
1346
1347/*
1348 * Copy <src> server SSL settings to <srv> server allocating
1349 * everything needed.
1350 */
1351#if defined(USE_OPENSSL)
1352static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1353{
1354 if (src->ssl_ctx.ca_file != NULL)
1355 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1356 if (src->ssl_ctx.crl_file != NULL)
1357 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1358 if (src->ssl_ctx.client_crt != NULL)
1359 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1360
1361 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1362
1363 if (src->ssl_ctx.verify_host != NULL)
1364 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1365 if (src->ssl_ctx.ciphers != NULL)
1366 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1367 if (src->sni_expr != NULL)
1368 srv->sni_expr = strdup(src->sni_expr);
1369}
1370#endif
1371
1372/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001373 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001374 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001375 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001376 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001377static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001378{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001379 char *hostname_dn;
1380 int hostname_len, hostname_dn_len;
1381
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001382 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001383 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001384
Christopher Faulet67957bd2017-09-27 11:00:59 +02001385 hostname_len = strlen(hostname);
1386 hostname_dn = trash.str;
1387 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1388 hostname_dn, trash.size);
1389 if (hostname_dn_len == -1)
1390 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001391
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001392
Christopher Faulet67957bd2017-09-27 11:00:59 +02001393 free(srv->hostname);
1394 free(srv->hostname_dn);
1395 srv->hostname = strdup(hostname);
1396 srv->hostname_dn = strdup(hostname_dn);
1397 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001398 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001399 goto err;
1400
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001401 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001402
1403 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001404 free(srv->hostname); srv->hostname = NULL;
1405 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001406 return -1;
1407}
1408
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001409/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001410 * Copy <src> server settings to <srv> server allocating
1411 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001412 * This function is not supposed to be called at any time, but only
1413 * during server settings parsing or during server allocations from
1414 * a server template, and just after having calloc()'ed a new server.
1415 * So, <src> may only be a default server (when parsing server settings)
1416 * or a server template (during server allocations from a server template).
1417 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1418 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001419 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001420static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001421{
1422 /* Connection source settings copy */
1423 srv_conn_src_cpy(srv, src);
1424
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001425 if (srv_tmpl) {
1426 srv->addr = src->addr;
1427 srv->svc_port = src->svc_port;
1428 }
1429
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001430 srv->pp_opts = src->pp_opts;
1431 if (src->rdr_pfx != NULL) {
1432 srv->rdr_pfx = strdup(src->rdr_pfx);
1433 srv->rdr_len = src->rdr_len;
1434 }
1435 if (src->cookie != NULL) {
1436 srv->cookie = strdup(src->cookie);
1437 srv->cklen = src->cklen;
1438 }
1439 srv->use_ssl = src->use_ssl;
1440 srv->check.addr = srv->agent.addr = src->check.addr;
1441 srv->check.use_ssl = src->check.use_ssl;
1442 srv->check.port = src->check.port;
1443 /* Note: 'flags' field has potentially been already initialized. */
1444 srv->flags |= src->flags;
1445 srv->do_check = src->do_check;
1446 srv->do_agent = src->do_agent;
1447 if (srv->check.port)
1448 srv->flags |= SRV_F_CHECKPORT;
1449 srv->check.inter = src->check.inter;
1450 srv->check.fastinter = src->check.fastinter;
1451 srv->check.downinter = src->check.downinter;
1452 srv->agent.use_ssl = src->agent.use_ssl;
1453 srv->agent.port = src->agent.port;
1454 if (src->agent.send_string != NULL)
1455 srv->agent.send_string = strdup(src->agent.send_string);
1456 srv->agent.send_string_len = src->agent.send_string_len;
1457 srv->agent.inter = src->agent.inter;
1458 srv->agent.fastinter = src->agent.fastinter;
1459 srv->agent.downinter = src->agent.downinter;
1460 srv->maxqueue = src->maxqueue;
1461 srv->minconn = src->minconn;
1462 srv->maxconn = src->maxconn;
1463 srv->slowstart = src->slowstart;
1464 srv->observe = src->observe;
1465 srv->onerror = src->onerror;
1466 srv->onmarkeddown = src->onmarkeddown;
1467 srv->onmarkedup = src->onmarkedup;
1468 if (src->trackit != NULL)
1469 srv->trackit = strdup(src->trackit);
1470 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1471 srv->uweight = srv->iweight = src->iweight;
1472
1473 srv->check.send_proxy = src->check.send_proxy;
1474 /* health: up, but will fall down at first failure */
1475 srv->check.rise = srv->check.health = src->check.rise;
1476 srv->check.fall = src->check.fall;
1477
1478 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001479 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1480 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1481 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001482 srv->check.state |= CHK_ST_PAUSED;
1483 srv->check.health = 0;
1484 }
1485
1486 /* health: up but will fall down at first failure */
1487 srv->agent.rise = srv->agent.health = src->agent.rise;
1488 srv->agent.fall = src->agent.fall;
1489
1490 if (src->resolvers_id != NULL)
1491 srv->resolvers_id = strdup(src->resolvers_id);
1492 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1493 if (srv->dns_opts.family_prio == AF_UNSPEC)
1494 srv->dns_opts.family_prio = AF_INET6;
1495 memcpy(srv->dns_opts.pref_net,
1496 src->dns_opts.pref_net,
1497 sizeof srv->dns_opts.pref_net);
1498 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1499
1500 srv->init_addr_methods = src->init_addr_methods;
1501 srv->init_addr = src->init_addr;
1502#if defined(USE_OPENSSL)
1503 srv_ssl_settings_cpy(srv, src);
1504#endif
1505#ifdef TCP_USER_TIMEOUT
1506 srv->tcp_ut = src->tcp_ut;
1507#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001508 if (srv_tmpl)
1509 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001510}
1511
1512static struct server *new_server(struct proxy *proxy)
1513{
1514 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001515 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001516
1517 srv = calloc(1, sizeof *srv);
1518 if (!srv)
1519 return NULL;
1520
1521 srv->obj_type = OBJ_TYPE_SERVER;
1522 srv->proxy = proxy;
1523 LIST_INIT(&srv->actconns);
1524 LIST_INIT(&srv->pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02001525
1526 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1527 goto free_srv;
1528 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1529 goto free_priv_conns;
1530 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1531 goto free_idle_conns;
1532
1533 for (i = 0; i < global.nbthread; i++) {
1534 LIST_INIT(&srv->priv_conns[i]);
1535 LIST_INIT(&srv->idle_conns[i]);
1536 LIST_INIT(&srv->safe_conns[i]);
1537 }
1538
Emeric Brun64cc49c2017-10-03 14:46:45 +02001539 LIST_INIT(&srv->update_status);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001540
Emeric Brun52a91d32017-08-31 14:41:55 +02001541 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001542 srv->last_change = now.tv_sec;
1543
1544 srv->check.status = HCHK_STATUS_INI;
1545 srv->check.server = srv;
1546 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1547
1548 srv->agent.status = HCHK_STATUS_INI;
1549 srv->agent.server = srv;
1550 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1551
1552 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001553
1554 free_idle_conns:
1555 free(srv->idle_conns);
1556 free_priv_conns:
1557 free(srv->priv_conns);
1558 free_srv:
1559 free(srv);
1560 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001561}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001562
1563/*
1564 * Validate <srv> server health-check settings.
1565 * Returns 0 if everything is OK, -1 if not.
1566 */
1567static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1568{
1569 struct tcpcheck_rule *r = NULL;
1570 struct list *l;
1571
1572 /*
1573 * We need at least a service port, a check port or the first tcp-check rule must
1574 * be a 'connect' one when checking an IPv4/IPv6 server.
1575 */
1576 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1577 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1578 return 0;
1579
1580 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1581 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001582 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1583 "Check has been disabled.\n",
1584 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001585 return -1;
1586 }
1587
1588 /* search the first action (connect / send / expect) in the list */
1589 l = &srv->proxy->tcpcheck_rules;
1590 list_for_each_entry(r, l, list) {
1591 if (r->action != TCPCHK_ACT_COMMENT)
1592 break;
1593 }
1594
1595 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001596 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1597 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1598 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001599 return -1;
1600 }
1601
1602 /* scan the tcp-check ruleset to ensure a port has been configured */
1603 l = &srv->proxy->tcpcheck_rules;
1604 list_for_each_entry(r, l, list) {
1605 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001606 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1607 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1608 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001609 return -1;
1610 }
1611 }
1612
1613 return 0;
1614}
1615
1616/*
1617 * Initialize <srv> health-check structure.
1618 * Returns the error string in case of memory allocation failure, NULL if not.
1619 */
1620static const char *do_health_check_init(struct server *srv, int check_type, int state)
1621{
1622 const char *ret;
1623
1624 if (!srv->do_check)
1625 return NULL;
1626
1627 ret = init_check(&srv->check, check_type);
1628 if (ret)
1629 return ret;
1630
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001631 srv->check.state |= state;
1632 global.maxsock++;
1633
1634 return NULL;
1635}
1636
1637static int server_health_check_init(const char *file, int linenum,
1638 struct server *srv, struct proxy *curproxy)
1639{
1640 const char *ret;
1641
1642 if (!srv->do_check)
1643 return 0;
1644
1645 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001646 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1647 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001648 return ERR_ALERT | ERR_FATAL;
1649 }
1650
1651 if (server_healthcheck_validate(file, linenum, srv) < 0)
1652 return ERR_ALERT | ERR_ABORT;
1653
1654 /* note: check type will be set during the config review phase */
1655 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1656 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001657 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001658 return ERR_ALERT | ERR_ABORT;
1659 }
1660
1661 return 0;
1662}
1663
1664/*
1665 * Initialize <srv> agent check structure.
1666 * Returns the error string in case of memory allocation failure, NULL if not.
1667 */
1668static const char *do_server_agent_check_init(struct server *srv, int state)
1669{
1670 const char *ret;
1671
1672 if (!srv->do_agent)
1673 return NULL;
1674
1675 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1676 if (ret)
1677 return ret;
1678
1679 if (!srv->agent.inter)
1680 srv->agent.inter = srv->check.inter;
1681
1682 srv->agent.state |= state;
1683 global.maxsock++;
1684
1685 return NULL;
1686}
1687
1688static int server_agent_check_init(const char *file, int linenum,
1689 struct server *srv, struct proxy *curproxy)
1690{
1691 const char *ret;
1692
1693 if (!srv->do_agent)
1694 return 0;
1695
1696 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001697 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 +02001698 file, linenum, srv->id);
1699 return ERR_ALERT | ERR_FATAL;
1700 }
1701
1702 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1703 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001704 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001705 return ERR_ALERT | ERR_ABORT;
1706 }
1707
1708 return 0;
1709}
1710
1711#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1712static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1713 struct server *srv, struct proxy *proxy)
1714{
1715 int ret;
1716 char *err = NULL;
1717
1718 if (!srv->sni_expr)
1719 return 0;
1720
1721 ret = server_parse_sni_expr(srv, proxy, &err);
1722 if (!ret)
1723 return 0;
1724
1725 display_parser_err(file, linenum, args, cur_arg, &err);
1726 free(err);
1727
1728 return ret;
1729}
1730#endif
1731
1732/*
1733 * Server initializations finalization.
1734 * Initialize health check, agent check and SNI expression if enabled.
1735 * Must not be called for a default server instance.
1736 */
1737static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1738 struct server *srv, struct proxy *px)
1739{
1740 int ret;
1741
1742 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1743 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1744 return ret;
1745 }
1746
1747#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1748 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1749 return ret;
1750#endif
1751
1752 if (srv->flags & SRV_F_BACKUP)
1753 px->srv_bck++;
1754 else
1755 px->srv_act++;
1756 srv_lb_commit_status(srv);
1757
1758 return 0;
1759}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001760
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001761/*
1762 * Parse as much as possible such a range string argument: low[-high]
1763 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1764 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1765 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1766 * Returns 0 if succeeded, -1 if not.
1767 */
1768static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1769{
1770 char *nb_high_arg;
1771
1772 *nb_high = 0;
1773 chunk_printf(&trash, "%s", arg);
1774 *nb_low = atoi(trash.str);
1775
1776 if ((nb_high_arg = strchr(trash.str, '-'))) {
1777 *nb_high_arg++ = '\0';
1778 *nb_high = atoi(nb_high_arg);
1779 }
1780 else {
1781 *nb_high += *nb_low;
1782 *nb_low = 1;
1783 }
1784
1785 if (*nb_low < 0 || *nb_high < *nb_low)
1786 return -1;
1787
1788 return 0;
1789}
1790
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001791static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1792{
1793 chunk_printf(&trash, "%s%d", prefix, nb);
1794 free(srv->id);
1795 srv->id = strdup(trash.str);
1796}
1797
1798/*
1799 * Initialize as much as possible servers from <srv> server template.
1800 * Note that a server template is a special server with
1801 * a few different parameters than a server which has
1802 * been parsed mostly the same way as a server.
1803 * Returns the number of servers succesfully allocated,
1804 * 'srv' template included.
1805 */
1806static int server_template_init(struct server *srv, struct proxy *px)
1807{
1808 int i;
1809 struct server *newsrv;
1810
1811 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1812 int check_init_state;
1813 int agent_init_state;
1814
1815 newsrv = new_server(px);
1816 if (!newsrv)
1817 goto err;
1818
1819 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001820 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001821#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1822 if (newsrv->sni_expr) {
1823 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1824 if (!newsrv->ssl_ctx.sni)
1825 goto err;
1826 }
1827#endif
1828 /* Set this new server ID. */
1829 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1830
1831 /* Initial checks states. */
1832 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1833 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1834
1835 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1836 do_server_agent_check_init(newsrv, agent_init_state))
1837 goto err;
1838
1839 /* Linked backwards first. This will be restablished after parsing. */
1840 newsrv->next = px->srv;
1841 px->srv = newsrv;
1842 }
1843 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1844
1845 return i - srv->tmpl_info.nb_low;
1846
1847 err:
1848 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1849 if (newsrv) {
1850#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1851 release_sample_expr(newsrv->ssl_ctx.sni);
1852#endif
1853 free_check(&newsrv->agent);
1854 free_check(&newsrv->check);
1855 }
1856 free(newsrv);
1857 return i - srv->tmpl_info.nb_low;
1858}
1859
Willy Tarreau272adea2014-03-31 10:39:59 +02001860int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1861{
1862 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001863 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001864 char *errmsg = NULL;
1865 int err_code = 0;
1866 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001867 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001868
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001869 if (!strcmp(args[0], "server") ||
1870 !strcmp(args[0], "default-server") ||
1871 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001872 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001873 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001874 int srv = !defsrv && !strcmp(args[0], "server");
1875 int srv_tmpl = !defsrv && !srv;
1876 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001877
1878 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001879 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001880 err_code |= ERR_ALERT | ERR_FATAL;
1881 goto out;
1882 }
1883 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1884 err_code |= ERR_ALERT | ERR_FATAL;
1885
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001886 /* There is no mandatory first arguments for default server. */
1887 if (srv) {
1888 if (!*args[2]) {
1889 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001890 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001891 file, linenum, args[0]);
1892 err_code |= ERR_ALERT | ERR_FATAL;
1893 goto out;
1894 }
1895
1896 err = invalid_char(args[1]);
1897 }
1898 else if (srv_tmpl) {
1899 if (!*args[3]) {
1900 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001901 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 +02001902 file, linenum, args[0]);
1903 err_code |= ERR_ALERT | ERR_FATAL;
1904 goto out;
1905 }
1906
1907 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001908 }
1909
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001910 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001911 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 +02001912 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001913 err_code |= ERR_ALERT | ERR_FATAL;
1914 goto out;
1915 }
1916
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001917 cur_arg = 2;
1918 if (srv_tmpl) {
1919 /* Parse server-template <nb | range> arg. */
1920 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001921 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001922 file, linenum, args[0], args[cur_arg]);
1923 err_code |= ERR_ALERT | ERR_FATAL;
1924 goto out;
1925 }
1926 cur_arg++;
1927 }
1928
Willy Tarreau272adea2014-03-31 10:39:59 +02001929 if (!defsrv) {
1930 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001931 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001932 struct protocol *proto;
1933
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001934 newsrv = new_server(curproxy);
1935 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001936 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02001937 err_code |= ERR_ALERT | ERR_ABORT;
1938 goto out;
1939 }
1940
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001941 if (srv_tmpl) {
1942 newsrv->tmpl_info.nb_low = tmpl_range_low;
1943 newsrv->tmpl_info.nb_high = tmpl_range_high;
1944 }
1945
Willy Tarreau272adea2014-03-31 10:39:59 +02001946 /* the servers are linked backwards first */
1947 newsrv->next = curproxy->srv;
1948 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02001949 newsrv->conf.file = strdup(file);
1950 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001951 /* Note: for a server template, its id is its prefix.
1952 * This is a temporary id which will be used for server allocations to come
1953 * after parsing.
1954 */
1955 if (srv)
1956 newsrv->id = strdup(args[1]);
1957 else
1958 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001959
1960 /* several ways to check the port component :
1961 * - IP => port=+0, relative (IPv4 only)
1962 * - IP: => port=+0, relative
1963 * - IP:N => port=N, absolute
1964 * - IP:+N => port=+N, relative
1965 * - IP:-N => port=-N, relative
1966 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001967 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001968 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001969 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02001970 err_code |= ERR_ALERT | ERR_FATAL;
1971 goto out;
1972 }
1973
1974 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001975 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001976 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02001977 file, linenum, args[0], args[1]);
1978 err_code |= ERR_ALERT | ERR_FATAL;
1979 goto out;
1980 }
1981
1982 if (!port1 || !port2) {
1983 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001984 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001985 }
1986 else if (port1 != port2) {
1987 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001988 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02001989 file, linenum, args[0], args[1], args[2]);
1990 err_code |= ERR_ALERT | ERR_FATAL;
1991 goto out;
1992 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001993
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001994 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02001995 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001996 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02001997 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001998 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
1999 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2000 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002001 err_code |= ERR_ALERT | ERR_FATAL;
2002 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002003 }
2004 }
2005 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002006 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002007 file, linenum, newsrv->id);
2008 err_code |= ERR_ALERT | ERR_FATAL;
2009 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002010 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002011 }
2012
Willy Tarreau272adea2014-03-31 10:39:59 +02002013 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002014 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002015
Olivier Houchard8da5f982017-08-04 18:35:36 +02002016 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002017 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002018 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002019 err_code |= ERR_ALERT | ERR_FATAL;
2020 goto out;
2021 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002022
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002023 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002024 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002025 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002026 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002027 } else {
2028 newsrv = &curproxy->defsrv;
2029 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002030 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002031 }
2032
2033 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002034 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002035 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2036 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002037 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002038 file, linenum, *err, newsrv->id);
2039 err_code |= ERR_ALERT | ERR_FATAL;
2040 goto out;
2041 }
2042 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002043 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002044 file, linenum, val, args[cur_arg], newsrv->id);
2045 err_code |= ERR_ALERT | ERR_FATAL;
2046 goto out;
2047 }
2048 newsrv->agent.inter = val;
2049 cur_arg += 2;
2050 }
Misiekea849332017-01-09 09:39:51 +01002051 else if (!strcmp(args[cur_arg], "agent-addr")) {
2052 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002053 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002054 goto out;
2055 }
2056
2057 cur_arg += 2;
2058 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002059 else if (!strcmp(args[cur_arg], "agent-port")) {
2060 global.maxsock++;
2061 newsrv->agent.port = atol(args[cur_arg + 1]);
2062 cur_arg += 2;
2063 }
James Brown55f9ff12015-10-21 18:19:05 -07002064 else if (!strcmp(args[cur_arg], "agent-send")) {
2065 global.maxsock++;
2066 free(newsrv->agent.send_string);
2067 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2068 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2069 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2070 cur_arg += 2;
2071 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002072 else if (!strcmp(args[cur_arg], "init-addr")) {
2073 char *p, *end;
2074 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002075 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002076
2077 newsrv->init_addr_methods = 0;
2078 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2079
2080 for (p = args[cur_arg + 1]; *p; p = end) {
2081 /* cut on next comma */
2082 for (end = p; *end && *end != ','; end++);
2083 if (*end)
2084 *(end++) = 0;
2085
Willy Tarreau4310d362016-11-02 15:05:56 +01002086 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002087 if (!strcmp(p, "libc")) {
2088 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2089 }
2090 else if (!strcmp(p, "last")) {
2091 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2092 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002093 else if (!strcmp(p, "none")) {
2094 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2095 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002096 else if (str2ip2(p, &sa, 0)) {
2097 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002098 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002099 file, linenum, args[cur_arg], p);
2100 err_code |= ERR_ALERT | ERR_FATAL;
2101 goto out;
2102 }
2103 newsrv->init_addr = sa;
2104 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2105 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002106 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002107 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 +02002108 file, linenum, args[cur_arg], p);
2109 err_code |= ERR_ALERT | ERR_FATAL;
2110 goto out;
2111 }
2112 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002113 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002114 file, linenum, args[cur_arg], p);
2115 err_code |= ERR_ALERT | ERR_FATAL;
2116 goto out;
2117 }
2118 }
2119 cur_arg += 2;
2120 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002121 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002122 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002123 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2124 cur_arg += 2;
2125 }
2126 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2127 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002128 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002129 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002130 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002131 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002132 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002133 file, linenum, args[cur_arg]);
2134 err_code |= ERR_ALERT | ERR_FATAL;
2135 goto out;
2136 }
2137 cur_arg += 2;
2138 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002139 else if (!strcmp(args[cur_arg], "resolve-net")) {
2140 char *p, *e;
2141 unsigned char mask;
2142 struct dns_options *opt;
2143
2144 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002145 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002146 file, linenum, args[cur_arg]);
2147 err_code |= ERR_ALERT | ERR_FATAL;
2148 goto out;
2149 }
2150
2151 opt = &newsrv->dns_opts;
2152
2153 /* Split arguments by comma, and convert it from ipv4 or ipv6
2154 * string network in in_addr or in6_addr.
2155 */
2156 p = args[cur_arg + 1];
2157 e = p;
2158 while (*p != '\0') {
2159 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002160 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002161 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002162 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2163 err_code |= ERR_ALERT | ERR_FATAL;
2164 goto out;
2165 }
2166 /* look for end or comma. */
2167 while (*e != ',' && *e != '\0')
2168 e++;
2169 if (*e == ',') {
2170 *e = '\0';
2171 e++;
2172 }
2173 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2174 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2175 /* Try to convert input string from ipv4 or ipv6 network. */
2176 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2177 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2178 &mask)) {
2179 /* Try to convert input string from ipv6 network. */
2180 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2181 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2182 } else {
2183 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002184 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002185 file, linenum, args[cur_arg], p);
2186 err_code |= ERR_ALERT | ERR_FATAL;
2187 goto out;
2188 }
2189 opt->pref_net_nb++;
2190 p = e;
2191 }
2192
2193 cur_arg += 2;
2194 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002195 else if (!strcmp(args[cur_arg], "rise")) {
2196 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002197 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002198 file, linenum, args[cur_arg]);
2199 err_code |= ERR_ALERT | ERR_FATAL;
2200 goto out;
2201 }
2202
2203 newsrv->check.rise = atol(args[cur_arg + 1]);
2204 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002205 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002206 file, linenum, args[cur_arg]);
2207 err_code |= ERR_ALERT | ERR_FATAL;
2208 goto out;
2209 }
2210
2211 if (newsrv->check.health)
2212 newsrv->check.health = newsrv->check.rise;
2213 cur_arg += 2;
2214 }
2215 else if (!strcmp(args[cur_arg], "fall")) {
2216 newsrv->check.fall = atol(args[cur_arg + 1]);
2217
2218 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002219 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002220 file, linenum, args[cur_arg]);
2221 err_code |= ERR_ALERT | ERR_FATAL;
2222 goto out;
2223 }
2224
2225 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002226 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002227 file, linenum, args[cur_arg]);
2228 err_code |= ERR_ALERT | ERR_FATAL;
2229 goto out;
2230 }
2231
2232 cur_arg += 2;
2233 }
2234 else if (!strcmp(args[cur_arg], "inter")) {
2235 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2236 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002237 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002238 file, linenum, *err, newsrv->id);
2239 err_code |= ERR_ALERT | ERR_FATAL;
2240 goto out;
2241 }
2242 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002243 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002244 file, linenum, val, args[cur_arg], newsrv->id);
2245 err_code |= ERR_ALERT | ERR_FATAL;
2246 goto out;
2247 }
2248 newsrv->check.inter = val;
2249 cur_arg += 2;
2250 }
2251 else if (!strcmp(args[cur_arg], "fastinter")) {
2252 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2253 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002254 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002255 file, linenum, *err, newsrv->id);
2256 err_code |= ERR_ALERT | ERR_FATAL;
2257 goto out;
2258 }
2259 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002260 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002261 file, linenum, val, args[cur_arg], newsrv->id);
2262 err_code |= ERR_ALERT | ERR_FATAL;
2263 goto out;
2264 }
2265 newsrv->check.fastinter = val;
2266 cur_arg += 2;
2267 }
2268 else if (!strcmp(args[cur_arg], "downinter")) {
2269 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2270 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002271 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002272 file, linenum, *err, newsrv->id);
2273 err_code |= ERR_ALERT | ERR_FATAL;
2274 goto out;
2275 }
2276 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002277 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002278 file, linenum, val, args[cur_arg], newsrv->id);
2279 err_code |= ERR_ALERT | ERR_FATAL;
2280 goto out;
2281 }
2282 newsrv->check.downinter = val;
2283 cur_arg += 2;
2284 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002285 else if (!strcmp(args[cur_arg], "port")) {
2286 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002287 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002288 cur_arg += 2;
2289 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002290 else if (!strcmp(args[cur_arg], "weight")) {
2291 int w;
2292 w = atol(args[cur_arg + 1]);
2293 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002294 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002295 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2296 err_code |= ERR_ALERT | ERR_FATAL;
2297 goto out;
2298 }
2299 newsrv->uweight = newsrv->iweight = w;
2300 cur_arg += 2;
2301 }
2302 else if (!strcmp(args[cur_arg], "minconn")) {
2303 newsrv->minconn = atol(args[cur_arg + 1]);
2304 cur_arg += 2;
2305 }
2306 else if (!strcmp(args[cur_arg], "maxconn")) {
2307 newsrv->maxconn = atol(args[cur_arg + 1]);
2308 cur_arg += 2;
2309 }
2310 else if (!strcmp(args[cur_arg], "maxqueue")) {
2311 newsrv->maxqueue = atol(args[cur_arg + 1]);
2312 cur_arg += 2;
2313 }
2314 else if (!strcmp(args[cur_arg], "slowstart")) {
2315 /* slowstart is stored in seconds */
2316 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2317 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002318 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002319 file, linenum, *err, newsrv->id);
2320 err_code |= ERR_ALERT | ERR_FATAL;
2321 goto out;
2322 }
2323 newsrv->slowstart = (val + 999) / 1000;
2324 cur_arg += 2;
2325 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002326 else if (!strcmp(args[cur_arg], "on-error")) {
2327 if (!strcmp(args[cur_arg + 1], "fastinter"))
2328 newsrv->onerror = HANA_ONERR_FASTINTER;
2329 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2330 newsrv->onerror = HANA_ONERR_FAILCHK;
2331 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2332 newsrv->onerror = HANA_ONERR_SUDDTH;
2333 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2334 newsrv->onerror = HANA_ONERR_MARKDWN;
2335 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002336 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002337 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2338 file, linenum, args[cur_arg], args[cur_arg + 1]);
2339 err_code |= ERR_ALERT | ERR_FATAL;
2340 goto out;
2341 }
2342
2343 cur_arg += 2;
2344 }
2345 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2346 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2347 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2348 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002349 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002350 file, linenum, args[cur_arg], args[cur_arg + 1]);
2351 err_code |= ERR_ALERT | ERR_FATAL;
2352 goto out;
2353 }
2354
2355 cur_arg += 2;
2356 }
2357 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2358 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2359 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2360 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002361 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002362 file, linenum, args[cur_arg], args[cur_arg + 1]);
2363 err_code |= ERR_ALERT | ERR_FATAL;
2364 goto out;
2365 }
2366
2367 cur_arg += 2;
2368 }
2369 else if (!strcmp(args[cur_arg], "error-limit")) {
2370 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002371 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002372 file, linenum, args[cur_arg]);
2373 err_code |= ERR_ALERT | ERR_FATAL;
2374 goto out;
2375 }
2376
2377 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2378
2379 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002380 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002381 file, linenum, args[cur_arg]);
2382 err_code |= ERR_ALERT | ERR_FATAL;
2383 goto out;
2384 }
2385 cur_arg += 2;
2386 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002387 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002388 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002389 file, linenum, "usesrc", "source");
2390 err_code |= ERR_ALERT | ERR_FATAL;
2391 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002392 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002393 else {
2394 static int srv_dumped;
2395 struct srv_kw *kw;
2396 char *err;
2397
2398 kw = srv_find_kw(args[cur_arg]);
2399 if (kw) {
2400 char *err = NULL;
2401 int code;
2402
2403 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002404 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 +02002405 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002406 if (kw->skip != -1)
2407 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002408 err_code |= ERR_ALERT | ERR_FATAL;
2409 goto out;
2410 }
2411
2412 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002413 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002414 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002415 if (kw->skip != -1)
2416 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002417 err_code |= ERR_ALERT;
2418 continue;
2419 }
2420
2421 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2422 err_code |= code;
2423
2424 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002425 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002426 if (code & ERR_FATAL) {
2427 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002428 if (kw->skip != -1)
2429 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002430 goto out;
2431 }
2432 }
2433 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002434 if (kw->skip != -1)
2435 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002436 continue;
2437 }
2438
2439 err = NULL;
2440 if (!srv_dumped) {
2441 srv_dump_kws(&err);
2442 indent_msg(&err, 4);
2443 srv_dumped = 1;
2444 }
2445
Christopher Faulet767a84b2017-11-24 16:50:31 +01002446 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002447 file, linenum, args[0], args[1], args[cur_arg],
2448 err ? " Registered keywords :" : "", err ? err : "");
2449 free(err);
2450
2451 err_code |= ERR_ALERT | ERR_FATAL;
2452 goto out;
2453 }
2454 }
2455
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002456 if (!defsrv)
2457 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2458 if (err_code & ERR_FATAL)
2459 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002460 if (srv_tmpl)
2461 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002462 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002463 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002464 return 0;
2465
2466 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002467 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002468 free(errmsg);
2469 return err_code;
2470}
2471
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002472/* Returns a pointer to the first server matching either id <id>.
2473 * NULL is returned if no match is found.
2474 * the lookup is performed in the backend <bk>
2475 */
2476struct server *server_find_by_id(struct proxy *bk, int id)
2477{
2478 struct eb32_node *eb32;
2479 struct server *curserver;
2480
2481 if (!bk || (id ==0))
2482 return NULL;
2483
2484 /* <bk> has no backend capabilities, so it can't have a server */
2485 if (!(bk->cap & PR_CAP_BE))
2486 return NULL;
2487
2488 curserver = NULL;
2489
2490 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2491 if (eb32)
2492 curserver = container_of(eb32, struct server, conf.id);
2493
2494 return curserver;
2495}
2496
2497/* Returns a pointer to the first server matching either name <name>, or id
2498 * if <name> starts with a '#'. NULL is returned if no match is found.
2499 * the lookup is performed in the backend <bk>
2500 */
2501struct server *server_find_by_name(struct proxy *bk, const char *name)
2502{
2503 struct server *curserver;
2504
2505 if (!bk || !name)
2506 return NULL;
2507
2508 /* <bk> has no backend capabilities, so it can't have a server */
2509 if (!(bk->cap & PR_CAP_BE))
2510 return NULL;
2511
2512 curserver = NULL;
2513 if (*name == '#') {
2514 curserver = server_find_by_id(bk, atoi(name + 1));
2515 if (curserver)
2516 return curserver;
2517 }
2518 else {
2519 curserver = bk->srv;
2520
2521 while (curserver && (strcmp(curserver->id, name) != 0))
2522 curserver = curserver->next;
2523
2524 if (curserver)
2525 return curserver;
2526 }
2527
2528 return NULL;
2529}
2530
2531struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2532{
2533 struct server *byname;
2534 struct server *byid;
2535
2536 if (!name && !id)
2537 return NULL;
2538
2539 if (diff)
2540 *diff = 0;
2541
2542 byname = byid = NULL;
2543
2544 if (name) {
2545 byname = server_find_by_name(bk, name);
2546 if (byname && (!id || byname->puid == id))
2547 return byname;
2548 }
2549
2550 /* remaining possibilities :
2551 * - name not set
2552 * - name set but not found
2553 * - name found but ID doesn't match
2554 */
2555 if (id) {
2556 byid = server_find_by_id(bk, id);
2557 if (byid) {
2558 if (byname) {
2559 /* use id only if forced by configuration */
2560 if (byid->flags & SRV_F_FORCED_ID) {
2561 if (diff)
2562 *diff |= 2;
2563 return byid;
2564 }
2565 else {
2566 if (diff)
2567 *diff |= 1;
2568 return byname;
2569 }
2570 }
2571
2572 /* remaining possibilities:
2573 * - name not set
2574 * - name set but not found
2575 */
2576 if (name && diff)
2577 *diff |= 2;
2578 return byid;
2579 }
2580
2581 /* id bot found */
2582 if (byname) {
2583 if (diff)
2584 *diff |= 1;
2585 return byname;
2586 }
2587 }
2588
2589 return NULL;
2590}
2591
Christopher Faulet5d42e092017-10-16 12:00:40 +02002592/* Registers changes to be applied asynchronously */
2593static void srv_register_update(struct server *srv)
2594{
2595 if (LIST_ISEMPTY(&srv->update_status)) {
2596 THREAD_WANT_SYNC();
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002597 HA_SPIN_LOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
Christopher Faulet5d42e092017-10-16 12:00:40 +02002598 if (LIST_ISEMPTY(&srv->update_status))
2599 LIST_ADDQ(&updated_servers, &srv->update_status);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002600 HA_SPIN_UNLOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
Christopher Faulet5d42e092017-10-16 12:00:40 +02002601 }
2602}
2603
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002604/* Update a server state using the parameters available in the params list */
2605static void srv_update_state(struct server *srv, int version, char **params)
2606{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002607 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002608 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002609
2610 /* fields since version 1
2611 * and common to all other upcoming versions
2612 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002613 enum srv_state srv_op_state;
2614 enum srv_admin srv_admin_state;
2615 unsigned srv_uweight, srv_iweight;
2616 unsigned long srv_last_time_change;
2617 short srv_check_status;
2618 enum chk_result srv_check_result;
2619 int srv_check_health;
2620 int srv_check_state, srv_agent_state;
2621 int bk_f_forced_id;
2622 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002623 int fqdn_set_by_cli;
2624 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002625 const char *port_str;
2626 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002627
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002628 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002629 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002630 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002631 switch (version) {
2632 case 1:
2633 /*
2634 * now we can proceed with server's state update:
2635 * srv_addr: params[0]
2636 * srv_op_state: params[1]
2637 * srv_admin_state: params[2]
2638 * srv_uweight: params[3]
2639 * srv_iweight: params[4]
2640 * srv_last_time_change: params[5]
2641 * srv_check_status: params[6]
2642 * srv_check_result: params[7]
2643 * srv_check_health: params[8]
2644 * srv_check_state: params[9]
2645 * srv_agent_state: params[10]
2646 * bk_f_forced_id: params[11]
2647 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002648 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002649 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002650 */
2651
2652 /* validating srv_op_state */
2653 p = NULL;
2654 errno = 0;
2655 srv_op_state = strtol(params[1], &p, 10);
2656 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2657 (srv_op_state != SRV_ST_STOPPED &&
2658 srv_op_state != SRV_ST_STARTING &&
2659 srv_op_state != SRV_ST_RUNNING &&
2660 srv_op_state != SRV_ST_STOPPING)) {
2661 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2662 }
2663
2664 /* validating srv_admin_state */
2665 p = NULL;
2666 errno = 0;
2667 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002668 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002669
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002670 /* inherited statuses will be recomputed later.
2671 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2672 */
2673 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002674
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002675 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2676 (srv_admin_state != 0 &&
2677 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002678 srv_admin_state != SRV_ADMF_CMAINT &&
2679 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002680 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002681 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002682 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2683 }
2684
2685 /* validating srv_uweight */
2686 p = NULL;
2687 errno = 0;
2688 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002689 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002690 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2691
2692 /* validating srv_iweight */
2693 p = NULL;
2694 errno = 0;
2695 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002696 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002697 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2698
2699 /* validating srv_last_time_change */
2700 p = NULL;
2701 errno = 0;
2702 srv_last_time_change = strtol(params[5], &p, 10);
2703 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2704 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2705
2706 /* validating srv_check_status */
2707 p = NULL;
2708 errno = 0;
2709 srv_check_status = strtol(params[6], &p, 10);
2710 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2711 (srv_check_status >= HCHK_STATUS_SIZE))
2712 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2713
2714 /* validating srv_check_result */
2715 p = NULL;
2716 errno = 0;
2717 srv_check_result = strtol(params[7], &p, 10);
2718 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2719 (srv_check_result != CHK_RES_UNKNOWN &&
2720 srv_check_result != CHK_RES_NEUTRAL &&
2721 srv_check_result != CHK_RES_FAILED &&
2722 srv_check_result != CHK_RES_PASSED &&
2723 srv_check_result != CHK_RES_CONDPASS)) {
2724 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2725 }
2726
2727 /* validating srv_check_health */
2728 p = NULL;
2729 errno = 0;
2730 srv_check_health = strtol(params[8], &p, 10);
2731 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2732 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2733
2734 /* validating srv_check_state */
2735 p = NULL;
2736 errno = 0;
2737 srv_check_state = strtol(params[9], &p, 10);
2738 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2739 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2740 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2741
2742 /* validating srv_agent_state */
2743 p = NULL;
2744 errno = 0;
2745 srv_agent_state = strtol(params[10], &p, 10);
2746 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2747 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2748 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2749
2750 /* validating bk_f_forced_id */
2751 p = NULL;
2752 errno = 0;
2753 bk_f_forced_id = strtol(params[11], &p, 10);
2754 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2755 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2756
2757 /* validating srv_f_forced_id */
2758 p = NULL;
2759 errno = 0;
2760 srv_f_forced_id = strtol(params[12], &p, 10);
2761 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2762 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2763
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002764 /* validating srv_fqdn */
2765 fqdn = params[13];
2766 if (fqdn && *fqdn == '-')
2767 fqdn = NULL;
2768 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2769 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2770 fqdn = NULL;
2771 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002772
Frédéric Lécaille31694712017-08-01 08:47:19 +02002773 port_str = params[14];
2774 if (port_str) {
2775 port = strl2uic(port_str, strlen(port_str));
2776 if (port > USHRT_MAX) {
2777 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2778 port_str = NULL;
2779 }
2780 }
2781
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002782 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002783 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002784 goto out;
2785
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002786 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002787 /* recover operational state and apply it to this server
2788 * and all servers tracking this one */
2789 switch (srv_op_state) {
2790 case SRV_ST_STOPPED:
2791 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002792 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002793 break;
2794 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002795 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002796 break;
2797 case SRV_ST_STOPPING:
2798 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002799 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002800 break;
2801 case SRV_ST_RUNNING:
2802 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002803 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002804 break;
2805 }
2806
2807 /* When applying server state, the following rules apply:
2808 * - in case of a configuration change, we apply the setting from the new
2809 * configuration, regardless of old running state
2810 * - if no configuration change, we apply old running state only if old running
2811 * state is different from new configuration state
2812 */
2813 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002814 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2815 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002816 srv_adm_set_maint(srv);
2817 else
2818 srv_adm_set_ready(srv);
2819 }
2820 /* configuration is the same, let's compate old running state and new conf state */
2821 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002822 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002823 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002824 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002825 srv_adm_set_ready(srv);
2826 }
2827 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002828 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002829 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002830 * (srv->iweight is the weight set up in configuration).
2831 * There are two possible reasons for FDRAIN to have been present :
2832 * - previous config weight was zero
2833 * - "set server b/s drain" was sent to the CLI
2834 *
2835 * In the first case, we simply want to drop this drain state
2836 * if the new weight is not zero anymore, meaning the administrator
2837 * has intentionally turned the weight back to a positive value to
2838 * enable the server again after an operation. In the second case,
2839 * the drain state was forced on the CLI regardless of the config's
2840 * weight so we don't want a change to the config weight to lose this
2841 * status. What this means is :
2842 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2843 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002844 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002845 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002846 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002847 }
2848
2849 srv->last_change = date.tv_sec - srv_last_time_change;
2850 srv->check.status = srv_check_status;
2851 srv->check.result = srv_check_result;
2852 srv->check.health = srv_check_health;
2853
2854 /* Only case we want to apply is removing ENABLED flag which could have been
2855 * done by the "disable health" command over the stats socket
2856 */
2857 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2858 (srv_check_state & CHK_ST_CONFIGURED) &&
2859 !(srv_check_state & CHK_ST_ENABLED))
2860 srv->check.state &= ~CHK_ST_ENABLED;
2861
2862 /* Only case we want to apply is removing ENABLED flag which could have been
2863 * done by the "disable agent" command over the stats socket
2864 */
2865 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2866 (srv_agent_state & CHK_ST_CONFIGURED) &&
2867 !(srv_agent_state & CHK_ST_ENABLED))
2868 srv->agent.state &= ~CHK_ST_ENABLED;
2869
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002870 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2871 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002872 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002873 * It means that a configuration file change has precedence over a unix socket change
2874 * for server's weight
2875 *
2876 * by default, HAProxy applies the following weight when parsing the configuration
2877 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002878 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002879 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002880 srv->uweight = srv_uweight;
2881 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002882 server_recalc_eweight(srv);
2883
Willy Tarreaue5a60682016-11-09 14:54:53 +01002884 /* load server IP address */
2885 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002886
2887 if (fqdn && srv->hostname) {
2888 if (!strcmp(srv->hostname, fqdn)) {
2889 /* Here we reset the 'set from stats socket FQDN' flag
2890 * to support such transitions:
2891 * Let's say initial FQDN value is foo1 (in configuration file).
2892 * - FQDN changed from stats socket, from foo1 to foo2 value,
2893 * - FQDN changed again from file configuration (with the same previous value
2894 set from stats socket, from foo1 to foo2 value),
2895 * - reload for any other reason than a FQDN modification,
2896 * the configuration file FQDN matches the fqdn server state file value.
2897 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
2898 * any futher FQDN modification.
2899 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002900 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002901 }
2902 else {
2903 /* If the FDQN has been changed from stats socket,
2904 * apply fqdn state file value (which is the value set
2905 * from stats socket).
2906 */
2907 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002908 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02002909 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002910 }
2911 }
2912 }
2913
Frédéric Lécaille31694712017-08-01 08:47:19 +02002914 if (port_str)
2915 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002916 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002917
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002918 break;
2919 default:
2920 chunk_appendf(msg, ", version '%d' not supported", version);
2921 }
2922
2923 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002924 if (msg->len) {
2925 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01002926 ha_warning("server-state application failed for server '%s/%s'%s",
2927 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002928 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002929}
2930
2931/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2932 * For each proxy, it does the following:
2933 * - opens its server state file (either one or local one)
2934 * - read whole file, line by line
2935 * - analyse each line to check if it matches our current backend:
2936 * - backend name matches
2937 * - backend id matches if id is forced and name doesn't match
2938 * - if the server pointed by the line is found, then state is applied
2939 *
2940 * If the running backend uuid or id differs from the state file, then HAProxy reports
2941 * a warning.
2942 */
2943void apply_server_state(void)
2944{
2945 char *cur, *end;
2946 char mybuf[SRV_STATE_LINE_MAXLEN];
2947 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002948 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
2949 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002950 int arg, srv_arg, version, diff;
2951 FILE *f;
2952 char *filepath;
2953 char globalfilepath[MAXPATHLEN + 1];
2954 char localfilepath[MAXPATHLEN + 1];
2955 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002956 struct proxy *curproxy, *bk;
2957 struct server *srv;
2958
2959 globalfilepathlen = 0;
2960 /* create the globalfilepath variable */
2961 if (global.server_state_file) {
2962 /* absolute path or no base directory provided */
2963 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2964 len = strlen(global.server_state_file);
2965 if (len > MAXPATHLEN) {
2966 globalfilepathlen = 0;
2967 goto globalfileerror;
2968 }
2969 memcpy(globalfilepath, global.server_state_file, len);
2970 globalfilepath[len] = '\0';
2971 globalfilepathlen = len;
2972 }
2973 else if (global.server_state_base) {
2974 len = strlen(global.server_state_base);
2975 globalfilepathlen += len;
2976
2977 if (globalfilepathlen > MAXPATHLEN) {
2978 globalfilepathlen = 0;
2979 goto globalfileerror;
2980 }
2981 strncpy(globalfilepath, global.server_state_base, len);
2982 globalfilepath[globalfilepathlen] = 0;
2983
2984 /* append a slash if needed */
2985 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2986 if (globalfilepathlen + 1 > MAXPATHLEN) {
2987 globalfilepathlen = 0;
2988 goto globalfileerror;
2989 }
2990 globalfilepath[globalfilepathlen++] = '/';
2991 }
2992
2993 len = strlen(global.server_state_file);
2994 if (globalfilepathlen + len > MAXPATHLEN) {
2995 globalfilepathlen = 0;
2996 goto globalfileerror;
2997 }
2998 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2999 globalfilepathlen += len;
3000 globalfilepath[globalfilepathlen++] = 0;
3001 }
3002 }
3003 globalfileerror:
3004 if (globalfilepathlen == 0)
3005 globalfilepath[0] = '\0';
3006
3007 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003008 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003009 /* servers are only in backends */
3010 if (!(curproxy->cap & PR_CAP_BE))
3011 continue;
3012 fileopenerr = 0;
3013 filepath = NULL;
3014
3015 /* search server state file path and name */
3016 switch (curproxy->load_server_state_from_file) {
3017 /* read servers state from global file */
3018 case PR_SRV_STATE_FILE_GLOBAL:
3019 /* there was an error while generating global server state file path */
3020 if (globalfilepathlen == 0)
3021 continue;
3022 filepath = globalfilepath;
3023 fileopenerr = 1;
3024 break;
3025 /* this backend has its own file */
3026 case PR_SRV_STATE_FILE_LOCAL:
3027 localfilepathlen = 0;
3028 localfilepath[0] = '\0';
3029 len = 0;
3030 /* create the localfilepath variable */
3031 /* absolute path or no base directory provided */
3032 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3033 len = strlen(curproxy->server_state_file_name);
3034 if (len > MAXPATHLEN) {
3035 localfilepathlen = 0;
3036 goto localfileerror;
3037 }
3038 memcpy(localfilepath, curproxy->server_state_file_name, len);
3039 localfilepath[len] = '\0';
3040 localfilepathlen = len;
3041 }
3042 else if (global.server_state_base) {
3043 len = strlen(global.server_state_base);
3044 localfilepathlen += len;
3045
3046 if (localfilepathlen > MAXPATHLEN) {
3047 localfilepathlen = 0;
3048 goto localfileerror;
3049 }
3050 strncpy(localfilepath, global.server_state_base, len);
3051 localfilepath[localfilepathlen] = 0;
3052
3053 /* append a slash if needed */
3054 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3055 if (localfilepathlen + 1 > MAXPATHLEN) {
3056 localfilepathlen = 0;
3057 goto localfileerror;
3058 }
3059 localfilepath[localfilepathlen++] = '/';
3060 }
3061
3062 len = strlen(curproxy->server_state_file_name);
3063 if (localfilepathlen + len > MAXPATHLEN) {
3064 localfilepathlen = 0;
3065 goto localfileerror;
3066 }
3067 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3068 localfilepathlen += len;
3069 localfilepath[localfilepathlen++] = 0;
3070 }
3071 filepath = localfilepath;
3072 localfileerror:
3073 if (localfilepathlen == 0)
3074 localfilepath[0] = '\0';
3075
3076 break;
3077 case PR_SRV_STATE_FILE_NONE:
3078 default:
3079 continue;
3080 }
3081
3082 /* preload global state file */
3083 errno = 0;
3084 f = fopen(filepath, "r");
3085 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003086 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003087 if (!f)
3088 continue;
3089
3090 mybuf[0] = '\0';
3091 mybuflen = 0;
3092 version = 0;
3093
3094 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003095 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003096 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003097 goto fileclose;
3098 }
3099
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003100 cur = mybuf;
3101 version = atoi(cur);
3102 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3103 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003104 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003105
3106 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3107 int bk_f_forced_id = 0;
3108 int check_id = 0;
3109 int check_name = 0;
3110
3111 mybuflen = strlen(mybuf);
3112 cur = mybuf;
3113 end = cur + mybuflen;
3114
3115 bk = NULL;
3116 srv = NULL;
3117
3118 /* we need at least one character */
3119 if (mybuflen == 0)
3120 continue;
3121
3122 /* ignore blank characters at the beginning of the line */
3123 while (isspace(*cur))
3124 ++cur;
3125
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003126 /* Ignore empty or commented lines */
3127 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003128 continue;
3129
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003130 /* truncated lines */
3131 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003132 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003133 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003134 }
3135
3136 /* Removes trailing '\n' */
3137 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003138
3139 /* we're now ready to move the line into *srv_params[] */
3140 params[0] = cur;
3141 arg = 1;
3142 srv_arg = 0;
3143 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3144 if (isspace(*cur)) {
3145 *cur = '\0';
3146 ++cur;
3147 while (isspace(*cur))
3148 ++cur;
3149 switch (version) {
3150 case 1:
3151 /*
3152 * srv_addr: params[4] => srv_params[0]
3153 * srv_op_state: params[5] => srv_params[1]
3154 * srv_admin_state: params[6] => srv_params[2]
3155 * srv_uweight: params[7] => srv_params[3]
3156 * srv_iweight: params[8] => srv_params[4]
3157 * srv_last_time_change: params[9] => srv_params[5]
3158 * srv_check_status: params[10] => srv_params[6]
3159 * srv_check_result: params[11] => srv_params[7]
3160 * srv_check_health: params[12] => srv_params[8]
3161 * srv_check_state: params[13] => srv_params[9]
3162 * srv_agent_state: params[14] => srv_params[10]
3163 * bk_f_forced_id: params[15] => srv_params[11]
3164 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003165 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003166 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003167 */
3168 if (arg >= 4) {
3169 srv_params[srv_arg] = cur;
3170 ++srv_arg;
3171 }
3172 break;
3173 }
3174
3175 params[arg] = cur;
3176 ++arg;
3177 }
3178 else {
3179 ++cur;
3180 }
3181 }
3182
3183 /* if line is incomplete line, then ignore it.
3184 * otherwise, update useful flags */
3185 switch (version) {
3186 case 1:
3187 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3188 continue;
3189 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3190 check_id = (atoi(params[0]) == curproxy->uuid);
3191 check_name = (strcmp(curproxy->id, params[1]) == 0);
3192 break;
3193 }
3194
3195 diff = 0;
3196 bk = curproxy;
3197
3198 /* if backend can't be found, let's continue */
3199 if (!check_id && !check_name)
3200 continue;
3201 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003202 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 +02003203 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3204 }
3205 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003206 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 +02003207 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3208 /* if name doesn't match, we still want to update curproxy if the backend id
3209 * was forced in previous the previous configuration */
3210 if (!bk_f_forced_id)
3211 continue;
3212 }
3213
3214 /* look for the server by its id: param[2] */
3215 /* else look for the server by its name: param[3] */
3216 diff = 0;
3217 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3218
3219 if (!srv) {
3220 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003221 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3222 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003223 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3224 params[3], params[2], params[0], params[1]);
3225 continue;
3226 }
3227 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003228 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 +02003229 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 +02003230 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003231 }
3232 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003233 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 +02003234 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 +02003235 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003236 }
3237
3238 /* now we can proceed with server's state update */
3239 srv_update_state(srv, version, srv_params);
3240 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003241fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003242 fclose(f);
3243 }
3244}
3245
Simon Horman7d09b9a2013-02-12 10:45:51 +09003246/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003247 * update a server's current IP address.
3248 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3249 * ip is in network format.
3250 * updater is a string which contains an information about the requester of the update.
3251 * updater is used if not NULL.
3252 *
3253 * A log line and a stderr warning message is generated based on server's backend options.
3254 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003255int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003256{
3257 /* generates a log line and a warning on stderr */
3258 if (1) {
3259 /* book enough space for both IPv4 and IPv6 */
3260 char oldip[INET6_ADDRSTRLEN];
3261 char newip[INET6_ADDRSTRLEN];
3262
3263 memset(oldip, '\0', INET6_ADDRSTRLEN);
3264 memset(newip, '\0', INET6_ADDRSTRLEN);
3265
3266 /* copy old IP address in a string */
3267 switch (s->addr.ss_family) {
3268 case AF_INET:
3269 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3270 break;
3271 case AF_INET6:
3272 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3273 break;
3274 };
3275
3276 /* copy new IP address in a string */
3277 switch (ip_sin_family) {
3278 case AF_INET:
3279 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3280 break;
3281 case AF_INET6:
3282 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3283 break;
3284 };
3285
3286 /* save log line into a buffer */
3287 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3288 s->proxy->id, s->id, oldip, newip, updater);
3289
3290 /* write the buffer on stderr */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003291 ha_warning("%s.\n", trash.str);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003292
3293 /* send a log */
3294 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3295 }
3296
3297 /* save the new IP family */
3298 s->addr.ss_family = ip_sin_family;
3299 /* save the new IP address */
3300 switch (ip_sin_family) {
3301 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003302 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003303 break;
3304 case AF_INET6:
3305 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3306 break;
3307 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003308 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003309
3310 return 0;
3311}
3312
3313/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003314 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3315 *
3316 * Caller can pass its name through <updater> to get it integrated in the response message
3317 * returned by the function.
3318 *
3319 * The function first does the following, in that order:
3320 * - validates the new addr and/or port
3321 * - checks if an update is required (new IP or port is different than current ones)
3322 * - checks the update is allowed:
3323 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3324 * - allow all changes if no CHECKS are configured
3325 * - if CHECK is configured:
3326 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3327 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3328 * conditions are met
3329 */
3330const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3331{
3332 struct sockaddr_storage sa;
3333 int ret, port_change_required;
3334 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003335 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003336 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003337 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003338
3339 msg = get_trash_chunk();
3340 chunk_reset(msg);
3341
3342 if (addr) {
3343 memset(&sa, 0, sizeof(struct sockaddr_storage));
3344 if (str2ip2(addr, &sa, 0) == NULL) {
3345 chunk_printf(msg, "Invalid addr '%s'", addr);
3346 goto out;
3347 }
3348
3349 /* changes are allowed on AF_INET* families only */
3350 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3351 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3352 goto out;
3353 }
3354
3355 /* collecting data currently setup */
3356 memset(current_addr, '\0', sizeof(current_addr));
3357 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3358 /* changes are allowed on AF_INET* families only */
3359 if ((ret != AF_INET) && (ret != AF_INET6)) {
3360 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3361 goto out;
3362 }
3363
3364 /* applying ADDR changes if required and allowed
3365 * ipcmp returns 0 when both ADDR are the same
3366 */
3367 if (ipcmp(&s->addr, &sa) == 0) {
3368 chunk_appendf(msg, "no need to change the addr");
3369 goto port;
3370 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003371 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003372 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003373
3374 /* we also need to update check's ADDR only if it uses the server's one */
3375 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003376 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003377 }
3378
3379 /* we also need to update agent ADDR only if it use the server's one */
3380 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003381 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003382 }
3383
3384 /* update report for caller */
3385 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3386 }
3387
3388 port:
3389 if (port) {
3390 char sign = '\0';
3391 char *endptr;
3392
3393 if (addr)
3394 chunk_appendf(msg, ", ");
3395
3396 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003397 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003398
3399 /* check if PORT change is required */
3400 port_change_required = 0;
3401
3402 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003403 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003404 new_port = strtol(port, &endptr, 10);
3405 if ((errno != 0) || (port == endptr)) {
3406 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3407 goto out;
3408 }
3409
3410 /* check if caller triggers a port mapped or offset */
3411 if (sign == '-' || (sign == '+')) {
3412 /* check if server currently uses port map */
3413 if (!(s->flags & SRV_F_MAPPORTS)) {
3414 /* switch from fixed port to port map mandatorily triggers
3415 * a port change */
3416 port_change_required = 1;
3417 /* check is configured
3418 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3419 * prevent PORT change if check doesn't have it's dedicated port while switching
3420 * to port mapping */
3421 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3422 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.");
3423 goto out;
3424 }
3425 }
3426 /* we're already using port maps */
3427 else {
3428 port_change_required = current_port != new_port;
3429 }
3430 }
3431 /* fixed port */
3432 else {
3433 port_change_required = current_port != new_port;
3434 }
3435
3436 /* applying PORT changes if required and update response message */
3437 if (port_change_required) {
3438 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003439 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003440 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003441
3442 /* prepare message */
3443 chunk_appendf(msg, "port changed from '");
3444 if (s->flags & SRV_F_MAPPORTS)
3445 chunk_appendf(msg, "+");
3446 chunk_appendf(msg, "%d' to '", current_port);
3447
3448 if (sign == '-') {
3449 s->flags |= SRV_F_MAPPORTS;
3450 chunk_appendf(msg, "%c", sign);
3451 /* just use for result output */
3452 new_port = -new_port;
3453 }
3454 else if (sign == '+') {
3455 s->flags |= SRV_F_MAPPORTS;
3456 chunk_appendf(msg, "%c", sign);
3457 }
3458 else {
3459 s->flags &= ~SRV_F_MAPPORTS;
3460 }
3461
3462 chunk_appendf(msg, "%d'", new_port);
3463
3464 /* we also need to update health checks port only if it uses server's realport */
3465 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3466 s->check.port = new_port;
3467 }
3468 }
3469 else {
3470 chunk_appendf(msg, "no need to change the port");
3471 }
3472 }
3473
3474out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003475 if (changed)
3476 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003477 if (updater)
3478 chunk_appendf(msg, " by '%s'", updater);
3479 chunk_appendf(msg, "\n");
3480 return msg->str;
3481}
3482
3483
3484/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003485 * update server status based on result of name resolution
3486 * returns:
3487 * 0 if server status is updated
3488 * 1 if server status has not changed
3489 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003490int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003491{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003492 struct dns_resolvers *resolvers = s->resolvers;
3493 struct dns_resolution *resolution = s->dns_requester->resolution;
3494 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003495
3496 switch (resolution->status) {
3497 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003498 /* status when HAProxy has just (re)started.
3499 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003500 break;
3501
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003502 case RSLV_STATUS_VALID:
3503 /*
3504 * resume health checks
3505 * server will be turned back on if health check is safe
3506 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003507 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003508 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003509 return 1;
3510 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3511 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003512 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003513 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003514
Emeric Brun52a91d32017-08-31 14:41:55 +02003515 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003516 return 1;
3517 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3518 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3519 s->proxy->id, s->id);
3520
Christopher Faulet767a84b2017-11-24 16:50:31 +01003521 ha_warning("%s.\n", trash.str);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003522 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3523 return 0;
3524
3525 case RSLV_STATUS_NX:
3526 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003527 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3528 if (!tick_is_expired(exp, now_ms))
3529 break;
3530
3531 if (s->next_admin & SRV_ADMF_RMAINT)
3532 return 1;
3533 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3534 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003535
3536 case RSLV_STATUS_TIMEOUT:
3537 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003538 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3539 if (!tick_is_expired(exp, now_ms))
3540 break;
3541
3542 if (s->next_admin & SRV_ADMF_RMAINT)
3543 return 1;
3544 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3545 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003546
3547 case RSLV_STATUS_REFUSED:
3548 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003549 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3550 if (!tick_is_expired(exp, now_ms))
3551 break;
3552
3553 if (s->next_admin & SRV_ADMF_RMAINT)
3554 return 1;
3555 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3556 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003557
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003558 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003559 /* stop server if resolution failed for a long enough period */
3560 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3561 if (!tick_is_expired(exp, now_ms))
3562 break;
3563
3564 if (s->next_admin & SRV_ADMF_RMAINT)
3565 return 1;
3566 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3567 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003568 }
3569
3570 return 1;
3571}
3572
3573/*
3574 * Server Name Resolution valid response callback
3575 * It expects:
3576 * - <nameserver>: the name server which answered the valid response
3577 * - <response>: buffer containing a valid DNS response
3578 * - <response_len>: size of <response>
3579 * It performs the following actions:
3580 * - ignore response if current ip found and server family not met
3581 * - update with first new ip found if family is met and current IP is not found
3582 * returns:
3583 * 0 on error
3584 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003585 *
3586 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003587 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003588int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003589{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003590 struct server *s = NULL;
3591 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003592 void *serverip, *firstip;
3593 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003594 int ret;
3595 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003596 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003597
Christopher Faulet67957bd2017-09-27 11:00:59 +02003598 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003599 if (!s)
3600 return 1;
3601
Christopher Faulet67957bd2017-09-27 11:00:59 +02003602 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003603
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003604 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003605 firstip = NULL; /* pointer to the first valid response found */
3606 /* it will be used as the new IP if a change is required */
3607 firstip_sin_family = AF_UNSPEC;
3608 serverip = NULL; /* current server IP address */
3609
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003610 /* initializing server IP pointer */
3611 server_sin_family = s->addr.ss_family;
3612 switch (server_sin_family) {
3613 case AF_INET:
3614 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3615 break;
3616
3617 case AF_INET6:
3618 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3619 break;
3620
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003621 case AF_UNSPEC:
3622 break;
3623
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003624 default:
3625 goto invalid;
3626 }
3627
Baptiste Assmann729c9012017-05-22 15:13:10 +02003628 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003629 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003630 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003631
3632 switch (ret) {
3633 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003634 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003635
3636 case DNS_UPD_SRVIP_NOT_FOUND:
3637 goto save_ip;
3638
3639 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003640 goto invalid;
3641
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003642 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003643 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003644 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003645
Baptiste Assmannfad03182015-10-28 02:03:32 +01003646 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003647 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003648 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003649 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003650
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003651 default:
3652 goto invalid;
3653
3654 }
3655
3656 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003657 if (nameserver) {
3658 nameserver->counters.update++;
3659 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003660 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003661 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003662 else
3663 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003664 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3665
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003666 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003667 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003668 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003669
3670 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003671 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003672 nameserver->counters.invalid++;
3673 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003674 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003675 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003676 return 0;
3677}
3678
3679/*
3680 * Server Name Resolution error management callback
3681 * returns:
3682 * 0 on error
3683 * 1 when no error or safe ignore
3684 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003685int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003686{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003687 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003688
Christopher Faulet67957bd2017-09-27 11:00:59 +02003689 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003690 if (!s)
3691 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003692 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003693 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003694 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003695 return 1;
3696}
3697
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003698/*
3699 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003700 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003701 * It returns a pointer to the first server found or NULL if <ip> is not yet
3702 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003703 *
3704 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003705 */
3706struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3707{
3708 struct server *tmpsrv;
3709 struct proxy *be;
3710
3711 if (!srv)
3712 return NULL;
3713
3714 be = srv->proxy;
3715 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003716 /* we found the current server is the same, ignore it */
3717 if (srv == tmpsrv)
3718 continue;
3719
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003720 /* We want to compare the IP in the record with the IP of the servers in the
3721 * same backend, only if:
3722 * * DNS resolution is enabled on the server
3723 * * the hostname used for the resolution by our server is the same than the
3724 * one used for the server found in the backend
3725 * * the server found in the backend is not our current server
3726 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003727 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003728 if ((tmpsrv->hostname_dn == NULL) ||
3729 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3730 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003731 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003732 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003733 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003734 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003735
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003736 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003737 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003738 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003739 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003740 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003741
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003742 /* At this point, we have 2 different servers using the same DNS hostname
3743 * for their respective resolution.
3744 */
3745 if (*ip_family == tmpsrv->addr.ss_family &&
3746 ((tmpsrv->addr.ss_family == AF_INET &&
3747 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3748 (tmpsrv->addr.ss_family == AF_INET6 &&
3749 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003750 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003751 return tmpsrv;
3752 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003753 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003754 }
3755
Emeric Brune9fd6b52017-11-02 17:20:39 +01003756
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003757 return NULL;
3758}
3759
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003760/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3761 * resolver. This is suited for initial address configuration. Returns 0 on
3762 * success otherwise a non-zero error code. In case of error, *err_code, if
3763 * not NULL, is filled up.
3764 */
3765int srv_set_addr_via_libc(struct server *srv, int *err_code)
3766{
3767 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003768 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003769 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003770 return 1;
3771 }
3772 return 0;
3773}
3774
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003775/* Set the server's FDQN (->hostname) from <hostname>.
3776 * Returns -1 if failed, 0 if not.
3777 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003778int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003779{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003780 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003781 char *hostname_dn;
3782 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003783
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003784 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003785 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003786 /* run time DNS resolution was not active for this server
3787 * and we can't enable it at run time for now.
3788 */
3789 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003790 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003791
3792 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003793 hostname_len = strlen(hostname);
3794 hostname_dn = trash.str;
3795 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3796 hostname_dn, trash.size);
3797 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003798 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003799
Christopher Faulet67957bd2017-09-27 11:00:59 +02003800 resolution = srv->dns_requester->resolution;
3801 if (resolution &&
3802 resolution->hostname_dn &&
3803 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02003804 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003805
Christopher Faulet67957bd2017-09-27 11:00:59 +02003806 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003807
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003808 free(srv->hostname);
3809 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003810 srv->hostname = strdup(hostname);
3811 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003812 srv->hostname_dn_len = hostname_dn_len;
3813 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003814 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003815
Olivier Houchard55dcdf42017-11-06 15:15:04 +01003816 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003817 goto err;
3818
3819 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003820 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003821 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003822 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003823
3824 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003825 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003826 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003827 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003828}
3829
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003830/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3831 * from the state file. This is suited for initial address configuration.
3832 * Returns 0 on success otherwise a non-zero error code. In case of error,
3833 * *err_code, if not NULL, is filled up.
3834 */
3835static int srv_apply_lastaddr(struct server *srv, int *err_code)
3836{
3837 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3838 if (err_code)
3839 *err_code |= ERR_WARN;
3840 return 1;
3841 }
3842 return 0;
3843}
3844
Willy Tarreau25e51522016-11-04 15:10:17 +01003845/* returns 0 if no error, otherwise a combination of ERR_* flags */
3846static int srv_iterate_initaddr(struct server *srv)
3847{
3848 int return_code = 0;
3849 int err_code;
3850 unsigned int methods;
3851
3852 methods = srv->init_addr_methods;
3853 if (!methods) { // default to "last,libc"
3854 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3855 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3856 }
3857
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003858 /* "-dr" : always append "none" so that server addresses resolution
3859 * failures are silently ignored, this is convenient to validate some
3860 * configs out of their environment.
3861 */
3862 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3863 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3864
Willy Tarreau25e51522016-11-04 15:10:17 +01003865 while (methods) {
3866 err_code = 0;
3867 switch (srv_get_next_initaddr(&methods)) {
3868 case SRV_IADDR_LAST:
3869 if (!srv->lastaddr)
3870 continue;
3871 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003872 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003873 return_code |= err_code;
3874 break;
3875
3876 case SRV_IADDR_LIBC:
3877 if (!srv->hostname)
3878 continue;
3879 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003880 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003881 return_code |= err_code;
3882 break;
3883
Willy Tarreau37ebe122016-11-04 15:17:58 +01003884 case SRV_IADDR_NONE:
3885 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003886 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003887 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3888 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003889 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003890 return return_code;
3891
Willy Tarreau4310d362016-11-02 15:05:56 +01003892 case SRV_IADDR_IP:
3893 ipcpy(&srv->init_addr, &srv->addr);
3894 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003895 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3896 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01003897 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003898 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003899
Willy Tarreau25e51522016-11-04 15:10:17 +01003900 default: /* unhandled method */
3901 break;
3902 }
3903 }
3904
3905 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003906 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01003907 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3908 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003909 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003910 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01003911 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3912 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003913
3914 return_code |= ERR_ALERT | ERR_FATAL;
3915 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003916out:
3917 srv_set_dyncookie(srv);
3918 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003919}
3920
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003921/*
3922 * This function parses all backends and all servers within each backend
3923 * and performs servers' addr resolution based on information provided by:
3924 * - configuration file
3925 * - server-state file (states provided by an 'old' haproxy process)
3926 *
3927 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3928 */
3929int srv_init_addr(void)
3930{
3931 struct proxy *curproxy;
3932 int return_code = 0;
3933
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003934 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003935 while (curproxy) {
3936 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003937
3938 /* servers are in backend only */
3939 if (!(curproxy->cap & PR_CAP_BE))
3940 goto srv_init_addr_next;
3941
Willy Tarreau25e51522016-11-04 15:10:17 +01003942 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003943 if (srv->hostname)
3944 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003945
3946 srv_init_addr_next:
3947 curproxy = curproxy->next;
3948 }
3949
3950 return return_code;
3951}
3952
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003953const 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 +02003954{
3955
3956 struct chunk *msg;
3957
3958 msg = get_trash_chunk();
3959 chunk_reset(msg);
3960
Olivier Houchard8da5f982017-08-04 18:35:36 +02003961 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003962 chunk_appendf(msg, "no need to change the FDQN");
3963 goto out;
3964 }
3965
3966 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
3967 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
3968 goto out;
3969 }
3970
3971 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
3972 server->proxy->id, server->id, server->hostname, fqdn);
3973
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003974 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003975 chunk_reset(msg);
3976 chunk_appendf(msg, "could not update %s/%s FQDN",
3977 server->proxy->id, server->id);
3978 goto out;
3979 }
3980
3981 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02003982 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003983
3984 out:
3985 if (updater)
3986 chunk_appendf(msg, " by '%s'", updater);
3987 chunk_appendf(msg, "\n");
3988
3989 return msg->str;
3990}
3991
3992
Willy Tarreau21b069d2016-11-23 17:15:08 +01003993/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3994 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003995 * 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 +01003996 * used for CLI commands requiring a server name.
3997 * Important: the <arg> is modified to remove the '/'.
3998 */
3999struct server *cli_find_server(struct appctx *appctx, char *arg)
4000{
4001 struct proxy *px;
4002 struct server *sv;
4003 char *line;
4004
4005 /* split "backend/server" and make <line> point to server */
4006 for (line = arg; *line; line++)
4007 if (*line == '/') {
4008 *line++ = '\0';
4009 break;
4010 }
4011
4012 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004013 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004014 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004015 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004016 return NULL;
4017 }
4018
4019 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004020 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004021 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004022 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004023 return NULL;
4024 }
4025
4026 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004027 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004028 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004029 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004030 return NULL;
4031 }
4032
4033 return sv;
4034}
4035
William Lallemand222baf22016-11-19 02:00:33 +01004036
4037static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4038{
4039 struct server *sv;
4040 const char *warning;
4041
4042 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4043 return 1;
4044
4045 sv = cli_find_server(appctx, args[2]);
4046 if (!sv)
4047 return 1;
4048
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004049 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004050
William Lallemand222baf22016-11-19 02:00:33 +01004051 if (strcmp(args[3], "weight") == 0) {
4052 warning = server_parse_weight_change_request(sv, args[4]);
4053 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004054 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004055 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004056 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004057 }
4058 }
4059 else if (strcmp(args[3], "state") == 0) {
4060 if (strcmp(args[4], "ready") == 0)
4061 srv_adm_set_ready(sv);
4062 else if (strcmp(args[4], "drain") == 0)
4063 srv_adm_set_drain(sv);
4064 else if (strcmp(args[4], "maint") == 0)
4065 srv_adm_set_maint(sv);
4066 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004067 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004068 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004069 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004070 }
4071 }
4072 else if (strcmp(args[3], "health") == 0) {
4073 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004074 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004075 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004076 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004077 }
4078 else if (strcmp(args[4], "up") == 0) {
4079 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004080 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004081 }
4082 else if (strcmp(args[4], "stopping") == 0) {
4083 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004084 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004085 }
4086 else if (strcmp(args[4], "down") == 0) {
4087 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004088 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004089 }
4090 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004091 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004092 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004093 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004094 }
4095 }
4096 else if (strcmp(args[3], "agent") == 0) {
4097 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004098 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004099 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004100 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004101 }
4102 else if (strcmp(args[4], "up") == 0) {
4103 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004104 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004105 }
4106 else if (strcmp(args[4], "down") == 0) {
4107 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004108 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004109 }
4110 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004111 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004112 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004113 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004114 }
4115 }
Misiek2da082d2017-01-09 09:40:42 +01004116 else if (strcmp(args[3], "agent-addr") == 0) {
4117 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004118 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004119 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4120 appctx->st0 = CLI_ST_PRINT;
4121 } else {
4122 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004123 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004124 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4125 appctx->st0 = CLI_ST_PRINT;
4126 }
4127 }
4128 }
4129 else if (strcmp(args[3], "agent-send") == 0) {
4130 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004131 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004132 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4133 appctx->st0 = CLI_ST_PRINT;
4134 } else {
4135 char *nss = strdup(args[4]);
4136 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004137 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004138 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4139 appctx->st0 = CLI_ST_PRINT;
4140 } else {
4141 free(sv->agent.send_string);
4142 sv->agent.send_string = nss;
4143 sv->agent.send_string_len = strlen(args[4]);
4144 }
4145 }
4146 }
William Lallemand222baf22016-11-19 02:00:33 +01004147 else if (strcmp(args[3], "check-port") == 0) {
4148 int i = 0;
4149 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004150 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004151 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004152 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004153 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004154 }
4155 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004156 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004157 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004158 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004159 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004160 }
4161 /* prevent the update of port to 0 if MAPPORTS are in use */
4162 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
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 = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004165 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004166 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004167 }
4168 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004169 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004170 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004171 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004172 }
4173 else if (strcmp(args[3], "addr") == 0) {
4174 char *addr = NULL;
4175 char *port = NULL;
4176 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004177 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004178 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004179 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004180 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004181 }
4182 else {
4183 addr = args[4];
4184 }
4185 if (strcmp(args[5], "port") == 0) {
4186 port = args[6];
4187 }
4188 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4189 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004190 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004191 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004192 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004193 }
4194 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4195 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004196 else if (strcmp(args[3], "fqdn") == 0) {
4197 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004198 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004199 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4200 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004201 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004202 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004203 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004204 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004205 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004206 appctx->ctx.cli.msg = warning;
4207 appctx->st0 = CLI_ST_PRINT;
4208 }
4209 }
William Lallemand222baf22016-11-19 02:00:33 +01004210 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004211 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004212 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 +01004213 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004214 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004215 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004216 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004217 return 1;
4218}
4219
William Lallemand6b160942016-11-22 12:34:35 +01004220static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4221{
4222 struct stream_interface *si = appctx->owner;
4223 struct proxy *px;
4224 struct server *sv;
4225 char *line;
4226
4227
4228 /* split "backend/server" and make <line> point to server */
4229 for (line = args[2]; *line; line++)
4230 if (*line == '/') {
4231 *line++ = '\0';
4232 break;
4233 }
4234
4235 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004236 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004237 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004238 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004239 return 1;
4240 }
4241
4242 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004243 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004244 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004245 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004246 return 1;
4247 }
4248
4249 /* return server's effective weight at the moment */
4250 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004251 if (ci_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004252 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004253 return 0;
4254 }
William Lallemand6b160942016-11-22 12:34:35 +01004255 return 1;
4256}
4257
4258static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4259{
4260 struct server *sv;
4261 const char *warning;
4262
4263 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4264 return 1;
4265
4266 sv = cli_find_server(appctx, args[2]);
4267 if (!sv)
4268 return 1;
4269
4270 warning = server_parse_weight_change_request(sv, args[3]);
4271 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004272 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004273 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004274 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004275 }
4276 return 1;
4277}
4278
Willy Tarreaub8026272016-11-23 11:26:56 +01004279/* parse a "set maxconn server" command. It always returns 1. */
4280static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4281{
4282 struct server *sv;
4283 const char *warning;
4284
4285 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4286 return 1;
4287
4288 sv = cli_find_server(appctx, args[3]);
4289 if (!sv)
4290 return 1;
4291
4292 warning = server_parse_maxconn_change_request(sv, args[4]);
4293 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004294 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004295 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004296 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004297 }
4298 return 1;
4299}
William Lallemand6b160942016-11-22 12:34:35 +01004300
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004301/* parse a "disable agent" command. It always returns 1. */
4302static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4303{
4304 struct server *sv;
4305
4306 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4307 return 1;
4308
4309 sv = cli_find_server(appctx, args[2]);
4310 if (!sv)
4311 return 1;
4312
4313 sv->agent.state &= ~CHK_ST_ENABLED;
4314 return 1;
4315}
4316
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004317/* parse a "disable health" command. It always returns 1. */
4318static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4319{
4320 struct server *sv;
4321
4322 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4323 return 1;
4324
4325 sv = cli_find_server(appctx, args[2]);
4326 if (!sv)
4327 return 1;
4328
4329 sv->check.state &= ~CHK_ST_ENABLED;
4330 return 1;
4331}
4332
Willy Tarreauffb4d582016-11-24 12:47:00 +01004333/* parse a "disable server" command. It always returns 1. */
4334static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4335{
4336 struct server *sv;
4337
4338 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4339 return 1;
4340
4341 sv = cli_find_server(appctx, args[2]);
4342 if (!sv)
4343 return 1;
4344
4345 srv_adm_set_maint(sv);
4346 return 1;
4347}
4348
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004349/* parse a "enable agent" command. It always returns 1. */
4350static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4351{
4352 struct server *sv;
4353
4354 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4355 return 1;
4356
4357 sv = cli_find_server(appctx, args[2]);
4358 if (!sv)
4359 return 1;
4360
4361 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004362 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004363 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004364 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004365 return 1;
4366 }
4367
4368 sv->agent.state |= CHK_ST_ENABLED;
4369 return 1;
4370}
4371
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004372/* parse a "enable health" command. It always returns 1. */
4373static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4374{
4375 struct server *sv;
4376
4377 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4378 return 1;
4379
4380 sv = cli_find_server(appctx, args[2]);
4381 if (!sv)
4382 return 1;
4383
4384 sv->check.state |= CHK_ST_ENABLED;
4385 return 1;
4386}
4387
Willy Tarreauffb4d582016-11-24 12:47:00 +01004388/* parse a "enable server" command. It always returns 1. */
4389static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4390{
4391 struct server *sv;
4392
4393 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4394 return 1;
4395
4396 sv = cli_find_server(appctx, args[2]);
4397 if (!sv)
4398 return 1;
4399
4400 srv_adm_set_ready(sv);
4401 return 1;
4402}
4403
William Lallemand222baf22016-11-19 02:00:33 +01004404/* register cli keywords */
4405static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004406 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004407 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004408 { { "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 +01004409 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004410 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004411 { { "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 +01004412 { { "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 +01004413 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004414 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4415 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4416
William Lallemand222baf22016-11-19 02:00:33 +01004417 {{},}
4418}};
4419
4420__attribute__((constructor))
4421static void __server_init(void)
4422{
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004423 HA_SPIN_INIT(&updated_servers_lock);
William Lallemand222baf22016-11-19 02:00:33 +01004424 cli_register_kw(&cli_kws);
4425}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004426
Emeric Brun64cc49c2017-10-03 14:46:45 +02004427/*
4428 * This function applies server's status changes, it is
4429 * is designed to be called asynchronously.
4430 *
4431 */
4432void srv_update_status(struct server *s)
4433{
4434 struct check *check = &s->check;
4435 int xferred;
4436 struct proxy *px = s->proxy;
4437 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4438 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4439 int log_level;
4440 struct chunk *tmptrash = NULL;
4441
4442
4443 /* If currently main is not set we try to apply pending state changes */
4444 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4445 int next_admin;
4446
4447 /* Backup next admin */
4448 next_admin = s->next_admin;
4449
4450 /* restore current admin state */
4451 s->next_admin = s->cur_admin;
4452
4453 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4454 s->last_change = now.tv_sec;
4455 if (s->proxy->lbprm.set_server_status_down)
4456 s->proxy->lbprm.set_server_status_down(s);
4457
4458 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4459 srv_shutdown_streams(s, SF_ERR_DOWN);
4460
4461 /* we might have streams queued on this server and waiting for
4462 * a connection. Those which are redispatchable will be queued
4463 * to another server or to the proxy itself.
4464 */
4465 xferred = pendconn_redistribute(s);
4466
4467 tmptrash = alloc_trash_chunk();
4468 if (tmptrash) {
4469 chunk_printf(tmptrash,
4470 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4471 s->proxy->id, s->id);
4472
Emeric Brun5a133512017-10-19 14:42:30 +02004473 srv_append_status(tmptrash, s, NULL, xferred, 0);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004474 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004475
4476 /* we don't send an alert if the server was previously paused */
4477 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
4478 send_log(s->proxy, log_level, "%s.\n", tmptrash->str);
4479 send_email_alert(s, log_level, "%s", tmptrash->str);
4480 free_trash_chunk(tmptrash);
4481 tmptrash = NULL;
4482 }
4483 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4484 set_backend_down(s->proxy);
4485
4486 s->counters.down_trans++;
4487 }
4488 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4489 s->last_change = now.tv_sec;
4490 if (s->proxy->lbprm.set_server_status_down)
4491 s->proxy->lbprm.set_server_status_down(s);
4492
4493 /* we might have streams queued on this server and waiting for
4494 * a connection. Those which are redispatchable will be queued
4495 * to another server or to the proxy itself.
4496 */
4497 xferred = pendconn_redistribute(s);
4498
4499 tmptrash = alloc_trash_chunk();
4500 if (tmptrash) {
4501 chunk_printf(tmptrash,
4502 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4503 s->proxy->id, s->id);
4504
Emeric Brun5a133512017-10-19 14:42:30 +02004505 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004506
Christopher Faulet767a84b2017-11-24 16:50:31 +01004507 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004508 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4509 free_trash_chunk(tmptrash);
4510 tmptrash = NULL;
4511 }
4512
4513 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4514 set_backend_down(s->proxy);
4515 }
4516 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4517 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4518 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4519 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4520 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4521 s->proxy->last_change = now.tv_sec;
4522 }
4523
4524 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4525 s->down_time += now.tv_sec - s->last_change;
4526
4527 s->last_change = now.tv_sec;
4528 if (s->next_state == SRV_ST_STARTING)
4529 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4530
4531 server_recalc_eweight(s);
4532 /* now propagate the status change to any LB algorithms */
4533 if (px->lbprm.update_server_eweight)
4534 px->lbprm.update_server_eweight(s);
4535 else if (srv_willbe_usable(s)) {
4536 if (px->lbprm.set_server_status_up)
4537 px->lbprm.set_server_status_up(s);
4538 }
4539 else {
4540 if (px->lbprm.set_server_status_down)
4541 px->lbprm.set_server_status_down(s);
4542 }
4543
4544 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4545 * and it's not a backup server and its effective weight is > 0,
4546 * then it can accept new connections, so we shut down all streams
4547 * on all backup servers.
4548 */
4549 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4550 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4551 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4552
4553 /* check if we can handle some connections queued at the proxy. We
4554 * will take as many as we can handle.
4555 */
4556 xferred = pendconn_grab_from_px(s);
4557
4558 tmptrash = alloc_trash_chunk();
4559 if (tmptrash) {
4560 chunk_printf(tmptrash,
4561 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4562 s->proxy->id, s->id);
4563
Emeric Brun5a133512017-10-19 14:42:30 +02004564 srv_append_status(tmptrash, s, NULL, xferred, 0);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004565 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004566 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4567 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4568 free_trash_chunk(tmptrash);
4569 tmptrash = NULL;
4570 }
4571
4572 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4573 set_backend_down(s->proxy);
4574 }
4575 else if (s->cur_eweight != s->next_eweight) {
4576 /* now propagate the status change to any LB algorithms */
4577 if (px->lbprm.update_server_eweight)
4578 px->lbprm.update_server_eweight(s);
4579 else if (srv_willbe_usable(s)) {
4580 if (px->lbprm.set_server_status_up)
4581 px->lbprm.set_server_status_up(s);
4582 }
4583 else {
4584 if (px->lbprm.set_server_status_down)
4585 px->lbprm.set_server_status_down(s);
4586 }
4587
4588 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4589 set_backend_down(s->proxy);
4590 }
4591
4592 s->next_admin = next_admin;
4593 }
4594
Emeric Brun5a133512017-10-19 14:42:30 +02004595 /* reset operational state change */
4596 *s->op_st_chg.reason = 0;
4597 s->op_st_chg.status = s->op_st_chg.code = -1;
4598 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004599
4600 /* Now we try to apply pending admin changes */
4601
4602 /* Maintenance must also disable health checks */
4603 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4604 if (s->check.state & CHK_ST_ENABLED) {
4605 s->check.state |= CHK_ST_PAUSED;
4606 check->health = 0;
4607 }
4608
4609 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004610 tmptrash = alloc_trash_chunk();
4611 if (tmptrash) {
4612 chunk_printf(tmptrash,
4613 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4614 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4615 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004616
Olivier Houchard796a2b32017-10-24 17:42:47 +02004617 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004618
Olivier Houchard796a2b32017-10-24 17:42:47 +02004619 if (!(global.mode & MODE_STARTING)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004620 ha_warning("%s.\n", tmptrash->str);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004621 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4622 }
4623 free_trash_chunk(tmptrash);
4624 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004625 }
Emeric Brun8f298292017-12-06 16:47:17 +01004626 /* commit new admin status */
4627
4628 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004629 }
4630 else { /* server was still running */
4631 check->health = 0; /* failure */
4632 s->last_change = now.tv_sec;
4633 if (s->proxy->lbprm.set_server_status_down)
4634 s->proxy->lbprm.set_server_status_down(s);
4635
4636 s->next_state = SRV_ST_STOPPED;
4637 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4638 srv_shutdown_streams(s, SF_ERR_DOWN);
4639
4640 /* we might have streams queued on this server and waiting for
4641 * a connection. Those which are redispatchable will be queued
4642 * to another server or to the proxy itself.
4643 */
4644 xferred = pendconn_redistribute(s);
4645
4646 tmptrash = alloc_trash_chunk();
4647 if (tmptrash) {
4648 chunk_printf(tmptrash,
4649 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4650 s->flags & SRV_F_BACKUP ? "Backup " : "",
4651 s->proxy->id, s->id,
4652 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4653
4654 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4655
4656 if (!(global.mode & MODE_STARTING)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004657 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004658 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", tmptrash->str);
4659 }
4660 free_trash_chunk(tmptrash);
4661 tmptrash = NULL;
4662 }
4663 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4664 set_backend_down(s->proxy);
4665
4666 s->counters.down_trans++;
4667 }
4668 }
4669 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4670 /* OK here we're leaving maintenance, we have many things to check,
4671 * because the server might possibly be coming back up depending on
4672 * its state. In practice, leaving maintenance means that we should
4673 * immediately turn to UP (more or less the slowstart) under the
4674 * following conditions :
4675 * - server is neither checked nor tracked
4676 * - server tracks another server which is not checked
4677 * - server tracks another server which is already up
4678 * Which sums up as something simpler :
4679 * "either the tracking server is up or the server's checks are disabled
4680 * or up". Otherwise we only re-enable health checks. There's a special
4681 * case associated to the stopping state which can be inherited. Note
4682 * that the server might still be in drain mode, which is naturally dealt
4683 * with by the lower level functions.
4684 */
4685
4686 if (s->check.state & CHK_ST_ENABLED) {
4687 s->check.state &= ~CHK_ST_PAUSED;
4688 check->health = check->rise; /* start OK but check immediately */
4689 }
4690
4691 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4692 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4693 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4694 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4695 s->next_state = SRV_ST_STOPPING;
4696 }
4697 else {
4698 s->next_state = SRV_ST_STARTING;
4699 if (s->slowstart > 0)
4700 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4701 else
4702 s->next_state = SRV_ST_RUNNING;
4703 }
4704
4705 }
4706
4707 tmptrash = alloc_trash_chunk();
4708 if (tmptrash) {
4709 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4710 chunk_printf(tmptrash,
4711 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4712 s->flags & SRV_F_BACKUP ? "Backup " : "",
4713 s->proxy->id, s->id,
4714 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4715 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4716 }
4717 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4718 chunk_printf(tmptrash,
4719 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4720 s->flags & SRV_F_BACKUP ? "Backup " : "",
4721 s->proxy->id, s->id, s->hostname,
4722 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4723 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4724 }
4725 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4726 chunk_printf(tmptrash,
4727 "%sServer %s/%s is %s/%s (leaving maintenance)",
4728 s->flags & SRV_F_BACKUP ? "Backup " : "",
4729 s->proxy->id, s->id,
4730 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4731 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4732 }
Christopher Faulet767a84b2017-11-24 16:50:31 +01004733 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004734 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4735 free_trash_chunk(tmptrash);
4736 tmptrash = NULL;
4737 }
4738
4739 server_recalc_eweight(s);
4740 /* now propagate the status change to any LB algorithms */
4741 if (px->lbprm.update_server_eweight)
4742 px->lbprm.update_server_eweight(s);
4743 else if (srv_willbe_usable(s)) {
4744 if (px->lbprm.set_server_status_up)
4745 px->lbprm.set_server_status_up(s);
4746 }
4747 else {
4748 if (px->lbprm.set_server_status_down)
4749 px->lbprm.set_server_status_down(s);
4750 }
4751
4752 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4753 set_backend_down(s->proxy);
4754
4755 }
4756 else if (s->next_admin & SRV_ADMF_MAINT) {
4757 /* remaining in maintenance mode, let's inform precisely about the
4758 * situation.
4759 */
4760 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4761 tmptrash = alloc_trash_chunk();
4762 if (tmptrash) {
4763 chunk_printf(tmptrash,
4764 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4765 s->flags & SRV_F_BACKUP ? "Backup " : "",
4766 s->proxy->id, s->id);
4767
4768 if (s->track) /* normally it's mandatory here */
4769 chunk_appendf(tmptrash, " via %s/%s",
4770 s->track->proxy->id, s->track->id);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004771 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004772 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4773 free_trash_chunk(tmptrash);
4774 tmptrash = NULL;
4775 }
4776 }
4777 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4778 tmptrash = alloc_trash_chunk();
4779 if (tmptrash) {
4780 chunk_printf(tmptrash,
4781 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4782 s->flags & SRV_F_BACKUP ? "Backup " : "",
4783 s->proxy->id, s->id, s->hostname);
4784
4785 if (s->track) /* normally it's mandatory here */
4786 chunk_appendf(tmptrash, " via %s/%s",
4787 s->track->proxy->id, s->track->id);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004788 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004789 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4790 free_trash_chunk(tmptrash);
4791 tmptrash = NULL;
4792 }
4793 }
4794 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4795 tmptrash = alloc_trash_chunk();
4796 if (tmptrash) {
4797 chunk_printf(tmptrash,
4798 "%sServer %s/%s remains in forced maintenance",
4799 s->flags & SRV_F_BACKUP ? "Backup " : "",
4800 s->proxy->id, s->id);
Christopher Faulet767a84b2017-11-24 16:50:31 +01004801 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004802 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4803 free_trash_chunk(tmptrash);
4804 tmptrash = NULL;
4805 }
4806 }
4807 /* don't report anything when leaving drain mode and remaining in maintenance */
4808
4809 s->cur_admin = s->next_admin;
4810 }
4811
4812 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4813 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4814 /* drain state is applied only if not yet in maint */
4815
4816 s->last_change = now.tv_sec;
4817 if (px->lbprm.set_server_status_down)
4818 px->lbprm.set_server_status_down(s);
4819
4820 /* we might have streams queued on this server and waiting for
4821 * a connection. Those which are redispatchable will be queued
4822 * to another server or to the proxy itself.
4823 */
4824 xferred = pendconn_redistribute(s);
4825
4826 tmptrash = alloc_trash_chunk();
4827 if (tmptrash) {
4828 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4829 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4830 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4831
4832 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4833
4834 if (!(global.mode & MODE_STARTING)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004835 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004836 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4837 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4838 }
4839 free_trash_chunk(tmptrash);
4840 tmptrash = NULL;
4841 }
4842
4843 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4844 set_backend_down(s->proxy);
4845 }
4846 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4847 /* OK completely leaving drain mode */
4848 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4849 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4850 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4851 s->proxy->last_change = now.tv_sec;
4852 }
4853
4854 if (s->last_change < now.tv_sec) // ignore negative times
4855 s->down_time += now.tv_sec - s->last_change;
4856 s->last_change = now.tv_sec;
4857 server_recalc_eweight(s);
4858
4859 tmptrash = alloc_trash_chunk();
4860 if (tmptrash) {
4861 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4862 chunk_printf(tmptrash,
4863 "%sServer %s/%s is %s (leaving forced drain)",
4864 s->flags & SRV_F_BACKUP ? "Backup " : "",
4865 s->proxy->id, s->id,
4866 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4867 }
4868 else {
4869 chunk_printf(tmptrash,
4870 "%sServer %s/%s is %s (leaving drain)",
4871 s->flags & SRV_F_BACKUP ? "Backup " : "",
4872 s->proxy->id, s->id,
4873 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4874 if (s->track) /* normally it's mandatory here */
4875 chunk_appendf(tmptrash, " via %s/%s",
4876 s->track->proxy->id, s->track->id);
4877 }
4878
Christopher Faulet767a84b2017-11-24 16:50:31 +01004879 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004880 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4881 free_trash_chunk(tmptrash);
4882 tmptrash = NULL;
4883 }
4884
4885 /* now propagate the status change to any LB algorithms */
4886 if (px->lbprm.update_server_eweight)
4887 px->lbprm.update_server_eweight(s);
4888 else if (srv_willbe_usable(s)) {
4889 if (px->lbprm.set_server_status_up)
4890 px->lbprm.set_server_status_up(s);
4891 }
4892 else {
4893 if (px->lbprm.set_server_status_down)
4894 px->lbprm.set_server_status_down(s);
4895 }
4896 }
4897 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
4898 /* remaining in drain mode after removing one of its flags */
4899
4900 tmptrash = alloc_trash_chunk();
4901 if (tmptrash) {
4902 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4903 chunk_printf(tmptrash,
4904 "%sServer %s/%s is leaving forced drain but remains in drain mode",
4905 s->flags & SRV_F_BACKUP ? "Backup " : "",
4906 s->proxy->id, s->id);
4907
4908 if (s->track) /* normally it's mandatory here */
4909 chunk_appendf(tmptrash, " via %s/%s",
4910 s->track->proxy->id, s->track->id);
4911 }
4912 else {
4913 chunk_printf(tmptrash,
4914 "%sServer %s/%s remains in forced drain mode",
4915 s->flags & SRV_F_BACKUP ? "Backup " : "",
4916 s->proxy->id, s->id);
4917 }
Christopher Faulet767a84b2017-11-24 16:50:31 +01004918 ha_warning("%s.\n", tmptrash->str);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004919 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4920 free_trash_chunk(tmptrash);
4921 tmptrash = NULL;
4922 }
4923
4924 /* commit new admin status */
4925
4926 s->cur_admin = s->next_admin;
4927 }
4928 }
4929
4930 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02004931 *s->adm_st_chg_cause = 0;
4932}
4933/*
4934 * This function loops on servers registered for asynchronous
4935 * status changes
Christopher Faulet5d42e092017-10-16 12:00:40 +02004936 *
4937 * NOTE: No needs to lock <updated_servers> list because it is called inside the
4938 * sync point.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004939 */
4940void servers_update_status(void) {
4941 struct server *s, *stmp;
4942
4943 list_for_each_entry_safe(s, stmp, &updated_servers, update_status) {
4944 srv_update_status(s);
4945 LIST_DEL(&s->update_status);
4946 LIST_INIT(&s->update_status);
4947 }
4948}
4949
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004950/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004951 * Local variables:
4952 * c-indent-level: 8
4953 * c-basic-offset: 8
4954 * End:
4955 */