blob: c712b889cb807830f305b5b92074968b25b292bc [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) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100872 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200873 s->op_st_chg.code = check->code;
874 s->op_st_chg.status = check->status;
875 s->op_st_chg.duration = check->duration;
876 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200877
Christopher Faulet5d42e092017-10-16 12:00:40 +0200878 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200879 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100880 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200881 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100882 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200883 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200884}
885
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200886/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200887 * in maintenance. The server is registered in a list to postpone the counting
888 * of the remaining servers on the proxy and tries to grab requests from the
889 * proxy at a sync point. Maintenance servers are ignored. It stores the
890 * <reason> if non-null as the reason for going down or the available data
891 * from the check struct to recompute this reason later.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200892 */
Emeric Brun5a133512017-10-19 14:42:30 +0200893void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200894{
895 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200896
Emeric Brun64cc49c2017-10-03 14:46:45 +0200897 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200898 return;
899
Emeric Brun52a91d32017-08-31 14:41:55 +0200900 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200901 return;
902
Emeric Brun52a91d32017-08-31 14:41:55 +0200903 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200904 *s->op_st_chg.reason = 0;
905 s->op_st_chg.status = -1;
906 if (reason) {
907 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
908 }
909 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100910 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200911 s->op_st_chg.code = check->code;
912 s->op_st_chg.status = check->status;
913 s->op_st_chg.duration = check->duration;
914 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200915
Emeric Brun64cc49c2017-10-03 14:46:45 +0200916 if (s->slowstart <= 0)
917 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200918
Christopher Faulet5d42e092017-10-16 12:00:40 +0200919 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200920 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100921 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200922 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100923 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200924 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200925}
926
Willy Tarreau8eb77842014-05-21 13:54:57 +0200927/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +0200928 * isn't in maintenance. The server is registered in a list to postpone the
929 * counting of the remaining servers on the proxy and tries to grab requests
930 * from the proxy. Maintenance servers are ignored. It stores the
931 * <reason> if non-null as the reason for going down or the available data
932 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +0200933 * up. Note that it makes use of the trash to build the log strings, so <reason>
934 * must not be placed there.
935 */
Emeric Brun5a133512017-10-19 14:42:30 +0200936void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200937{
938 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200939
Emeric Brun64cc49c2017-10-03 14:46:45 +0200940 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200941 return;
942
Emeric Brun52a91d32017-08-31 14:41:55 +0200943 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200944 return;
945
Emeric Brun52a91d32017-08-31 14:41:55 +0200946 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +0200947 *s->op_st_chg.reason = 0;
948 s->op_st_chg.status = -1;
949 if (reason) {
950 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
951 }
952 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100953 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200954 s->op_st_chg.code = check->code;
955 s->op_st_chg.status = check->status;
956 s->op_st_chg.duration = check->duration;
957 }
Willy Tarreau8eb77842014-05-21 13:54:57 +0200958
Christopher Faulet5d42e092017-10-16 12:00:40 +0200959 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200960 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100961 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200962 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100963 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200964 }
Willy Tarreau8eb77842014-05-21 13:54:57 +0200965}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200966
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200967/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
968 * enforce either maint mode or drain mode. It is not allowed to set more than
969 * one flag at once. The equivalent "inherited" flag is propagated to all
970 * tracking servers. Maintenance mode disables health checks (but not agent
971 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100972 * is done. If <cause> is non-null, it will be displayed at the end of the log
973 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200974 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100975void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200976{
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200977 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200978
979 if (!mode)
980 return;
981
982 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +0200983 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200984 return;
985
Emeric Brun52a91d32017-08-31 14:41:55 +0200986 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +0200987 if (cause)
988 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
989
Christopher Faulet5d42e092017-10-16 12:00:40 +0200990 srv_register_update(s);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200991
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200992 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +0200993 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
994 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200995 return;
996
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200997 /* compute the inherited flag to propagate */
998 if (mode & SRV_ADMF_MAINT)
999 mode = SRV_ADMF_IMAINT;
1000 else if (mode & SRV_ADMF_DRAIN)
1001 mode = SRV_ADMF_IDRAIN;
1002
Emeric Brun9f0b4582017-10-23 14:39:51 +02001003 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001004 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001005 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001006 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001007 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001008}
1009
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001010/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1011 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1012 * than one flag at once. The equivalent "inherited" flag is propagated to all
1013 * tracking servers. Leaving maintenance mode re-enables health checks. When
1014 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001015 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001016void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001017{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001018 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001019
1020 if (!mode)
1021 return;
1022
1023 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001024 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001025 return;
1026
Emeric Brun52a91d32017-08-31 14:41:55 +02001027 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001028
Christopher Faulet5d42e092017-10-16 12:00:40 +02001029 srv_register_update(s);
1030
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001031 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001032 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1033 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001034 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001035
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001036 if (mode & SRV_ADMF_MAINT)
1037 mode = SRV_ADMF_IMAINT;
1038 else if (mode & SRV_ADMF_DRAIN)
1039 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001040
Emeric Brun9f0b4582017-10-23 14:39:51 +02001041 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001042 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001043 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001044 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001045 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001046}
1047
Willy Tarreau757478e2016-11-03 19:22:19 +01001048/* principle: propagate maint and drain to tracking servers. This is useful
1049 * upon startup so that inherited states are correct.
1050 */
1051static void srv_propagate_admin_state(struct server *srv)
1052{
1053 struct server *srv2;
1054
1055 if (!srv->trackers)
1056 return;
1057
1058 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001059 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001060 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001061 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001062
Emeric Brun52a91d32017-08-31 14:41:55 +02001063 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001064 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001065 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001066 }
1067}
1068
1069/* Compute and propagate the admin states for all servers in proxy <px>.
1070 * Only servers *not* tracking another one are considered, because other
1071 * ones will be handled when the server they track is visited.
1072 */
1073void srv_compute_all_admin_states(struct proxy *px)
1074{
1075 struct server *srv;
1076
1077 for (srv = px->srv; srv; srv = srv->next) {
1078 if (srv->track)
1079 continue;
1080 srv_propagate_admin_state(srv);
1081 }
1082}
1083
Willy Tarreaudff55432012-10-10 17:51:05 +02001084/* Note: must not be declared <const> as its list will be overwritten.
1085 * Please take care of keeping this list alphabetically sorted, doing so helps
1086 * all code contributors.
1087 * Optional keywords are also declared with a NULL ->parse() function so that
1088 * the config parser can report an appropriate error when a known keyword was
1089 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001090 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001091 */
1092static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001093 { "addr", srv_parse_addr, 1, 1 }, /* IP address to send health to or to probe from agent-check */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001094 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001095 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001096 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001097 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1 }, /* enable PROXY protocol for health checks */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001098 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001099 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1100 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001101 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001102 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001103 { "no-agent-check", srv_parse_no_agent_check, 0, 1 }, /* Do not enable any auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001104 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001105 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001106 { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1 }, /* disable PROXY protol for health checks */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001107 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1108 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001109 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001110 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Frédéric Lécaille16186232017-03-14 16:42:49 +01001111 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001112 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1113 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1 }, /* Enforce use of PROXY V2 protocol */
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001114 { "source", srv_parse_source, -1, 1 }, /* Set the source address to be used to connect to the server */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001115 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001116 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001117 { NULL, NULL, 0 },
1118}};
1119
1120__attribute__((constructor))
1121static void __listener_init(void)
1122{
1123 srv_register_keywords(&srv_kws);
1124}
1125
Willy Tarreau004e0452013-11-21 11:22:01 +01001126/* Recomputes the server's eweight based on its state, uweight, the current time,
1127 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1128 * state is automatically disabled if the time is elapsed.
1129 */
1130void server_recalc_eweight(struct server *sv)
1131{
1132 struct proxy *px = sv->proxy;
1133 unsigned w;
1134
1135 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1136 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001137 if (sv->next_state == SRV_ST_STARTING)
1138 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001139 }
1140
1141 /* We must take care of not pushing the server to full throttle during slow starts.
1142 * It must also start immediately, at least at the minimal step when leaving maintenance.
1143 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001144 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001145 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1146 else
1147 w = px->lbprm.wdiv;
1148
Emeric Brun52a91d32017-08-31 14:41:55 +02001149 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001150
Christopher Faulet5d42e092017-10-16 12:00:40 +02001151 srv_register_update(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001152}
1153
Willy Tarreaubaaee002006-06-26 02:48:02 +02001154/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001155 * Parses weight_str and configures sv accordingly.
1156 * Returns NULL on success, error message string otherwise.
1157 */
1158const char *server_parse_weight_change_request(struct server *sv,
1159 const char *weight_str)
1160{
1161 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001162 long int w;
1163 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001164
1165 px = sv->proxy;
1166
1167 /* if the weight is terminated with '%', it is set relative to
1168 * the initial weight, otherwise it is absolute.
1169 */
1170 if (!*weight_str)
1171 return "Require <weight> or <weight%>.\n";
1172
Simon Hormanb796afa2013-02-12 10:45:53 +09001173 w = strtol(weight_str, &end, 10);
1174 if (end == weight_str)
1175 return "Empty weight string empty or preceded by garbage";
1176 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001177 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001178 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001179 /* Avoid integer overflow */
1180 if (w > 25600)
1181 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001182 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001183 if (w > 256)
1184 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001185 }
1186 else if (w < 0 || w > 256)
1187 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001188 else if (end[0] != '\0')
1189 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001190
1191 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1192 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1193
1194 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001195 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001196
1197 return NULL;
1198}
1199
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001200/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001201 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1202 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001203 * Returns:
1204 * - error string on error
1205 * - NULL on success
1206 */
1207const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001208 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001209{
1210 unsigned char ip[INET6_ADDRSTRLEN];
1211
1212 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001213 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001214 return NULL;
1215 }
1216 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001217 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001218 return NULL;
1219 }
1220
1221 return "Could not understand IP address format.\n";
1222}
1223
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001224const char *server_parse_maxconn_change_request(struct server *sv,
1225 const char *maxconn_str)
1226{
1227 long int v;
1228 char *end;
1229
1230 if (!*maxconn_str)
1231 return "Require <maxconn>.\n";
1232
1233 v = strtol(maxconn_str, &end, 10);
1234 if (end == maxconn_str)
1235 return "maxconn string empty or preceded by garbage";
1236 else if (end[0] != '\0')
1237 return "Trailing garbage in maxconn string";
1238
1239 if (sv->maxconn == sv->minconn) { // static maxconn
1240 sv->maxconn = sv->minconn = v;
1241 } else { // dynamic maxconn
1242 sv->maxconn = v;
1243 }
1244
1245 if (may_dequeue_tasks(sv, sv->proxy))
1246 process_srv_queue(sv);
1247
1248 return NULL;
1249}
1250
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001251#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001252static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1253 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001254{
1255 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001256 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001257 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001258 NULL,
1259 };
1260
1261 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001262 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001263
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001264 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1265}
1266
1267static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1268{
1269 struct sample_expr *expr;
1270
1271 expr = srv_sni_sample_parse_expr(newsrv, px, px->conf.file, px->conf.line, err);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001272 if (!expr) {
1273 memprintf(err, "error detected while parsing sni expression : %s", *err);
1274 return ERR_ALERT | ERR_FATAL;
1275 }
1276
1277 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1278 memprintf(err, "error detected while parsing sni expression : "
1279 " fetch method '%s' extracts information from '%s', "
1280 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001281 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001282 return ERR_ALERT | ERR_FATAL;
1283 }
1284
1285 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1286 release_sample_expr(newsrv->ssl_ctx.sni);
1287 newsrv->ssl_ctx.sni = expr;
1288
1289 return 0;
1290}
1291#endif
1292
1293static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1294{
1295 if (err && *err) {
1296 indent_msg(err, 2);
1297 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1298 }
1299 else
1300 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1301 file, linenum, args[0], args[1], args[cur_arg]);
1302}
1303
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001304static void srv_conn_src_sport_range_cpy(struct server *srv,
1305 struct server *src)
1306{
1307 int range_sz;
1308
1309 range_sz = src->conn_src.sport_range->size;
1310 if (range_sz > 0) {
1311 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1312 if (srv->conn_src.sport_range != NULL) {
1313 int i;
1314
1315 for (i = 0; i < range_sz; i++) {
1316 srv->conn_src.sport_range->ports[i] =
1317 src->conn_src.sport_range->ports[i];
1318 }
1319 }
1320 }
1321}
1322
1323/*
1324 * Copy <src> server connection source settings to <srv> server everything needed.
1325 */
1326static void srv_conn_src_cpy(struct server *srv, struct server *src)
1327{
1328 srv->conn_src.opts = src->conn_src.opts;
1329 srv->conn_src.source_addr = src->conn_src.source_addr;
1330
1331 /* Source port range copy. */
1332 if (src->conn_src.sport_range != NULL)
1333 srv_conn_src_sport_range_cpy(srv, src);
1334
1335#ifdef CONFIG_HAP_TRANSPARENT
1336 if (src->conn_src.bind_hdr_name != NULL) {
1337 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1338 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1339 }
1340 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1341 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1342#endif
1343 if (src->conn_src.iface_name != NULL)
1344 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1345}
1346
1347/*
1348 * Copy <src> server SSL settings to <srv> server allocating
1349 * everything needed.
1350 */
1351#if defined(USE_OPENSSL)
1352static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1353{
1354 if (src->ssl_ctx.ca_file != NULL)
1355 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1356 if (src->ssl_ctx.crl_file != NULL)
1357 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1358 if (src->ssl_ctx.client_crt != NULL)
1359 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1360
1361 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1362
1363 if (src->ssl_ctx.verify_host != NULL)
1364 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1365 if (src->ssl_ctx.ciphers != NULL)
1366 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1367 if (src->sni_expr != NULL)
1368 srv->sni_expr = strdup(src->sni_expr);
1369}
1370#endif
1371
1372/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001373 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001374 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001375 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001376 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001377static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001378{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001379 char *hostname_dn;
1380 int hostname_len, hostname_dn_len;
1381
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001382 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001383 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001384
Christopher Faulet67957bd2017-09-27 11:00:59 +02001385 hostname_len = strlen(hostname);
1386 hostname_dn = trash.str;
1387 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1388 hostname_dn, trash.size);
1389 if (hostname_dn_len == -1)
1390 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001391
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001392
Christopher Faulet67957bd2017-09-27 11:00:59 +02001393 free(srv->hostname);
1394 free(srv->hostname_dn);
1395 srv->hostname = strdup(hostname);
1396 srv->hostname_dn = strdup(hostname_dn);
1397 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001398 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001399 goto err;
1400
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001401 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001402
1403 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001404 free(srv->hostname); srv->hostname = NULL;
1405 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001406 return -1;
1407}
1408
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001409/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001410 * Copy <src> server settings to <srv> server allocating
1411 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001412 * This function is not supposed to be called at any time, but only
1413 * during server settings parsing or during server allocations from
1414 * a server template, and just after having calloc()'ed a new server.
1415 * So, <src> may only be a default server (when parsing server settings)
1416 * or a server template (during server allocations from a server template).
1417 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1418 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001419 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001420static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001421{
1422 /* Connection source settings copy */
1423 srv_conn_src_cpy(srv, src);
1424
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001425 if (srv_tmpl) {
1426 srv->addr = src->addr;
1427 srv->svc_port = src->svc_port;
1428 }
1429
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001430 srv->pp_opts = src->pp_opts;
1431 if (src->rdr_pfx != NULL) {
1432 srv->rdr_pfx = strdup(src->rdr_pfx);
1433 srv->rdr_len = src->rdr_len;
1434 }
1435 if (src->cookie != NULL) {
1436 srv->cookie = strdup(src->cookie);
1437 srv->cklen = src->cklen;
1438 }
1439 srv->use_ssl = src->use_ssl;
1440 srv->check.addr = srv->agent.addr = src->check.addr;
1441 srv->check.use_ssl = src->check.use_ssl;
1442 srv->check.port = src->check.port;
1443 /* Note: 'flags' field has potentially been already initialized. */
1444 srv->flags |= src->flags;
1445 srv->do_check = src->do_check;
1446 srv->do_agent = src->do_agent;
1447 if (srv->check.port)
1448 srv->flags |= SRV_F_CHECKPORT;
1449 srv->check.inter = src->check.inter;
1450 srv->check.fastinter = src->check.fastinter;
1451 srv->check.downinter = src->check.downinter;
1452 srv->agent.use_ssl = src->agent.use_ssl;
1453 srv->agent.port = src->agent.port;
1454 if (src->agent.send_string != NULL)
1455 srv->agent.send_string = strdup(src->agent.send_string);
1456 srv->agent.send_string_len = src->agent.send_string_len;
1457 srv->agent.inter = src->agent.inter;
1458 srv->agent.fastinter = src->agent.fastinter;
1459 srv->agent.downinter = src->agent.downinter;
1460 srv->maxqueue = src->maxqueue;
1461 srv->minconn = src->minconn;
1462 srv->maxconn = src->maxconn;
1463 srv->slowstart = src->slowstart;
1464 srv->observe = src->observe;
1465 srv->onerror = src->onerror;
1466 srv->onmarkeddown = src->onmarkeddown;
1467 srv->onmarkedup = src->onmarkedup;
1468 if (src->trackit != NULL)
1469 srv->trackit = strdup(src->trackit);
1470 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1471 srv->uweight = srv->iweight = src->iweight;
1472
1473 srv->check.send_proxy = src->check.send_proxy;
1474 /* health: up, but will fall down at first failure */
1475 srv->check.rise = srv->check.health = src->check.rise;
1476 srv->check.fall = src->check.fall;
1477
1478 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001479 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1480 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1481 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001482 srv->check.state |= CHK_ST_PAUSED;
1483 srv->check.health = 0;
1484 }
1485
1486 /* health: up but will fall down at first failure */
1487 srv->agent.rise = srv->agent.health = src->agent.rise;
1488 srv->agent.fall = src->agent.fall;
1489
1490 if (src->resolvers_id != NULL)
1491 srv->resolvers_id = strdup(src->resolvers_id);
1492 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1493 if (srv->dns_opts.family_prio == AF_UNSPEC)
1494 srv->dns_opts.family_prio = AF_INET6;
1495 memcpy(srv->dns_opts.pref_net,
1496 src->dns_opts.pref_net,
1497 sizeof srv->dns_opts.pref_net);
1498 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1499
1500 srv->init_addr_methods = src->init_addr_methods;
1501 srv->init_addr = src->init_addr;
1502#if defined(USE_OPENSSL)
1503 srv_ssl_settings_cpy(srv, src);
1504#endif
1505#ifdef TCP_USER_TIMEOUT
1506 srv->tcp_ut = src->tcp_ut;
1507#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001508 if (srv_tmpl)
1509 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001510}
1511
1512static struct server *new_server(struct proxy *proxy)
1513{
1514 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001515 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001516
1517 srv = calloc(1, sizeof *srv);
1518 if (!srv)
1519 return NULL;
1520
1521 srv->obj_type = OBJ_TYPE_SERVER;
1522 srv->proxy = proxy;
1523 LIST_INIT(&srv->actconns);
1524 LIST_INIT(&srv->pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02001525
1526 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1527 goto free_srv;
1528 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1529 goto free_priv_conns;
1530 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1531 goto free_idle_conns;
1532
1533 for (i = 0; i < global.nbthread; i++) {
1534 LIST_INIT(&srv->priv_conns[i]);
1535 LIST_INIT(&srv->idle_conns[i]);
1536 LIST_INIT(&srv->safe_conns[i]);
1537 }
1538
Emeric Brun64cc49c2017-10-03 14:46:45 +02001539 LIST_INIT(&srv->update_status);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001540
Emeric Brun52a91d32017-08-31 14:41:55 +02001541 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001542 srv->last_change = now.tv_sec;
1543
1544 srv->check.status = HCHK_STATUS_INI;
1545 srv->check.server = srv;
1546 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1547
1548 srv->agent.status = HCHK_STATUS_INI;
1549 srv->agent.server = srv;
1550 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1551
1552 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001553
1554 free_idle_conns:
1555 free(srv->idle_conns);
1556 free_priv_conns:
1557 free(srv->priv_conns);
1558 free_srv:
1559 free(srv);
1560 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001561}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001562
1563/*
1564 * Validate <srv> server health-check settings.
1565 * Returns 0 if everything is OK, -1 if not.
1566 */
1567static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1568{
1569 struct tcpcheck_rule *r = NULL;
1570 struct list *l;
1571
1572 /*
1573 * We need at least a service port, a check port or the first tcp-check rule must
1574 * be a 'connect' one when checking an IPv4/IPv6 server.
1575 */
1576 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1577 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1578 return 0;
1579
1580 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1581 if (!r) {
1582 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1583 "Check has been disabled.\n",
1584 file, linenum, srv->id);
1585 return -1;
1586 }
1587
1588 /* search the first action (connect / send / expect) in the list */
1589 l = &srv->proxy->tcpcheck_rules;
1590 list_for_each_entry(r, l, list) {
1591 if (r->action != TCPCHK_ACT_COMMENT)
1592 break;
1593 }
1594
1595 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1596 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1597 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1598 file, linenum, srv->id);
1599 return -1;
1600 }
1601
1602 /* scan the tcp-check ruleset to ensure a port has been configured */
1603 l = &srv->proxy->tcpcheck_rules;
1604 list_for_each_entry(r, l, list) {
1605 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1606 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1607 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1608 file, linenum, srv->id);
1609 return -1;
1610 }
1611 }
1612
1613 return 0;
1614}
1615
1616/*
1617 * Initialize <srv> health-check structure.
1618 * Returns the error string in case of memory allocation failure, NULL if not.
1619 */
1620static const char *do_health_check_init(struct server *srv, int check_type, int state)
1621{
1622 const char *ret;
1623
1624 if (!srv->do_check)
1625 return NULL;
1626
1627 ret = init_check(&srv->check, check_type);
1628 if (ret)
1629 return ret;
1630
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001631 srv->check.state |= state;
1632 global.maxsock++;
1633
1634 return NULL;
1635}
1636
1637static int server_health_check_init(const char *file, int linenum,
1638 struct server *srv, struct proxy *curproxy)
1639{
1640 const char *ret;
1641
1642 if (!srv->do_check)
1643 return 0;
1644
1645 if (srv->trackit) {
1646 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1647 file, linenum);
1648 return ERR_ALERT | ERR_FATAL;
1649 }
1650
1651 if (server_healthcheck_validate(file, linenum, srv) < 0)
1652 return ERR_ALERT | ERR_ABORT;
1653
1654 /* note: check type will be set during the config review phase */
1655 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1656 if (ret) {
1657 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1658 return ERR_ALERT | ERR_ABORT;
1659 }
1660
1661 return 0;
1662}
1663
1664/*
1665 * Initialize <srv> agent check structure.
1666 * Returns the error string in case of memory allocation failure, NULL if not.
1667 */
1668static const char *do_server_agent_check_init(struct server *srv, int state)
1669{
1670 const char *ret;
1671
1672 if (!srv->do_agent)
1673 return NULL;
1674
1675 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1676 if (ret)
1677 return ret;
1678
1679 if (!srv->agent.inter)
1680 srv->agent.inter = srv->check.inter;
1681
1682 srv->agent.state |= state;
1683 global.maxsock++;
1684
1685 return NULL;
1686}
1687
1688static int server_agent_check_init(const char *file, int linenum,
1689 struct server *srv, struct proxy *curproxy)
1690{
1691 const char *ret;
1692
1693 if (!srv->do_agent)
1694 return 0;
1695
1696 if (!srv->agent.port) {
1697 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1698 file, linenum, srv->id);
1699 return ERR_ALERT | ERR_FATAL;
1700 }
1701
1702 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1703 if (ret) {
1704 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1705 return ERR_ALERT | ERR_ABORT;
1706 }
1707
1708 return 0;
1709}
1710
1711#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1712static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1713 struct server *srv, struct proxy *proxy)
1714{
1715 int ret;
1716 char *err = NULL;
1717
1718 if (!srv->sni_expr)
1719 return 0;
1720
1721 ret = server_parse_sni_expr(srv, proxy, &err);
1722 if (!ret)
1723 return 0;
1724
1725 display_parser_err(file, linenum, args, cur_arg, &err);
1726 free(err);
1727
1728 return ret;
1729}
1730#endif
1731
1732/*
1733 * Server initializations finalization.
1734 * Initialize health check, agent check and SNI expression if enabled.
1735 * Must not be called for a default server instance.
1736 */
1737static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1738 struct server *srv, struct proxy *px)
1739{
1740 int ret;
1741
1742 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1743 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1744 return ret;
1745 }
1746
1747#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1748 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1749 return ret;
1750#endif
1751
1752 if (srv->flags & SRV_F_BACKUP)
1753 px->srv_bck++;
1754 else
1755 px->srv_act++;
1756 srv_lb_commit_status(srv);
1757
1758 return 0;
1759}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001760
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001761/*
1762 * Parse as much as possible such a range string argument: low[-high]
1763 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1764 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1765 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1766 * Returns 0 if succeeded, -1 if not.
1767 */
1768static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1769{
1770 char *nb_high_arg;
1771
1772 *nb_high = 0;
1773 chunk_printf(&trash, "%s", arg);
1774 *nb_low = atoi(trash.str);
1775
1776 if ((nb_high_arg = strchr(trash.str, '-'))) {
1777 *nb_high_arg++ = '\0';
1778 *nb_high = atoi(nb_high_arg);
1779 }
1780 else {
1781 *nb_high += *nb_low;
1782 *nb_low = 1;
1783 }
1784
1785 if (*nb_low < 0 || *nb_high < *nb_low)
1786 return -1;
1787
1788 return 0;
1789}
1790
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001791static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1792{
1793 chunk_printf(&trash, "%s%d", prefix, nb);
1794 free(srv->id);
1795 srv->id = strdup(trash.str);
1796}
1797
1798/*
1799 * Initialize as much as possible servers from <srv> server template.
1800 * Note that a server template is a special server with
1801 * a few different parameters than a server which has
1802 * been parsed mostly the same way as a server.
1803 * Returns the number of servers succesfully allocated,
1804 * 'srv' template included.
1805 */
1806static int server_template_init(struct server *srv, struct proxy *px)
1807{
1808 int i;
1809 struct server *newsrv;
1810
1811 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1812 int check_init_state;
1813 int agent_init_state;
1814
1815 newsrv = new_server(px);
1816 if (!newsrv)
1817 goto err;
1818
1819 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001820 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001821#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1822 if (newsrv->sni_expr) {
1823 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1824 if (!newsrv->ssl_ctx.sni)
1825 goto err;
1826 }
1827#endif
1828 /* Set this new server ID. */
1829 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1830
1831 /* Initial checks states. */
1832 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1833 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1834
1835 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1836 do_server_agent_check_init(newsrv, agent_init_state))
1837 goto err;
1838
1839 /* Linked backwards first. This will be restablished after parsing. */
1840 newsrv->next = px->srv;
1841 px->srv = newsrv;
1842 }
1843 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1844
1845 return i - srv->tmpl_info.nb_low;
1846
1847 err:
1848 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1849 if (newsrv) {
1850#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1851 release_sample_expr(newsrv->ssl_ctx.sni);
1852#endif
1853 free_check(&newsrv->agent);
1854 free_check(&newsrv->check);
1855 }
1856 free(newsrv);
1857 return i - srv->tmpl_info.nb_low;
1858}
1859
Willy Tarreau272adea2014-03-31 10:39:59 +02001860int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1861{
1862 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001863 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001864 char *errmsg = NULL;
1865 int err_code = 0;
1866 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001867 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001868
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001869 if (!strcmp(args[0], "server") ||
1870 !strcmp(args[0], "default-server") ||
1871 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001872 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001873 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001874 int srv = !defsrv && !strcmp(args[0], "server");
1875 int srv_tmpl = !defsrv && !srv;
1876 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001877
1878 if (!defsrv && curproxy == defproxy) {
1879 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1880 err_code |= ERR_ALERT | ERR_FATAL;
1881 goto out;
1882 }
1883 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1884 err_code |= ERR_ALERT | ERR_FATAL;
1885
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001886 /* There is no mandatory first arguments for default server. */
1887 if (srv) {
1888 if (!*args[2]) {
1889 /* 'server' line number of argument check. */
1890 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1891 file, linenum, args[0]);
1892 err_code |= ERR_ALERT | ERR_FATAL;
1893 goto out;
1894 }
1895
1896 err = invalid_char(args[1]);
1897 }
1898 else if (srv_tmpl) {
1899 if (!*args[3]) {
1900 /* 'server-template' line number of argument check. */
1901 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
1902 file, linenum, args[0]);
1903 err_code |= ERR_ALERT | ERR_FATAL;
1904 goto out;
1905 }
1906
1907 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001908 }
1909
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001910 if (err) {
1911 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
1912 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001913 err_code |= ERR_ALERT | ERR_FATAL;
1914 goto out;
1915 }
1916
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001917 cur_arg = 2;
1918 if (srv_tmpl) {
1919 /* Parse server-template <nb | range> arg. */
1920 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
1921 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
1922 file, linenum, args[0], args[cur_arg]);
1923 err_code |= ERR_ALERT | ERR_FATAL;
1924 goto out;
1925 }
1926 cur_arg++;
1927 }
1928
Willy Tarreau272adea2014-03-31 10:39:59 +02001929 if (!defsrv) {
1930 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001931 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001932 struct protocol *proto;
1933
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001934 newsrv = new_server(curproxy);
1935 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001936 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1937 err_code |= ERR_ALERT | ERR_ABORT;
1938 goto out;
1939 }
1940
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001941 if (srv_tmpl) {
1942 newsrv->tmpl_info.nb_low = tmpl_range_low;
1943 newsrv->tmpl_info.nb_high = tmpl_range_high;
1944 }
1945
Willy Tarreau272adea2014-03-31 10:39:59 +02001946 /* the servers are linked backwards first */
1947 newsrv->next = curproxy->srv;
1948 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02001949 newsrv->conf.file = strdup(file);
1950 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001951 /* Note: for a server template, its id is its prefix.
1952 * This is a temporary id which will be used for server allocations to come
1953 * after parsing.
1954 */
1955 if (srv)
1956 newsrv->id = strdup(args[1]);
1957 else
1958 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001959
1960 /* several ways to check the port component :
1961 * - IP => port=+0, relative (IPv4 only)
1962 * - IP: => port=+0, relative
1963 * - IP:N => port=N, absolute
1964 * - IP:+N => port=+N, relative
1965 * - IP:-N => port=-N, relative
1966 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001967 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001968 if (!sk) {
1969 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1970 err_code |= ERR_ALERT | ERR_FATAL;
1971 goto out;
1972 }
1973
1974 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001975 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001976 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1977 file, linenum, args[0], args[1]);
1978 err_code |= ERR_ALERT | ERR_FATAL;
1979 goto out;
1980 }
1981
1982 if (!port1 || !port2) {
1983 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001984 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001985 }
1986 else if (port1 != port2) {
1987 /* port range */
1988 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1989 file, linenum, args[0], args[1], args[2]);
1990 err_code |= ERR_ALERT | ERR_FATAL;
1991 goto out;
1992 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001993
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001994 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02001995 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001996 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02001997 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001998 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
1999 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2000 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002001 err_code |= ERR_ALERT | ERR_FATAL;
2002 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002003 }
2004 }
2005 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
2006 Alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
2007 file, linenum, newsrv->id);
2008 err_code |= ERR_ALERT | ERR_FATAL;
2009 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002010 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002011 }
2012
Willy Tarreau272adea2014-03-31 10:39:59 +02002013 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002014 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002015
Olivier Houchard8da5f982017-08-04 18:35:36 +02002016 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002017 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002018 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002019 err_code |= ERR_ALERT | ERR_FATAL;
2020 goto out;
2021 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002022
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002023 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002024 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002025 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002026 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002027 } else {
2028 newsrv = &curproxy->defsrv;
2029 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002030 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002031 }
2032
2033 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002034 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002035 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2036 if (err) {
2037 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2038 file, linenum, *err, newsrv->id);
2039 err_code |= ERR_ALERT | ERR_FATAL;
2040 goto out;
2041 }
2042 if (val <= 0) {
2043 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2044 file, linenum, val, args[cur_arg], newsrv->id);
2045 err_code |= ERR_ALERT | ERR_FATAL;
2046 goto out;
2047 }
2048 newsrv->agent.inter = val;
2049 cur_arg += 2;
2050 }
Misiekea849332017-01-09 09:39:51 +01002051 else if (!strcmp(args[cur_arg], "agent-addr")) {
2052 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2053 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2054 goto out;
2055 }
2056
2057 cur_arg += 2;
2058 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002059 else if (!strcmp(args[cur_arg], "agent-port")) {
2060 global.maxsock++;
2061 newsrv->agent.port = atol(args[cur_arg + 1]);
2062 cur_arg += 2;
2063 }
James Brown55f9ff12015-10-21 18:19:05 -07002064 else if (!strcmp(args[cur_arg], "agent-send")) {
2065 global.maxsock++;
2066 free(newsrv->agent.send_string);
2067 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2068 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2069 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2070 cur_arg += 2;
2071 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002072 else if (!strcmp(args[cur_arg], "init-addr")) {
2073 char *p, *end;
2074 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002075 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002076
2077 newsrv->init_addr_methods = 0;
2078 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2079
2080 for (p = args[cur_arg + 1]; *p; p = end) {
2081 /* cut on next comma */
2082 for (end = p; *end && *end != ','; end++);
2083 if (*end)
2084 *(end++) = 0;
2085
Willy Tarreau4310d362016-11-02 15:05:56 +01002086 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002087 if (!strcmp(p, "libc")) {
2088 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2089 }
2090 else if (!strcmp(p, "last")) {
2091 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2092 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002093 else if (!strcmp(p, "none")) {
2094 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2095 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002096 else if (str2ip2(p, &sa, 0)) {
2097 if (is_addr(&newsrv->init_addr)) {
2098 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2099 file, linenum, args[cur_arg], p);
2100 err_code |= ERR_ALERT | ERR_FATAL;
2101 goto out;
2102 }
2103 newsrv->init_addr = sa;
2104 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2105 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002106 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002107 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002108 file, linenum, args[cur_arg], p);
2109 err_code |= ERR_ALERT | ERR_FATAL;
2110 goto out;
2111 }
2112 if (!done) {
2113 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2114 file, linenum, args[cur_arg], p);
2115 err_code |= ERR_ALERT | ERR_FATAL;
2116 goto out;
2117 }
2118 }
2119 cur_arg += 2;
2120 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002121 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002122 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002123 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2124 cur_arg += 2;
2125 }
2126 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2127 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002128 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002129 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002130 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002131 else {
2132 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2133 file, linenum, args[cur_arg]);
2134 err_code |= ERR_ALERT | ERR_FATAL;
2135 goto out;
2136 }
2137 cur_arg += 2;
2138 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002139 else if (!strcmp(args[cur_arg], "resolve-net")) {
2140 char *p, *e;
2141 unsigned char mask;
2142 struct dns_options *opt;
2143
2144 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
2145 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2146 file, linenum, args[cur_arg]);
2147 err_code |= ERR_ALERT | ERR_FATAL;
2148 goto out;
2149 }
2150
2151 opt = &newsrv->dns_opts;
2152
2153 /* Split arguments by comma, and convert it from ipv4 or ipv6
2154 * string network in in_addr or in6_addr.
2155 */
2156 p = args[cur_arg + 1];
2157 e = p;
2158 while (*p != '\0') {
2159 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002160 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002161 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2162 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2163 err_code |= ERR_ALERT | ERR_FATAL;
2164 goto out;
2165 }
2166 /* look for end or comma. */
2167 while (*e != ',' && *e != '\0')
2168 e++;
2169 if (*e == ',') {
2170 *e = '\0';
2171 e++;
2172 }
2173 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2174 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2175 /* Try to convert input string from ipv4 or ipv6 network. */
2176 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2177 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2178 &mask)) {
2179 /* Try to convert input string from ipv6 network. */
2180 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2181 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2182 } else {
2183 /* All network conversions fail, retrun error. */
2184 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2185 file, linenum, args[cur_arg], p);
2186 err_code |= ERR_ALERT | ERR_FATAL;
2187 goto out;
2188 }
2189 opt->pref_net_nb++;
2190 p = e;
2191 }
2192
2193 cur_arg += 2;
2194 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002195 else if (!strcmp(args[cur_arg], "rise")) {
2196 if (!*args[cur_arg + 1]) {
2197 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2198 file, linenum, args[cur_arg]);
2199 err_code |= ERR_ALERT | ERR_FATAL;
2200 goto out;
2201 }
2202
2203 newsrv->check.rise = atol(args[cur_arg + 1]);
2204 if (newsrv->check.rise <= 0) {
2205 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2206 file, linenum, args[cur_arg]);
2207 err_code |= ERR_ALERT | ERR_FATAL;
2208 goto out;
2209 }
2210
2211 if (newsrv->check.health)
2212 newsrv->check.health = newsrv->check.rise;
2213 cur_arg += 2;
2214 }
2215 else if (!strcmp(args[cur_arg], "fall")) {
2216 newsrv->check.fall = atol(args[cur_arg + 1]);
2217
2218 if (!*args[cur_arg + 1]) {
2219 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2220 file, linenum, args[cur_arg]);
2221 err_code |= ERR_ALERT | ERR_FATAL;
2222 goto out;
2223 }
2224
2225 if (newsrv->check.fall <= 0) {
2226 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2227 file, linenum, args[cur_arg]);
2228 err_code |= ERR_ALERT | ERR_FATAL;
2229 goto out;
2230 }
2231
2232 cur_arg += 2;
2233 }
2234 else if (!strcmp(args[cur_arg], "inter")) {
2235 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2236 if (err) {
2237 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2238 file, linenum, *err, newsrv->id);
2239 err_code |= ERR_ALERT | ERR_FATAL;
2240 goto out;
2241 }
2242 if (val <= 0) {
2243 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2244 file, linenum, val, args[cur_arg], newsrv->id);
2245 err_code |= ERR_ALERT | ERR_FATAL;
2246 goto out;
2247 }
2248 newsrv->check.inter = val;
2249 cur_arg += 2;
2250 }
2251 else if (!strcmp(args[cur_arg], "fastinter")) {
2252 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2253 if (err) {
2254 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2255 file, linenum, *err, newsrv->id);
2256 err_code |= ERR_ALERT | ERR_FATAL;
2257 goto out;
2258 }
2259 if (val <= 0) {
2260 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2261 file, linenum, val, args[cur_arg], newsrv->id);
2262 err_code |= ERR_ALERT | ERR_FATAL;
2263 goto out;
2264 }
2265 newsrv->check.fastinter = val;
2266 cur_arg += 2;
2267 }
2268 else if (!strcmp(args[cur_arg], "downinter")) {
2269 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2270 if (err) {
2271 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2272 file, linenum, *err, newsrv->id);
2273 err_code |= ERR_ALERT | ERR_FATAL;
2274 goto out;
2275 }
2276 if (val <= 0) {
2277 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2278 file, linenum, val, args[cur_arg], newsrv->id);
2279 err_code |= ERR_ALERT | ERR_FATAL;
2280 goto out;
2281 }
2282 newsrv->check.downinter = val;
2283 cur_arg += 2;
2284 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002285 else if (!strcmp(args[cur_arg], "port")) {
2286 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002287 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002288 cur_arg += 2;
2289 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002290 else if (!strcmp(args[cur_arg], "weight")) {
2291 int w;
2292 w = atol(args[cur_arg + 1]);
2293 if (w < 0 || w > SRV_UWGHT_MAX) {
2294 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2295 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2296 err_code |= ERR_ALERT | ERR_FATAL;
2297 goto out;
2298 }
2299 newsrv->uweight = newsrv->iweight = w;
2300 cur_arg += 2;
2301 }
2302 else if (!strcmp(args[cur_arg], "minconn")) {
2303 newsrv->minconn = atol(args[cur_arg + 1]);
2304 cur_arg += 2;
2305 }
2306 else if (!strcmp(args[cur_arg], "maxconn")) {
2307 newsrv->maxconn = atol(args[cur_arg + 1]);
2308 cur_arg += 2;
2309 }
2310 else if (!strcmp(args[cur_arg], "maxqueue")) {
2311 newsrv->maxqueue = atol(args[cur_arg + 1]);
2312 cur_arg += 2;
2313 }
2314 else if (!strcmp(args[cur_arg], "slowstart")) {
2315 /* slowstart is stored in seconds */
2316 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2317 if (err) {
2318 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2319 file, linenum, *err, newsrv->id);
2320 err_code |= ERR_ALERT | ERR_FATAL;
2321 goto out;
2322 }
2323 newsrv->slowstart = (val + 999) / 1000;
2324 cur_arg += 2;
2325 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002326 else if (!strcmp(args[cur_arg], "on-error")) {
2327 if (!strcmp(args[cur_arg + 1], "fastinter"))
2328 newsrv->onerror = HANA_ONERR_FASTINTER;
2329 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2330 newsrv->onerror = HANA_ONERR_FAILCHK;
2331 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2332 newsrv->onerror = HANA_ONERR_SUDDTH;
2333 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2334 newsrv->onerror = HANA_ONERR_MARKDWN;
2335 else {
2336 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2337 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2338 file, linenum, args[cur_arg], args[cur_arg + 1]);
2339 err_code |= ERR_ALERT | ERR_FATAL;
2340 goto out;
2341 }
2342
2343 cur_arg += 2;
2344 }
2345 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2346 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2347 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2348 else {
2349 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2350 file, linenum, args[cur_arg], args[cur_arg + 1]);
2351 err_code |= ERR_ALERT | ERR_FATAL;
2352 goto out;
2353 }
2354
2355 cur_arg += 2;
2356 }
2357 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2358 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2359 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2360 else {
2361 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2362 file, linenum, args[cur_arg], args[cur_arg + 1]);
2363 err_code |= ERR_ALERT | ERR_FATAL;
2364 goto out;
2365 }
2366
2367 cur_arg += 2;
2368 }
2369 else if (!strcmp(args[cur_arg], "error-limit")) {
2370 if (!*args[cur_arg + 1]) {
2371 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2372 file, linenum, args[cur_arg]);
2373 err_code |= ERR_ALERT | ERR_FATAL;
2374 goto out;
2375 }
2376
2377 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2378
2379 if (newsrv->consecutive_errors_limit <= 0) {
2380 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2381 file, linenum, args[cur_arg]);
2382 err_code |= ERR_ALERT | ERR_FATAL;
2383 goto out;
2384 }
2385 cur_arg += 2;
2386 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002387 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002388 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2389 file, linenum, "usesrc", "source");
2390 err_code |= ERR_ALERT | ERR_FATAL;
2391 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002392 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002393 else {
2394 static int srv_dumped;
2395 struct srv_kw *kw;
2396 char *err;
2397
2398 kw = srv_find_kw(args[cur_arg]);
2399 if (kw) {
2400 char *err = NULL;
2401 int code;
2402
2403 if (!kw->parse) {
2404 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2405 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002406 if (kw->skip != -1)
2407 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002408 err_code |= ERR_ALERT | ERR_FATAL;
2409 goto out;
2410 }
2411
2412 if (defsrv && !kw->default_ok) {
2413 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2414 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002415 if (kw->skip != -1)
2416 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002417 err_code |= ERR_ALERT;
2418 continue;
2419 }
2420
2421 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2422 err_code |= code;
2423
2424 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002425 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002426 if (code & ERR_FATAL) {
2427 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002428 if (kw->skip != -1)
2429 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002430 goto out;
2431 }
2432 }
2433 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002434 if (kw->skip != -1)
2435 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002436 continue;
2437 }
2438
2439 err = NULL;
2440 if (!srv_dumped) {
2441 srv_dump_kws(&err);
2442 indent_msg(&err, 4);
2443 srv_dumped = 1;
2444 }
2445
2446 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2447 file, linenum, args[0], args[1], args[cur_arg],
2448 err ? " Registered keywords :" : "", err ? err : "");
2449 free(err);
2450
2451 err_code |= ERR_ALERT | ERR_FATAL;
2452 goto out;
2453 }
2454 }
2455
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002456 if (!defsrv)
2457 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2458 if (err_code & ERR_FATAL)
2459 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002460 if (srv_tmpl)
2461 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002462 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002463 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002464 return 0;
2465
2466 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002467 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002468 free(errmsg);
2469 return err_code;
2470}
2471
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002472/* Returns a pointer to the first server matching either id <id>.
2473 * NULL is returned if no match is found.
2474 * the lookup is performed in the backend <bk>
2475 */
2476struct server *server_find_by_id(struct proxy *bk, int id)
2477{
2478 struct eb32_node *eb32;
2479 struct server *curserver;
2480
2481 if (!bk || (id ==0))
2482 return NULL;
2483
2484 /* <bk> has no backend capabilities, so it can't have a server */
2485 if (!(bk->cap & PR_CAP_BE))
2486 return NULL;
2487
2488 curserver = NULL;
2489
2490 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2491 if (eb32)
2492 curserver = container_of(eb32, struct server, conf.id);
2493
2494 return curserver;
2495}
2496
2497/* Returns a pointer to the first server matching either name <name>, or id
2498 * if <name> starts with a '#'. NULL is returned if no match is found.
2499 * the lookup is performed in the backend <bk>
2500 */
2501struct server *server_find_by_name(struct proxy *bk, const char *name)
2502{
2503 struct server *curserver;
2504
2505 if (!bk || !name)
2506 return NULL;
2507
2508 /* <bk> has no backend capabilities, so it can't have a server */
2509 if (!(bk->cap & PR_CAP_BE))
2510 return NULL;
2511
2512 curserver = NULL;
2513 if (*name == '#') {
2514 curserver = server_find_by_id(bk, atoi(name + 1));
2515 if (curserver)
2516 return curserver;
2517 }
2518 else {
2519 curserver = bk->srv;
2520
2521 while (curserver && (strcmp(curserver->id, name) != 0))
2522 curserver = curserver->next;
2523
2524 if (curserver)
2525 return curserver;
2526 }
2527
2528 return NULL;
2529}
2530
2531struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2532{
2533 struct server *byname;
2534 struct server *byid;
2535
2536 if (!name && !id)
2537 return NULL;
2538
2539 if (diff)
2540 *diff = 0;
2541
2542 byname = byid = NULL;
2543
2544 if (name) {
2545 byname = server_find_by_name(bk, name);
2546 if (byname && (!id || byname->puid == id))
2547 return byname;
2548 }
2549
2550 /* remaining possibilities :
2551 * - name not set
2552 * - name set but not found
2553 * - name found but ID doesn't match
2554 */
2555 if (id) {
2556 byid = server_find_by_id(bk, id);
2557 if (byid) {
2558 if (byname) {
2559 /* use id only if forced by configuration */
2560 if (byid->flags & SRV_F_FORCED_ID) {
2561 if (diff)
2562 *diff |= 2;
2563 return byid;
2564 }
2565 else {
2566 if (diff)
2567 *diff |= 1;
2568 return byname;
2569 }
2570 }
2571
2572 /* remaining possibilities:
2573 * - name not set
2574 * - name set but not found
2575 */
2576 if (name && diff)
2577 *diff |= 2;
2578 return byid;
2579 }
2580
2581 /* id bot found */
2582 if (byname) {
2583 if (diff)
2584 *diff |= 1;
2585 return byname;
2586 }
2587 }
2588
2589 return NULL;
2590}
2591
Christopher Faulet5d42e092017-10-16 12:00:40 +02002592/* Registers changes to be applied asynchronously */
2593static void srv_register_update(struct server *srv)
2594{
2595 if (LIST_ISEMPTY(&srv->update_status)) {
2596 THREAD_WANT_SYNC();
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002597 HA_SPIN_LOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
Christopher Faulet5d42e092017-10-16 12:00:40 +02002598 if (LIST_ISEMPTY(&srv->update_status))
2599 LIST_ADDQ(&updated_servers, &srv->update_status);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002600 HA_SPIN_UNLOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
Christopher Faulet5d42e092017-10-16 12:00:40 +02002601 }
2602}
2603
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002604/* Update a server state using the parameters available in the params list */
2605static void srv_update_state(struct server *srv, int version, char **params)
2606{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002607 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002608 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002609
2610 /* fields since version 1
2611 * and common to all other upcoming versions
2612 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002613 enum srv_state srv_op_state;
2614 enum srv_admin srv_admin_state;
2615 unsigned srv_uweight, srv_iweight;
2616 unsigned long srv_last_time_change;
2617 short srv_check_status;
2618 enum chk_result srv_check_result;
2619 int srv_check_health;
2620 int srv_check_state, srv_agent_state;
2621 int bk_f_forced_id;
2622 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002623 int fqdn_set_by_cli;
2624 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002625 const char *port_str;
2626 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002627
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002628 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002629 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002630 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002631 switch (version) {
2632 case 1:
2633 /*
2634 * now we can proceed with server's state update:
2635 * srv_addr: params[0]
2636 * srv_op_state: params[1]
2637 * srv_admin_state: params[2]
2638 * srv_uweight: params[3]
2639 * srv_iweight: params[4]
2640 * srv_last_time_change: params[5]
2641 * srv_check_status: params[6]
2642 * srv_check_result: params[7]
2643 * srv_check_health: params[8]
2644 * srv_check_state: params[9]
2645 * srv_agent_state: params[10]
2646 * bk_f_forced_id: params[11]
2647 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002648 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002649 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002650 */
2651
2652 /* validating srv_op_state */
2653 p = NULL;
2654 errno = 0;
2655 srv_op_state = strtol(params[1], &p, 10);
2656 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2657 (srv_op_state != SRV_ST_STOPPED &&
2658 srv_op_state != SRV_ST_STARTING &&
2659 srv_op_state != SRV_ST_RUNNING &&
2660 srv_op_state != SRV_ST_STOPPING)) {
2661 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2662 }
2663
2664 /* validating srv_admin_state */
2665 p = NULL;
2666 errno = 0;
2667 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002668 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002669
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002670 /* inherited statuses will be recomputed later.
2671 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2672 */
2673 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002674
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002675 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2676 (srv_admin_state != 0 &&
2677 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002678 srv_admin_state != SRV_ADMF_CMAINT &&
2679 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002680 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002681 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002682 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2683 }
2684
2685 /* validating srv_uweight */
2686 p = NULL;
2687 errno = 0;
2688 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002689 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002690 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2691
2692 /* validating srv_iweight */
2693 p = NULL;
2694 errno = 0;
2695 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002696 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002697 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2698
2699 /* validating srv_last_time_change */
2700 p = NULL;
2701 errno = 0;
2702 srv_last_time_change = strtol(params[5], &p, 10);
2703 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2704 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2705
2706 /* validating srv_check_status */
2707 p = NULL;
2708 errno = 0;
2709 srv_check_status = strtol(params[6], &p, 10);
2710 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2711 (srv_check_status >= HCHK_STATUS_SIZE))
2712 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2713
2714 /* validating srv_check_result */
2715 p = NULL;
2716 errno = 0;
2717 srv_check_result = strtol(params[7], &p, 10);
2718 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2719 (srv_check_result != CHK_RES_UNKNOWN &&
2720 srv_check_result != CHK_RES_NEUTRAL &&
2721 srv_check_result != CHK_RES_FAILED &&
2722 srv_check_result != CHK_RES_PASSED &&
2723 srv_check_result != CHK_RES_CONDPASS)) {
2724 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2725 }
2726
2727 /* validating srv_check_health */
2728 p = NULL;
2729 errno = 0;
2730 srv_check_health = strtol(params[8], &p, 10);
2731 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2732 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2733
2734 /* validating srv_check_state */
2735 p = NULL;
2736 errno = 0;
2737 srv_check_state = strtol(params[9], &p, 10);
2738 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2739 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2740 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2741
2742 /* validating srv_agent_state */
2743 p = NULL;
2744 errno = 0;
2745 srv_agent_state = strtol(params[10], &p, 10);
2746 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2747 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2748 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2749
2750 /* validating bk_f_forced_id */
2751 p = NULL;
2752 errno = 0;
2753 bk_f_forced_id = strtol(params[11], &p, 10);
2754 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2755 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2756
2757 /* validating srv_f_forced_id */
2758 p = NULL;
2759 errno = 0;
2760 srv_f_forced_id = strtol(params[12], &p, 10);
2761 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2762 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2763
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002764 /* validating srv_fqdn */
2765 fqdn = params[13];
2766 if (fqdn && *fqdn == '-')
2767 fqdn = NULL;
2768 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2769 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2770 fqdn = NULL;
2771 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002772
Frédéric Lécaille31694712017-08-01 08:47:19 +02002773 port_str = params[14];
2774 if (port_str) {
2775 port = strl2uic(port_str, strlen(port_str));
2776 if (port > USHRT_MAX) {
2777 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2778 port_str = NULL;
2779 }
2780 }
2781
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002782 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002783 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002784 goto out;
2785
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002786 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002787 /* recover operational state and apply it to this server
2788 * and all servers tracking this one */
2789 switch (srv_op_state) {
2790 case SRV_ST_STOPPED:
2791 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002792 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002793 break;
2794 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002795 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002796 break;
2797 case SRV_ST_STOPPING:
2798 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002799 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002800 break;
2801 case SRV_ST_RUNNING:
2802 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002803 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002804 break;
2805 }
2806
2807 /* When applying server state, the following rules apply:
2808 * - in case of a configuration change, we apply the setting from the new
2809 * configuration, regardless of old running state
2810 * - if no configuration change, we apply old running state only if old running
2811 * state is different from new configuration state
2812 */
2813 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002814 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2815 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002816 srv_adm_set_maint(srv);
2817 else
2818 srv_adm_set_ready(srv);
2819 }
2820 /* configuration is the same, let's compate old running state and new conf state */
2821 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002822 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002823 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002824 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002825 srv_adm_set_ready(srv);
2826 }
2827 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002828 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002829 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002830 * (srv->iweight is the weight set up in configuration).
2831 * There are two possible reasons for FDRAIN to have been present :
2832 * - previous config weight was zero
2833 * - "set server b/s drain" was sent to the CLI
2834 *
2835 * In the first case, we simply want to drop this drain state
2836 * if the new weight is not zero anymore, meaning the administrator
2837 * has intentionally turned the weight back to a positive value to
2838 * enable the server again after an operation. In the second case,
2839 * the drain state was forced on the CLI regardless of the config's
2840 * weight so we don't want a change to the config weight to lose this
2841 * status. What this means is :
2842 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2843 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002844 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002845 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002846 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002847 }
2848
2849 srv->last_change = date.tv_sec - srv_last_time_change;
2850 srv->check.status = srv_check_status;
2851 srv->check.result = srv_check_result;
2852 srv->check.health = srv_check_health;
2853
2854 /* Only case we want to apply is removing ENABLED flag which could have been
2855 * done by the "disable health" command over the stats socket
2856 */
2857 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2858 (srv_check_state & CHK_ST_CONFIGURED) &&
2859 !(srv_check_state & CHK_ST_ENABLED))
2860 srv->check.state &= ~CHK_ST_ENABLED;
2861
2862 /* Only case we want to apply is removing ENABLED flag which could have been
2863 * done by the "disable agent" command over the stats socket
2864 */
2865 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2866 (srv_agent_state & CHK_ST_CONFIGURED) &&
2867 !(srv_agent_state & CHK_ST_ENABLED))
2868 srv->agent.state &= ~CHK_ST_ENABLED;
2869
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002870 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2871 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002872 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002873 * It means that a configuration file change has precedence over a unix socket change
2874 * for server's weight
2875 *
2876 * by default, HAProxy applies the following weight when parsing the configuration
2877 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002878 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002879 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002880 srv->uweight = srv_uweight;
2881 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002882 server_recalc_eweight(srv);
2883
Willy Tarreaue5a60682016-11-09 14:54:53 +01002884 /* load server IP address */
2885 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002886
2887 if (fqdn && srv->hostname) {
2888 if (!strcmp(srv->hostname, fqdn)) {
2889 /* Here we reset the 'set from stats socket FQDN' flag
2890 * to support such transitions:
2891 * Let's say initial FQDN value is foo1 (in configuration file).
2892 * - FQDN changed from stats socket, from foo1 to foo2 value,
2893 * - FQDN changed again from file configuration (with the same previous value
2894 set from stats socket, from foo1 to foo2 value),
2895 * - reload for any other reason than a FQDN modification,
2896 * the configuration file FQDN matches the fqdn server state file value.
2897 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
2898 * any futher FQDN modification.
2899 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002900 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002901 }
2902 else {
2903 /* If the FDQN has been changed from stats socket,
2904 * apply fqdn state file value (which is the value set
2905 * from stats socket).
2906 */
2907 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002908 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02002909 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002910 }
2911 }
2912 }
2913
Frédéric Lécaille31694712017-08-01 08:47:19 +02002914 if (port_str)
2915 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002916 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002917
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002918 break;
2919 default:
2920 chunk_appendf(msg, ", version '%d' not supported", version);
2921 }
2922
2923 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002924 if (msg->len) {
2925 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002926 Warning("server-state application failed for server '%s/%s'%s",
2927 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002928 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002929}
2930
2931/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2932 * For each proxy, it does the following:
2933 * - opens its server state file (either one or local one)
2934 * - read whole file, line by line
2935 * - analyse each line to check if it matches our current backend:
2936 * - backend name matches
2937 * - backend id matches if id is forced and name doesn't match
2938 * - if the server pointed by the line is found, then state is applied
2939 *
2940 * If the running backend uuid or id differs from the state file, then HAProxy reports
2941 * a warning.
2942 */
2943void apply_server_state(void)
2944{
2945 char *cur, *end;
2946 char mybuf[SRV_STATE_LINE_MAXLEN];
2947 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002948 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
2949 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002950 int arg, srv_arg, version, diff;
2951 FILE *f;
2952 char *filepath;
2953 char globalfilepath[MAXPATHLEN + 1];
2954 char localfilepath[MAXPATHLEN + 1];
2955 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2956 extern struct proxy *proxy;
2957 struct proxy *curproxy, *bk;
2958 struct server *srv;
2959
2960 globalfilepathlen = 0;
2961 /* create the globalfilepath variable */
2962 if (global.server_state_file) {
2963 /* absolute path or no base directory provided */
2964 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2965 len = strlen(global.server_state_file);
2966 if (len > MAXPATHLEN) {
2967 globalfilepathlen = 0;
2968 goto globalfileerror;
2969 }
2970 memcpy(globalfilepath, global.server_state_file, len);
2971 globalfilepath[len] = '\0';
2972 globalfilepathlen = len;
2973 }
2974 else if (global.server_state_base) {
2975 len = strlen(global.server_state_base);
2976 globalfilepathlen += len;
2977
2978 if (globalfilepathlen > MAXPATHLEN) {
2979 globalfilepathlen = 0;
2980 goto globalfileerror;
2981 }
2982 strncpy(globalfilepath, global.server_state_base, len);
2983 globalfilepath[globalfilepathlen] = 0;
2984
2985 /* append a slash if needed */
2986 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2987 if (globalfilepathlen + 1 > MAXPATHLEN) {
2988 globalfilepathlen = 0;
2989 goto globalfileerror;
2990 }
2991 globalfilepath[globalfilepathlen++] = '/';
2992 }
2993
2994 len = strlen(global.server_state_file);
2995 if (globalfilepathlen + len > MAXPATHLEN) {
2996 globalfilepathlen = 0;
2997 goto globalfileerror;
2998 }
2999 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3000 globalfilepathlen += len;
3001 globalfilepath[globalfilepathlen++] = 0;
3002 }
3003 }
3004 globalfileerror:
3005 if (globalfilepathlen == 0)
3006 globalfilepath[0] = '\0';
3007
3008 /* read servers state from local file */
3009 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3010 /* servers are only in backends */
3011 if (!(curproxy->cap & PR_CAP_BE))
3012 continue;
3013 fileopenerr = 0;
3014 filepath = NULL;
3015
3016 /* search server state file path and name */
3017 switch (curproxy->load_server_state_from_file) {
3018 /* read servers state from global file */
3019 case PR_SRV_STATE_FILE_GLOBAL:
3020 /* there was an error while generating global server state file path */
3021 if (globalfilepathlen == 0)
3022 continue;
3023 filepath = globalfilepath;
3024 fileopenerr = 1;
3025 break;
3026 /* this backend has its own file */
3027 case PR_SRV_STATE_FILE_LOCAL:
3028 localfilepathlen = 0;
3029 localfilepath[0] = '\0';
3030 len = 0;
3031 /* create the localfilepath variable */
3032 /* absolute path or no base directory provided */
3033 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3034 len = strlen(curproxy->server_state_file_name);
3035 if (len > MAXPATHLEN) {
3036 localfilepathlen = 0;
3037 goto localfileerror;
3038 }
3039 memcpy(localfilepath, curproxy->server_state_file_name, len);
3040 localfilepath[len] = '\0';
3041 localfilepathlen = len;
3042 }
3043 else if (global.server_state_base) {
3044 len = strlen(global.server_state_base);
3045 localfilepathlen += len;
3046
3047 if (localfilepathlen > MAXPATHLEN) {
3048 localfilepathlen = 0;
3049 goto localfileerror;
3050 }
3051 strncpy(localfilepath, global.server_state_base, len);
3052 localfilepath[localfilepathlen] = 0;
3053
3054 /* append a slash if needed */
3055 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3056 if (localfilepathlen + 1 > MAXPATHLEN) {
3057 localfilepathlen = 0;
3058 goto localfileerror;
3059 }
3060 localfilepath[localfilepathlen++] = '/';
3061 }
3062
3063 len = strlen(curproxy->server_state_file_name);
3064 if (localfilepathlen + len > MAXPATHLEN) {
3065 localfilepathlen = 0;
3066 goto localfileerror;
3067 }
3068 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3069 localfilepathlen += len;
3070 localfilepath[localfilepathlen++] = 0;
3071 }
3072 filepath = localfilepath;
3073 localfileerror:
3074 if (localfilepathlen == 0)
3075 localfilepath[0] = '\0';
3076
3077 break;
3078 case PR_SRV_STATE_FILE_NONE:
3079 default:
3080 continue;
3081 }
3082
3083 /* preload global state file */
3084 errno = 0;
3085 f = fopen(filepath, "r");
3086 if (errno && fileopenerr)
3087 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3088 if (!f)
3089 continue;
3090
3091 mybuf[0] = '\0';
3092 mybuflen = 0;
3093 version = 0;
3094
3095 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003096 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3097 Warning("Can't read first line of the server state file '%s'\n", filepath);
3098 goto fileclose;
3099 }
3100
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003101 cur = mybuf;
3102 version = atoi(cur);
3103 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3104 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003105 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003106
3107 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3108 int bk_f_forced_id = 0;
3109 int check_id = 0;
3110 int check_name = 0;
3111
3112 mybuflen = strlen(mybuf);
3113 cur = mybuf;
3114 end = cur + mybuflen;
3115
3116 bk = NULL;
3117 srv = NULL;
3118
3119 /* we need at least one character */
3120 if (mybuflen == 0)
3121 continue;
3122
3123 /* ignore blank characters at the beginning of the line */
3124 while (isspace(*cur))
3125 ++cur;
3126
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003127 /* Ignore empty or commented lines */
3128 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003129 continue;
3130
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003131 /* truncated lines */
3132 if (mybuf[mybuflen - 1] != '\n') {
3133 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003134 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003135 }
3136
3137 /* Removes trailing '\n' */
3138 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003139
3140 /* we're now ready to move the line into *srv_params[] */
3141 params[0] = cur;
3142 arg = 1;
3143 srv_arg = 0;
3144 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3145 if (isspace(*cur)) {
3146 *cur = '\0';
3147 ++cur;
3148 while (isspace(*cur))
3149 ++cur;
3150 switch (version) {
3151 case 1:
3152 /*
3153 * srv_addr: params[4] => srv_params[0]
3154 * srv_op_state: params[5] => srv_params[1]
3155 * srv_admin_state: params[6] => srv_params[2]
3156 * srv_uweight: params[7] => srv_params[3]
3157 * srv_iweight: params[8] => srv_params[4]
3158 * srv_last_time_change: params[9] => srv_params[5]
3159 * srv_check_status: params[10] => srv_params[6]
3160 * srv_check_result: params[11] => srv_params[7]
3161 * srv_check_health: params[12] => srv_params[8]
3162 * srv_check_state: params[13] => srv_params[9]
3163 * srv_agent_state: params[14] => srv_params[10]
3164 * bk_f_forced_id: params[15] => srv_params[11]
3165 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003166 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003167 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003168 */
3169 if (arg >= 4) {
3170 srv_params[srv_arg] = cur;
3171 ++srv_arg;
3172 }
3173 break;
3174 }
3175
3176 params[arg] = cur;
3177 ++arg;
3178 }
3179 else {
3180 ++cur;
3181 }
3182 }
3183
3184 /* if line is incomplete line, then ignore it.
3185 * otherwise, update useful flags */
3186 switch (version) {
3187 case 1:
3188 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3189 continue;
3190 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3191 check_id = (atoi(params[0]) == curproxy->uuid);
3192 check_name = (strcmp(curproxy->id, params[1]) == 0);
3193 break;
3194 }
3195
3196 diff = 0;
3197 bk = curproxy;
3198
3199 /* if backend can't be found, let's continue */
3200 if (!check_id && !check_name)
3201 continue;
3202 else if (!check_id && check_name) {
3203 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3204 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3205 }
3206 else if (check_id && !check_name) {
3207 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3208 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3209 /* if name doesn't match, we still want to update curproxy if the backend id
3210 * was forced in previous the previous configuration */
3211 if (!bk_f_forced_id)
3212 continue;
3213 }
3214
3215 /* look for the server by its id: param[2] */
3216 /* else look for the server by its name: param[3] */
3217 diff = 0;
3218 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3219
3220 if (!srv) {
3221 /* if no server found, then warning and continue with next line */
3222 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3223 params[3], params[2], params[0], params[1]);
3224 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3225 params[3], params[2], params[0], params[1]);
3226 continue;
3227 }
3228 else if (diff & PR_FBM_MISMATCH_ID) {
3229 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);
3230 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 +02003231 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003232 }
3233 else if (diff & PR_FBM_MISMATCH_NAME) {
3234 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);
3235 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 +02003236 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003237 }
3238
3239 /* now we can proceed with server's state update */
3240 srv_update_state(srv, version, srv_params);
3241 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003242fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003243 fclose(f);
3244 }
3245}
3246
Simon Horman7d09b9a2013-02-12 10:45:51 +09003247/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003248 * update a server's current IP address.
3249 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3250 * ip is in network format.
3251 * updater is a string which contains an information about the requester of the update.
3252 * updater is used if not NULL.
3253 *
3254 * A log line and a stderr warning message is generated based on server's backend options.
3255 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003256int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003257{
3258 /* generates a log line and a warning on stderr */
3259 if (1) {
3260 /* book enough space for both IPv4 and IPv6 */
3261 char oldip[INET6_ADDRSTRLEN];
3262 char newip[INET6_ADDRSTRLEN];
3263
3264 memset(oldip, '\0', INET6_ADDRSTRLEN);
3265 memset(newip, '\0', INET6_ADDRSTRLEN);
3266
3267 /* copy old IP address in a string */
3268 switch (s->addr.ss_family) {
3269 case AF_INET:
3270 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3271 break;
3272 case AF_INET6:
3273 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3274 break;
3275 };
3276
3277 /* copy new IP address in a string */
3278 switch (ip_sin_family) {
3279 case AF_INET:
3280 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3281 break;
3282 case AF_INET6:
3283 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3284 break;
3285 };
3286
3287 /* save log line into a buffer */
3288 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3289 s->proxy->id, s->id, oldip, newip, updater);
3290
3291 /* write the buffer on stderr */
3292 Warning("%s.\n", trash.str);
3293
3294 /* send a log */
3295 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3296 }
3297
3298 /* save the new IP family */
3299 s->addr.ss_family = ip_sin_family;
3300 /* save the new IP address */
3301 switch (ip_sin_family) {
3302 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003303 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003304 break;
3305 case AF_INET6:
3306 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3307 break;
3308 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003309 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003310
3311 return 0;
3312}
3313
3314/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003315 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3316 *
3317 * Caller can pass its name through <updater> to get it integrated in the response message
3318 * returned by the function.
3319 *
3320 * The function first does the following, in that order:
3321 * - validates the new addr and/or port
3322 * - checks if an update is required (new IP or port is different than current ones)
3323 * - checks the update is allowed:
3324 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3325 * - allow all changes if no CHECKS are configured
3326 * - if CHECK is configured:
3327 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3328 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3329 * conditions are met
3330 */
3331const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3332{
3333 struct sockaddr_storage sa;
3334 int ret, port_change_required;
3335 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003336 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003337 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003338 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003339
3340 msg = get_trash_chunk();
3341 chunk_reset(msg);
3342
3343 if (addr) {
3344 memset(&sa, 0, sizeof(struct sockaddr_storage));
3345 if (str2ip2(addr, &sa, 0) == NULL) {
3346 chunk_printf(msg, "Invalid addr '%s'", addr);
3347 goto out;
3348 }
3349
3350 /* changes are allowed on AF_INET* families only */
3351 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3352 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3353 goto out;
3354 }
3355
3356 /* collecting data currently setup */
3357 memset(current_addr, '\0', sizeof(current_addr));
3358 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3359 /* changes are allowed on AF_INET* families only */
3360 if ((ret != AF_INET) && (ret != AF_INET6)) {
3361 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3362 goto out;
3363 }
3364
3365 /* applying ADDR changes if required and allowed
3366 * ipcmp returns 0 when both ADDR are the same
3367 */
3368 if (ipcmp(&s->addr, &sa) == 0) {
3369 chunk_appendf(msg, "no need to change the addr");
3370 goto port;
3371 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003372 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003373 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003374
3375 /* we also need to update check's ADDR only if it uses the server's one */
3376 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003377 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003378 }
3379
3380 /* we also need to update agent ADDR only if it use the server's one */
3381 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003382 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003383 }
3384
3385 /* update report for caller */
3386 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3387 }
3388
3389 port:
3390 if (port) {
3391 char sign = '\0';
3392 char *endptr;
3393
3394 if (addr)
3395 chunk_appendf(msg, ", ");
3396
3397 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003398 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003399
3400 /* check if PORT change is required */
3401 port_change_required = 0;
3402
3403 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003404 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003405 new_port = strtol(port, &endptr, 10);
3406 if ((errno != 0) || (port == endptr)) {
3407 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3408 goto out;
3409 }
3410
3411 /* check if caller triggers a port mapped or offset */
3412 if (sign == '-' || (sign == '+')) {
3413 /* check if server currently uses port map */
3414 if (!(s->flags & SRV_F_MAPPORTS)) {
3415 /* switch from fixed port to port map mandatorily triggers
3416 * a port change */
3417 port_change_required = 1;
3418 /* check is configured
3419 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3420 * prevent PORT change if check doesn't have it's dedicated port while switching
3421 * to port mapping */
3422 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3423 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.");
3424 goto out;
3425 }
3426 }
3427 /* we're already using port maps */
3428 else {
3429 port_change_required = current_port != new_port;
3430 }
3431 }
3432 /* fixed port */
3433 else {
3434 port_change_required = current_port != new_port;
3435 }
3436
3437 /* applying PORT changes if required and update response message */
3438 if (port_change_required) {
3439 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003440 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003441 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003442
3443 /* prepare message */
3444 chunk_appendf(msg, "port changed from '");
3445 if (s->flags & SRV_F_MAPPORTS)
3446 chunk_appendf(msg, "+");
3447 chunk_appendf(msg, "%d' to '", current_port);
3448
3449 if (sign == '-') {
3450 s->flags |= SRV_F_MAPPORTS;
3451 chunk_appendf(msg, "%c", sign);
3452 /* just use for result output */
3453 new_port = -new_port;
3454 }
3455 else if (sign == '+') {
3456 s->flags |= SRV_F_MAPPORTS;
3457 chunk_appendf(msg, "%c", sign);
3458 }
3459 else {
3460 s->flags &= ~SRV_F_MAPPORTS;
3461 }
3462
3463 chunk_appendf(msg, "%d'", new_port);
3464
3465 /* we also need to update health checks port only if it uses server's realport */
3466 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3467 s->check.port = new_port;
3468 }
3469 }
3470 else {
3471 chunk_appendf(msg, "no need to change the port");
3472 }
3473 }
3474
3475out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003476 if (changed)
3477 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003478 if (updater)
3479 chunk_appendf(msg, " by '%s'", updater);
3480 chunk_appendf(msg, "\n");
3481 return msg->str;
3482}
3483
3484
3485/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003486 * update server status based on result of name resolution
3487 * returns:
3488 * 0 if server status is updated
3489 * 1 if server status has not changed
3490 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003491int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003492{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003493 struct dns_resolvers *resolvers = s->resolvers;
3494 struct dns_resolution *resolution = s->dns_requester->resolution;
3495 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003496
3497 switch (resolution->status) {
3498 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003499 /* status when HAProxy has just (re)started.
3500 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003501 break;
3502
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003503 case RSLV_STATUS_VALID:
3504 /*
3505 * resume health checks
3506 * server will be turned back on if health check is safe
3507 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003508 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003509 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003510 return 1;
3511 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3512 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003513 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003514 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003515
Emeric Brun52a91d32017-08-31 14:41:55 +02003516 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003517 return 1;
3518 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3519 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3520 s->proxy->id, s->id);
3521
3522 Warning("%s.\n", trash.str);
3523 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3524 return 0;
3525
3526 case RSLV_STATUS_NX:
3527 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003528 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3529 if (!tick_is_expired(exp, now_ms))
3530 break;
3531
3532 if (s->next_admin & SRV_ADMF_RMAINT)
3533 return 1;
3534 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3535 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003536
3537 case RSLV_STATUS_TIMEOUT:
3538 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003539 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3540 if (!tick_is_expired(exp, now_ms))
3541 break;
3542
3543 if (s->next_admin & SRV_ADMF_RMAINT)
3544 return 1;
3545 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3546 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003547
3548 case RSLV_STATUS_REFUSED:
3549 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003550 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3551 if (!tick_is_expired(exp, now_ms))
3552 break;
3553
3554 if (s->next_admin & SRV_ADMF_RMAINT)
3555 return 1;
3556 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3557 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003558
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003559 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003560 /* stop server if resolution failed for a long enough period */
3561 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3562 if (!tick_is_expired(exp, now_ms))
3563 break;
3564
3565 if (s->next_admin & SRV_ADMF_RMAINT)
3566 return 1;
3567 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3568 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003569 }
3570
3571 return 1;
3572}
3573
3574/*
3575 * Server Name Resolution valid response callback
3576 * It expects:
3577 * - <nameserver>: the name server which answered the valid response
3578 * - <response>: buffer containing a valid DNS response
3579 * - <response_len>: size of <response>
3580 * It performs the following actions:
3581 * - ignore response if current ip found and server family not met
3582 * - update with first new ip found if family is met and current IP is not found
3583 * returns:
3584 * 0 on error
3585 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003586 *
3587 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003588 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003589int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003590{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003591 struct server *s = NULL;
3592 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003593 void *serverip, *firstip;
3594 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003595 int ret;
3596 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003597 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003598
Christopher Faulet67957bd2017-09-27 11:00:59 +02003599 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003600 if (!s)
3601 return 1;
3602
Christopher Faulet67957bd2017-09-27 11:00:59 +02003603 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003604
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003605 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003606 firstip = NULL; /* pointer to the first valid response found */
3607 /* it will be used as the new IP if a change is required */
3608 firstip_sin_family = AF_UNSPEC;
3609 serverip = NULL; /* current server IP address */
3610
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003611 /* initializing server IP pointer */
3612 server_sin_family = s->addr.ss_family;
3613 switch (server_sin_family) {
3614 case AF_INET:
3615 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3616 break;
3617
3618 case AF_INET6:
3619 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3620 break;
3621
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003622 case AF_UNSPEC:
3623 break;
3624
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003625 default:
3626 goto invalid;
3627 }
3628
Baptiste Assmann729c9012017-05-22 15:13:10 +02003629 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003630 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003631 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003632
3633 switch (ret) {
3634 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003635 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003636
3637 case DNS_UPD_SRVIP_NOT_FOUND:
3638 goto save_ip;
3639
3640 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003641 goto invalid;
3642
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003643 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003644 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003645 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003646
Baptiste Assmannfad03182015-10-28 02:03:32 +01003647 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003648 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003649 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003650 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003651
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003652 default:
3653 goto invalid;
3654
3655 }
3656
3657 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003658 if (nameserver) {
3659 nameserver->counters.update++;
3660 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003661 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003662 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003663 else
3664 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003665 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3666
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003667 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003668 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003669 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003670
3671 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003672 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003673 nameserver->counters.invalid++;
3674 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003675 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003676 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003677 return 0;
3678}
3679
3680/*
3681 * Server Name Resolution error management callback
3682 * returns:
3683 * 0 on error
3684 * 1 when no error or safe ignore
3685 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003686int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003687{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003688 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003689
Christopher Faulet67957bd2017-09-27 11:00:59 +02003690 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003691 if (!s)
3692 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003693 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003694 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003695 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003696 return 1;
3697}
3698
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003699/*
3700 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003701 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003702 * It returns a pointer to the first server found or NULL if <ip> is not yet
3703 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003704 *
3705 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003706 */
3707struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3708{
3709 struct server *tmpsrv;
3710 struct proxy *be;
3711
3712 if (!srv)
3713 return NULL;
3714
3715 be = srv->proxy;
3716 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003717 /* we found the current server is the same, ignore it */
3718 if (srv == tmpsrv)
3719 continue;
3720
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003721 /* We want to compare the IP in the record with the IP of the servers in the
3722 * same backend, only if:
3723 * * DNS resolution is enabled on the server
3724 * * the hostname used for the resolution by our server is the same than the
3725 * one used for the server found in the backend
3726 * * the server found in the backend is not our current server
3727 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003728 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003729 if ((tmpsrv->hostname_dn == NULL) ||
3730 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3731 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003732 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003733 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003734 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003735 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003736
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003737 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003738 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003739 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003740 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003741 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003742
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003743 /* At this point, we have 2 different servers using the same DNS hostname
3744 * for their respective resolution.
3745 */
3746 if (*ip_family == tmpsrv->addr.ss_family &&
3747 ((tmpsrv->addr.ss_family == AF_INET &&
3748 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3749 (tmpsrv->addr.ss_family == AF_INET6 &&
3750 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003751 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003752 return tmpsrv;
3753 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003754 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003755 }
3756
Emeric Brune9fd6b52017-11-02 17:20:39 +01003757
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003758 return NULL;
3759}
3760
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003761/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3762 * resolver. This is suited for initial address configuration. Returns 0 on
3763 * success otherwise a non-zero error code. In case of error, *err_code, if
3764 * not NULL, is filled up.
3765 */
3766int srv_set_addr_via_libc(struct server *srv, int *err_code)
3767{
3768 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003769 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003770 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003771 return 1;
3772 }
3773 return 0;
3774}
3775
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003776/* Set the server's FDQN (->hostname) from <hostname>.
3777 * Returns -1 if failed, 0 if not.
3778 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003779int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003780{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003781 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003782 char *hostname_dn;
3783 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003784
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003785 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003786 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003787 /* run time DNS resolution was not active for this server
3788 * and we can't enable it at run time for now.
3789 */
3790 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003791 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003792
3793 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003794 hostname_len = strlen(hostname);
3795 hostname_dn = trash.str;
3796 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3797 hostname_dn, trash.size);
3798 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003799 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003800
Christopher Faulet67957bd2017-09-27 11:00:59 +02003801 resolution = srv->dns_requester->resolution;
3802 if (resolution &&
3803 resolution->hostname_dn &&
3804 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02003805 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003806
Christopher Faulet67957bd2017-09-27 11:00:59 +02003807 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003808
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003809 free(srv->hostname);
3810 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003811 srv->hostname = strdup(hostname);
3812 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003813 srv->hostname_dn_len = hostname_dn_len;
3814 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003815 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003816
Olivier Houchard55dcdf42017-11-06 15:15:04 +01003817 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003818 goto err;
3819
3820 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003821 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003822 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003823 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003824
3825 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003826 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003827 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003828 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003829}
3830
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003831/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3832 * from the state file. This is suited for initial address configuration.
3833 * Returns 0 on success otherwise a non-zero error code. In case of error,
3834 * *err_code, if not NULL, is filled up.
3835 */
3836static int srv_apply_lastaddr(struct server *srv, int *err_code)
3837{
3838 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3839 if (err_code)
3840 *err_code |= ERR_WARN;
3841 return 1;
3842 }
3843 return 0;
3844}
3845
Willy Tarreau25e51522016-11-04 15:10:17 +01003846/* returns 0 if no error, otherwise a combination of ERR_* flags */
3847static int srv_iterate_initaddr(struct server *srv)
3848{
3849 int return_code = 0;
3850 int err_code;
3851 unsigned int methods;
3852
3853 methods = srv->init_addr_methods;
3854 if (!methods) { // default to "last,libc"
3855 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3856 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3857 }
3858
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003859 /* "-dr" : always append "none" so that server addresses resolution
3860 * failures are silently ignored, this is convenient to validate some
3861 * configs out of their environment.
3862 */
3863 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3864 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3865
Willy Tarreau25e51522016-11-04 15:10:17 +01003866 while (methods) {
3867 err_code = 0;
3868 switch (srv_get_next_initaddr(&methods)) {
3869 case SRV_IADDR_LAST:
3870 if (!srv->lastaddr)
3871 continue;
3872 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003873 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003874 return_code |= err_code;
3875 break;
3876
3877 case SRV_IADDR_LIBC:
3878 if (!srv->hostname)
3879 continue;
3880 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003881 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003882 return_code |= err_code;
3883 break;
3884
Willy Tarreau37ebe122016-11-04 15:17:58 +01003885 case SRV_IADDR_NONE:
3886 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003887 if (return_code) {
3888 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3889 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3890 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003891 return return_code;
3892
Willy Tarreau4310d362016-11-02 15:05:56 +01003893 case SRV_IADDR_IP:
3894 ipcpy(&srv->init_addr, &srv->addr);
3895 if (return_code) {
3896 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3897 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3898 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003899 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003900
Willy Tarreau25e51522016-11-04 15:10:17 +01003901 default: /* unhandled method */
3902 break;
3903 }
3904 }
3905
3906 if (!return_code) {
3907 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3908 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3909 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003910 else {
3911 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3912 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3913 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003914
3915 return_code |= ERR_ALERT | ERR_FATAL;
3916 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003917out:
3918 srv_set_dyncookie(srv);
3919 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003920}
3921
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003922/*
3923 * This function parses all backends and all servers within each backend
3924 * and performs servers' addr resolution based on information provided by:
3925 * - configuration file
3926 * - server-state file (states provided by an 'old' haproxy process)
3927 *
3928 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3929 */
3930int srv_init_addr(void)
3931{
3932 struct proxy *curproxy;
3933 int return_code = 0;
3934
3935 curproxy = proxy;
3936 while (curproxy) {
3937 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003938
3939 /* servers are in backend only */
3940 if (!(curproxy->cap & PR_CAP_BE))
3941 goto srv_init_addr_next;
3942
Willy Tarreau25e51522016-11-04 15:10:17 +01003943 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003944 if (srv->hostname)
3945 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003946
3947 srv_init_addr_next:
3948 curproxy = curproxy->next;
3949 }
3950
3951 return return_code;
3952}
3953
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003954const 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 +02003955{
3956
3957 struct chunk *msg;
3958
3959 msg = get_trash_chunk();
3960 chunk_reset(msg);
3961
Olivier Houchard8da5f982017-08-04 18:35:36 +02003962 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003963 chunk_appendf(msg, "no need to change the FDQN");
3964 goto out;
3965 }
3966
3967 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
3968 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
3969 goto out;
3970 }
3971
3972 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
3973 server->proxy->id, server->id, server->hostname, fqdn);
3974
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003975 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003976 chunk_reset(msg);
3977 chunk_appendf(msg, "could not update %s/%s FQDN",
3978 server->proxy->id, server->id);
3979 goto out;
3980 }
3981
3982 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02003983 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003984
3985 out:
3986 if (updater)
3987 chunk_appendf(msg, " by '%s'", updater);
3988 chunk_appendf(msg, "\n");
3989
3990 return msg->str;
3991}
3992
3993
Willy Tarreau21b069d2016-11-23 17:15:08 +01003994/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3995 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003996 * 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 +01003997 * used for CLI commands requiring a server name.
3998 * Important: the <arg> is modified to remove the '/'.
3999 */
4000struct server *cli_find_server(struct appctx *appctx, char *arg)
4001{
4002 struct proxy *px;
4003 struct server *sv;
4004 char *line;
4005
4006 /* split "backend/server" and make <line> point to server */
4007 for (line = arg; *line; line++)
4008 if (*line == '/') {
4009 *line++ = '\0';
4010 break;
4011 }
4012
4013 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004014 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004015 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004016 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004017 return NULL;
4018 }
4019
4020 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004021 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004022 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004023 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004024 return NULL;
4025 }
4026
4027 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004028 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004029 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004030 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004031 return NULL;
4032 }
4033
4034 return sv;
4035}
4036
William Lallemand222baf22016-11-19 02:00:33 +01004037
4038static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4039{
4040 struct server *sv;
4041 const char *warning;
4042
4043 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4044 return 1;
4045
4046 sv = cli_find_server(appctx, args[2]);
4047 if (!sv)
4048 return 1;
4049
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004050 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004051
William Lallemand222baf22016-11-19 02:00:33 +01004052 if (strcmp(args[3], "weight") == 0) {
4053 warning = server_parse_weight_change_request(sv, args[4]);
4054 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004055 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004056 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004057 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004058 }
4059 }
4060 else if (strcmp(args[3], "state") == 0) {
4061 if (strcmp(args[4], "ready") == 0)
4062 srv_adm_set_ready(sv);
4063 else if (strcmp(args[4], "drain") == 0)
4064 srv_adm_set_drain(sv);
4065 else if (strcmp(args[4], "maint") == 0)
4066 srv_adm_set_maint(sv);
4067 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004068 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004069 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004070 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004071 }
4072 }
4073 else if (strcmp(args[3], "health") == 0) {
4074 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004075 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004076 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004077 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004078 }
4079 else if (strcmp(args[4], "up") == 0) {
4080 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004081 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004082 }
4083 else if (strcmp(args[4], "stopping") == 0) {
4084 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004085 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004086 }
4087 else if (strcmp(args[4], "down") == 0) {
4088 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004089 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004090 }
4091 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004092 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004093 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004094 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004095 }
4096 }
4097 else if (strcmp(args[3], "agent") == 0) {
4098 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004099 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004100 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004101 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004102 }
4103 else if (strcmp(args[4], "up") == 0) {
4104 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004105 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004106 }
4107 else if (strcmp(args[4], "down") == 0) {
4108 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004109 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004110 }
4111 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004112 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004113 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004114 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004115 }
4116 }
Misiek2da082d2017-01-09 09:40:42 +01004117 else if (strcmp(args[3], "agent-addr") == 0) {
4118 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004119 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004120 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4121 appctx->st0 = CLI_ST_PRINT;
4122 } else {
4123 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004124 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004125 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4126 appctx->st0 = CLI_ST_PRINT;
4127 }
4128 }
4129 }
4130 else if (strcmp(args[3], "agent-send") == 0) {
4131 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004132 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004133 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4134 appctx->st0 = CLI_ST_PRINT;
4135 } else {
4136 char *nss = strdup(args[4]);
4137 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004138 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004139 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4140 appctx->st0 = CLI_ST_PRINT;
4141 } else {
4142 free(sv->agent.send_string);
4143 sv->agent.send_string = nss;
4144 sv->agent.send_string_len = strlen(args[4]);
4145 }
4146 }
4147 }
William Lallemand222baf22016-11-19 02:00:33 +01004148 else if (strcmp(args[3], "check-port") == 0) {
4149 int i = 0;
4150 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004151 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004152 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004153 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004154 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004155 }
4156 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004157 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004158 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004159 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004160 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004161 }
4162 /* prevent the update of port to 0 if MAPPORTS are in use */
4163 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004164 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004165 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004166 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004167 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004168 }
4169 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004170 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004171 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004172 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004173 }
4174 else if (strcmp(args[3], "addr") == 0) {
4175 char *addr = NULL;
4176 char *port = NULL;
4177 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004178 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004179 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004180 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004181 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004182 }
4183 else {
4184 addr = args[4];
4185 }
4186 if (strcmp(args[5], "port") == 0) {
4187 port = args[6];
4188 }
4189 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4190 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004191 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004192 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004193 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004194 }
4195 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4196 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004197 else if (strcmp(args[3], "fqdn") == 0) {
4198 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004199 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004200 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4201 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004202 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004203 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004204 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004205 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004206 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004207 appctx->ctx.cli.msg = warning;
4208 appctx->st0 = CLI_ST_PRINT;
4209 }
4210 }
William Lallemand222baf22016-11-19 02:00:33 +01004211 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004212 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004213 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 +01004214 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004215 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004216 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004217 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004218 return 1;
4219}
4220
William Lallemand6b160942016-11-22 12:34:35 +01004221static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4222{
4223 struct stream_interface *si = appctx->owner;
4224 struct proxy *px;
4225 struct server *sv;
4226 char *line;
4227
4228
4229 /* split "backend/server" and make <line> point to server */
4230 for (line = args[2]; *line; line++)
4231 if (*line == '/') {
4232 *line++ = '\0';
4233 break;
4234 }
4235
4236 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004237 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004238 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004239 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004240 return 1;
4241 }
4242
4243 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004244 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004245 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004246 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004247 return 1;
4248 }
4249
4250 /* return server's effective weight at the moment */
4251 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004252 if (ci_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004253 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004254 return 0;
4255 }
William Lallemand6b160942016-11-22 12:34:35 +01004256 return 1;
4257}
4258
4259static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4260{
4261 struct server *sv;
4262 const char *warning;
4263
4264 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4265 return 1;
4266
4267 sv = cli_find_server(appctx, args[2]);
4268 if (!sv)
4269 return 1;
4270
4271 warning = server_parse_weight_change_request(sv, args[3]);
4272 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004273 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004274 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004275 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004276 }
4277 return 1;
4278}
4279
Willy Tarreaub8026272016-11-23 11:26:56 +01004280/* parse a "set maxconn server" command. It always returns 1. */
4281static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4282{
4283 struct server *sv;
4284 const char *warning;
4285
4286 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4287 return 1;
4288
4289 sv = cli_find_server(appctx, args[3]);
4290 if (!sv)
4291 return 1;
4292
4293 warning = server_parse_maxconn_change_request(sv, args[4]);
4294 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004295 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004296 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004297 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004298 }
4299 return 1;
4300}
William Lallemand6b160942016-11-22 12:34:35 +01004301
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004302/* parse a "disable agent" command. It always returns 1. */
4303static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4304{
4305 struct server *sv;
4306
4307 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4308 return 1;
4309
4310 sv = cli_find_server(appctx, args[2]);
4311 if (!sv)
4312 return 1;
4313
4314 sv->agent.state &= ~CHK_ST_ENABLED;
4315 return 1;
4316}
4317
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004318/* parse a "disable health" command. It always returns 1. */
4319static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4320{
4321 struct server *sv;
4322
4323 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4324 return 1;
4325
4326 sv = cli_find_server(appctx, args[2]);
4327 if (!sv)
4328 return 1;
4329
4330 sv->check.state &= ~CHK_ST_ENABLED;
4331 return 1;
4332}
4333
Willy Tarreauffb4d582016-11-24 12:47:00 +01004334/* parse a "disable server" command. It always returns 1. */
4335static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4336{
4337 struct server *sv;
4338
4339 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4340 return 1;
4341
4342 sv = cli_find_server(appctx, args[2]);
4343 if (!sv)
4344 return 1;
4345
4346 srv_adm_set_maint(sv);
4347 return 1;
4348}
4349
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004350/* parse a "enable agent" command. It always returns 1. */
4351static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4352{
4353 struct server *sv;
4354
4355 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4356 return 1;
4357
4358 sv = cli_find_server(appctx, args[2]);
4359 if (!sv)
4360 return 1;
4361
4362 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004363 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004364 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004365 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004366 return 1;
4367 }
4368
4369 sv->agent.state |= CHK_ST_ENABLED;
4370 return 1;
4371}
4372
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004373/* parse a "enable health" command. It always returns 1. */
4374static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4375{
4376 struct server *sv;
4377
4378 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4379 return 1;
4380
4381 sv = cli_find_server(appctx, args[2]);
4382 if (!sv)
4383 return 1;
4384
4385 sv->check.state |= CHK_ST_ENABLED;
4386 return 1;
4387}
4388
Willy Tarreauffb4d582016-11-24 12:47:00 +01004389/* parse a "enable server" command. It always returns 1. */
4390static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4391{
4392 struct server *sv;
4393
4394 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4395 return 1;
4396
4397 sv = cli_find_server(appctx, args[2]);
4398 if (!sv)
4399 return 1;
4400
4401 srv_adm_set_ready(sv);
4402 return 1;
4403}
4404
William Lallemand222baf22016-11-19 02:00:33 +01004405/* register cli keywords */
4406static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004407 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004408 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004409 { { "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 +01004410 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004411 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004412 { { "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 +01004413 { { "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 +01004414 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004415 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4416 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4417
William Lallemand222baf22016-11-19 02:00:33 +01004418 {{},}
4419}};
4420
4421__attribute__((constructor))
4422static void __server_init(void)
4423{
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004424 HA_SPIN_INIT(&updated_servers_lock);
William Lallemand222baf22016-11-19 02:00:33 +01004425 cli_register_kw(&cli_kws);
4426}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004427
Emeric Brun64cc49c2017-10-03 14:46:45 +02004428/*
4429 * This function applies server's status changes, it is
4430 * is designed to be called asynchronously.
4431 *
4432 */
4433void srv_update_status(struct server *s)
4434{
4435 struct check *check = &s->check;
4436 int xferred;
4437 struct proxy *px = s->proxy;
4438 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4439 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4440 int log_level;
4441 struct chunk *tmptrash = NULL;
4442
4443
4444 /* If currently main is not set we try to apply pending state changes */
4445 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4446 int next_admin;
4447
4448 /* Backup next admin */
4449 next_admin = s->next_admin;
4450
4451 /* restore current admin state */
4452 s->next_admin = s->cur_admin;
4453
4454 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4455 s->last_change = now.tv_sec;
4456 if (s->proxy->lbprm.set_server_status_down)
4457 s->proxy->lbprm.set_server_status_down(s);
4458
4459 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4460 srv_shutdown_streams(s, SF_ERR_DOWN);
4461
4462 /* we might have streams queued on this server and waiting for
4463 * a connection. Those which are redispatchable will be queued
4464 * to another server or to the proxy itself.
4465 */
4466 xferred = pendconn_redistribute(s);
4467
4468 tmptrash = alloc_trash_chunk();
4469 if (tmptrash) {
4470 chunk_printf(tmptrash,
4471 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4472 s->proxy->id, s->id);
4473
Emeric Brun5a133512017-10-19 14:42:30 +02004474 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004475 Warning("%s.\n", tmptrash->str);
4476
4477 /* we don't send an alert if the server was previously paused */
4478 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
4479 send_log(s->proxy, log_level, "%s.\n", tmptrash->str);
4480 send_email_alert(s, log_level, "%s", tmptrash->str);
4481 free_trash_chunk(tmptrash);
4482 tmptrash = NULL;
4483 }
4484 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4485 set_backend_down(s->proxy);
4486
4487 s->counters.down_trans++;
4488 }
4489 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4490 s->last_change = now.tv_sec;
4491 if (s->proxy->lbprm.set_server_status_down)
4492 s->proxy->lbprm.set_server_status_down(s);
4493
4494 /* we might have streams queued on this server and waiting for
4495 * a connection. Those which are redispatchable will be queued
4496 * to another server or to the proxy itself.
4497 */
4498 xferred = pendconn_redistribute(s);
4499
4500 tmptrash = alloc_trash_chunk();
4501 if (tmptrash) {
4502 chunk_printf(tmptrash,
4503 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4504 s->proxy->id, s->id);
4505
Emeric Brun5a133512017-10-19 14:42:30 +02004506 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004507
4508 Warning("%s.\n", tmptrash->str);
4509 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4510 free_trash_chunk(tmptrash);
4511 tmptrash = NULL;
4512 }
4513
4514 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4515 set_backend_down(s->proxy);
4516 }
4517 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4518 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4519 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4520 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4521 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4522 s->proxy->last_change = now.tv_sec;
4523 }
4524
4525 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4526 s->down_time += now.tv_sec - s->last_change;
4527
4528 s->last_change = now.tv_sec;
4529 if (s->next_state == SRV_ST_STARTING)
4530 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4531
4532 server_recalc_eweight(s);
4533 /* now propagate the status change to any LB algorithms */
4534 if (px->lbprm.update_server_eweight)
4535 px->lbprm.update_server_eweight(s);
4536 else if (srv_willbe_usable(s)) {
4537 if (px->lbprm.set_server_status_up)
4538 px->lbprm.set_server_status_up(s);
4539 }
4540 else {
4541 if (px->lbprm.set_server_status_down)
4542 px->lbprm.set_server_status_down(s);
4543 }
4544
4545 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4546 * and it's not a backup server and its effective weight is > 0,
4547 * then it can accept new connections, so we shut down all streams
4548 * on all backup servers.
4549 */
4550 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4551 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4552 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4553
4554 /* check if we can handle some connections queued at the proxy. We
4555 * will take as many as we can handle.
4556 */
4557 xferred = pendconn_grab_from_px(s);
4558
4559 tmptrash = alloc_trash_chunk();
4560 if (tmptrash) {
4561 chunk_printf(tmptrash,
4562 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4563 s->proxy->id, s->id);
4564
Emeric Brun5a133512017-10-19 14:42:30 +02004565 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004566 Warning("%s.\n", tmptrash->str);
4567 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4568 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4569 free_trash_chunk(tmptrash);
4570 tmptrash = NULL;
4571 }
4572
4573 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4574 set_backend_down(s->proxy);
4575 }
4576 else if (s->cur_eweight != s->next_eweight) {
4577 /* now propagate the status change to any LB algorithms */
4578 if (px->lbprm.update_server_eweight)
4579 px->lbprm.update_server_eweight(s);
4580 else if (srv_willbe_usable(s)) {
4581 if (px->lbprm.set_server_status_up)
4582 px->lbprm.set_server_status_up(s);
4583 }
4584 else {
4585 if (px->lbprm.set_server_status_down)
4586 px->lbprm.set_server_status_down(s);
4587 }
4588
4589 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4590 set_backend_down(s->proxy);
4591 }
4592
4593 s->next_admin = next_admin;
4594 }
4595
Emeric Brun5a133512017-10-19 14:42:30 +02004596 /* reset operational state change */
4597 *s->op_st_chg.reason = 0;
4598 s->op_st_chg.status = s->op_st_chg.code = -1;
4599 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004600
4601 /* Now we try to apply pending admin changes */
4602
4603 /* Maintenance must also disable health checks */
4604 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4605 if (s->check.state & CHK_ST_ENABLED) {
4606 s->check.state |= CHK_ST_PAUSED;
4607 check->health = 0;
4608 }
4609
4610 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004611 tmptrash = alloc_trash_chunk();
4612 if (tmptrash) {
4613 chunk_printf(tmptrash,
4614 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4615 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4616 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004617
Olivier Houchard796a2b32017-10-24 17:42:47 +02004618 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004619
Olivier Houchard796a2b32017-10-24 17:42:47 +02004620 if (!(global.mode & MODE_STARTING)) {
4621 Warning("%s.\n", tmptrash->str);
4622 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4623 }
4624 free_trash_chunk(tmptrash);
4625 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004626 }
4627 }
4628 else { /* server was still running */
4629 check->health = 0; /* failure */
4630 s->last_change = now.tv_sec;
4631 if (s->proxy->lbprm.set_server_status_down)
4632 s->proxy->lbprm.set_server_status_down(s);
4633
4634 s->next_state = SRV_ST_STOPPED;
4635 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4636 srv_shutdown_streams(s, SF_ERR_DOWN);
4637
4638 /* we might have streams queued on this server and waiting for
4639 * a connection. Those which are redispatchable will be queued
4640 * to another server or to the proxy itself.
4641 */
4642 xferred = pendconn_redistribute(s);
4643
4644 tmptrash = alloc_trash_chunk();
4645 if (tmptrash) {
4646 chunk_printf(tmptrash,
4647 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4648 s->flags & SRV_F_BACKUP ? "Backup " : "",
4649 s->proxy->id, s->id,
4650 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4651
4652 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4653
4654 if (!(global.mode & MODE_STARTING)) {
4655 Warning("%s.\n", tmptrash->str);
4656 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", tmptrash->str);
4657 }
4658 free_trash_chunk(tmptrash);
4659 tmptrash = NULL;
4660 }
4661 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4662 set_backend_down(s->proxy);
4663
4664 s->counters.down_trans++;
4665 }
4666 }
4667 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4668 /* OK here we're leaving maintenance, we have many things to check,
4669 * because the server might possibly be coming back up depending on
4670 * its state. In practice, leaving maintenance means that we should
4671 * immediately turn to UP (more or less the slowstart) under the
4672 * following conditions :
4673 * - server is neither checked nor tracked
4674 * - server tracks another server which is not checked
4675 * - server tracks another server which is already up
4676 * Which sums up as something simpler :
4677 * "either the tracking server is up or the server's checks are disabled
4678 * or up". Otherwise we only re-enable health checks. There's a special
4679 * case associated to the stopping state which can be inherited. Note
4680 * that the server might still be in drain mode, which is naturally dealt
4681 * with by the lower level functions.
4682 */
4683
4684 if (s->check.state & CHK_ST_ENABLED) {
4685 s->check.state &= ~CHK_ST_PAUSED;
4686 check->health = check->rise; /* start OK but check immediately */
4687 }
4688
4689 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4690 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4691 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4692 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4693 s->next_state = SRV_ST_STOPPING;
4694 }
4695 else {
4696 s->next_state = SRV_ST_STARTING;
4697 if (s->slowstart > 0)
4698 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4699 else
4700 s->next_state = SRV_ST_RUNNING;
4701 }
4702
4703 }
4704
4705 tmptrash = alloc_trash_chunk();
4706 if (tmptrash) {
4707 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4708 chunk_printf(tmptrash,
4709 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4710 s->flags & SRV_F_BACKUP ? "Backup " : "",
4711 s->proxy->id, s->id,
4712 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4713 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4714 }
4715 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4716 chunk_printf(tmptrash,
4717 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4718 s->flags & SRV_F_BACKUP ? "Backup " : "",
4719 s->proxy->id, s->id, s->hostname,
4720 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4721 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4722 }
4723 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4724 chunk_printf(tmptrash,
4725 "%sServer %s/%s is %s/%s (leaving maintenance)",
4726 s->flags & SRV_F_BACKUP ? "Backup " : "",
4727 s->proxy->id, s->id,
4728 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4729 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4730 }
4731 Warning("%s.\n", tmptrash->str);
4732 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4733 free_trash_chunk(tmptrash);
4734 tmptrash = NULL;
4735 }
4736
4737 server_recalc_eweight(s);
4738 /* now propagate the status change to any LB algorithms */
4739 if (px->lbprm.update_server_eweight)
4740 px->lbprm.update_server_eweight(s);
4741 else if (srv_willbe_usable(s)) {
4742 if (px->lbprm.set_server_status_up)
4743 px->lbprm.set_server_status_up(s);
4744 }
4745 else {
4746 if (px->lbprm.set_server_status_down)
4747 px->lbprm.set_server_status_down(s);
4748 }
4749
4750 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4751 set_backend_down(s->proxy);
4752
4753 }
4754 else if (s->next_admin & SRV_ADMF_MAINT) {
4755 /* remaining in maintenance mode, let's inform precisely about the
4756 * situation.
4757 */
4758 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4759 tmptrash = alloc_trash_chunk();
4760 if (tmptrash) {
4761 chunk_printf(tmptrash,
4762 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4763 s->flags & SRV_F_BACKUP ? "Backup " : "",
4764 s->proxy->id, s->id);
4765
4766 if (s->track) /* normally it's mandatory here */
4767 chunk_appendf(tmptrash, " via %s/%s",
4768 s->track->proxy->id, s->track->id);
4769 Warning("%s.\n", tmptrash->str);
4770 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4771 free_trash_chunk(tmptrash);
4772 tmptrash = NULL;
4773 }
4774 }
4775 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4776 tmptrash = alloc_trash_chunk();
4777 if (tmptrash) {
4778 chunk_printf(tmptrash,
4779 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4780 s->flags & SRV_F_BACKUP ? "Backup " : "",
4781 s->proxy->id, s->id, s->hostname);
4782
4783 if (s->track) /* normally it's mandatory here */
4784 chunk_appendf(tmptrash, " via %s/%s",
4785 s->track->proxy->id, s->track->id);
4786 Warning("%s.\n", tmptrash->str);
4787 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4788 free_trash_chunk(tmptrash);
4789 tmptrash = NULL;
4790 }
4791 }
4792 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4793 tmptrash = alloc_trash_chunk();
4794 if (tmptrash) {
4795 chunk_printf(tmptrash,
4796 "%sServer %s/%s remains in forced maintenance",
4797 s->flags & SRV_F_BACKUP ? "Backup " : "",
4798 s->proxy->id, s->id);
4799 Warning("%s.\n", tmptrash->str);
4800 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4801 free_trash_chunk(tmptrash);
4802 tmptrash = NULL;
4803 }
4804 }
4805 /* don't report anything when leaving drain mode and remaining in maintenance */
4806
4807 s->cur_admin = s->next_admin;
4808 }
4809
4810 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4811 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4812 /* drain state is applied only if not yet in maint */
4813
4814 s->last_change = now.tv_sec;
4815 if (px->lbprm.set_server_status_down)
4816 px->lbprm.set_server_status_down(s);
4817
4818 /* we might have streams queued on this server and waiting for
4819 * a connection. Those which are redispatchable will be queued
4820 * to another server or to the proxy itself.
4821 */
4822 xferred = pendconn_redistribute(s);
4823
4824 tmptrash = alloc_trash_chunk();
4825 if (tmptrash) {
4826 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4827 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4828 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4829
4830 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4831
4832 if (!(global.mode & MODE_STARTING)) {
4833 Warning("%s.\n", tmptrash->str);
4834 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4835 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4836 }
4837 free_trash_chunk(tmptrash);
4838 tmptrash = NULL;
4839 }
4840
4841 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4842 set_backend_down(s->proxy);
4843 }
4844 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4845 /* OK completely leaving drain mode */
4846 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4847 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4848 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4849 s->proxy->last_change = now.tv_sec;
4850 }
4851
4852 if (s->last_change < now.tv_sec) // ignore negative times
4853 s->down_time += now.tv_sec - s->last_change;
4854 s->last_change = now.tv_sec;
4855 server_recalc_eweight(s);
4856
4857 tmptrash = alloc_trash_chunk();
4858 if (tmptrash) {
4859 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4860 chunk_printf(tmptrash,
4861 "%sServer %s/%s is %s (leaving forced drain)",
4862 s->flags & SRV_F_BACKUP ? "Backup " : "",
4863 s->proxy->id, s->id,
4864 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4865 }
4866 else {
4867 chunk_printf(tmptrash,
4868 "%sServer %s/%s is %s (leaving drain)",
4869 s->flags & SRV_F_BACKUP ? "Backup " : "",
4870 s->proxy->id, s->id,
4871 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4872 if (s->track) /* normally it's mandatory here */
4873 chunk_appendf(tmptrash, " via %s/%s",
4874 s->track->proxy->id, s->track->id);
4875 }
4876
4877 Warning("%s.\n", tmptrash->str);
4878 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4879 free_trash_chunk(tmptrash);
4880 tmptrash = NULL;
4881 }
4882
4883 /* now propagate the status change to any LB algorithms */
4884 if (px->lbprm.update_server_eweight)
4885 px->lbprm.update_server_eweight(s);
4886 else if (srv_willbe_usable(s)) {
4887 if (px->lbprm.set_server_status_up)
4888 px->lbprm.set_server_status_up(s);
4889 }
4890 else {
4891 if (px->lbprm.set_server_status_down)
4892 px->lbprm.set_server_status_down(s);
4893 }
4894 }
4895 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
4896 /* remaining in drain mode after removing one of its flags */
4897
4898 tmptrash = alloc_trash_chunk();
4899 if (tmptrash) {
4900 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4901 chunk_printf(tmptrash,
4902 "%sServer %s/%s is leaving forced drain but remains in drain mode",
4903 s->flags & SRV_F_BACKUP ? "Backup " : "",
4904 s->proxy->id, s->id);
4905
4906 if (s->track) /* normally it's mandatory here */
4907 chunk_appendf(tmptrash, " via %s/%s",
4908 s->track->proxy->id, s->track->id);
4909 }
4910 else {
4911 chunk_printf(tmptrash,
4912 "%sServer %s/%s remains in forced drain mode",
4913 s->flags & SRV_F_BACKUP ? "Backup " : "",
4914 s->proxy->id, s->id);
4915 }
4916 Warning("%s.\n", tmptrash->str);
4917 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4918 free_trash_chunk(tmptrash);
4919 tmptrash = NULL;
4920 }
4921
4922 /* commit new admin status */
4923
4924 s->cur_admin = s->next_admin;
4925 }
4926 }
4927
4928 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02004929 *s->adm_st_chg_cause = 0;
4930}
4931/*
4932 * This function loops on servers registered for asynchronous
4933 * status changes
Christopher Faulet5d42e092017-10-16 12:00:40 +02004934 *
4935 * NOTE: No needs to lock <updated_servers> list because it is called inside the
4936 * sync point.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004937 */
4938void servers_update_status(void) {
4939 struct server *s, *stmp;
4940
4941 list_for_each_entry_safe(s, stmp, &updated_servers, update_status) {
4942 srv_update_status(s);
4943 LIST_DEL(&s->update_status);
4944 LIST_INIT(&s->update_status);
4945 }
4946}
4947
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004948/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004949 * Local variables:
4950 * c-indent-level: 8
4951 * c-basic-offset: 8
4952 * End:
4953 */