blob: 5e995f8a781a68f8513e8931fefdc7a23a7ed139 [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) {
149 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) {
584 Alert("'%s %s' : connect() not supported for this address family.\n",
585 args[*cur_arg], args[*cur_arg + 1]);
586 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) {
596 Alert("'%s' does not support port offsets (found '%s').\n",
597 args[*cur_arg], args[*cur_arg + 1]);
598 goto err;
599 }
600
601 if (port_low <= 0 || port_low > 65535 ||
602 port_high <= 0 || port_high > 65535 ||
603 port_low > port_high) {
604 Alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
605 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]) {
617 Alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
618 "or 'hdr_ip(name,#)' as argument.\n");
619 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) {
663 Alert("usesrc hdr_ip(name,num) does not support negative"
664 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
665 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) {
675 Alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
676 goto err;
677 }
678
679 proto = protocol_by_family(sk->ss_family);
680 if (!proto || !proto->connect) {
681 Alert("'%s %s' : connect() not supported for this address family.\n",
682 args[*cur_arg], args[*cur_arg + 1]);
683 goto err;
684 }
685
686 if (port1 != port2) {
687 Alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
688 args[*cur_arg], args[*cur_arg + 1]);
689 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 */
698 Alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
699 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]) {
706 Alert("'%s' : missing interface name.\n", args[0]);
707 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
714 Alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
715 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) {
872 if (check->desc)
873 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
874 s->op_st_chg.code = check->code;
875 s->op_st_chg.status = check->status;
876 s->op_st_chg.duration = check->duration;
877 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200878
Christopher Faulet5d42e092017-10-16 12:00:40 +0200879 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200880 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100881 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200882 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100883 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200884 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200885}
886
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200887/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200888 * in maintenance. The server is registered in a list to postpone the counting
889 * of the remaining servers on the proxy and tries to grab requests from the
890 * proxy at a sync point. Maintenance servers are ignored. It stores the
891 * <reason> if non-null as the reason for going down or the available data
892 * from the check struct to recompute this reason later.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200893 */
Emeric Brun5a133512017-10-19 14:42:30 +0200894void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200895{
896 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200897
Emeric Brun64cc49c2017-10-03 14:46:45 +0200898 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200899 return;
900
Emeric Brun52a91d32017-08-31 14:41:55 +0200901 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200902 return;
903
Emeric Brun52a91d32017-08-31 14:41:55 +0200904 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200905 *s->op_st_chg.reason = 0;
906 s->op_st_chg.status = -1;
907 if (reason) {
908 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
909 }
910 else if (check) {
911 if (check->desc)
912 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
913 s->op_st_chg.code = check->code;
914 s->op_st_chg.status = check->status;
915 s->op_st_chg.duration = check->duration;
916 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200917
Emeric Brun64cc49c2017-10-03 14:46:45 +0200918 if (s->slowstart <= 0)
919 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200920
Christopher Faulet5d42e092017-10-16 12:00:40 +0200921 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200922 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100923 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200924 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100925 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200926 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200927}
928
Willy Tarreau8eb77842014-05-21 13:54:57 +0200929/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +0200930 * isn't in maintenance. The server is registered in a list to postpone the
931 * counting of the remaining servers on the proxy and tries to grab requests
932 * from the proxy. Maintenance servers are ignored. It stores the
933 * <reason> if non-null as the reason for going down or the available data
934 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +0200935 * up. Note that it makes use of the trash to build the log strings, so <reason>
936 * must not be placed there.
937 */
Emeric Brun5a133512017-10-19 14:42:30 +0200938void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200939{
940 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200941
Emeric Brun64cc49c2017-10-03 14:46:45 +0200942 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200943 return;
944
Emeric Brun52a91d32017-08-31 14:41:55 +0200945 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200946 return;
947
Emeric Brun52a91d32017-08-31 14:41:55 +0200948 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +0200949 *s->op_st_chg.reason = 0;
950 s->op_st_chg.status = -1;
951 if (reason) {
952 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
953 }
954 else if (check) {
955 if (check->desc)
956 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
957 s->op_st_chg.code = check->code;
958 s->op_st_chg.status = check->status;
959 s->op_st_chg.duration = check->duration;
960 }
Willy Tarreau8eb77842014-05-21 13:54:57 +0200961
Christopher Faulet5d42e092017-10-16 12:00:40 +0200962 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200963 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100964 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200965 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100966 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200967 }
Willy Tarreau8eb77842014-05-21 13:54:57 +0200968}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200969
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200970/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
971 * enforce either maint mode or drain mode. It is not allowed to set more than
972 * one flag at once. The equivalent "inherited" flag is propagated to all
973 * tracking servers. Maintenance mode disables health checks (but not agent
974 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100975 * is done. If <cause> is non-null, it will be displayed at the end of the log
976 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200977 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100978void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200979{
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200980 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200981
982 if (!mode)
983 return;
984
985 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +0200986 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200987 return;
988
Emeric Brun52a91d32017-08-31 14:41:55 +0200989 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +0200990 if (cause)
991 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
992
Christopher Faulet5d42e092017-10-16 12:00:40 +0200993 srv_register_update(s);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200994
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200995 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +0200996 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
997 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200998 return;
999
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001000 /* compute the inherited flag to propagate */
1001 if (mode & SRV_ADMF_MAINT)
1002 mode = SRV_ADMF_IMAINT;
1003 else if (mode & SRV_ADMF_DRAIN)
1004 mode = SRV_ADMF_IDRAIN;
1005
Emeric Brun9f0b4582017-10-23 14:39:51 +02001006 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001007 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001008 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001009 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001010 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001011}
1012
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001013/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1014 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1015 * than one flag at once. The equivalent "inherited" flag is propagated to all
1016 * tracking servers. Leaving maintenance mode re-enables health checks. When
1017 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001018 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001019void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001020{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001021 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001022
1023 if (!mode)
1024 return;
1025
1026 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001027 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001028 return;
1029
Emeric Brun52a91d32017-08-31 14:41:55 +02001030 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001031
Christopher Faulet5d42e092017-10-16 12:00:40 +02001032 srv_register_update(s);
1033
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001034 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001035 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1036 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001037 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001038
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001039 if (mode & SRV_ADMF_MAINT)
1040 mode = SRV_ADMF_IMAINT;
1041 else if (mode & SRV_ADMF_DRAIN)
1042 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001043
Emeric Brun9f0b4582017-10-23 14:39:51 +02001044 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001045 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001046 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001047 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001048 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001049}
1050
Willy Tarreau757478e2016-11-03 19:22:19 +01001051/* principle: propagate maint and drain to tracking servers. This is useful
1052 * upon startup so that inherited states are correct.
1053 */
1054static void srv_propagate_admin_state(struct server *srv)
1055{
1056 struct server *srv2;
1057
1058 if (!srv->trackers)
1059 return;
1060
1061 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001062 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001063 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001064 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001065
Emeric Brun52a91d32017-08-31 14:41:55 +02001066 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001067 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001068 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001069 }
1070}
1071
1072/* Compute and propagate the admin states for all servers in proxy <px>.
1073 * Only servers *not* tracking another one are considered, because other
1074 * ones will be handled when the server they track is visited.
1075 */
1076void srv_compute_all_admin_states(struct proxy *px)
1077{
1078 struct server *srv;
1079
1080 for (srv = px->srv; srv; srv = srv->next) {
1081 if (srv->track)
1082 continue;
1083 srv_propagate_admin_state(srv);
1084 }
1085}
1086
Willy Tarreaudff55432012-10-10 17:51:05 +02001087/* Note: must not be declared <const> as its list will be overwritten.
1088 * Please take care of keeping this list alphabetically sorted, doing so helps
1089 * all code contributors.
1090 * Optional keywords are also declared with a NULL ->parse() function so that
1091 * the config parser can report an appropriate error when a known keyword was
1092 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001093 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001094 */
1095static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001096 { "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 +01001097 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001098 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001099 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001100 { "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 +01001101 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001102 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1103 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001104 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001105 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001106 { "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 +01001107 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001108 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001109 { "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 +01001110 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1111 { "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 +01001112 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001113 { "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 +01001114 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001115 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1116 { "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 +02001117 { "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 +01001118 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001119 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001120 { NULL, NULL, 0 },
1121}};
1122
1123__attribute__((constructor))
1124static void __listener_init(void)
1125{
1126 srv_register_keywords(&srv_kws);
1127}
1128
Willy Tarreau004e0452013-11-21 11:22:01 +01001129/* Recomputes the server's eweight based on its state, uweight, the current time,
1130 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1131 * state is automatically disabled if the time is elapsed.
1132 */
1133void server_recalc_eweight(struct server *sv)
1134{
1135 struct proxy *px = sv->proxy;
1136 unsigned w;
1137
1138 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1139 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001140 if (sv->next_state == SRV_ST_STARTING)
1141 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001142 }
1143
1144 /* We must take care of not pushing the server to full throttle during slow starts.
1145 * It must also start immediately, at least at the minimal step when leaving maintenance.
1146 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001147 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001148 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1149 else
1150 w = px->lbprm.wdiv;
1151
Emeric Brun52a91d32017-08-31 14:41:55 +02001152 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001153
Christopher Faulet5d42e092017-10-16 12:00:40 +02001154 srv_register_update(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001155}
1156
Willy Tarreaubaaee002006-06-26 02:48:02 +02001157/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001158 * Parses weight_str and configures sv accordingly.
1159 * Returns NULL on success, error message string otherwise.
1160 */
1161const char *server_parse_weight_change_request(struct server *sv,
1162 const char *weight_str)
1163{
1164 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001165 long int w;
1166 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001167
1168 px = sv->proxy;
1169
1170 /* if the weight is terminated with '%', it is set relative to
1171 * the initial weight, otherwise it is absolute.
1172 */
1173 if (!*weight_str)
1174 return "Require <weight> or <weight%>.\n";
1175
Simon Hormanb796afa2013-02-12 10:45:53 +09001176 w = strtol(weight_str, &end, 10);
1177 if (end == weight_str)
1178 return "Empty weight string empty or preceded by garbage";
1179 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001180 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001181 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001182 /* Avoid integer overflow */
1183 if (w > 25600)
1184 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001185 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001186 if (w > 256)
1187 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001188 }
1189 else if (w < 0 || w > 256)
1190 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001191 else if (end[0] != '\0')
1192 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001193
1194 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1195 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1196
1197 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001198 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001199
1200 return NULL;
1201}
1202
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001203/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001204 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1205 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001206 * Returns:
1207 * - error string on error
1208 * - NULL on success
1209 */
1210const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001211 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001212{
1213 unsigned char ip[INET6_ADDRSTRLEN];
1214
1215 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001216 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001217 return NULL;
1218 }
1219 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001220 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001221 return NULL;
1222 }
1223
1224 return "Could not understand IP address format.\n";
1225}
1226
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001227const char *server_parse_maxconn_change_request(struct server *sv,
1228 const char *maxconn_str)
1229{
1230 long int v;
1231 char *end;
1232
1233 if (!*maxconn_str)
1234 return "Require <maxconn>.\n";
1235
1236 v = strtol(maxconn_str, &end, 10);
1237 if (end == maxconn_str)
1238 return "maxconn string empty or preceded by garbage";
1239 else if (end[0] != '\0')
1240 return "Trailing garbage in maxconn string";
1241
1242 if (sv->maxconn == sv->minconn) { // static maxconn
1243 sv->maxconn = sv->minconn = v;
1244 } else { // dynamic maxconn
1245 sv->maxconn = v;
1246 }
1247
1248 if (may_dequeue_tasks(sv, sv->proxy))
1249 process_srv_queue(sv);
1250
1251 return NULL;
1252}
1253
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001254#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001255static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1256 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001257{
1258 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001259 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001260 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001261 NULL,
1262 };
1263
1264 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001265 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001266
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001267 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1268}
1269
1270static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1271{
1272 struct sample_expr *expr;
1273
1274 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 +01001275 if (!expr) {
1276 memprintf(err, "error detected while parsing sni expression : %s", *err);
1277 return ERR_ALERT | ERR_FATAL;
1278 }
1279
1280 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1281 memprintf(err, "error detected while parsing sni expression : "
1282 " fetch method '%s' extracts information from '%s', "
1283 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001284 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001285 return ERR_ALERT | ERR_FATAL;
1286 }
1287
1288 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1289 release_sample_expr(newsrv->ssl_ctx.sni);
1290 newsrv->ssl_ctx.sni = expr;
1291
1292 return 0;
1293}
1294#endif
1295
1296static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1297{
1298 if (err && *err) {
1299 indent_msg(err, 2);
1300 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1301 }
1302 else
1303 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1304 file, linenum, args[0], args[1], args[cur_arg]);
1305}
1306
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001307static void srv_conn_src_sport_range_cpy(struct server *srv,
1308 struct server *src)
1309{
1310 int range_sz;
1311
1312 range_sz = src->conn_src.sport_range->size;
1313 if (range_sz > 0) {
1314 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1315 if (srv->conn_src.sport_range != NULL) {
1316 int i;
1317
1318 for (i = 0; i < range_sz; i++) {
1319 srv->conn_src.sport_range->ports[i] =
1320 src->conn_src.sport_range->ports[i];
1321 }
1322 }
1323 }
1324}
1325
1326/*
1327 * Copy <src> server connection source settings to <srv> server everything needed.
1328 */
1329static void srv_conn_src_cpy(struct server *srv, struct server *src)
1330{
1331 srv->conn_src.opts = src->conn_src.opts;
1332 srv->conn_src.source_addr = src->conn_src.source_addr;
1333
1334 /* Source port range copy. */
1335 if (src->conn_src.sport_range != NULL)
1336 srv_conn_src_sport_range_cpy(srv, src);
1337
1338#ifdef CONFIG_HAP_TRANSPARENT
1339 if (src->conn_src.bind_hdr_name != NULL) {
1340 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1341 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1342 }
1343 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1344 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1345#endif
1346 if (src->conn_src.iface_name != NULL)
1347 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1348}
1349
1350/*
1351 * Copy <src> server SSL settings to <srv> server allocating
1352 * everything needed.
1353 */
1354#if defined(USE_OPENSSL)
1355static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1356{
1357 if (src->ssl_ctx.ca_file != NULL)
1358 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1359 if (src->ssl_ctx.crl_file != NULL)
1360 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1361 if (src->ssl_ctx.client_crt != NULL)
1362 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1363
1364 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1365
1366 if (src->ssl_ctx.verify_host != NULL)
1367 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1368 if (src->ssl_ctx.ciphers != NULL)
1369 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1370 if (src->sni_expr != NULL)
1371 srv->sni_expr = strdup(src->sni_expr);
1372}
1373#endif
1374
1375/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001376 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001377 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001378 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001379 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001380static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001381{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001382 char *hostname_dn;
1383 int hostname_len, hostname_dn_len;
1384
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001385 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001386 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001387
Christopher Faulet67957bd2017-09-27 11:00:59 +02001388 hostname_len = strlen(hostname);
1389 hostname_dn = trash.str;
1390 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1391 hostname_dn, trash.size);
1392 if (hostname_dn_len == -1)
1393 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001394
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001395
Christopher Faulet67957bd2017-09-27 11:00:59 +02001396 free(srv->hostname);
1397 free(srv->hostname_dn);
1398 srv->hostname = strdup(hostname);
1399 srv->hostname_dn = strdup(hostname_dn);
1400 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001401 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001402 goto err;
1403
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001404 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001405
1406 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001407 free(srv->hostname); srv->hostname = NULL;
1408 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001409 return -1;
1410}
1411
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001412/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001413 * Copy <src> server settings to <srv> server allocating
1414 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001415 * This function is not supposed to be called at any time, but only
1416 * during server settings parsing or during server allocations from
1417 * a server template, and just after having calloc()'ed a new server.
1418 * So, <src> may only be a default server (when parsing server settings)
1419 * or a server template (during server allocations from a server template).
1420 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1421 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001422 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001423static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001424{
1425 /* Connection source settings copy */
1426 srv_conn_src_cpy(srv, src);
1427
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001428 if (srv_tmpl) {
1429 srv->addr = src->addr;
1430 srv->svc_port = src->svc_port;
1431 }
1432
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001433 srv->pp_opts = src->pp_opts;
1434 if (src->rdr_pfx != NULL) {
1435 srv->rdr_pfx = strdup(src->rdr_pfx);
1436 srv->rdr_len = src->rdr_len;
1437 }
1438 if (src->cookie != NULL) {
1439 srv->cookie = strdup(src->cookie);
1440 srv->cklen = src->cklen;
1441 }
1442 srv->use_ssl = src->use_ssl;
1443 srv->check.addr = srv->agent.addr = src->check.addr;
1444 srv->check.use_ssl = src->check.use_ssl;
1445 srv->check.port = src->check.port;
1446 /* Note: 'flags' field has potentially been already initialized. */
1447 srv->flags |= src->flags;
1448 srv->do_check = src->do_check;
1449 srv->do_agent = src->do_agent;
1450 if (srv->check.port)
1451 srv->flags |= SRV_F_CHECKPORT;
1452 srv->check.inter = src->check.inter;
1453 srv->check.fastinter = src->check.fastinter;
1454 srv->check.downinter = src->check.downinter;
1455 srv->agent.use_ssl = src->agent.use_ssl;
1456 srv->agent.port = src->agent.port;
1457 if (src->agent.send_string != NULL)
1458 srv->agent.send_string = strdup(src->agent.send_string);
1459 srv->agent.send_string_len = src->agent.send_string_len;
1460 srv->agent.inter = src->agent.inter;
1461 srv->agent.fastinter = src->agent.fastinter;
1462 srv->agent.downinter = src->agent.downinter;
1463 srv->maxqueue = src->maxqueue;
1464 srv->minconn = src->minconn;
1465 srv->maxconn = src->maxconn;
1466 srv->slowstart = src->slowstart;
1467 srv->observe = src->observe;
1468 srv->onerror = src->onerror;
1469 srv->onmarkeddown = src->onmarkeddown;
1470 srv->onmarkedup = src->onmarkedup;
1471 if (src->trackit != NULL)
1472 srv->trackit = strdup(src->trackit);
1473 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1474 srv->uweight = srv->iweight = src->iweight;
1475
1476 srv->check.send_proxy = src->check.send_proxy;
1477 /* health: up, but will fall down at first failure */
1478 srv->check.rise = srv->check.health = src->check.rise;
1479 srv->check.fall = src->check.fall;
1480
1481 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001482 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1483 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1484 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001485 srv->check.state |= CHK_ST_PAUSED;
1486 srv->check.health = 0;
1487 }
1488
1489 /* health: up but will fall down at first failure */
1490 srv->agent.rise = srv->agent.health = src->agent.rise;
1491 srv->agent.fall = src->agent.fall;
1492
1493 if (src->resolvers_id != NULL)
1494 srv->resolvers_id = strdup(src->resolvers_id);
1495 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1496 if (srv->dns_opts.family_prio == AF_UNSPEC)
1497 srv->dns_opts.family_prio = AF_INET6;
1498 memcpy(srv->dns_opts.pref_net,
1499 src->dns_opts.pref_net,
1500 sizeof srv->dns_opts.pref_net);
1501 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1502
1503 srv->init_addr_methods = src->init_addr_methods;
1504 srv->init_addr = src->init_addr;
1505#if defined(USE_OPENSSL)
1506 srv_ssl_settings_cpy(srv, src);
1507#endif
1508#ifdef TCP_USER_TIMEOUT
1509 srv->tcp_ut = src->tcp_ut;
1510#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001511 if (srv_tmpl)
1512 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001513}
1514
1515static struct server *new_server(struct proxy *proxy)
1516{
1517 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001518 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001519
1520 srv = calloc(1, sizeof *srv);
1521 if (!srv)
1522 return NULL;
1523
1524 srv->obj_type = OBJ_TYPE_SERVER;
1525 srv->proxy = proxy;
1526 LIST_INIT(&srv->actconns);
1527 LIST_INIT(&srv->pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02001528
1529 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1530 goto free_srv;
1531 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1532 goto free_priv_conns;
1533 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1534 goto free_idle_conns;
1535
1536 for (i = 0; i < global.nbthread; i++) {
1537 LIST_INIT(&srv->priv_conns[i]);
1538 LIST_INIT(&srv->idle_conns[i]);
1539 LIST_INIT(&srv->safe_conns[i]);
1540 }
1541
Emeric Brun64cc49c2017-10-03 14:46:45 +02001542 LIST_INIT(&srv->update_status);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001543
Emeric Brun52a91d32017-08-31 14:41:55 +02001544 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001545 srv->last_change = now.tv_sec;
1546
1547 srv->check.status = HCHK_STATUS_INI;
1548 srv->check.server = srv;
1549 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1550
1551 srv->agent.status = HCHK_STATUS_INI;
1552 srv->agent.server = srv;
1553 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1554
1555 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001556
1557 free_idle_conns:
1558 free(srv->idle_conns);
1559 free_priv_conns:
1560 free(srv->priv_conns);
1561 free_srv:
1562 free(srv);
1563 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001564}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001565
1566/*
1567 * Validate <srv> server health-check settings.
1568 * Returns 0 if everything is OK, -1 if not.
1569 */
1570static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1571{
1572 struct tcpcheck_rule *r = NULL;
1573 struct list *l;
1574
1575 /*
1576 * We need at least a service port, a check port or the first tcp-check rule must
1577 * be a 'connect' one when checking an IPv4/IPv6 server.
1578 */
1579 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1580 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1581 return 0;
1582
1583 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1584 if (!r) {
1585 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1586 "Check has been disabled.\n",
1587 file, linenum, srv->id);
1588 return -1;
1589 }
1590
1591 /* search the first action (connect / send / expect) in the list */
1592 l = &srv->proxy->tcpcheck_rules;
1593 list_for_each_entry(r, l, list) {
1594 if (r->action != TCPCHK_ACT_COMMENT)
1595 break;
1596 }
1597
1598 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1599 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1600 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1601 file, linenum, srv->id);
1602 return -1;
1603 }
1604
1605 /* scan the tcp-check ruleset to ensure a port has been configured */
1606 l = &srv->proxy->tcpcheck_rules;
1607 list_for_each_entry(r, l, list) {
1608 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1609 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1610 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1611 file, linenum, srv->id);
1612 return -1;
1613 }
1614 }
1615
1616 return 0;
1617}
1618
1619/*
1620 * Initialize <srv> health-check structure.
1621 * Returns the error string in case of memory allocation failure, NULL if not.
1622 */
1623static const char *do_health_check_init(struct server *srv, int check_type, int state)
1624{
1625 const char *ret;
1626
1627 if (!srv->do_check)
1628 return NULL;
1629
1630 ret = init_check(&srv->check, check_type);
1631 if (ret)
1632 return ret;
1633
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001634 srv->check.state |= state;
1635 global.maxsock++;
1636
1637 return NULL;
1638}
1639
1640static int server_health_check_init(const char *file, int linenum,
1641 struct server *srv, struct proxy *curproxy)
1642{
1643 const char *ret;
1644
1645 if (!srv->do_check)
1646 return 0;
1647
1648 if (srv->trackit) {
1649 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1650 file, linenum);
1651 return ERR_ALERT | ERR_FATAL;
1652 }
1653
1654 if (server_healthcheck_validate(file, linenum, srv) < 0)
1655 return ERR_ALERT | ERR_ABORT;
1656
1657 /* note: check type will be set during the config review phase */
1658 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1659 if (ret) {
1660 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1661 return ERR_ALERT | ERR_ABORT;
1662 }
1663
1664 return 0;
1665}
1666
1667/*
1668 * Initialize <srv> agent check structure.
1669 * Returns the error string in case of memory allocation failure, NULL if not.
1670 */
1671static const char *do_server_agent_check_init(struct server *srv, int state)
1672{
1673 const char *ret;
1674
1675 if (!srv->do_agent)
1676 return NULL;
1677
1678 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1679 if (ret)
1680 return ret;
1681
1682 if (!srv->agent.inter)
1683 srv->agent.inter = srv->check.inter;
1684
1685 srv->agent.state |= state;
1686 global.maxsock++;
1687
1688 return NULL;
1689}
1690
1691static int server_agent_check_init(const char *file, int linenum,
1692 struct server *srv, struct proxy *curproxy)
1693{
1694 const char *ret;
1695
1696 if (!srv->do_agent)
1697 return 0;
1698
1699 if (!srv->agent.port) {
1700 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1701 file, linenum, srv->id);
1702 return ERR_ALERT | ERR_FATAL;
1703 }
1704
1705 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1706 if (ret) {
1707 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1708 return ERR_ALERT | ERR_ABORT;
1709 }
1710
1711 return 0;
1712}
1713
1714#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1715static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1716 struct server *srv, struct proxy *proxy)
1717{
1718 int ret;
1719 char *err = NULL;
1720
1721 if (!srv->sni_expr)
1722 return 0;
1723
1724 ret = server_parse_sni_expr(srv, proxy, &err);
1725 if (!ret)
1726 return 0;
1727
1728 display_parser_err(file, linenum, args, cur_arg, &err);
1729 free(err);
1730
1731 return ret;
1732}
1733#endif
1734
1735/*
1736 * Server initializations finalization.
1737 * Initialize health check, agent check and SNI expression if enabled.
1738 * Must not be called for a default server instance.
1739 */
1740static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1741 struct server *srv, struct proxy *px)
1742{
1743 int ret;
1744
1745 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1746 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1747 return ret;
1748 }
1749
1750#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1751 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1752 return ret;
1753#endif
1754
1755 if (srv->flags & SRV_F_BACKUP)
1756 px->srv_bck++;
1757 else
1758 px->srv_act++;
1759 srv_lb_commit_status(srv);
1760
1761 return 0;
1762}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001763
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001764/*
1765 * Parse as much as possible such a range string argument: low[-high]
1766 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1767 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1768 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1769 * Returns 0 if succeeded, -1 if not.
1770 */
1771static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1772{
1773 char *nb_high_arg;
1774
1775 *nb_high = 0;
1776 chunk_printf(&trash, "%s", arg);
1777 *nb_low = atoi(trash.str);
1778
1779 if ((nb_high_arg = strchr(trash.str, '-'))) {
1780 *nb_high_arg++ = '\0';
1781 *nb_high = atoi(nb_high_arg);
1782 }
1783 else {
1784 *nb_high += *nb_low;
1785 *nb_low = 1;
1786 }
1787
1788 if (*nb_low < 0 || *nb_high < *nb_low)
1789 return -1;
1790
1791 return 0;
1792}
1793
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001794static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1795{
1796 chunk_printf(&trash, "%s%d", prefix, nb);
1797 free(srv->id);
1798 srv->id = strdup(trash.str);
1799}
1800
1801/*
1802 * Initialize as much as possible servers from <srv> server template.
1803 * Note that a server template is a special server with
1804 * a few different parameters than a server which has
1805 * been parsed mostly the same way as a server.
1806 * Returns the number of servers succesfully allocated,
1807 * 'srv' template included.
1808 */
1809static int server_template_init(struct server *srv, struct proxy *px)
1810{
1811 int i;
1812 struct server *newsrv;
1813
1814 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1815 int check_init_state;
1816 int agent_init_state;
1817
1818 newsrv = new_server(px);
1819 if (!newsrv)
1820 goto err;
1821
1822 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001823 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001824#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1825 if (newsrv->sni_expr) {
1826 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1827 if (!newsrv->ssl_ctx.sni)
1828 goto err;
1829 }
1830#endif
1831 /* Set this new server ID. */
1832 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1833
1834 /* Initial checks states. */
1835 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1836 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1837
1838 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1839 do_server_agent_check_init(newsrv, agent_init_state))
1840 goto err;
1841
1842 /* Linked backwards first. This will be restablished after parsing. */
1843 newsrv->next = px->srv;
1844 px->srv = newsrv;
1845 }
1846 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1847
1848 return i - srv->tmpl_info.nb_low;
1849
1850 err:
1851 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1852 if (newsrv) {
1853#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1854 release_sample_expr(newsrv->ssl_ctx.sni);
1855#endif
1856 free_check(&newsrv->agent);
1857 free_check(&newsrv->check);
1858 }
1859 free(newsrv);
1860 return i - srv->tmpl_info.nb_low;
1861}
1862
Willy Tarreau272adea2014-03-31 10:39:59 +02001863int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1864{
1865 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001866 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001867 char *errmsg = NULL;
1868 int err_code = 0;
1869 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001870 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001871
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001872 if (!strcmp(args[0], "server") ||
1873 !strcmp(args[0], "default-server") ||
1874 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001875 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001876 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001877 int srv = !defsrv && !strcmp(args[0], "server");
1878 int srv_tmpl = !defsrv && !srv;
1879 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001880
1881 if (!defsrv && curproxy == defproxy) {
1882 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1883 err_code |= ERR_ALERT | ERR_FATAL;
1884 goto out;
1885 }
1886 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1887 err_code |= ERR_ALERT | ERR_FATAL;
1888
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001889 /* There is no mandatory first arguments for default server. */
1890 if (srv) {
1891 if (!*args[2]) {
1892 /* 'server' line number of argument check. */
1893 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1894 file, linenum, args[0]);
1895 err_code |= ERR_ALERT | ERR_FATAL;
1896 goto out;
1897 }
1898
1899 err = invalid_char(args[1]);
1900 }
1901 else if (srv_tmpl) {
1902 if (!*args[3]) {
1903 /* 'server-template' line number of argument check. */
1904 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
1905 file, linenum, args[0]);
1906 err_code |= ERR_ALERT | ERR_FATAL;
1907 goto out;
1908 }
1909
1910 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001911 }
1912
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001913 if (err) {
1914 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
1915 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001916 err_code |= ERR_ALERT | ERR_FATAL;
1917 goto out;
1918 }
1919
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001920 cur_arg = 2;
1921 if (srv_tmpl) {
1922 /* Parse server-template <nb | range> arg. */
1923 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
1924 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
1925 file, linenum, args[0], args[cur_arg]);
1926 err_code |= ERR_ALERT | ERR_FATAL;
1927 goto out;
1928 }
1929 cur_arg++;
1930 }
1931
Willy Tarreau272adea2014-03-31 10:39:59 +02001932 if (!defsrv) {
1933 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001934 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001935 struct protocol *proto;
1936
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001937 newsrv = new_server(curproxy);
1938 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001939 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1940 err_code |= ERR_ALERT | ERR_ABORT;
1941 goto out;
1942 }
1943
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001944 if (srv_tmpl) {
1945 newsrv->tmpl_info.nb_low = tmpl_range_low;
1946 newsrv->tmpl_info.nb_high = tmpl_range_high;
1947 }
1948
Willy Tarreau272adea2014-03-31 10:39:59 +02001949 /* the servers are linked backwards first */
1950 newsrv->next = curproxy->srv;
1951 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02001952 newsrv->conf.file = strdup(file);
1953 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001954 /* Note: for a server template, its id is its prefix.
1955 * This is a temporary id which will be used for server allocations to come
1956 * after parsing.
1957 */
1958 if (srv)
1959 newsrv->id = strdup(args[1]);
1960 else
1961 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001962
1963 /* several ways to check the port component :
1964 * - IP => port=+0, relative (IPv4 only)
1965 * - IP: => port=+0, relative
1966 * - IP:N => port=N, absolute
1967 * - IP:+N => port=+N, relative
1968 * - IP:-N => port=-N, relative
1969 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001970 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001971 if (!sk) {
1972 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1973 err_code |= ERR_ALERT | ERR_FATAL;
1974 goto out;
1975 }
1976
1977 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001978 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001979 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1980 file, linenum, args[0], args[1]);
1981 err_code |= ERR_ALERT | ERR_FATAL;
1982 goto out;
1983 }
1984
1985 if (!port1 || !port2) {
1986 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001987 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001988 }
1989 else if (port1 != port2) {
1990 /* port range */
1991 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1992 file, linenum, args[0], args[1], args[2]);
1993 err_code |= ERR_ALERT | ERR_FATAL;
1994 goto out;
1995 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001996
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001997 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02001998 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001999 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002000 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002001 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2002 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2003 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002004 err_code |= ERR_ALERT | ERR_FATAL;
2005 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002006 }
2007 }
2008 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
2009 Alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
2010 file, linenum, newsrv->id);
2011 err_code |= ERR_ALERT | ERR_FATAL;
2012 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002013 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002014 }
2015
Willy Tarreau272adea2014-03-31 10:39:59 +02002016 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002017 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002018
Olivier Houchard8da5f982017-08-04 18:35:36 +02002019 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002020 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002021 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002022 err_code |= ERR_ALERT | ERR_FATAL;
2023 goto out;
2024 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002025
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002026 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002027 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002028 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002029 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002030 } else {
2031 newsrv = &curproxy->defsrv;
2032 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002033 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002034 }
2035
2036 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002037 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002038 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2039 if (err) {
2040 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2041 file, linenum, *err, newsrv->id);
2042 err_code |= ERR_ALERT | ERR_FATAL;
2043 goto out;
2044 }
2045 if (val <= 0) {
2046 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2047 file, linenum, val, args[cur_arg], newsrv->id);
2048 err_code |= ERR_ALERT | ERR_FATAL;
2049 goto out;
2050 }
2051 newsrv->agent.inter = val;
2052 cur_arg += 2;
2053 }
Misiekea849332017-01-09 09:39:51 +01002054 else if (!strcmp(args[cur_arg], "agent-addr")) {
2055 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2056 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2057 goto out;
2058 }
2059
2060 cur_arg += 2;
2061 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002062 else if (!strcmp(args[cur_arg], "agent-port")) {
2063 global.maxsock++;
2064 newsrv->agent.port = atol(args[cur_arg + 1]);
2065 cur_arg += 2;
2066 }
James Brown55f9ff12015-10-21 18:19:05 -07002067 else if (!strcmp(args[cur_arg], "agent-send")) {
2068 global.maxsock++;
2069 free(newsrv->agent.send_string);
2070 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2071 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2072 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2073 cur_arg += 2;
2074 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002075 else if (!strcmp(args[cur_arg], "init-addr")) {
2076 char *p, *end;
2077 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002078 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002079
2080 newsrv->init_addr_methods = 0;
2081 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2082
2083 for (p = args[cur_arg + 1]; *p; p = end) {
2084 /* cut on next comma */
2085 for (end = p; *end && *end != ','; end++);
2086 if (*end)
2087 *(end++) = 0;
2088
Willy Tarreau4310d362016-11-02 15:05:56 +01002089 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002090 if (!strcmp(p, "libc")) {
2091 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2092 }
2093 else if (!strcmp(p, "last")) {
2094 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2095 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002096 else if (!strcmp(p, "none")) {
2097 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2098 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002099 else if (str2ip2(p, &sa, 0)) {
2100 if (is_addr(&newsrv->init_addr)) {
2101 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2102 file, linenum, args[cur_arg], p);
2103 err_code |= ERR_ALERT | ERR_FATAL;
2104 goto out;
2105 }
2106 newsrv->init_addr = sa;
2107 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2108 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002109 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002110 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002111 file, linenum, args[cur_arg], p);
2112 err_code |= ERR_ALERT | ERR_FATAL;
2113 goto out;
2114 }
2115 if (!done) {
2116 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2117 file, linenum, args[cur_arg], p);
2118 err_code |= ERR_ALERT | ERR_FATAL;
2119 goto out;
2120 }
2121 }
2122 cur_arg += 2;
2123 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002124 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002125 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002126 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2127 cur_arg += 2;
2128 }
2129 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2130 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002131 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002132 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002133 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002134 else {
2135 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2136 file, linenum, args[cur_arg]);
2137 err_code |= ERR_ALERT | ERR_FATAL;
2138 goto out;
2139 }
2140 cur_arg += 2;
2141 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002142 else if (!strcmp(args[cur_arg], "resolve-net")) {
2143 char *p, *e;
2144 unsigned char mask;
2145 struct dns_options *opt;
2146
2147 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
2148 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2149 file, linenum, args[cur_arg]);
2150 err_code |= ERR_ALERT | ERR_FATAL;
2151 goto out;
2152 }
2153
2154 opt = &newsrv->dns_opts;
2155
2156 /* Split arguments by comma, and convert it from ipv4 or ipv6
2157 * string network in in_addr or in6_addr.
2158 */
2159 p = args[cur_arg + 1];
2160 e = p;
2161 while (*p != '\0') {
2162 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002163 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002164 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2165 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2166 err_code |= ERR_ALERT | ERR_FATAL;
2167 goto out;
2168 }
2169 /* look for end or comma. */
2170 while (*e != ',' && *e != '\0')
2171 e++;
2172 if (*e == ',') {
2173 *e = '\0';
2174 e++;
2175 }
2176 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2177 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2178 /* Try to convert input string from ipv4 or ipv6 network. */
2179 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2180 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2181 &mask)) {
2182 /* Try to convert input string from ipv6 network. */
2183 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2184 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2185 } else {
2186 /* All network conversions fail, retrun error. */
2187 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2188 file, linenum, args[cur_arg], p);
2189 err_code |= ERR_ALERT | ERR_FATAL;
2190 goto out;
2191 }
2192 opt->pref_net_nb++;
2193 p = e;
2194 }
2195
2196 cur_arg += 2;
2197 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002198 else if (!strcmp(args[cur_arg], "rise")) {
2199 if (!*args[cur_arg + 1]) {
2200 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2201 file, linenum, args[cur_arg]);
2202 err_code |= ERR_ALERT | ERR_FATAL;
2203 goto out;
2204 }
2205
2206 newsrv->check.rise = atol(args[cur_arg + 1]);
2207 if (newsrv->check.rise <= 0) {
2208 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2209 file, linenum, args[cur_arg]);
2210 err_code |= ERR_ALERT | ERR_FATAL;
2211 goto out;
2212 }
2213
2214 if (newsrv->check.health)
2215 newsrv->check.health = newsrv->check.rise;
2216 cur_arg += 2;
2217 }
2218 else if (!strcmp(args[cur_arg], "fall")) {
2219 newsrv->check.fall = atol(args[cur_arg + 1]);
2220
2221 if (!*args[cur_arg + 1]) {
2222 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2223 file, linenum, args[cur_arg]);
2224 err_code |= ERR_ALERT | ERR_FATAL;
2225 goto out;
2226 }
2227
2228 if (newsrv->check.fall <= 0) {
2229 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2230 file, linenum, args[cur_arg]);
2231 err_code |= ERR_ALERT | ERR_FATAL;
2232 goto out;
2233 }
2234
2235 cur_arg += 2;
2236 }
2237 else if (!strcmp(args[cur_arg], "inter")) {
2238 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2239 if (err) {
2240 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2241 file, linenum, *err, newsrv->id);
2242 err_code |= ERR_ALERT | ERR_FATAL;
2243 goto out;
2244 }
2245 if (val <= 0) {
2246 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2247 file, linenum, val, args[cur_arg], newsrv->id);
2248 err_code |= ERR_ALERT | ERR_FATAL;
2249 goto out;
2250 }
2251 newsrv->check.inter = val;
2252 cur_arg += 2;
2253 }
2254 else if (!strcmp(args[cur_arg], "fastinter")) {
2255 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2256 if (err) {
2257 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2258 file, linenum, *err, newsrv->id);
2259 err_code |= ERR_ALERT | ERR_FATAL;
2260 goto out;
2261 }
2262 if (val <= 0) {
2263 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2264 file, linenum, val, args[cur_arg], newsrv->id);
2265 err_code |= ERR_ALERT | ERR_FATAL;
2266 goto out;
2267 }
2268 newsrv->check.fastinter = val;
2269 cur_arg += 2;
2270 }
2271 else if (!strcmp(args[cur_arg], "downinter")) {
2272 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2273 if (err) {
2274 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2275 file, linenum, *err, newsrv->id);
2276 err_code |= ERR_ALERT | ERR_FATAL;
2277 goto out;
2278 }
2279 if (val <= 0) {
2280 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2281 file, linenum, val, args[cur_arg], newsrv->id);
2282 err_code |= ERR_ALERT | ERR_FATAL;
2283 goto out;
2284 }
2285 newsrv->check.downinter = val;
2286 cur_arg += 2;
2287 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002288 else if (!strcmp(args[cur_arg], "port")) {
2289 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002290 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002291 cur_arg += 2;
2292 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002293 else if (!strcmp(args[cur_arg], "weight")) {
2294 int w;
2295 w = atol(args[cur_arg + 1]);
2296 if (w < 0 || w > SRV_UWGHT_MAX) {
2297 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2298 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2299 err_code |= ERR_ALERT | ERR_FATAL;
2300 goto out;
2301 }
2302 newsrv->uweight = newsrv->iweight = w;
2303 cur_arg += 2;
2304 }
2305 else if (!strcmp(args[cur_arg], "minconn")) {
2306 newsrv->minconn = atol(args[cur_arg + 1]);
2307 cur_arg += 2;
2308 }
2309 else if (!strcmp(args[cur_arg], "maxconn")) {
2310 newsrv->maxconn = atol(args[cur_arg + 1]);
2311 cur_arg += 2;
2312 }
2313 else if (!strcmp(args[cur_arg], "maxqueue")) {
2314 newsrv->maxqueue = atol(args[cur_arg + 1]);
2315 cur_arg += 2;
2316 }
2317 else if (!strcmp(args[cur_arg], "slowstart")) {
2318 /* slowstart is stored in seconds */
2319 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2320 if (err) {
2321 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2322 file, linenum, *err, newsrv->id);
2323 err_code |= ERR_ALERT | ERR_FATAL;
2324 goto out;
2325 }
2326 newsrv->slowstart = (val + 999) / 1000;
2327 cur_arg += 2;
2328 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002329 else if (!strcmp(args[cur_arg], "on-error")) {
2330 if (!strcmp(args[cur_arg + 1], "fastinter"))
2331 newsrv->onerror = HANA_ONERR_FASTINTER;
2332 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2333 newsrv->onerror = HANA_ONERR_FAILCHK;
2334 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2335 newsrv->onerror = HANA_ONERR_SUDDTH;
2336 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2337 newsrv->onerror = HANA_ONERR_MARKDWN;
2338 else {
2339 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2340 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2341 file, linenum, args[cur_arg], args[cur_arg + 1]);
2342 err_code |= ERR_ALERT | ERR_FATAL;
2343 goto out;
2344 }
2345
2346 cur_arg += 2;
2347 }
2348 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2349 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2350 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2351 else {
2352 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2353 file, linenum, args[cur_arg], args[cur_arg + 1]);
2354 err_code |= ERR_ALERT | ERR_FATAL;
2355 goto out;
2356 }
2357
2358 cur_arg += 2;
2359 }
2360 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2361 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2362 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2363 else {
2364 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2365 file, linenum, args[cur_arg], args[cur_arg + 1]);
2366 err_code |= ERR_ALERT | ERR_FATAL;
2367 goto out;
2368 }
2369
2370 cur_arg += 2;
2371 }
2372 else if (!strcmp(args[cur_arg], "error-limit")) {
2373 if (!*args[cur_arg + 1]) {
2374 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2375 file, linenum, args[cur_arg]);
2376 err_code |= ERR_ALERT | ERR_FATAL;
2377 goto out;
2378 }
2379
2380 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2381
2382 if (newsrv->consecutive_errors_limit <= 0) {
2383 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2384 file, linenum, args[cur_arg]);
2385 err_code |= ERR_ALERT | ERR_FATAL;
2386 goto out;
2387 }
2388 cur_arg += 2;
2389 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002390 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002391 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2392 file, linenum, "usesrc", "source");
2393 err_code |= ERR_ALERT | ERR_FATAL;
2394 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002395 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002396 else {
2397 static int srv_dumped;
2398 struct srv_kw *kw;
2399 char *err;
2400
2401 kw = srv_find_kw(args[cur_arg]);
2402 if (kw) {
2403 char *err = NULL;
2404 int code;
2405
2406 if (!kw->parse) {
2407 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2408 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002409 if (kw->skip != -1)
2410 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002411 err_code |= ERR_ALERT | ERR_FATAL;
2412 goto out;
2413 }
2414
2415 if (defsrv && !kw->default_ok) {
2416 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2417 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002418 if (kw->skip != -1)
2419 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002420 err_code |= ERR_ALERT;
2421 continue;
2422 }
2423
2424 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2425 err_code |= code;
2426
2427 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002428 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002429 if (code & ERR_FATAL) {
2430 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002431 if (kw->skip != -1)
2432 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002433 goto out;
2434 }
2435 }
2436 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002437 if (kw->skip != -1)
2438 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002439 continue;
2440 }
2441
2442 err = NULL;
2443 if (!srv_dumped) {
2444 srv_dump_kws(&err);
2445 indent_msg(&err, 4);
2446 srv_dumped = 1;
2447 }
2448
2449 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2450 file, linenum, args[0], args[1], args[cur_arg],
2451 err ? " Registered keywords :" : "", err ? err : "");
2452 free(err);
2453
2454 err_code |= ERR_ALERT | ERR_FATAL;
2455 goto out;
2456 }
2457 }
2458
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002459 if (!defsrv)
2460 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2461 if (err_code & ERR_FATAL)
2462 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002463 if (srv_tmpl)
2464 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002465 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002466 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002467 return 0;
2468
2469 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002470 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002471 free(errmsg);
2472 return err_code;
2473}
2474
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002475/* Returns a pointer to the first server matching either id <id>.
2476 * NULL is returned if no match is found.
2477 * the lookup is performed in the backend <bk>
2478 */
2479struct server *server_find_by_id(struct proxy *bk, int id)
2480{
2481 struct eb32_node *eb32;
2482 struct server *curserver;
2483
2484 if (!bk || (id ==0))
2485 return NULL;
2486
2487 /* <bk> has no backend capabilities, so it can't have a server */
2488 if (!(bk->cap & PR_CAP_BE))
2489 return NULL;
2490
2491 curserver = NULL;
2492
2493 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2494 if (eb32)
2495 curserver = container_of(eb32, struct server, conf.id);
2496
2497 return curserver;
2498}
2499
2500/* Returns a pointer to the first server matching either name <name>, or id
2501 * if <name> starts with a '#'. NULL is returned if no match is found.
2502 * the lookup is performed in the backend <bk>
2503 */
2504struct server *server_find_by_name(struct proxy *bk, const char *name)
2505{
2506 struct server *curserver;
2507
2508 if (!bk || !name)
2509 return NULL;
2510
2511 /* <bk> has no backend capabilities, so it can't have a server */
2512 if (!(bk->cap & PR_CAP_BE))
2513 return NULL;
2514
2515 curserver = NULL;
2516 if (*name == '#') {
2517 curserver = server_find_by_id(bk, atoi(name + 1));
2518 if (curserver)
2519 return curserver;
2520 }
2521 else {
2522 curserver = bk->srv;
2523
2524 while (curserver && (strcmp(curserver->id, name) != 0))
2525 curserver = curserver->next;
2526
2527 if (curserver)
2528 return curserver;
2529 }
2530
2531 return NULL;
2532}
2533
2534struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2535{
2536 struct server *byname;
2537 struct server *byid;
2538
2539 if (!name && !id)
2540 return NULL;
2541
2542 if (diff)
2543 *diff = 0;
2544
2545 byname = byid = NULL;
2546
2547 if (name) {
2548 byname = server_find_by_name(bk, name);
2549 if (byname && (!id || byname->puid == id))
2550 return byname;
2551 }
2552
2553 /* remaining possibilities :
2554 * - name not set
2555 * - name set but not found
2556 * - name found but ID doesn't match
2557 */
2558 if (id) {
2559 byid = server_find_by_id(bk, id);
2560 if (byid) {
2561 if (byname) {
2562 /* use id only if forced by configuration */
2563 if (byid->flags & SRV_F_FORCED_ID) {
2564 if (diff)
2565 *diff |= 2;
2566 return byid;
2567 }
2568 else {
2569 if (diff)
2570 *diff |= 1;
2571 return byname;
2572 }
2573 }
2574
2575 /* remaining possibilities:
2576 * - name not set
2577 * - name set but not found
2578 */
2579 if (name && diff)
2580 *diff |= 2;
2581 return byid;
2582 }
2583
2584 /* id bot found */
2585 if (byname) {
2586 if (diff)
2587 *diff |= 1;
2588 return byname;
2589 }
2590 }
2591
2592 return NULL;
2593}
2594
Christopher Faulet5d42e092017-10-16 12:00:40 +02002595/* Registers changes to be applied asynchronously */
2596static void srv_register_update(struct server *srv)
2597{
2598 if (LIST_ISEMPTY(&srv->update_status)) {
2599 THREAD_WANT_SYNC();
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002600 HA_SPIN_LOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
Christopher Faulet5d42e092017-10-16 12:00:40 +02002601 if (LIST_ISEMPTY(&srv->update_status))
2602 LIST_ADDQ(&updated_servers, &srv->update_status);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002603 HA_SPIN_UNLOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
Christopher Faulet5d42e092017-10-16 12:00:40 +02002604 }
2605}
2606
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002607/* Update a server state using the parameters available in the params list */
2608static void srv_update_state(struct server *srv, int version, char **params)
2609{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002610 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002611 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002612
2613 /* fields since version 1
2614 * and common to all other upcoming versions
2615 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002616 enum srv_state srv_op_state;
2617 enum srv_admin srv_admin_state;
2618 unsigned srv_uweight, srv_iweight;
2619 unsigned long srv_last_time_change;
2620 short srv_check_status;
2621 enum chk_result srv_check_result;
2622 int srv_check_health;
2623 int srv_check_state, srv_agent_state;
2624 int bk_f_forced_id;
2625 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002626 int fqdn_set_by_cli;
2627 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002628 const char *port_str;
2629 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002630
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002631 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002632 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002633 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002634 switch (version) {
2635 case 1:
2636 /*
2637 * now we can proceed with server's state update:
2638 * srv_addr: params[0]
2639 * srv_op_state: params[1]
2640 * srv_admin_state: params[2]
2641 * srv_uweight: params[3]
2642 * srv_iweight: params[4]
2643 * srv_last_time_change: params[5]
2644 * srv_check_status: params[6]
2645 * srv_check_result: params[7]
2646 * srv_check_health: params[8]
2647 * srv_check_state: params[9]
2648 * srv_agent_state: params[10]
2649 * bk_f_forced_id: params[11]
2650 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002651 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002652 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002653 */
2654
2655 /* validating srv_op_state */
2656 p = NULL;
2657 errno = 0;
2658 srv_op_state = strtol(params[1], &p, 10);
2659 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2660 (srv_op_state != SRV_ST_STOPPED &&
2661 srv_op_state != SRV_ST_STARTING &&
2662 srv_op_state != SRV_ST_RUNNING &&
2663 srv_op_state != SRV_ST_STOPPING)) {
2664 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2665 }
2666
2667 /* validating srv_admin_state */
2668 p = NULL;
2669 errno = 0;
2670 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002671 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002672
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002673 /* inherited statuses will be recomputed later.
2674 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2675 */
2676 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002677
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002678 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2679 (srv_admin_state != 0 &&
2680 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002681 srv_admin_state != SRV_ADMF_CMAINT &&
2682 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002683 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002684 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002685 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2686 }
2687
2688 /* validating srv_uweight */
2689 p = NULL;
2690 errno = 0;
2691 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002692 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002693 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2694
2695 /* validating srv_iweight */
2696 p = NULL;
2697 errno = 0;
2698 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002699 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002700 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2701
2702 /* validating srv_last_time_change */
2703 p = NULL;
2704 errno = 0;
2705 srv_last_time_change = strtol(params[5], &p, 10);
2706 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2707 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2708
2709 /* validating srv_check_status */
2710 p = NULL;
2711 errno = 0;
2712 srv_check_status = strtol(params[6], &p, 10);
2713 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2714 (srv_check_status >= HCHK_STATUS_SIZE))
2715 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2716
2717 /* validating srv_check_result */
2718 p = NULL;
2719 errno = 0;
2720 srv_check_result = strtol(params[7], &p, 10);
2721 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2722 (srv_check_result != CHK_RES_UNKNOWN &&
2723 srv_check_result != CHK_RES_NEUTRAL &&
2724 srv_check_result != CHK_RES_FAILED &&
2725 srv_check_result != CHK_RES_PASSED &&
2726 srv_check_result != CHK_RES_CONDPASS)) {
2727 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2728 }
2729
2730 /* validating srv_check_health */
2731 p = NULL;
2732 errno = 0;
2733 srv_check_health = strtol(params[8], &p, 10);
2734 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2735 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2736
2737 /* validating srv_check_state */
2738 p = NULL;
2739 errno = 0;
2740 srv_check_state = strtol(params[9], &p, 10);
2741 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2742 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2743 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2744
2745 /* validating srv_agent_state */
2746 p = NULL;
2747 errno = 0;
2748 srv_agent_state = strtol(params[10], &p, 10);
2749 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2750 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2751 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2752
2753 /* validating bk_f_forced_id */
2754 p = NULL;
2755 errno = 0;
2756 bk_f_forced_id = strtol(params[11], &p, 10);
2757 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2758 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2759
2760 /* validating srv_f_forced_id */
2761 p = NULL;
2762 errno = 0;
2763 srv_f_forced_id = strtol(params[12], &p, 10);
2764 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2765 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2766
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002767 /* validating srv_fqdn */
2768 fqdn = params[13];
2769 if (fqdn && *fqdn == '-')
2770 fqdn = NULL;
2771 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2772 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2773 fqdn = NULL;
2774 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002775
Frédéric Lécaille31694712017-08-01 08:47:19 +02002776 port_str = params[14];
2777 if (port_str) {
2778 port = strl2uic(port_str, strlen(port_str));
2779 if (port > USHRT_MAX) {
2780 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2781 port_str = NULL;
2782 }
2783 }
2784
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002785 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002786 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002787 goto out;
2788
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002789 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002790 /* recover operational state and apply it to this server
2791 * and all servers tracking this one */
2792 switch (srv_op_state) {
2793 case SRV_ST_STOPPED:
2794 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002795 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002796 break;
2797 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002798 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002799 break;
2800 case SRV_ST_STOPPING:
2801 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002802 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002803 break;
2804 case SRV_ST_RUNNING:
2805 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002806 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002807 break;
2808 }
2809
2810 /* When applying server state, the following rules apply:
2811 * - in case of a configuration change, we apply the setting from the new
2812 * configuration, regardless of old running state
2813 * - if no configuration change, we apply old running state only if old running
2814 * state is different from new configuration state
2815 */
2816 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002817 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2818 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002819 srv_adm_set_maint(srv);
2820 else
2821 srv_adm_set_ready(srv);
2822 }
2823 /* configuration is the same, let's compate old running state and new conf state */
2824 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002825 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002826 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002827 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002828 srv_adm_set_ready(srv);
2829 }
2830 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002831 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002832 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002833 * (srv->iweight is the weight set up in configuration).
2834 * There are two possible reasons for FDRAIN to have been present :
2835 * - previous config weight was zero
2836 * - "set server b/s drain" was sent to the CLI
2837 *
2838 * In the first case, we simply want to drop this drain state
2839 * if the new weight is not zero anymore, meaning the administrator
2840 * has intentionally turned the weight back to a positive value to
2841 * enable the server again after an operation. In the second case,
2842 * the drain state was forced on the CLI regardless of the config's
2843 * weight so we don't want a change to the config weight to lose this
2844 * status. What this means is :
2845 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2846 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002847 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002848 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002849 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002850 }
2851
2852 srv->last_change = date.tv_sec - srv_last_time_change;
2853 srv->check.status = srv_check_status;
2854 srv->check.result = srv_check_result;
2855 srv->check.health = srv_check_health;
2856
2857 /* Only case we want to apply is removing ENABLED flag which could have been
2858 * done by the "disable health" command over the stats socket
2859 */
2860 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2861 (srv_check_state & CHK_ST_CONFIGURED) &&
2862 !(srv_check_state & CHK_ST_ENABLED))
2863 srv->check.state &= ~CHK_ST_ENABLED;
2864
2865 /* Only case we want to apply is removing ENABLED flag which could have been
2866 * done by the "disable agent" command over the stats socket
2867 */
2868 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2869 (srv_agent_state & CHK_ST_CONFIGURED) &&
2870 !(srv_agent_state & CHK_ST_ENABLED))
2871 srv->agent.state &= ~CHK_ST_ENABLED;
2872
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002873 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2874 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002875 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002876 * It means that a configuration file change has precedence over a unix socket change
2877 * for server's weight
2878 *
2879 * by default, HAProxy applies the following weight when parsing the configuration
2880 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002881 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002882 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002883 srv->uweight = srv_uweight;
2884 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002885 server_recalc_eweight(srv);
2886
Willy Tarreaue5a60682016-11-09 14:54:53 +01002887 /* load server IP address */
2888 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002889
2890 if (fqdn && srv->hostname) {
2891 if (!strcmp(srv->hostname, fqdn)) {
2892 /* Here we reset the 'set from stats socket FQDN' flag
2893 * to support such transitions:
2894 * Let's say initial FQDN value is foo1 (in configuration file).
2895 * - FQDN changed from stats socket, from foo1 to foo2 value,
2896 * - FQDN changed again from file configuration (with the same previous value
2897 set from stats socket, from foo1 to foo2 value),
2898 * - reload for any other reason than a FQDN modification,
2899 * the configuration file FQDN matches the fqdn server state file value.
2900 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
2901 * any futher FQDN modification.
2902 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002903 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002904 }
2905 else {
2906 /* If the FDQN has been changed from stats socket,
2907 * apply fqdn state file value (which is the value set
2908 * from stats socket).
2909 */
2910 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002911 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02002912 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002913 }
2914 }
2915 }
2916
Frédéric Lécaille31694712017-08-01 08:47:19 +02002917 if (port_str)
2918 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002919 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002920
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002921 break;
2922 default:
2923 chunk_appendf(msg, ", version '%d' not supported", version);
2924 }
2925
2926 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002927 if (msg->len) {
2928 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002929 Warning("server-state application failed for server '%s/%s'%s",
2930 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002931 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002932}
2933
2934/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2935 * For each proxy, it does the following:
2936 * - opens its server state file (either one or local one)
2937 * - read whole file, line by line
2938 * - analyse each line to check if it matches our current backend:
2939 * - backend name matches
2940 * - backend id matches if id is forced and name doesn't match
2941 * - if the server pointed by the line is found, then state is applied
2942 *
2943 * If the running backend uuid or id differs from the state file, then HAProxy reports
2944 * a warning.
2945 */
2946void apply_server_state(void)
2947{
2948 char *cur, *end;
2949 char mybuf[SRV_STATE_LINE_MAXLEN];
2950 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002951 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
2952 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002953 int arg, srv_arg, version, diff;
2954 FILE *f;
2955 char *filepath;
2956 char globalfilepath[MAXPATHLEN + 1];
2957 char localfilepath[MAXPATHLEN + 1];
2958 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2959 extern struct proxy *proxy;
2960 struct proxy *curproxy, *bk;
2961 struct server *srv;
2962
2963 globalfilepathlen = 0;
2964 /* create the globalfilepath variable */
2965 if (global.server_state_file) {
2966 /* absolute path or no base directory provided */
2967 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2968 len = strlen(global.server_state_file);
2969 if (len > MAXPATHLEN) {
2970 globalfilepathlen = 0;
2971 goto globalfileerror;
2972 }
2973 memcpy(globalfilepath, global.server_state_file, len);
2974 globalfilepath[len] = '\0';
2975 globalfilepathlen = len;
2976 }
2977 else if (global.server_state_base) {
2978 len = strlen(global.server_state_base);
2979 globalfilepathlen += len;
2980
2981 if (globalfilepathlen > MAXPATHLEN) {
2982 globalfilepathlen = 0;
2983 goto globalfileerror;
2984 }
2985 strncpy(globalfilepath, global.server_state_base, len);
2986 globalfilepath[globalfilepathlen] = 0;
2987
2988 /* append a slash if needed */
2989 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2990 if (globalfilepathlen + 1 > MAXPATHLEN) {
2991 globalfilepathlen = 0;
2992 goto globalfileerror;
2993 }
2994 globalfilepath[globalfilepathlen++] = '/';
2995 }
2996
2997 len = strlen(global.server_state_file);
2998 if (globalfilepathlen + len > MAXPATHLEN) {
2999 globalfilepathlen = 0;
3000 goto globalfileerror;
3001 }
3002 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3003 globalfilepathlen += len;
3004 globalfilepath[globalfilepathlen++] = 0;
3005 }
3006 }
3007 globalfileerror:
3008 if (globalfilepathlen == 0)
3009 globalfilepath[0] = '\0';
3010
3011 /* read servers state from local file */
3012 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3013 /* servers are only in backends */
3014 if (!(curproxy->cap & PR_CAP_BE))
3015 continue;
3016 fileopenerr = 0;
3017 filepath = NULL;
3018
3019 /* search server state file path and name */
3020 switch (curproxy->load_server_state_from_file) {
3021 /* read servers state from global file */
3022 case PR_SRV_STATE_FILE_GLOBAL:
3023 /* there was an error while generating global server state file path */
3024 if (globalfilepathlen == 0)
3025 continue;
3026 filepath = globalfilepath;
3027 fileopenerr = 1;
3028 break;
3029 /* this backend has its own file */
3030 case PR_SRV_STATE_FILE_LOCAL:
3031 localfilepathlen = 0;
3032 localfilepath[0] = '\0';
3033 len = 0;
3034 /* create the localfilepath variable */
3035 /* absolute path or no base directory provided */
3036 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3037 len = strlen(curproxy->server_state_file_name);
3038 if (len > MAXPATHLEN) {
3039 localfilepathlen = 0;
3040 goto localfileerror;
3041 }
3042 memcpy(localfilepath, curproxy->server_state_file_name, len);
3043 localfilepath[len] = '\0';
3044 localfilepathlen = len;
3045 }
3046 else if (global.server_state_base) {
3047 len = strlen(global.server_state_base);
3048 localfilepathlen += len;
3049
3050 if (localfilepathlen > MAXPATHLEN) {
3051 localfilepathlen = 0;
3052 goto localfileerror;
3053 }
3054 strncpy(localfilepath, global.server_state_base, len);
3055 localfilepath[localfilepathlen] = 0;
3056
3057 /* append a slash if needed */
3058 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3059 if (localfilepathlen + 1 > MAXPATHLEN) {
3060 localfilepathlen = 0;
3061 goto localfileerror;
3062 }
3063 localfilepath[localfilepathlen++] = '/';
3064 }
3065
3066 len = strlen(curproxy->server_state_file_name);
3067 if (localfilepathlen + len > MAXPATHLEN) {
3068 localfilepathlen = 0;
3069 goto localfileerror;
3070 }
3071 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3072 localfilepathlen += len;
3073 localfilepath[localfilepathlen++] = 0;
3074 }
3075 filepath = localfilepath;
3076 localfileerror:
3077 if (localfilepathlen == 0)
3078 localfilepath[0] = '\0';
3079
3080 break;
3081 case PR_SRV_STATE_FILE_NONE:
3082 default:
3083 continue;
3084 }
3085
3086 /* preload global state file */
3087 errno = 0;
3088 f = fopen(filepath, "r");
3089 if (errno && fileopenerr)
3090 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3091 if (!f)
3092 continue;
3093
3094 mybuf[0] = '\0';
3095 mybuflen = 0;
3096 version = 0;
3097
3098 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003099 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3100 Warning("Can't read first line of the server state file '%s'\n", filepath);
3101 goto fileclose;
3102 }
3103
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003104 cur = mybuf;
3105 version = atoi(cur);
3106 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3107 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003108 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003109
3110 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3111 int bk_f_forced_id = 0;
3112 int check_id = 0;
3113 int check_name = 0;
3114
3115 mybuflen = strlen(mybuf);
3116 cur = mybuf;
3117 end = cur + mybuflen;
3118
3119 bk = NULL;
3120 srv = NULL;
3121
3122 /* we need at least one character */
3123 if (mybuflen == 0)
3124 continue;
3125
3126 /* ignore blank characters at the beginning of the line */
3127 while (isspace(*cur))
3128 ++cur;
3129
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003130 /* Ignore empty or commented lines */
3131 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003132 continue;
3133
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003134 /* truncated lines */
3135 if (mybuf[mybuflen - 1] != '\n') {
3136 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003137 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003138 }
3139
3140 /* Removes trailing '\n' */
3141 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003142
3143 /* we're now ready to move the line into *srv_params[] */
3144 params[0] = cur;
3145 arg = 1;
3146 srv_arg = 0;
3147 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3148 if (isspace(*cur)) {
3149 *cur = '\0';
3150 ++cur;
3151 while (isspace(*cur))
3152 ++cur;
3153 switch (version) {
3154 case 1:
3155 /*
3156 * srv_addr: params[4] => srv_params[0]
3157 * srv_op_state: params[5] => srv_params[1]
3158 * srv_admin_state: params[6] => srv_params[2]
3159 * srv_uweight: params[7] => srv_params[3]
3160 * srv_iweight: params[8] => srv_params[4]
3161 * srv_last_time_change: params[9] => srv_params[5]
3162 * srv_check_status: params[10] => srv_params[6]
3163 * srv_check_result: params[11] => srv_params[7]
3164 * srv_check_health: params[12] => srv_params[8]
3165 * srv_check_state: params[13] => srv_params[9]
3166 * srv_agent_state: params[14] => srv_params[10]
3167 * bk_f_forced_id: params[15] => srv_params[11]
3168 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003169 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003170 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003171 */
3172 if (arg >= 4) {
3173 srv_params[srv_arg] = cur;
3174 ++srv_arg;
3175 }
3176 break;
3177 }
3178
3179 params[arg] = cur;
3180 ++arg;
3181 }
3182 else {
3183 ++cur;
3184 }
3185 }
3186
3187 /* if line is incomplete line, then ignore it.
3188 * otherwise, update useful flags */
3189 switch (version) {
3190 case 1:
3191 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3192 continue;
3193 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3194 check_id = (atoi(params[0]) == curproxy->uuid);
3195 check_name = (strcmp(curproxy->id, params[1]) == 0);
3196 break;
3197 }
3198
3199 diff = 0;
3200 bk = curproxy;
3201
3202 /* if backend can't be found, let's continue */
3203 if (!check_id && !check_name)
3204 continue;
3205 else if (!check_id && check_name) {
3206 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3207 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3208 }
3209 else if (check_id && !check_name) {
3210 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3211 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3212 /* if name doesn't match, we still want to update curproxy if the backend id
3213 * was forced in previous the previous configuration */
3214 if (!bk_f_forced_id)
3215 continue;
3216 }
3217
3218 /* look for the server by its id: param[2] */
3219 /* else look for the server by its name: param[3] */
3220 diff = 0;
3221 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3222
3223 if (!srv) {
3224 /* if no server found, then warning and continue with next line */
3225 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3226 params[3], params[2], params[0], params[1]);
3227 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3228 params[3], params[2], params[0], params[1]);
3229 continue;
3230 }
3231 else if (diff & PR_FBM_MISMATCH_ID) {
3232 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);
3233 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 +02003234 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003235 }
3236 else if (diff & PR_FBM_MISMATCH_NAME) {
3237 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);
3238 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 +02003239 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003240 }
3241
3242 /* now we can proceed with server's state update */
3243 srv_update_state(srv, version, srv_params);
3244 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003245fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003246 fclose(f);
3247 }
3248}
3249
Simon Horman7d09b9a2013-02-12 10:45:51 +09003250/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003251 * update a server's current IP address.
3252 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3253 * ip is in network format.
3254 * updater is a string which contains an information about the requester of the update.
3255 * updater is used if not NULL.
3256 *
3257 * A log line and a stderr warning message is generated based on server's backend options.
3258 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003259int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003260{
3261 /* generates a log line and a warning on stderr */
3262 if (1) {
3263 /* book enough space for both IPv4 and IPv6 */
3264 char oldip[INET6_ADDRSTRLEN];
3265 char newip[INET6_ADDRSTRLEN];
3266
3267 memset(oldip, '\0', INET6_ADDRSTRLEN);
3268 memset(newip, '\0', INET6_ADDRSTRLEN);
3269
3270 /* copy old IP address in a string */
3271 switch (s->addr.ss_family) {
3272 case AF_INET:
3273 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3274 break;
3275 case AF_INET6:
3276 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3277 break;
3278 };
3279
3280 /* copy new IP address in a string */
3281 switch (ip_sin_family) {
3282 case AF_INET:
3283 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3284 break;
3285 case AF_INET6:
3286 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3287 break;
3288 };
3289
3290 /* save log line into a buffer */
3291 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3292 s->proxy->id, s->id, oldip, newip, updater);
3293
3294 /* write the buffer on stderr */
3295 Warning("%s.\n", trash.str);
3296
3297 /* send a log */
3298 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3299 }
3300
3301 /* save the new IP family */
3302 s->addr.ss_family = ip_sin_family;
3303 /* save the new IP address */
3304 switch (ip_sin_family) {
3305 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003306 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003307 break;
3308 case AF_INET6:
3309 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3310 break;
3311 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003312 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003313
3314 return 0;
3315}
3316
3317/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003318 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3319 *
3320 * Caller can pass its name through <updater> to get it integrated in the response message
3321 * returned by the function.
3322 *
3323 * The function first does the following, in that order:
3324 * - validates the new addr and/or port
3325 * - checks if an update is required (new IP or port is different than current ones)
3326 * - checks the update is allowed:
3327 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3328 * - allow all changes if no CHECKS are configured
3329 * - if CHECK is configured:
3330 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3331 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3332 * conditions are met
3333 */
3334const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3335{
3336 struct sockaddr_storage sa;
3337 int ret, port_change_required;
3338 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003339 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003340 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003341 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003342
3343 msg = get_trash_chunk();
3344 chunk_reset(msg);
3345
3346 if (addr) {
3347 memset(&sa, 0, sizeof(struct sockaddr_storage));
3348 if (str2ip2(addr, &sa, 0) == NULL) {
3349 chunk_printf(msg, "Invalid addr '%s'", addr);
3350 goto out;
3351 }
3352
3353 /* changes are allowed on AF_INET* families only */
3354 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3355 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3356 goto out;
3357 }
3358
3359 /* collecting data currently setup */
3360 memset(current_addr, '\0', sizeof(current_addr));
3361 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3362 /* changes are allowed on AF_INET* families only */
3363 if ((ret != AF_INET) && (ret != AF_INET6)) {
3364 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3365 goto out;
3366 }
3367
3368 /* applying ADDR changes if required and allowed
3369 * ipcmp returns 0 when both ADDR are the same
3370 */
3371 if (ipcmp(&s->addr, &sa) == 0) {
3372 chunk_appendf(msg, "no need to change the addr");
3373 goto port;
3374 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003375 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003376 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003377
3378 /* we also need to update check's ADDR only if it uses the server's one */
3379 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003380 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003381 }
3382
3383 /* we also need to update agent ADDR only if it use the server's one */
3384 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003385 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003386 }
3387
3388 /* update report for caller */
3389 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3390 }
3391
3392 port:
3393 if (port) {
3394 char sign = '\0';
3395 char *endptr;
3396
3397 if (addr)
3398 chunk_appendf(msg, ", ");
3399
3400 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003401 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003402
3403 /* check if PORT change is required */
3404 port_change_required = 0;
3405
3406 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003407 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003408 new_port = strtol(port, &endptr, 10);
3409 if ((errno != 0) || (port == endptr)) {
3410 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3411 goto out;
3412 }
3413
3414 /* check if caller triggers a port mapped or offset */
3415 if (sign == '-' || (sign == '+')) {
3416 /* check if server currently uses port map */
3417 if (!(s->flags & SRV_F_MAPPORTS)) {
3418 /* switch from fixed port to port map mandatorily triggers
3419 * a port change */
3420 port_change_required = 1;
3421 /* check is configured
3422 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3423 * prevent PORT change if check doesn't have it's dedicated port while switching
3424 * to port mapping */
3425 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3426 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.");
3427 goto out;
3428 }
3429 }
3430 /* we're already using port maps */
3431 else {
3432 port_change_required = current_port != new_port;
3433 }
3434 }
3435 /* fixed port */
3436 else {
3437 port_change_required = current_port != new_port;
3438 }
3439
3440 /* applying PORT changes if required and update response message */
3441 if (port_change_required) {
3442 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003443 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003444 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003445
3446 /* prepare message */
3447 chunk_appendf(msg, "port changed from '");
3448 if (s->flags & SRV_F_MAPPORTS)
3449 chunk_appendf(msg, "+");
3450 chunk_appendf(msg, "%d' to '", current_port);
3451
3452 if (sign == '-') {
3453 s->flags |= SRV_F_MAPPORTS;
3454 chunk_appendf(msg, "%c", sign);
3455 /* just use for result output */
3456 new_port = -new_port;
3457 }
3458 else if (sign == '+') {
3459 s->flags |= SRV_F_MAPPORTS;
3460 chunk_appendf(msg, "%c", sign);
3461 }
3462 else {
3463 s->flags &= ~SRV_F_MAPPORTS;
3464 }
3465
3466 chunk_appendf(msg, "%d'", new_port);
3467
3468 /* we also need to update health checks port only if it uses server's realport */
3469 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3470 s->check.port = new_port;
3471 }
3472 }
3473 else {
3474 chunk_appendf(msg, "no need to change the port");
3475 }
3476 }
3477
3478out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003479 if (changed)
3480 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003481 if (updater)
3482 chunk_appendf(msg, " by '%s'", updater);
3483 chunk_appendf(msg, "\n");
3484 return msg->str;
3485}
3486
3487
3488/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003489 * update server status based on result of name resolution
3490 * returns:
3491 * 0 if server status is updated
3492 * 1 if server status has not changed
3493 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003494int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003495{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003496 struct dns_resolvers *resolvers = s->resolvers;
3497 struct dns_resolution *resolution = s->dns_requester->resolution;
3498 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003499
3500 switch (resolution->status) {
3501 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003502 /* status when HAProxy has just (re)started.
3503 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003504 break;
3505
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003506 case RSLV_STATUS_VALID:
3507 /*
3508 * resume health checks
3509 * server will be turned back on if health check is safe
3510 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003511 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003512 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003513 return 1;
3514 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3515 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003516 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003517 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003518
Emeric Brun52a91d32017-08-31 14:41:55 +02003519 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003520 return 1;
3521 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3522 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3523 s->proxy->id, s->id);
3524
3525 Warning("%s.\n", trash.str);
3526 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3527 return 0;
3528
3529 case RSLV_STATUS_NX:
3530 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003531 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3532 if (!tick_is_expired(exp, now_ms))
3533 break;
3534
3535 if (s->next_admin & SRV_ADMF_RMAINT)
3536 return 1;
3537 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3538 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003539
3540 case RSLV_STATUS_TIMEOUT:
3541 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003542 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3543 if (!tick_is_expired(exp, now_ms))
3544 break;
3545
3546 if (s->next_admin & SRV_ADMF_RMAINT)
3547 return 1;
3548 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3549 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003550
3551 case RSLV_STATUS_REFUSED:
3552 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003553 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3554 if (!tick_is_expired(exp, now_ms))
3555 break;
3556
3557 if (s->next_admin & SRV_ADMF_RMAINT)
3558 return 1;
3559 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3560 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003561
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003562 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003563 /* stop server if resolution failed for a long enough period */
3564 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3565 if (!tick_is_expired(exp, now_ms))
3566 break;
3567
3568 if (s->next_admin & SRV_ADMF_RMAINT)
3569 return 1;
3570 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3571 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003572 }
3573
3574 return 1;
3575}
3576
3577/*
3578 * Server Name Resolution valid response callback
3579 * It expects:
3580 * - <nameserver>: the name server which answered the valid response
3581 * - <response>: buffer containing a valid DNS response
3582 * - <response_len>: size of <response>
3583 * It performs the following actions:
3584 * - ignore response if current ip found and server family not met
3585 * - update with first new ip found if family is met and current IP is not found
3586 * returns:
3587 * 0 on error
3588 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003589 *
3590 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003591 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003592int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003593{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003594 struct server *s = NULL;
3595 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003596 void *serverip, *firstip;
3597 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003598 int ret;
3599 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003600 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003601
Christopher Faulet67957bd2017-09-27 11:00:59 +02003602 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003603 if (!s)
3604 return 1;
3605
Christopher Faulet67957bd2017-09-27 11:00:59 +02003606 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003607
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003608 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003609 firstip = NULL; /* pointer to the first valid response found */
3610 /* it will be used as the new IP if a change is required */
3611 firstip_sin_family = AF_UNSPEC;
3612 serverip = NULL; /* current server IP address */
3613
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003614 /* initializing server IP pointer */
3615 server_sin_family = s->addr.ss_family;
3616 switch (server_sin_family) {
3617 case AF_INET:
3618 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3619 break;
3620
3621 case AF_INET6:
3622 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3623 break;
3624
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003625 case AF_UNSPEC:
3626 break;
3627
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003628 default:
3629 goto invalid;
3630 }
3631
Baptiste Assmann729c9012017-05-22 15:13:10 +02003632 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003633 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003634 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003635
3636 switch (ret) {
3637 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003638 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003639
3640 case DNS_UPD_SRVIP_NOT_FOUND:
3641 goto save_ip;
3642
3643 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003644 goto invalid;
3645
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003646 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003647 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003648 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003649
Baptiste Assmannfad03182015-10-28 02:03:32 +01003650 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003651 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003652 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003653 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003654
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003655 default:
3656 goto invalid;
3657
3658 }
3659
3660 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003661 if (nameserver) {
3662 nameserver->counters.update++;
3663 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003664 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003665 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003666 else
3667 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003668 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3669
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003670 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003671 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003672 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003673
3674 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003675 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003676 nameserver->counters.invalid++;
3677 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003678 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003679 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003680 return 0;
3681}
3682
3683/*
3684 * Server Name Resolution error management callback
3685 * returns:
3686 * 0 on error
3687 * 1 when no error or safe ignore
3688 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003689int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003690{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003691 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003692
Christopher Faulet67957bd2017-09-27 11:00:59 +02003693 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003694 if (!s)
3695 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003696 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003697 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003698 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003699 return 1;
3700}
3701
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003702/*
3703 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003704 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003705 * It returns a pointer to the first server found or NULL if <ip> is not yet
3706 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003707 *
3708 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003709 */
3710struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3711{
3712 struct server *tmpsrv;
3713 struct proxy *be;
3714
3715 if (!srv)
3716 return NULL;
3717
3718 be = srv->proxy;
3719 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003720 /* we found the current server is the same, ignore it */
3721 if (srv == tmpsrv)
3722 continue;
3723
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003724 /* We want to compare the IP in the record with the IP of the servers in the
3725 * same backend, only if:
3726 * * DNS resolution is enabled on the server
3727 * * the hostname used for the resolution by our server is the same than the
3728 * one used for the server found in the backend
3729 * * the server found in the backend is not our current server
3730 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003731 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003732 if ((tmpsrv->hostname_dn == NULL) ||
3733 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3734 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003735 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003736 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003737 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003738 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003739
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003740 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003741 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003742 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003743 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003744 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003745
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003746 /* At this point, we have 2 different servers using the same DNS hostname
3747 * for their respective resolution.
3748 */
3749 if (*ip_family == tmpsrv->addr.ss_family &&
3750 ((tmpsrv->addr.ss_family == AF_INET &&
3751 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3752 (tmpsrv->addr.ss_family == AF_INET6 &&
3753 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003754 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003755 return tmpsrv;
3756 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003757 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003758 }
3759
Emeric Brune9fd6b52017-11-02 17:20:39 +01003760
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003761 return NULL;
3762}
3763
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003764/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3765 * resolver. This is suited for initial address configuration. Returns 0 on
3766 * success otherwise a non-zero error code. In case of error, *err_code, if
3767 * not NULL, is filled up.
3768 */
3769int srv_set_addr_via_libc(struct server *srv, int *err_code)
3770{
3771 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003772 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003773 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003774 return 1;
3775 }
3776 return 0;
3777}
3778
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003779/* Set the server's FDQN (->hostname) from <hostname>.
3780 * Returns -1 if failed, 0 if not.
3781 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003782int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003783{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003784 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003785 char *hostname_dn;
3786 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003787
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003788 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003789 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003790 /* run time DNS resolution was not active for this server
3791 * and we can't enable it at run time for now.
3792 */
3793 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003794 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003795
3796 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003797 hostname_len = strlen(hostname);
3798 hostname_dn = trash.str;
3799 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3800 hostname_dn, trash.size);
3801 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003802 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003803
Christopher Faulet67957bd2017-09-27 11:00:59 +02003804 resolution = srv->dns_requester->resolution;
3805 if (resolution &&
3806 resolution->hostname_dn &&
3807 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02003808 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003809
Christopher Faulet67957bd2017-09-27 11:00:59 +02003810 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003811
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003812 free(srv->hostname);
3813 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003814 srv->hostname = strdup(hostname);
3815 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003816 srv->hostname_dn_len = hostname_dn_len;
3817 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003818 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003819
Olivier Houchard55dcdf42017-11-06 15:15:04 +01003820 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003821 goto err;
3822
3823 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003824 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003825 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003826 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003827
3828 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003829 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003830 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003831 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003832}
3833
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003834/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3835 * from the state file. This is suited for initial address configuration.
3836 * Returns 0 on success otherwise a non-zero error code. In case of error,
3837 * *err_code, if not NULL, is filled up.
3838 */
3839static int srv_apply_lastaddr(struct server *srv, int *err_code)
3840{
3841 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3842 if (err_code)
3843 *err_code |= ERR_WARN;
3844 return 1;
3845 }
3846 return 0;
3847}
3848
Willy Tarreau25e51522016-11-04 15:10:17 +01003849/* returns 0 if no error, otherwise a combination of ERR_* flags */
3850static int srv_iterate_initaddr(struct server *srv)
3851{
3852 int return_code = 0;
3853 int err_code;
3854 unsigned int methods;
3855
3856 methods = srv->init_addr_methods;
3857 if (!methods) { // default to "last,libc"
3858 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3859 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3860 }
3861
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003862 /* "-dr" : always append "none" so that server addresses resolution
3863 * failures are silently ignored, this is convenient to validate some
3864 * configs out of their environment.
3865 */
3866 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3867 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3868
Willy Tarreau25e51522016-11-04 15:10:17 +01003869 while (methods) {
3870 err_code = 0;
3871 switch (srv_get_next_initaddr(&methods)) {
3872 case SRV_IADDR_LAST:
3873 if (!srv->lastaddr)
3874 continue;
3875 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003876 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003877 return_code |= err_code;
3878 break;
3879
3880 case SRV_IADDR_LIBC:
3881 if (!srv->hostname)
3882 continue;
3883 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003884 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003885 return_code |= err_code;
3886 break;
3887
Willy Tarreau37ebe122016-11-04 15:17:58 +01003888 case SRV_IADDR_NONE:
3889 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003890 if (return_code) {
3891 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3892 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3893 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003894 return return_code;
3895
Willy Tarreau4310d362016-11-02 15:05:56 +01003896 case SRV_IADDR_IP:
3897 ipcpy(&srv->init_addr, &srv->addr);
3898 if (return_code) {
3899 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3900 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3901 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003902 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003903
Willy Tarreau25e51522016-11-04 15:10:17 +01003904 default: /* unhandled method */
3905 break;
3906 }
3907 }
3908
3909 if (!return_code) {
3910 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3911 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3912 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003913 else {
3914 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3915 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3916 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003917
3918 return_code |= ERR_ALERT | ERR_FATAL;
3919 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003920out:
3921 srv_set_dyncookie(srv);
3922 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003923}
3924
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003925/*
3926 * This function parses all backends and all servers within each backend
3927 * and performs servers' addr resolution based on information provided by:
3928 * - configuration file
3929 * - server-state file (states provided by an 'old' haproxy process)
3930 *
3931 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3932 */
3933int srv_init_addr(void)
3934{
3935 struct proxy *curproxy;
3936 int return_code = 0;
3937
3938 curproxy = proxy;
3939 while (curproxy) {
3940 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003941
3942 /* servers are in backend only */
3943 if (!(curproxy->cap & PR_CAP_BE))
3944 goto srv_init_addr_next;
3945
Willy Tarreau25e51522016-11-04 15:10:17 +01003946 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003947 if (srv->hostname)
3948 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003949
3950 srv_init_addr_next:
3951 curproxy = curproxy->next;
3952 }
3953
3954 return return_code;
3955}
3956
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003957const 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 +02003958{
3959
3960 struct chunk *msg;
3961
3962 msg = get_trash_chunk();
3963 chunk_reset(msg);
3964
Olivier Houchard8da5f982017-08-04 18:35:36 +02003965 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003966 chunk_appendf(msg, "no need to change the FDQN");
3967 goto out;
3968 }
3969
3970 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
3971 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
3972 goto out;
3973 }
3974
3975 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
3976 server->proxy->id, server->id, server->hostname, fqdn);
3977
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003978 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003979 chunk_reset(msg);
3980 chunk_appendf(msg, "could not update %s/%s FQDN",
3981 server->proxy->id, server->id);
3982 goto out;
3983 }
3984
3985 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02003986 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003987
3988 out:
3989 if (updater)
3990 chunk_appendf(msg, " by '%s'", updater);
3991 chunk_appendf(msg, "\n");
3992
3993 return msg->str;
3994}
3995
3996
Willy Tarreau21b069d2016-11-23 17:15:08 +01003997/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3998 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003999 * 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 +01004000 * used for CLI commands requiring a server name.
4001 * Important: the <arg> is modified to remove the '/'.
4002 */
4003struct server *cli_find_server(struct appctx *appctx, char *arg)
4004{
4005 struct proxy *px;
4006 struct server *sv;
4007 char *line;
4008
4009 /* split "backend/server" and make <line> point to server */
4010 for (line = arg; *line; line++)
4011 if (*line == '/') {
4012 *line++ = '\0';
4013 break;
4014 }
4015
4016 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004017 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004018 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004019 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004020 return NULL;
4021 }
4022
4023 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004024 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004025 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004026 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004027 return NULL;
4028 }
4029
4030 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004031 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004032 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004033 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004034 return NULL;
4035 }
4036
4037 return sv;
4038}
4039
William Lallemand222baf22016-11-19 02:00:33 +01004040
4041static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4042{
4043 struct server *sv;
4044 const char *warning;
4045
4046 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4047 return 1;
4048
4049 sv = cli_find_server(appctx, args[2]);
4050 if (!sv)
4051 return 1;
4052
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004053 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004054
William Lallemand222baf22016-11-19 02:00:33 +01004055 if (strcmp(args[3], "weight") == 0) {
4056 warning = server_parse_weight_change_request(sv, args[4]);
4057 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004058 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004059 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004060 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004061 }
4062 }
4063 else if (strcmp(args[3], "state") == 0) {
4064 if (strcmp(args[4], "ready") == 0)
4065 srv_adm_set_ready(sv);
4066 else if (strcmp(args[4], "drain") == 0)
4067 srv_adm_set_drain(sv);
4068 else if (strcmp(args[4], "maint") == 0)
4069 srv_adm_set_maint(sv);
4070 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004071 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004072 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004073 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004074 }
4075 }
4076 else if (strcmp(args[3], "health") == 0) {
4077 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004078 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004079 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004080 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004081 }
4082 else if (strcmp(args[4], "up") == 0) {
4083 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004084 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004085 }
4086 else if (strcmp(args[4], "stopping") == 0) {
4087 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004088 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004089 }
4090 else if (strcmp(args[4], "down") == 0) {
4091 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004092 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004093 }
4094 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004095 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004096 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004097 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004098 }
4099 }
4100 else if (strcmp(args[3], "agent") == 0) {
4101 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004102 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004103 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004104 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004105 }
4106 else if (strcmp(args[4], "up") == 0) {
4107 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004108 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004109 }
4110 else if (strcmp(args[4], "down") == 0) {
4111 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004112 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004113 }
4114 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004115 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004116 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004117 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004118 }
4119 }
Misiek2da082d2017-01-09 09:40:42 +01004120 else if (strcmp(args[3], "agent-addr") == 0) {
4121 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004122 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004123 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4124 appctx->st0 = CLI_ST_PRINT;
4125 } else {
4126 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004127 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004128 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4129 appctx->st0 = CLI_ST_PRINT;
4130 }
4131 }
4132 }
4133 else if (strcmp(args[3], "agent-send") == 0) {
4134 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004135 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004136 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4137 appctx->st0 = CLI_ST_PRINT;
4138 } else {
4139 char *nss = strdup(args[4]);
4140 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004141 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004142 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4143 appctx->st0 = CLI_ST_PRINT;
4144 } else {
4145 free(sv->agent.send_string);
4146 sv->agent.send_string = nss;
4147 sv->agent.send_string_len = strlen(args[4]);
4148 }
4149 }
4150 }
William Lallemand222baf22016-11-19 02:00:33 +01004151 else if (strcmp(args[3], "check-port") == 0) {
4152 int i = 0;
4153 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004154 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004155 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004156 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004157 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004158 }
4159 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004160 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004161 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004162 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004163 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004164 }
4165 /* prevent the update of port to 0 if MAPPORTS are in use */
4166 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004167 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004168 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004169 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004170 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004171 }
4172 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004173 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004174 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004175 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004176 }
4177 else if (strcmp(args[3], "addr") == 0) {
4178 char *addr = NULL;
4179 char *port = NULL;
4180 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004181 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004182 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004183 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004184 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004185 }
4186 else {
4187 addr = args[4];
4188 }
4189 if (strcmp(args[5], "port") == 0) {
4190 port = args[6];
4191 }
4192 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4193 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004194 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004195 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004196 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004197 }
4198 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4199 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004200 else if (strcmp(args[3], "fqdn") == 0) {
4201 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004202 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004203 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4204 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004205 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004206 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004207 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004208 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004209 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004210 appctx->ctx.cli.msg = warning;
4211 appctx->st0 = CLI_ST_PRINT;
4212 }
4213 }
William Lallemand222baf22016-11-19 02:00:33 +01004214 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004215 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004216 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 +01004217 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004218 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004219 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004220 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004221 return 1;
4222}
4223
William Lallemand6b160942016-11-22 12:34:35 +01004224static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4225{
4226 struct stream_interface *si = appctx->owner;
4227 struct proxy *px;
4228 struct server *sv;
4229 char *line;
4230
4231
4232 /* split "backend/server" and make <line> point to server */
4233 for (line = args[2]; *line; line++)
4234 if (*line == '/') {
4235 *line++ = '\0';
4236 break;
4237 }
4238
4239 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004240 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004241 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004242 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004243 return 1;
4244 }
4245
4246 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004247 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004248 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004249 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004250 return 1;
4251 }
4252
4253 /* return server's effective weight at the moment */
4254 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004255 if (ci_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004256 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004257 return 0;
4258 }
William Lallemand6b160942016-11-22 12:34:35 +01004259 return 1;
4260}
4261
4262static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4263{
4264 struct server *sv;
4265 const char *warning;
4266
4267 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4268 return 1;
4269
4270 sv = cli_find_server(appctx, args[2]);
4271 if (!sv)
4272 return 1;
4273
4274 warning = server_parse_weight_change_request(sv, args[3]);
4275 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004276 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004277 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004278 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004279 }
4280 return 1;
4281}
4282
Willy Tarreaub8026272016-11-23 11:26:56 +01004283/* parse a "set maxconn server" command. It always returns 1. */
4284static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4285{
4286 struct server *sv;
4287 const char *warning;
4288
4289 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4290 return 1;
4291
4292 sv = cli_find_server(appctx, args[3]);
4293 if (!sv)
4294 return 1;
4295
4296 warning = server_parse_maxconn_change_request(sv, args[4]);
4297 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004298 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004299 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004300 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004301 }
4302 return 1;
4303}
William Lallemand6b160942016-11-22 12:34:35 +01004304
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004305/* parse a "disable agent" command. It always returns 1. */
4306static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4307{
4308 struct server *sv;
4309
4310 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4311 return 1;
4312
4313 sv = cli_find_server(appctx, args[2]);
4314 if (!sv)
4315 return 1;
4316
4317 sv->agent.state &= ~CHK_ST_ENABLED;
4318 return 1;
4319}
4320
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004321/* parse a "disable health" command. It always returns 1. */
4322static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4323{
4324 struct server *sv;
4325
4326 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4327 return 1;
4328
4329 sv = cli_find_server(appctx, args[2]);
4330 if (!sv)
4331 return 1;
4332
4333 sv->check.state &= ~CHK_ST_ENABLED;
4334 return 1;
4335}
4336
Willy Tarreauffb4d582016-11-24 12:47:00 +01004337/* parse a "disable server" command. It always returns 1. */
4338static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4339{
4340 struct server *sv;
4341
4342 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4343 return 1;
4344
4345 sv = cli_find_server(appctx, args[2]);
4346 if (!sv)
4347 return 1;
4348
4349 srv_adm_set_maint(sv);
4350 return 1;
4351}
4352
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004353/* parse a "enable agent" command. It always returns 1. */
4354static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4355{
4356 struct server *sv;
4357
4358 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4359 return 1;
4360
4361 sv = cli_find_server(appctx, args[2]);
4362 if (!sv)
4363 return 1;
4364
4365 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004366 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004367 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004368 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004369 return 1;
4370 }
4371
4372 sv->agent.state |= CHK_ST_ENABLED;
4373 return 1;
4374}
4375
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004376/* parse a "enable health" command. It always returns 1. */
4377static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4378{
4379 struct server *sv;
4380
4381 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4382 return 1;
4383
4384 sv = cli_find_server(appctx, args[2]);
4385 if (!sv)
4386 return 1;
4387
4388 sv->check.state |= CHK_ST_ENABLED;
4389 return 1;
4390}
4391
Willy Tarreauffb4d582016-11-24 12:47:00 +01004392/* parse a "enable server" command. It always returns 1. */
4393static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4394{
4395 struct server *sv;
4396
4397 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4398 return 1;
4399
4400 sv = cli_find_server(appctx, args[2]);
4401 if (!sv)
4402 return 1;
4403
4404 srv_adm_set_ready(sv);
4405 return 1;
4406}
4407
William Lallemand222baf22016-11-19 02:00:33 +01004408/* register cli keywords */
4409static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004410 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004411 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004412 { { "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 +01004413 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004414 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004415 { { "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 +01004416 { { "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 +01004417 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004418 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4419 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4420
William Lallemand222baf22016-11-19 02:00:33 +01004421 {{},}
4422}};
4423
4424__attribute__((constructor))
4425static void __server_init(void)
4426{
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004427 HA_SPIN_INIT(&updated_servers_lock);
William Lallemand222baf22016-11-19 02:00:33 +01004428 cli_register_kw(&cli_kws);
4429}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004430
Emeric Brun64cc49c2017-10-03 14:46:45 +02004431/*
4432 * This function applies server's status changes, it is
4433 * is designed to be called asynchronously.
4434 *
4435 */
4436void srv_update_status(struct server *s)
4437{
4438 struct check *check = &s->check;
4439 int xferred;
4440 struct proxy *px = s->proxy;
4441 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4442 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4443 int log_level;
4444 struct chunk *tmptrash = NULL;
4445
4446
4447 /* If currently main is not set we try to apply pending state changes */
4448 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4449 int next_admin;
4450
4451 /* Backup next admin */
4452 next_admin = s->next_admin;
4453
4454 /* restore current admin state */
4455 s->next_admin = s->cur_admin;
4456
4457 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4458 s->last_change = now.tv_sec;
4459 if (s->proxy->lbprm.set_server_status_down)
4460 s->proxy->lbprm.set_server_status_down(s);
4461
4462 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4463 srv_shutdown_streams(s, SF_ERR_DOWN);
4464
4465 /* we might have streams queued on this server and waiting for
4466 * a connection. Those which are redispatchable will be queued
4467 * to another server or to the proxy itself.
4468 */
4469 xferred = pendconn_redistribute(s);
4470
4471 tmptrash = alloc_trash_chunk();
4472 if (tmptrash) {
4473 chunk_printf(tmptrash,
4474 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4475 s->proxy->id, s->id);
4476
Emeric Brun5a133512017-10-19 14:42:30 +02004477 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004478 Warning("%s.\n", tmptrash->str);
4479
4480 /* we don't send an alert if the server was previously paused */
4481 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
4482 send_log(s->proxy, log_level, "%s.\n", tmptrash->str);
4483 send_email_alert(s, log_level, "%s", tmptrash->str);
4484 free_trash_chunk(tmptrash);
4485 tmptrash = NULL;
4486 }
4487 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4488 set_backend_down(s->proxy);
4489
4490 s->counters.down_trans++;
4491 }
4492 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4493 s->last_change = now.tv_sec;
4494 if (s->proxy->lbprm.set_server_status_down)
4495 s->proxy->lbprm.set_server_status_down(s);
4496
4497 /* we might have streams queued on this server and waiting for
4498 * a connection. Those which are redispatchable will be queued
4499 * to another server or to the proxy itself.
4500 */
4501 xferred = pendconn_redistribute(s);
4502
4503 tmptrash = alloc_trash_chunk();
4504 if (tmptrash) {
4505 chunk_printf(tmptrash,
4506 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4507 s->proxy->id, s->id);
4508
Emeric Brun5a133512017-10-19 14:42:30 +02004509 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004510
4511 Warning("%s.\n", tmptrash->str);
4512 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4513 free_trash_chunk(tmptrash);
4514 tmptrash = NULL;
4515 }
4516
4517 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4518 set_backend_down(s->proxy);
4519 }
4520 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4521 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4522 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4523 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4524 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4525 s->proxy->last_change = now.tv_sec;
4526 }
4527
4528 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4529 s->down_time += now.tv_sec - s->last_change;
4530
4531 s->last_change = now.tv_sec;
4532 if (s->next_state == SRV_ST_STARTING)
4533 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4534
4535 server_recalc_eweight(s);
4536 /* now propagate the status change to any LB algorithms */
4537 if (px->lbprm.update_server_eweight)
4538 px->lbprm.update_server_eweight(s);
4539 else if (srv_willbe_usable(s)) {
4540 if (px->lbprm.set_server_status_up)
4541 px->lbprm.set_server_status_up(s);
4542 }
4543 else {
4544 if (px->lbprm.set_server_status_down)
4545 px->lbprm.set_server_status_down(s);
4546 }
4547
4548 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4549 * and it's not a backup server and its effective weight is > 0,
4550 * then it can accept new connections, so we shut down all streams
4551 * on all backup servers.
4552 */
4553 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4554 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4555 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4556
4557 /* check if we can handle some connections queued at the proxy. We
4558 * will take as many as we can handle.
4559 */
4560 xferred = pendconn_grab_from_px(s);
4561
4562 tmptrash = alloc_trash_chunk();
4563 if (tmptrash) {
4564 chunk_printf(tmptrash,
4565 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4566 s->proxy->id, s->id);
4567
Emeric Brun5a133512017-10-19 14:42:30 +02004568 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004569 Warning("%s.\n", tmptrash->str);
4570 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4571 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4572 free_trash_chunk(tmptrash);
4573 tmptrash = NULL;
4574 }
4575
4576 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4577 set_backend_down(s->proxy);
4578 }
4579 else if (s->cur_eweight != s->next_eweight) {
4580 /* now propagate the status change to any LB algorithms */
4581 if (px->lbprm.update_server_eweight)
4582 px->lbprm.update_server_eweight(s);
4583 else if (srv_willbe_usable(s)) {
4584 if (px->lbprm.set_server_status_up)
4585 px->lbprm.set_server_status_up(s);
4586 }
4587 else {
4588 if (px->lbprm.set_server_status_down)
4589 px->lbprm.set_server_status_down(s);
4590 }
4591
4592 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4593 set_backend_down(s->proxy);
4594 }
4595
4596 s->next_admin = next_admin;
4597 }
4598
Emeric Brun5a133512017-10-19 14:42:30 +02004599 /* reset operational state change */
4600 *s->op_st_chg.reason = 0;
4601 s->op_st_chg.status = s->op_st_chg.code = -1;
4602 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004603
4604 /* Now we try to apply pending admin changes */
4605
4606 /* Maintenance must also disable health checks */
4607 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4608 if (s->check.state & CHK_ST_ENABLED) {
4609 s->check.state |= CHK_ST_PAUSED;
4610 check->health = 0;
4611 }
4612
4613 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004614 tmptrash = alloc_trash_chunk();
4615 if (tmptrash) {
4616 chunk_printf(tmptrash,
4617 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4618 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4619 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004620
Olivier Houchard796a2b32017-10-24 17:42:47 +02004621 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004622
Olivier Houchard796a2b32017-10-24 17:42:47 +02004623 if (!(global.mode & MODE_STARTING)) {
4624 Warning("%s.\n", tmptrash->str);
4625 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4626 }
4627 free_trash_chunk(tmptrash);
4628 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004629 }
4630 }
4631 else { /* server was still running */
4632 check->health = 0; /* failure */
4633 s->last_change = now.tv_sec;
4634 if (s->proxy->lbprm.set_server_status_down)
4635 s->proxy->lbprm.set_server_status_down(s);
4636
4637 s->next_state = SRV_ST_STOPPED;
4638 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4639 srv_shutdown_streams(s, SF_ERR_DOWN);
4640
4641 /* we might have streams queued on this server and waiting for
4642 * a connection. Those which are redispatchable will be queued
4643 * to another server or to the proxy itself.
4644 */
4645 xferred = pendconn_redistribute(s);
4646
4647 tmptrash = alloc_trash_chunk();
4648 if (tmptrash) {
4649 chunk_printf(tmptrash,
4650 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4651 s->flags & SRV_F_BACKUP ? "Backup " : "",
4652 s->proxy->id, s->id,
4653 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4654
4655 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4656
4657 if (!(global.mode & MODE_STARTING)) {
4658 Warning("%s.\n", tmptrash->str);
4659 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", tmptrash->str);
4660 }
4661 free_trash_chunk(tmptrash);
4662 tmptrash = NULL;
4663 }
4664 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4665 set_backend_down(s->proxy);
4666
4667 s->counters.down_trans++;
4668 }
4669 }
4670 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4671 /* OK here we're leaving maintenance, we have many things to check,
4672 * because the server might possibly be coming back up depending on
4673 * its state. In practice, leaving maintenance means that we should
4674 * immediately turn to UP (more or less the slowstart) under the
4675 * following conditions :
4676 * - server is neither checked nor tracked
4677 * - server tracks another server which is not checked
4678 * - server tracks another server which is already up
4679 * Which sums up as something simpler :
4680 * "either the tracking server is up or the server's checks are disabled
4681 * or up". Otherwise we only re-enable health checks. There's a special
4682 * case associated to the stopping state which can be inherited. Note
4683 * that the server might still be in drain mode, which is naturally dealt
4684 * with by the lower level functions.
4685 */
4686
4687 if (s->check.state & CHK_ST_ENABLED) {
4688 s->check.state &= ~CHK_ST_PAUSED;
4689 check->health = check->rise; /* start OK but check immediately */
4690 }
4691
4692 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4693 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4694 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4695 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4696 s->next_state = SRV_ST_STOPPING;
4697 }
4698 else {
4699 s->next_state = SRV_ST_STARTING;
4700 if (s->slowstart > 0)
4701 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4702 else
4703 s->next_state = SRV_ST_RUNNING;
4704 }
4705
4706 }
4707
4708 tmptrash = alloc_trash_chunk();
4709 if (tmptrash) {
4710 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4711 chunk_printf(tmptrash,
4712 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4713 s->flags & SRV_F_BACKUP ? "Backup " : "",
4714 s->proxy->id, s->id,
4715 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4716 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4717 }
4718 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4719 chunk_printf(tmptrash,
4720 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4721 s->flags & SRV_F_BACKUP ? "Backup " : "",
4722 s->proxy->id, s->id, s->hostname,
4723 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4724 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4725 }
4726 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4727 chunk_printf(tmptrash,
4728 "%sServer %s/%s is %s/%s (leaving maintenance)",
4729 s->flags & SRV_F_BACKUP ? "Backup " : "",
4730 s->proxy->id, s->id,
4731 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4732 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4733 }
4734 Warning("%s.\n", tmptrash->str);
4735 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4736 free_trash_chunk(tmptrash);
4737 tmptrash = NULL;
4738 }
4739
4740 server_recalc_eweight(s);
4741 /* now propagate the status change to any LB algorithms */
4742 if (px->lbprm.update_server_eweight)
4743 px->lbprm.update_server_eweight(s);
4744 else if (srv_willbe_usable(s)) {
4745 if (px->lbprm.set_server_status_up)
4746 px->lbprm.set_server_status_up(s);
4747 }
4748 else {
4749 if (px->lbprm.set_server_status_down)
4750 px->lbprm.set_server_status_down(s);
4751 }
4752
4753 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4754 set_backend_down(s->proxy);
4755
4756 }
4757 else if (s->next_admin & SRV_ADMF_MAINT) {
4758 /* remaining in maintenance mode, let's inform precisely about the
4759 * situation.
4760 */
4761 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4762 tmptrash = alloc_trash_chunk();
4763 if (tmptrash) {
4764 chunk_printf(tmptrash,
4765 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4766 s->flags & SRV_F_BACKUP ? "Backup " : "",
4767 s->proxy->id, s->id);
4768
4769 if (s->track) /* normally it's mandatory here */
4770 chunk_appendf(tmptrash, " via %s/%s",
4771 s->track->proxy->id, s->track->id);
4772 Warning("%s.\n", tmptrash->str);
4773 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4774 free_trash_chunk(tmptrash);
4775 tmptrash = NULL;
4776 }
4777 }
4778 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4779 tmptrash = alloc_trash_chunk();
4780 if (tmptrash) {
4781 chunk_printf(tmptrash,
4782 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4783 s->flags & SRV_F_BACKUP ? "Backup " : "",
4784 s->proxy->id, s->id, s->hostname);
4785
4786 if (s->track) /* normally it's mandatory here */
4787 chunk_appendf(tmptrash, " via %s/%s",
4788 s->track->proxy->id, s->track->id);
4789 Warning("%s.\n", tmptrash->str);
4790 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4791 free_trash_chunk(tmptrash);
4792 tmptrash = NULL;
4793 }
4794 }
4795 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4796 tmptrash = alloc_trash_chunk();
4797 if (tmptrash) {
4798 chunk_printf(tmptrash,
4799 "%sServer %s/%s remains in forced maintenance",
4800 s->flags & SRV_F_BACKUP ? "Backup " : "",
4801 s->proxy->id, s->id);
4802 Warning("%s.\n", tmptrash->str);
4803 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4804 free_trash_chunk(tmptrash);
4805 tmptrash = NULL;
4806 }
4807 }
4808 /* don't report anything when leaving drain mode and remaining in maintenance */
4809
4810 s->cur_admin = s->next_admin;
4811 }
4812
4813 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4814 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4815 /* drain state is applied only if not yet in maint */
4816
4817 s->last_change = now.tv_sec;
4818 if (px->lbprm.set_server_status_down)
4819 px->lbprm.set_server_status_down(s);
4820
4821 /* we might have streams queued on this server and waiting for
4822 * a connection. Those which are redispatchable will be queued
4823 * to another server or to the proxy itself.
4824 */
4825 xferred = pendconn_redistribute(s);
4826
4827 tmptrash = alloc_trash_chunk();
4828 if (tmptrash) {
4829 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4830 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4831 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4832
4833 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4834
4835 if (!(global.mode & MODE_STARTING)) {
4836 Warning("%s.\n", tmptrash->str);
4837 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4838 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4839 }
4840 free_trash_chunk(tmptrash);
4841 tmptrash = NULL;
4842 }
4843
4844 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4845 set_backend_down(s->proxy);
4846 }
4847 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4848 /* OK completely leaving drain mode */
4849 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4850 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4851 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4852 s->proxy->last_change = now.tv_sec;
4853 }
4854
4855 if (s->last_change < now.tv_sec) // ignore negative times
4856 s->down_time += now.tv_sec - s->last_change;
4857 s->last_change = now.tv_sec;
4858 server_recalc_eweight(s);
4859
4860 tmptrash = alloc_trash_chunk();
4861 if (tmptrash) {
4862 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4863 chunk_printf(tmptrash,
4864 "%sServer %s/%s is %s (leaving forced drain)",
4865 s->flags & SRV_F_BACKUP ? "Backup " : "",
4866 s->proxy->id, s->id,
4867 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4868 }
4869 else {
4870 chunk_printf(tmptrash,
4871 "%sServer %s/%s is %s (leaving drain)",
4872 s->flags & SRV_F_BACKUP ? "Backup " : "",
4873 s->proxy->id, s->id,
4874 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4875 if (s->track) /* normally it's mandatory here */
4876 chunk_appendf(tmptrash, " via %s/%s",
4877 s->track->proxy->id, s->track->id);
4878 }
4879
4880 Warning("%s.\n", tmptrash->str);
4881 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4882 free_trash_chunk(tmptrash);
4883 tmptrash = NULL;
4884 }
4885
4886 /* now propagate the status change to any LB algorithms */
4887 if (px->lbprm.update_server_eweight)
4888 px->lbprm.update_server_eweight(s);
4889 else if (srv_willbe_usable(s)) {
4890 if (px->lbprm.set_server_status_up)
4891 px->lbprm.set_server_status_up(s);
4892 }
4893 else {
4894 if (px->lbprm.set_server_status_down)
4895 px->lbprm.set_server_status_down(s);
4896 }
4897 }
4898 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
4899 /* remaining in drain mode after removing one of its flags */
4900
4901 tmptrash = alloc_trash_chunk();
4902 if (tmptrash) {
4903 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4904 chunk_printf(tmptrash,
4905 "%sServer %s/%s is leaving forced drain but remains in drain mode",
4906 s->flags & SRV_F_BACKUP ? "Backup " : "",
4907 s->proxy->id, s->id);
4908
4909 if (s->track) /* normally it's mandatory here */
4910 chunk_appendf(tmptrash, " via %s/%s",
4911 s->track->proxy->id, s->track->id);
4912 }
4913 else {
4914 chunk_printf(tmptrash,
4915 "%sServer %s/%s remains in forced drain mode",
4916 s->flags & SRV_F_BACKUP ? "Backup " : "",
4917 s->proxy->id, s->id);
4918 }
4919 Warning("%s.\n", tmptrash->str);
4920 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4921 free_trash_chunk(tmptrash);
4922 tmptrash = NULL;
4923 }
4924
4925 /* commit new admin status */
4926
4927 s->cur_admin = s->next_admin;
4928 }
4929 }
4930
4931 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02004932 *s->adm_st_chg_cause = 0;
4933}
4934/*
4935 * This function loops on servers registered for asynchronous
4936 * status changes
Christopher Faulet5d42e092017-10-16 12:00:40 +02004937 *
4938 * NOTE: No needs to lock <updated_servers> list because it is called inside the
4939 * sync point.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004940 */
4941void servers_update_status(void) {
4942 struct server *s, *stmp;
4943
4944 list_for_each_entry_safe(s, stmp, &updated_servers, update_status) {
4945 srv_update_status(s);
4946 LIST_DEL(&s->update_status);
4947 LIST_INIT(&s->update_status);
4948 }
4949}
4950
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004951/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004952 * Local variables:
4953 * c-indent-level: 8
4954 * c-basic-offset: 8
4955 * End:
4956 */