blob: 1d7a5a771e435f8654dda66da9d881dc0e6f8c39 [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>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020035#include <proto/connection.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020036#include <proto/port_range.h>
37#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020038#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010039#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020040#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020041#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010042#include <proto/stream_interface.h>
43#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020044#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020045#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010046#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020047
Willy Tarreau3ff577e2018-08-02 11:48:52 +020048static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020049static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010050static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010051static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Willy Tarreaubaaee002006-06-26 02:48:02 +020052
Willy Tarreau21faa912012-10-10 08:27:36 +020053/* List head of all known server keywords */
54static struct srv_kw_list srv_keywords = {
55 .list = LIST_HEAD_INIT(srv_keywords.list)
56};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020057
Simon Hormana3608442013-11-01 16:46:15 +090058int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020059{
Emeric Brun52a91d32017-08-31 14:41:55 +020060 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020061 return s->down_time;
62
63 return now.tv_sec - s->last_change + s->down_time;
64}
Willy Tarreaubaaee002006-06-26 02:48:02 +020065
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050066int srv_lastsession(const struct server *s)
67{
68 if (s->counters.last_sess)
69 return now.tv_sec - s->counters.last_sess;
70
71 return -1;
72}
73
Simon Horman4a741432013-02-23 15:35:38 +090074int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020075{
Simon Horman4a741432013-02-23 15:35:38 +090076 const struct server *s = check->server;
77
Willy Tarreauff5ae352013-12-11 20:36:34 +010078 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090079 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010080
Emeric Brun52a91d32017-08-31 14:41:55 +020081 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090082 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010083
Simon Horman4a741432013-02-23 15:35:38 +090084 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010085}
86
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010087/*
88 * Check that we did not get a hash collision.
89 * Unlikely, but it can happen.
90 */
91static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +010092{
93 struct proxy *p = s->proxy;
94 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010095
96 for (tmpserv = p->srv; tmpserv != NULL;
97 tmpserv = tmpserv->next) {
98 if (tmpserv == s)
99 continue;
100 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
101 continue;
102 if (tmpserv->cookie &&
103 strcmp(tmpserv->cookie, s->cookie) == 0) {
104 ha_warning("We generated two equal cookies for two different servers.\n"
105 "Please change the secret key for '%s'.\n",
106 s->proxy->id);
107 }
108 }
109
110}
111
112void srv_set_dyncookie(struct server *s)
113{
114 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100115 char *tmpbuf;
116 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100117 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100118 size_t buffer_len;
119 int addr_len;
120 int port;
121
122 if ((s->flags & SRV_F_COOKIESET) ||
123 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
124 s->proxy->dyncookie_key == NULL)
125 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100126 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100127
128 if (s->addr.ss_family != AF_INET &&
129 s->addr.ss_family != AF_INET6)
130 return;
131 /*
132 * Buffer to calculate the cookie value.
133 * The buffer contains the secret key + the server IP address
134 * + the TCP port.
135 */
136 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
137 /*
138 * The TCP port should use only 2 bytes, but is stored in
139 * an unsigned int in struct server, so let's use 4, to be
140 * on the safe side.
141 */
142 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200143 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100144 memcpy(tmpbuf, p->dyncookie_key, key_len);
145 memcpy(&(tmpbuf[key_len]),
146 s->addr.ss_family == AF_INET ?
147 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
148 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
149 addr_len);
150 /*
151 * Make sure it's the same across all the load balancers,
152 * no matter their endianness.
153 */
154 port = htonl(s->svc_port);
155 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
156 hash_value = XXH64(tmpbuf, buffer_len, 0);
157 memprintf(&s->cookie, "%016llx", hash_value);
158 if (!s->cookie)
159 return;
160 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100161
162 /* Don't bother checking if the dyncookie is duplicated if
163 * the server is marked as "disabled", maybe it doesn't have
164 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100165 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100166 if (!(s->next_admin & SRV_ADMF_FMAINT))
167 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100168}
169
Willy Tarreau21faa912012-10-10 08:27:36 +0200170/*
171 * Registers the server keyword list <kwl> as a list of valid keywords for next
172 * parsing sessions.
173 */
174void srv_register_keywords(struct srv_kw_list *kwl)
175{
176 LIST_ADDQ(&srv_keywords.list, &kwl->list);
177}
178
179/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
180 * keyword is found with a NULL ->parse() function, then an attempt is made to
181 * find one with a valid ->parse() function. This way it is possible to declare
182 * platform-dependant, known keywords as NULL, then only declare them as valid
183 * if some options are met. Note that if the requested keyword contains an
184 * opening parenthesis, everything from this point is ignored.
185 */
186struct srv_kw *srv_find_kw(const char *kw)
187{
188 int index;
189 const char *kwend;
190 struct srv_kw_list *kwl;
191 struct srv_kw *ret = NULL;
192
193 kwend = strchr(kw, '(');
194 if (!kwend)
195 kwend = kw + strlen(kw);
196
197 list_for_each_entry(kwl, &srv_keywords.list, list) {
198 for (index = 0; kwl->kw[index].kw != NULL; index++) {
199 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
200 kwl->kw[index].kw[kwend-kw] == 0) {
201 if (kwl->kw[index].parse)
202 return &kwl->kw[index]; /* found it !*/
203 else
204 ret = &kwl->kw[index]; /* may be OK */
205 }
206 }
207 }
208 return ret;
209}
210
211/* Dumps all registered "server" keywords to the <out> string pointer. The
212 * unsupported keywords are only dumped if their supported form was not
213 * found.
214 */
215void srv_dump_kws(char **out)
216{
217 struct srv_kw_list *kwl;
218 int index;
219
220 *out = NULL;
221 list_for_each_entry(kwl, &srv_keywords.list, list) {
222 for (index = 0; kwl->kw[index].kw != NULL; index++) {
223 if (kwl->kw[index].parse ||
224 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
225 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
226 kwl->scope,
227 kwl->kw[index].kw,
228 kwl->kw[index].skip ? " <arg>" : "",
229 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
230 kwl->kw[index].parse ? "" : " (not supported)");
231 }
232 }
233 }
234}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100235
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100236/* Parse the "addr" server keyword */
237static int srv_parse_addr(char **args, int *cur_arg,
238 struct proxy *curproxy, struct server *newsrv, char **err)
239{
240 char *errmsg, *arg;
241 struct sockaddr_storage *sk;
242 int port1, port2;
243 struct protocol *proto;
244
245 errmsg = NULL;
246 arg = args[*cur_arg + 1];
247
248 if (!*arg) {
249 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
250 goto err;
251 }
252
253 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
254 if (!sk) {
255 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
256 goto err;
257 }
258
259 proto = protocol_by_family(sk->ss_family);
260 if (!proto || !proto->connect) {
261 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
262 args[*cur_arg], arg);
263 goto err;
264 }
265
266 if (port1 != port2) {
267 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
268 args[*cur_arg], arg);
269 goto err;
270 }
271
272 newsrv->check.addr = newsrv->agent.addr = *sk;
273 newsrv->flags |= SRV_F_CHECKADDR;
274 newsrv->flags |= SRV_F_AGENTADDR;
275
276 return 0;
277
278 err:
279 free(errmsg);
280 return ERR_ALERT | ERR_FATAL;
281}
282
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100283/* Parse the "agent-check" server keyword */
284static int srv_parse_agent_check(char **args, int *cur_arg,
285 struct proxy *curproxy, struct server *newsrv, char **err)
286{
287 newsrv->do_agent = 1;
288 return 0;
289}
290
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100291/* Parse the "backup" server keyword */
292static int srv_parse_backup(char **args, int *cur_arg,
293 struct proxy *curproxy, struct server *newsrv, char **err)
294{
295 newsrv->flags |= SRV_F_BACKUP;
296 return 0;
297}
298
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100299/* Parse the "check" server keyword */
300static int srv_parse_check(char **args, int *cur_arg,
301 struct proxy *curproxy, struct server *newsrv, char **err)
302{
303 newsrv->do_check = 1;
304 return 0;
305}
306
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100307/* Parse the "check-send-proxy" server keyword */
308static int srv_parse_check_send_proxy(char **args, int *cur_arg,
309 struct proxy *curproxy, struct server *newsrv, char **err)
310{
311 newsrv->check.send_proxy = 1;
312 return 0;
313}
314
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100315/* Parse the "cookie" server keyword */
316static int srv_parse_cookie(char **args, int *cur_arg,
317 struct proxy *curproxy, struct server *newsrv, char **err)
318{
319 char *arg;
320
321 arg = args[*cur_arg + 1];
322 if (!*arg) {
323 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
324 return ERR_ALERT | ERR_FATAL;
325 }
326
327 free(newsrv->cookie);
328 newsrv->cookie = strdup(arg);
329 newsrv->cklen = strlen(arg);
330 newsrv->flags |= SRV_F_COOKIESET;
331 return 0;
332}
333
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100334/* Parse the "disabled" server keyword */
335static int srv_parse_disabled(char **args, int *cur_arg,
336 struct proxy *curproxy, struct server *newsrv, char **err)
337{
Emeric Brun52a91d32017-08-31 14:41:55 +0200338 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
339 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100340 newsrv->check.state |= CHK_ST_PAUSED;
341 newsrv->check.health = 0;
342 return 0;
343}
344
345/* Parse the "enabled" server keyword */
346static int srv_parse_enabled(char **args, int *cur_arg,
347 struct proxy *curproxy, struct server *newsrv, char **err)
348{
Emeric Brun52a91d32017-08-31 14:41:55 +0200349 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
350 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100351 newsrv->check.state &= ~CHK_ST_PAUSED;
352 newsrv->check.health = newsrv->check.rise;
353 return 0;
354}
355
Willy Tarreaudff55432012-10-10 17:51:05 +0200356/* parse the "id" server keyword */
357static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
358{
359 struct eb32_node *node;
360
361 if (!*args[*cur_arg + 1]) {
362 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
363 return ERR_ALERT | ERR_FATAL;
364 }
365
366 newsrv->puid = atol(args[*cur_arg + 1]);
367 newsrv->conf.id.key = newsrv->puid;
368
369 if (newsrv->puid <= 0) {
370 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
371 return ERR_ALERT | ERR_FATAL;
372 }
373
374 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
375 if (node) {
376 struct server *target = container_of(node, struct server, conf.id);
377 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
378 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
379 target->id);
380 return ERR_ALERT | ERR_FATAL;
381 }
382
383 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200384 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200385 return 0;
386}
387
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100388/* Parse the "namespace" server keyword */
389static int srv_parse_namespace(char **args, int *cur_arg,
390 struct proxy *curproxy, struct server *newsrv, char **err)
391{
392#ifdef CONFIG_HAP_NS
393 char *arg;
394
395 arg = args[*cur_arg + 1];
396 if (!*arg) {
397 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
398 return ERR_ALERT | ERR_FATAL;
399 }
400
401 if (!strcmp(arg, "*")) {
402 /* Use the namespace associated with the connection (if present). */
403 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
404 return 0;
405 }
406
407 /*
408 * As this parser may be called several times for the same 'default-server'
409 * object, or for a new 'server' instance deriving from a 'default-server'
410 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
411 */
412 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
413
414 newsrv->netns = netns_store_lookup(arg, strlen(arg));
415 if (!newsrv->netns)
416 newsrv->netns = netns_store_insert(arg);
417
418 if (!newsrv->netns) {
419 memprintf(err, "Cannot open namespace '%s'", arg);
420 return ERR_ALERT | ERR_FATAL;
421 }
422
423 return 0;
424#else
425 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
426 return ERR_ALERT | ERR_FATAL;
427#endif
428}
429
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100430/* Parse the "no-agent-check" server keyword */
431static int srv_parse_no_agent_check(char **args, int *cur_arg,
432 struct proxy *curproxy, struct server *newsrv, char **err)
433{
434 free_check(&newsrv->agent);
435 newsrv->agent.inter = 0;
436 newsrv->agent.port = 0;
437 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
438 newsrv->do_agent = 0;
439 return 0;
440}
441
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100442/* Parse the "no-backup" server keyword */
443static int srv_parse_no_backup(char **args, int *cur_arg,
444 struct proxy *curproxy, struct server *newsrv, char **err)
445{
446 newsrv->flags &= ~SRV_F_BACKUP;
447 return 0;
448}
449
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100450/* Parse the "no-check" server keyword */
451static int srv_parse_no_check(char **args, int *cur_arg,
452 struct proxy *curproxy, struct server *newsrv, char **err)
453{
454 free_check(&newsrv->check);
455 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
456 newsrv->do_check = 0;
457 return 0;
458}
459
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100460/* Parse the "no-check-send-proxy" server keyword */
461static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
462 struct proxy *curproxy, struct server *newsrv, char **err)
463{
464 newsrv->check.send_proxy = 0;
465 return 0;
466}
467
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100468/* Disable server PROXY protocol flags. */
469static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
470{
471 srv->pp_opts &= ~flags;
472 return 0;
473}
474
475/* Parse the "no-send-proxy" server keyword */
476static int srv_parse_no_send_proxy(char **args, int *cur_arg,
477 struct proxy *curproxy, struct server *newsrv, char **err)
478{
479 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
480}
481
482/* Parse the "no-send-proxy-v2" server keyword */
483static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
484 struct proxy *curproxy, struct server *newsrv, char **err)
485{
486 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
487}
488
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100489/* Parse the "non-stick" server keyword */
490static int srv_parse_non_stick(char **args, int *cur_arg,
491 struct proxy *curproxy, struct server *newsrv, char **err)
492{
493 newsrv->flags |= SRV_F_NON_STICK;
494 return 0;
495}
496
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100497/* Enable server PROXY protocol flags. */
498static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
499{
500 srv->pp_opts |= flags;
501 return 0;
502}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200503/* parse the "proto" server keyword */
504static int srv_parse_proto(char **args, int *cur_arg,
505 struct proxy *px, struct server *newsrv, char **err)
506{
507 struct ist proto;
508
509 if (!*args[*cur_arg + 1]) {
510 memprintf(err, "'%s' : missing value", args[*cur_arg]);
511 return ERR_ALERT | ERR_FATAL;
512 }
513 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
514 newsrv->mux_proto = get_mux_proto(proto);
515 if (!newsrv->mux_proto) {
516 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
517 return ERR_ALERT | ERR_FATAL;
518 }
519 else if (!(newsrv->mux_proto->side & PROTO_SIDE_BE)) {
520 memprintf(err, "'%s' : MUX protocol '%s' cannot be used for outgoing connections",
521 args[*cur_arg], args[*cur_arg+1]);
522 return ERR_ALERT | ERR_FATAL;
523 }
524 return 0;
525}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100526
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100527/* parse the "proxy-v2-options" */
528static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
529 struct proxy *px, struct server *newsrv, char **err)
530{
531 char *p, *n;
532 for (p = args[*cur_arg+1]; p; p = n) {
533 n = strchr(p, ',');
534 if (n)
535 *n++ = '\0';
536 if (!strcmp(p, "ssl")) {
537 newsrv->pp_opts |= SRV_PP_V2_SSL;
538 } else if (!strcmp(p, "cert-cn")) {
539 newsrv->pp_opts |= SRV_PP_V2_SSL;
540 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100541 } else if (!strcmp(p, "cert-key")) {
542 newsrv->pp_opts |= SRV_PP_V2_SSL;
543 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
544 } else if (!strcmp(p, "cert-sig")) {
545 newsrv->pp_opts |= SRV_PP_V2_SSL;
546 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
547 } else if (!strcmp(p, "ssl-cipher")) {
548 newsrv->pp_opts |= SRV_PP_V2_SSL;
549 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100550 } else if (!strcmp(p, "authority")) {
551 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100552 } else if (!strcmp(p, "crc32c")) {
553 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100554 } else
555 goto fail;
556 }
557 return 0;
558 fail:
559 if (err)
560 memprintf(err, "'%s' : proxy v2 option not implemented", p);
561 return ERR_ALERT | ERR_FATAL;
562}
563
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100564/* Parse the "observe" server keyword */
565static int srv_parse_observe(char **args, int *cur_arg,
566 struct proxy *curproxy, struct server *newsrv, char **err)
567{
568 char *arg;
569
570 arg = args[*cur_arg + 1];
571 if (!*arg) {
572 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
573 return ERR_ALERT | ERR_FATAL;
574 }
575
576 if (!strcmp(arg, "none")) {
577 newsrv->observe = HANA_OBS_NONE;
578 }
579 else if (!strcmp(arg, "layer4")) {
580 newsrv->observe = HANA_OBS_LAYER4;
581 }
582 else if (!strcmp(arg, "layer7")) {
583 if (curproxy->mode != PR_MODE_HTTP) {
584 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
585 return ERR_ALERT;
586 }
587 newsrv->observe = HANA_OBS_LAYER7;
588 }
589 else {
590 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
591 "but got '%s'\n", args[*cur_arg], arg);
592 return ERR_ALERT | ERR_FATAL;
593 }
594
595 return 0;
596}
597
Frédéric Lécaille16186232017-03-14 16:42:49 +0100598/* Parse the "redir" server keyword */
599static int srv_parse_redir(char **args, int *cur_arg,
600 struct proxy *curproxy, struct server *newsrv, char **err)
601{
602 char *arg;
603
604 arg = args[*cur_arg + 1];
605 if (!*arg) {
606 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
607 return ERR_ALERT | ERR_FATAL;
608 }
609
610 free(newsrv->rdr_pfx);
611 newsrv->rdr_pfx = strdup(arg);
612 newsrv->rdr_len = strlen(arg);
613
614 return 0;
615}
616
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100617/* Parse the "send-proxy" server keyword */
618static int srv_parse_send_proxy(char **args, int *cur_arg,
619 struct proxy *curproxy, struct server *newsrv, char **err)
620{
621 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
622}
623
624/* Parse the "send-proxy-v2" server keyword */
625static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
626 struct proxy *curproxy, struct server *newsrv, char **err)
627{
628 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
629}
630
Frédéric Lécailledba97072017-03-17 15:33:50 +0100631
632/* Parse the "source" server keyword */
633static int srv_parse_source(char **args, int *cur_arg,
634 struct proxy *curproxy, struct server *newsrv, char **err)
635{
636 char *errmsg;
637 int port_low, port_high;
638 struct sockaddr_storage *sk;
639 struct protocol *proto;
640
641 errmsg = NULL;
642
643 if (!*args[*cur_arg + 1]) {
644 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
645 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
646 goto err;
647 }
648
649 /* 'sk' is statically allocated (no need to be freed). */
650 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
651 if (!sk) {
652 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
653 goto err;
654 }
655
656 proto = protocol_by_family(sk->ss_family);
657 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100658 ha_alert("'%s %s' : connect() not supported for this address family.\n",
659 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100660 goto err;
661 }
662
663 newsrv->conn_src.opts |= CO_SRC_BIND;
664 newsrv->conn_src.source_addr = *sk;
665
666 if (port_low != port_high) {
667 int i;
668
669 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100670 ha_alert("'%s' does not support port offsets (found '%s').\n",
671 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100672 goto err;
673 }
674
675 if (port_low <= 0 || port_low > 65535 ||
676 port_high <= 0 || port_high > 65535 ||
677 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100678 ha_alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100679 goto err;
680 }
681 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
682 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
683 newsrv->conn_src.sport_range->ports[i] = port_low + i;
684 }
685
686 *cur_arg += 2;
687 while (*(args[*cur_arg])) {
688 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
689#if defined(CONFIG_HAP_TRANSPARENT)
690 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100691 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
692 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100693 goto err;
694 }
695 if (!strcmp(args[*cur_arg + 1], "client")) {
696 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
697 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
698 }
699 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
700 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
701 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
702 }
703 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
704 char *name, *end;
705
706 name = args[*cur_arg + 1] + 7;
707 while (isspace(*name))
708 name++;
709
710 end = name;
711 while (*end && !isspace(*end) && *end != ',' && *end != ')')
712 end++;
713
714 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
715 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
716 free(newsrv->conn_src.bind_hdr_name);
717 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
718 newsrv->conn_src.bind_hdr_len = end - name;
719 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
720 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
721 newsrv->conn_src.bind_hdr_occ = -1;
722
723 /* now look for an occurrence number */
724 while (isspace(*end))
725 end++;
726 if (*end == ',') {
727 end++;
728 name = end;
729 if (*end == '-')
730 end++;
731 while (isdigit((int)*end))
732 end++;
733 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
734 }
735
736 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100737 ha_alert("usesrc hdr_ip(name,num) does not support negative"
738 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100739 goto err;
740 }
741 }
742 else {
743 struct sockaddr_storage *sk;
744 int port1, port2;
745
746 /* 'sk' is statically allocated (no need to be freed). */
747 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
748 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100749 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100750 goto err;
751 }
752
753 proto = protocol_by_family(sk->ss_family);
754 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100755 ha_alert("'%s %s' : connect() not supported for this address family.\n",
756 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100757 goto err;
758 }
759
760 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100761 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
762 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100763 goto err;
764 }
765 newsrv->conn_src.tproxy_addr = *sk;
766 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
767 }
768 global.last_checks |= LSTCHK_NETADM;
769 *cur_arg += 2;
770 continue;
771#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100772 ha_alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100773 goto err;
774#endif /* defined(CONFIG_HAP_TRANSPARENT) */
775 } /* "usesrc" */
776
777 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
778#ifdef SO_BINDTODEVICE
779 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100780 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100781 goto err;
782 }
783 free(newsrv->conn_src.iface_name);
784 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
785 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
786 global.last_checks |= LSTCHK_NETADM;
787#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100788 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100789 goto err;
790#endif
791 *cur_arg += 2;
792 continue;
793 }
794 /* this keyword in not an option of "source" */
795 break;
796 } /* while */
797
798 return 0;
799
800 err:
801 free(errmsg);
802 return ERR_ALERT | ERR_FATAL;
803}
804
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100805/* Parse the "stick" server keyword */
806static int srv_parse_stick(char **args, int *cur_arg,
807 struct proxy *curproxy, struct server *newsrv, char **err)
808{
809 newsrv->flags &= ~SRV_F_NON_STICK;
810 return 0;
811}
812
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100813/* Parse the "track" server keyword */
814static int srv_parse_track(char **args, int *cur_arg,
815 struct proxy *curproxy, struct server *newsrv, char **err)
816{
817 char *arg;
818
819 arg = args[*cur_arg + 1];
820 if (!*arg) {
821 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
822 return ERR_ALERT | ERR_FATAL;
823 }
824
825 free(newsrv->trackit);
826 newsrv->trackit = strdup(arg);
827
828 return 0;
829}
830
Frédéric Lécailledba97072017-03-17 15:33:50 +0100831
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200832/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200833 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200834 * shutdown.
835 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200836void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200837{
Willy Tarreau87b09662015-04-03 00:22:06 +0200838 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200839
Willy Tarreau87b09662015-04-03 00:22:06 +0200840 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
841 if (stream->srv_conn == srv)
842 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200843}
844
845/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200846 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200847 * the reason for the shutdown.
848 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200849void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200850{
851 struct server *srv;
852
853 for (srv = px->srv; srv != NULL; srv = srv->next)
854 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200855 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200856}
857
Willy Tarreaubda92272014-05-20 21:55:30 +0200858/* Appends some information to a message string related to a server going UP or
859 * DOWN. If both <forced> and <reason> are null and the server tracks another
860 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200861 * If <check> is non-null, an entire string describing the check result will be
862 * appended after a comma and a space (eg: to report some information from the
863 * check that changed the state). In the other case, the string will be built
864 * using the check results stored into the struct server if present.
865 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200866 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200867 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200868void srv_append_status(struct buffer *msg, struct server *s,
869 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200870{
Emeric Brun5a133512017-10-19 14:42:30 +0200871 short status = s->op_st_chg.status;
872 short code = s->op_st_chg.code;
873 long duration = s->op_st_chg.duration;
874 char *desc = s->op_st_chg.reason;
875
876 if (check) {
877 status = check->status;
878 code = check->code;
879 duration = check->duration;
880 desc = check->desc;
881 }
882
883 if (status != -1) {
884 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
885
886 if (status >= HCHK_STATUS_L57DATA)
887 chunk_appendf(msg, ", code: %d", code);
888
889 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200890 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200891
892 chunk_appendf(msg, ", info: \"");
893
894 chunk_initlen(&src, desc, 0, strlen(desc));
895 chunk_asciiencode(msg, &src, '"');
896
897 chunk_appendf(msg, "\"");
898 }
899
900 if (duration >= 0)
901 chunk_appendf(msg, ", check duration: %ldms", duration);
902 }
903 else if (desc && *desc) {
904 chunk_appendf(msg, ", %s", desc);
905 }
906 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200907 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200908 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200909
910 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200911 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200912 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
913 " %d sessions active, %d requeued, %d remaining in queue",
914 s->proxy->srv_act, s->proxy->srv_bck,
915 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
916 s->cur_sess, xferred, s->nbpend);
917 else
918 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
919 " %d sessions requeued, %d total in queue",
920 s->proxy->srv_act, s->proxy->srv_bck,
921 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
922 xferred, s->nbpend);
923 }
924}
925
Emeric Brun5a133512017-10-19 14:42:30 +0200926/* Marks server <s> down, regardless of its checks' statuses. The server is
927 * registered in a list to postpone the counting of the remaining servers on
928 * the proxy and transfers queued streams whenever possible to other servers at
929 * a sync point. Maintenance servers are ignored. It stores the <reason> if
930 * non-null as the reason for going down or the available data from the check
931 * struct to recompute this reason later.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200932 */
Emeric Brun5a133512017-10-19 14:42:30 +0200933void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200934{
935 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200936
Emeric Brun64cc49c2017-10-03 14:46:45 +0200937 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200938 return;
939
Emeric Brun52a91d32017-08-31 14:41:55 +0200940 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200941 *s->op_st_chg.reason = 0;
942 s->op_st_chg.status = -1;
943 if (reason) {
944 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
945 }
946 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100947 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200948 s->op_st_chg.code = check->code;
949 s->op_st_chg.status = check->status;
950 s->op_st_chg.duration = check->duration;
951 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200952
Emeric Brun9f0b4582017-10-23 14:39:51 +0200953 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100954 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200955 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100956 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200957 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +0200958
959 /* propagate changes */
960 thread_isolate();
961 srv_update_status(s);
962 thread_release();
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200963}
964
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200965/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200966 * in maintenance. The server is registered in a list to postpone the counting
967 * of the remaining servers on the proxy and tries to grab requests from the
968 * proxy at a sync point. Maintenance servers are ignored. It stores the
969 * <reason> if non-null as the reason for going down or the available data
970 * from the check struct to recompute this reason later.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200971 */
Emeric Brun5a133512017-10-19 14:42:30 +0200972void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200973{
974 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200975
Emeric Brun64cc49c2017-10-03 14:46:45 +0200976 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200977 return;
978
Emeric Brun52a91d32017-08-31 14:41:55 +0200979 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200980 return;
981
Emeric Brun52a91d32017-08-31 14:41:55 +0200982 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200983 *s->op_st_chg.reason = 0;
984 s->op_st_chg.status = -1;
985 if (reason) {
986 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
987 }
988 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100989 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200990 s->op_st_chg.code = check->code;
991 s->op_st_chg.status = check->status;
992 s->op_st_chg.duration = check->duration;
993 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200994
Emeric Brun64cc49c2017-10-03 14:46:45 +0200995 if (s->slowstart <= 0)
996 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200997
Emeric Brun9f0b4582017-10-23 14:39:51 +0200998 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100999 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001000 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001001 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001002 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001003
1004 /* propagate changes */
1005 thread_isolate();
1006 srv_update_status(s);
1007 thread_release();
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001008}
1009
Willy Tarreau8eb77842014-05-21 13:54:57 +02001010/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001011 * isn't in maintenance. The server is registered in a list to postpone the
1012 * counting of the remaining servers on the proxy and tries to grab requests
1013 * from the proxy. Maintenance servers are ignored. It stores the
1014 * <reason> if non-null as the reason for going down or the available data
1015 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001016 * up. Note that it makes use of the trash to build the log strings, so <reason>
1017 * must not be placed there.
1018 */
Emeric Brun5a133512017-10-19 14:42:30 +02001019void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001020{
1021 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001022
Emeric Brun64cc49c2017-10-03 14:46:45 +02001023 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001024 return;
1025
Emeric Brun52a91d32017-08-31 14:41:55 +02001026 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001027 return;
1028
Emeric Brun52a91d32017-08-31 14:41:55 +02001029 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001030 *s->op_st_chg.reason = 0;
1031 s->op_st_chg.status = -1;
1032 if (reason) {
1033 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1034 }
1035 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001036 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001037 s->op_st_chg.code = check->code;
1038 s->op_st_chg.status = check->status;
1039 s->op_st_chg.duration = check->duration;
1040 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001041
Emeric Brun9f0b4582017-10-23 14:39:51 +02001042 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001043 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001044 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001045 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001046 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001047
1048 /* propagate changes */
1049 thread_isolate();
1050 srv_update_status(s);
1051 thread_release();
Willy Tarreau8eb77842014-05-21 13:54:57 +02001052}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001053
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001054/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1055 * enforce either maint mode or drain mode. It is not allowed to set more than
1056 * one flag at once. The equivalent "inherited" flag is propagated to all
1057 * tracking servers. Maintenance mode disables health checks (but not agent
1058 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001059 * is done. If <cause> is non-null, it will be displayed at the end of the log
1060 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001061 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001062void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001063{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001064 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001065
1066 if (!mode)
1067 return;
1068
1069 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001070 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001071 return;
1072
Emeric Brun52a91d32017-08-31 14:41:55 +02001073 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001074 if (cause)
1075 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1076
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001077 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001078 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1079 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001080 goto end;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001081
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001082 /* compute the inherited flag to propagate */
1083 if (mode & SRV_ADMF_MAINT)
1084 mode = SRV_ADMF_IMAINT;
1085 else if (mode & SRV_ADMF_DRAIN)
1086 mode = SRV_ADMF_IDRAIN;
1087
Emeric Brun9f0b4582017-10-23 14:39:51 +02001088 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001089 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001090 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001091 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001092 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001093
1094 end:
1095 /* propagate changes */
1096 thread_isolate();
1097 srv_update_status(s);
1098 thread_release();
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001099}
1100
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001101/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1102 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1103 * than one flag at once. The equivalent "inherited" flag is propagated to all
1104 * tracking servers. Leaving maintenance mode re-enables health checks. When
1105 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001106 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001107void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001108{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001109 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001110
1111 if (!mode)
1112 return;
1113
1114 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001115 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001116 return;
1117
Emeric Brun52a91d32017-08-31 14:41:55 +02001118 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001119
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001120 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001121 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1122 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001123 goto end;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001124
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001125 if (mode & SRV_ADMF_MAINT)
1126 mode = SRV_ADMF_IMAINT;
1127 else if (mode & SRV_ADMF_DRAIN)
1128 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001129
Emeric Brun9f0b4582017-10-23 14:39:51 +02001130 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001131 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001132 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001133 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001134 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001135
1136 end:
1137 /* propagate changes */
1138 thread_isolate();
1139 srv_update_status(s);
1140 thread_release();
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001141}
1142
Willy Tarreau757478e2016-11-03 19:22:19 +01001143/* principle: propagate maint and drain to tracking servers. This is useful
1144 * upon startup so that inherited states are correct.
1145 */
1146static void srv_propagate_admin_state(struct server *srv)
1147{
1148 struct server *srv2;
1149
1150 if (!srv->trackers)
1151 return;
1152
1153 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001154 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001155 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001156 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001157
Emeric Brun52a91d32017-08-31 14:41:55 +02001158 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001159 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001160 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001161 }
1162}
1163
1164/* Compute and propagate the admin states for all servers in proxy <px>.
1165 * Only servers *not* tracking another one are considered, because other
1166 * ones will be handled when the server they track is visited.
1167 */
1168void srv_compute_all_admin_states(struct proxy *px)
1169{
1170 struct server *srv;
1171
1172 for (srv = px->srv; srv; srv = srv->next) {
1173 if (srv->track)
1174 continue;
1175 srv_propagate_admin_state(srv);
1176 }
1177}
1178
Willy Tarreaudff55432012-10-10 17:51:05 +02001179/* Note: must not be declared <const> as its list will be overwritten.
1180 * Please take care of keeping this list alphabetically sorted, doing so helps
1181 * all code contributors.
1182 * Optional keywords are also declared with a NULL ->parse() function so that
1183 * the config parser can report an appropriate error when a known keyword was
1184 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001185 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001186 */
1187static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001188 { "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 +01001189 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001190 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001191 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001192 { "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 +01001193 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001194 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1195 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001196 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001197 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001198 { "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 +01001199 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001200 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001201 { "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 +01001202 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1203 { "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 +01001204 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001205 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001206 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001207 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1 }, /* options for send-proxy-v2 */
Frédéric Lécaille16186232017-03-14 16:42:49 +01001208 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001209 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1210 { "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 +02001211 { "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 +01001212 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001213 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001214 { NULL, NULL, 0 },
1215}};
1216
1217__attribute__((constructor))
1218static void __listener_init(void)
1219{
1220 srv_register_keywords(&srv_kws);
1221}
1222
Willy Tarreau004e0452013-11-21 11:22:01 +01001223/* Recomputes the server's eweight based on its state, uweight, the current time,
1224 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001225 * state is automatically disabled if the time is elapsed. If <must_update> is
1226 * not zero, the update will be propagated immediately.
Willy Tarreau004e0452013-11-21 11:22:01 +01001227 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001228void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001229{
1230 struct proxy *px = sv->proxy;
1231 unsigned w;
1232
1233 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1234 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001235 if (sv->next_state == SRV_ST_STARTING)
1236 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001237 }
1238
1239 /* We must take care of not pushing the server to full throttle during slow starts.
1240 * It must also start immediately, at least at the minimal step when leaving maintenance.
1241 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001242 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001243 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1244 else
1245 w = px->lbprm.wdiv;
1246
Emeric Brun52a91d32017-08-31 14:41:55 +02001247 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001248
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001249 /* propagate changes only if needed (i.e. not recursively) */
1250 if (must_update) {
1251 thread_isolate();
1252 srv_update_status(sv);
1253 thread_release();
1254 }
Willy Tarreau004e0452013-11-21 11:22:01 +01001255}
1256
Willy Tarreaubaaee002006-06-26 02:48:02 +02001257/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001258 * Parses weight_str and configures sv accordingly.
1259 * Returns NULL on success, error message string otherwise.
1260 */
1261const char *server_parse_weight_change_request(struct server *sv,
1262 const char *weight_str)
1263{
1264 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001265 long int w;
1266 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001267
1268 px = sv->proxy;
1269
1270 /* if the weight is terminated with '%', it is set relative to
1271 * the initial weight, otherwise it is absolute.
1272 */
1273 if (!*weight_str)
1274 return "Require <weight> or <weight%>.\n";
1275
Simon Hormanb796afa2013-02-12 10:45:53 +09001276 w = strtol(weight_str, &end, 10);
1277 if (end == weight_str)
1278 return "Empty weight string empty or preceded by garbage";
1279 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001280 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001281 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001282 /* Avoid integer overflow */
1283 if (w > 25600)
1284 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001285 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001286 if (w > 256)
1287 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001288 }
1289 else if (w < 0 || w > 256)
1290 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001291 else if (end[0] != '\0')
1292 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001293
1294 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1295 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1296
1297 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001298 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001299
1300 return NULL;
1301}
1302
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001303/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001304 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1305 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001306 * Returns:
1307 * - error string on error
1308 * - NULL on success
1309 */
1310const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001311 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001312{
1313 unsigned char ip[INET6_ADDRSTRLEN];
1314
1315 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001316 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001317 return NULL;
1318 }
1319 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001320 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001321 return NULL;
1322 }
1323
1324 return "Could not understand IP address format.\n";
1325}
1326
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001327const char *server_parse_maxconn_change_request(struct server *sv,
1328 const char *maxconn_str)
1329{
1330 long int v;
1331 char *end;
1332
1333 if (!*maxconn_str)
1334 return "Require <maxconn>.\n";
1335
1336 v = strtol(maxconn_str, &end, 10);
1337 if (end == maxconn_str)
1338 return "maxconn string empty or preceded by garbage";
1339 else if (end[0] != '\0')
1340 return "Trailing garbage in maxconn string";
1341
1342 if (sv->maxconn == sv->minconn) { // static maxconn
1343 sv->maxconn = sv->minconn = v;
1344 } else { // dynamic maxconn
1345 sv->maxconn = v;
1346 }
1347
1348 if (may_dequeue_tasks(sv, sv->proxy))
1349 process_srv_queue(sv);
1350
1351 return NULL;
1352}
1353
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001354#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001355static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1356 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001357{
1358 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001359 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001360 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001361 NULL,
1362 };
1363
1364 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001365 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001366
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001367 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1368}
1369
1370static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1371{
1372 struct sample_expr *expr;
1373
1374 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 +01001375 if (!expr) {
1376 memprintf(err, "error detected while parsing sni expression : %s", *err);
1377 return ERR_ALERT | ERR_FATAL;
1378 }
1379
1380 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1381 memprintf(err, "error detected while parsing sni expression : "
1382 " fetch method '%s' extracts information from '%s', "
1383 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001384 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001385 return ERR_ALERT | ERR_FATAL;
1386 }
1387
1388 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1389 release_sample_expr(newsrv->ssl_ctx.sni);
1390 newsrv->ssl_ctx.sni = expr;
1391
1392 return 0;
1393}
1394#endif
1395
1396static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1397{
1398 if (err && *err) {
1399 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001400 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001401 }
1402 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001403 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1404 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001405}
1406
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001407static void srv_conn_src_sport_range_cpy(struct server *srv,
1408 struct server *src)
1409{
1410 int range_sz;
1411
1412 range_sz = src->conn_src.sport_range->size;
1413 if (range_sz > 0) {
1414 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1415 if (srv->conn_src.sport_range != NULL) {
1416 int i;
1417
1418 for (i = 0; i < range_sz; i++) {
1419 srv->conn_src.sport_range->ports[i] =
1420 src->conn_src.sport_range->ports[i];
1421 }
1422 }
1423 }
1424}
1425
1426/*
1427 * Copy <src> server connection source settings to <srv> server everything needed.
1428 */
1429static void srv_conn_src_cpy(struct server *srv, struct server *src)
1430{
1431 srv->conn_src.opts = src->conn_src.opts;
1432 srv->conn_src.source_addr = src->conn_src.source_addr;
1433
1434 /* Source port range copy. */
1435 if (src->conn_src.sport_range != NULL)
1436 srv_conn_src_sport_range_cpy(srv, src);
1437
1438#ifdef CONFIG_HAP_TRANSPARENT
1439 if (src->conn_src.bind_hdr_name != NULL) {
1440 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1441 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1442 }
1443 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1444 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1445#endif
1446 if (src->conn_src.iface_name != NULL)
1447 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1448}
1449
1450/*
1451 * Copy <src> server SSL settings to <srv> server allocating
1452 * everything needed.
1453 */
1454#if defined(USE_OPENSSL)
1455static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1456{
1457 if (src->ssl_ctx.ca_file != NULL)
1458 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1459 if (src->ssl_ctx.crl_file != NULL)
1460 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1461 if (src->ssl_ctx.client_crt != NULL)
1462 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1463
1464 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1465
1466 if (src->ssl_ctx.verify_host != NULL)
1467 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1468 if (src->ssl_ctx.ciphers != NULL)
1469 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1470 if (src->sni_expr != NULL)
1471 srv->sni_expr = strdup(src->sni_expr);
1472}
1473#endif
1474
1475/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001476 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001477 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001478 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001479 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001480static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001481{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001482 char *hostname_dn;
1483 int hostname_len, hostname_dn_len;
1484
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001485 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001486 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001487
Christopher Faulet67957bd2017-09-27 11:00:59 +02001488 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001489 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001490 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1491 hostname_dn, trash.size);
1492 if (hostname_dn_len == -1)
1493 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001494
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001495
Christopher Faulet67957bd2017-09-27 11:00:59 +02001496 free(srv->hostname);
1497 free(srv->hostname_dn);
1498 srv->hostname = strdup(hostname);
1499 srv->hostname_dn = strdup(hostname_dn);
1500 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001501 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001502 goto err;
1503
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001504 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001505
1506 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001507 free(srv->hostname); srv->hostname = NULL;
1508 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001509 return -1;
1510}
1511
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001512/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001513 * Copy <src> server settings to <srv> server allocating
1514 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001515 * This function is not supposed to be called at any time, but only
1516 * during server settings parsing or during server allocations from
1517 * a server template, and just after having calloc()'ed a new server.
1518 * So, <src> may only be a default server (when parsing server settings)
1519 * or a server template (during server allocations from a server template).
1520 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1521 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001522 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001523static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001524{
1525 /* Connection source settings copy */
1526 srv_conn_src_cpy(srv, src);
1527
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001528 if (srv_tmpl) {
1529 srv->addr = src->addr;
1530 srv->svc_port = src->svc_port;
1531 }
1532
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001533 srv->pp_opts = src->pp_opts;
1534 if (src->rdr_pfx != NULL) {
1535 srv->rdr_pfx = strdup(src->rdr_pfx);
1536 srv->rdr_len = src->rdr_len;
1537 }
1538 if (src->cookie != NULL) {
1539 srv->cookie = strdup(src->cookie);
1540 srv->cklen = src->cklen;
1541 }
1542 srv->use_ssl = src->use_ssl;
1543 srv->check.addr = srv->agent.addr = src->check.addr;
1544 srv->check.use_ssl = src->check.use_ssl;
1545 srv->check.port = src->check.port;
1546 /* Note: 'flags' field has potentially been already initialized. */
1547 srv->flags |= src->flags;
1548 srv->do_check = src->do_check;
1549 srv->do_agent = src->do_agent;
1550 if (srv->check.port)
1551 srv->flags |= SRV_F_CHECKPORT;
1552 srv->check.inter = src->check.inter;
1553 srv->check.fastinter = src->check.fastinter;
1554 srv->check.downinter = src->check.downinter;
1555 srv->agent.use_ssl = src->agent.use_ssl;
1556 srv->agent.port = src->agent.port;
1557 if (src->agent.send_string != NULL)
1558 srv->agent.send_string = strdup(src->agent.send_string);
1559 srv->agent.send_string_len = src->agent.send_string_len;
1560 srv->agent.inter = src->agent.inter;
1561 srv->agent.fastinter = src->agent.fastinter;
1562 srv->agent.downinter = src->agent.downinter;
1563 srv->maxqueue = src->maxqueue;
1564 srv->minconn = src->minconn;
1565 srv->maxconn = src->maxconn;
1566 srv->slowstart = src->slowstart;
1567 srv->observe = src->observe;
1568 srv->onerror = src->onerror;
1569 srv->onmarkeddown = src->onmarkeddown;
1570 srv->onmarkedup = src->onmarkedup;
1571 if (src->trackit != NULL)
1572 srv->trackit = strdup(src->trackit);
1573 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1574 srv->uweight = srv->iweight = src->iweight;
1575
1576 srv->check.send_proxy = src->check.send_proxy;
1577 /* health: up, but will fall down at first failure */
1578 srv->check.rise = srv->check.health = src->check.rise;
1579 srv->check.fall = src->check.fall;
1580
1581 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001582 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1583 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1584 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001585 srv->check.state |= CHK_ST_PAUSED;
1586 srv->check.health = 0;
1587 }
1588
1589 /* health: up but will fall down at first failure */
1590 srv->agent.rise = srv->agent.health = src->agent.rise;
1591 srv->agent.fall = src->agent.fall;
1592
1593 if (src->resolvers_id != NULL)
1594 srv->resolvers_id = strdup(src->resolvers_id);
1595 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001596 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001597 if (srv->dns_opts.family_prio == AF_UNSPEC)
1598 srv->dns_opts.family_prio = AF_INET6;
1599 memcpy(srv->dns_opts.pref_net,
1600 src->dns_opts.pref_net,
1601 sizeof srv->dns_opts.pref_net);
1602 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1603
1604 srv->init_addr_methods = src->init_addr_methods;
1605 srv->init_addr = src->init_addr;
1606#if defined(USE_OPENSSL)
1607 srv_ssl_settings_cpy(srv, src);
1608#endif
1609#ifdef TCP_USER_TIMEOUT
1610 srv->tcp_ut = src->tcp_ut;
1611#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001612 srv->mux_proto = src->mux_proto;
1613
Olivier Houchard8da5f982017-08-04 18:35:36 +02001614 if (srv_tmpl)
1615 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001616}
1617
1618static struct server *new_server(struct proxy *proxy)
1619{
1620 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001621 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001622
1623 srv = calloc(1, sizeof *srv);
1624 if (!srv)
1625 return NULL;
1626
1627 srv->obj_type = OBJ_TYPE_SERVER;
1628 srv->proxy = proxy;
1629 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001630 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001631
1632 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1633 goto free_srv;
1634 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1635 goto free_priv_conns;
1636 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1637 goto free_idle_conns;
1638
1639 for (i = 0; i < global.nbthread; i++) {
1640 LIST_INIT(&srv->priv_conns[i]);
1641 LIST_INIT(&srv->idle_conns[i]);
1642 LIST_INIT(&srv->safe_conns[i]);
1643 }
1644
Emeric Brun52a91d32017-08-31 14:41:55 +02001645 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001646 srv->last_change = now.tv_sec;
1647
1648 srv->check.status = HCHK_STATUS_INI;
1649 srv->check.server = srv;
1650 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1651
1652 srv->agent.status = HCHK_STATUS_INI;
1653 srv->agent.server = srv;
1654 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1655
1656 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001657
1658 free_idle_conns:
1659 free(srv->idle_conns);
1660 free_priv_conns:
1661 free(srv->priv_conns);
1662 free_srv:
1663 free(srv);
1664 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001665}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001666
1667/*
1668 * Validate <srv> server health-check settings.
1669 * Returns 0 if everything is OK, -1 if not.
1670 */
1671static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1672{
1673 struct tcpcheck_rule *r = NULL;
1674 struct list *l;
1675
1676 /*
1677 * We need at least a service port, a check port or the first tcp-check rule must
1678 * be a 'connect' one when checking an IPv4/IPv6 server.
1679 */
1680 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1681 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1682 return 0;
1683
1684 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1685 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001686 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1687 "Check has been disabled.\n",
1688 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001689 return -1;
1690 }
1691
1692 /* search the first action (connect / send / expect) in the list */
1693 l = &srv->proxy->tcpcheck_rules;
1694 list_for_each_entry(r, l, list) {
1695 if (r->action != TCPCHK_ACT_COMMENT)
1696 break;
1697 }
1698
1699 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001700 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1701 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1702 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001703 return -1;
1704 }
1705
1706 /* scan the tcp-check ruleset to ensure a port has been configured */
1707 l = &srv->proxy->tcpcheck_rules;
1708 list_for_each_entry(r, l, list) {
1709 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001710 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1711 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1712 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001713 return -1;
1714 }
1715 }
1716
1717 return 0;
1718}
1719
1720/*
1721 * Initialize <srv> health-check structure.
1722 * Returns the error string in case of memory allocation failure, NULL if not.
1723 */
1724static const char *do_health_check_init(struct server *srv, int check_type, int state)
1725{
1726 const char *ret;
1727
1728 if (!srv->do_check)
1729 return NULL;
1730
1731 ret = init_check(&srv->check, check_type);
1732 if (ret)
1733 return ret;
1734
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001735 srv->check.state |= state;
1736 global.maxsock++;
1737
1738 return NULL;
1739}
1740
1741static int server_health_check_init(const char *file, int linenum,
1742 struct server *srv, struct proxy *curproxy)
1743{
1744 const char *ret;
1745
1746 if (!srv->do_check)
1747 return 0;
1748
1749 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001750 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1751 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001752 return ERR_ALERT | ERR_FATAL;
1753 }
1754
1755 if (server_healthcheck_validate(file, linenum, srv) < 0)
1756 return ERR_ALERT | ERR_ABORT;
1757
1758 /* note: check type will be set during the config review phase */
1759 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1760 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001761 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001762 return ERR_ALERT | ERR_ABORT;
1763 }
1764
1765 return 0;
1766}
1767
1768/*
1769 * Initialize <srv> agent check structure.
1770 * Returns the error string in case of memory allocation failure, NULL if not.
1771 */
1772static const char *do_server_agent_check_init(struct server *srv, int state)
1773{
1774 const char *ret;
1775
1776 if (!srv->do_agent)
1777 return NULL;
1778
1779 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1780 if (ret)
1781 return ret;
1782
1783 if (!srv->agent.inter)
1784 srv->agent.inter = srv->check.inter;
1785
1786 srv->agent.state |= state;
1787 global.maxsock++;
1788
1789 return NULL;
1790}
1791
1792static int server_agent_check_init(const char *file, int linenum,
1793 struct server *srv, struct proxy *curproxy)
1794{
1795 const char *ret;
1796
1797 if (!srv->do_agent)
1798 return 0;
1799
1800 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001801 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001802 file, linenum, srv->id);
1803 return ERR_ALERT | ERR_FATAL;
1804 }
1805
1806 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1807 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001808 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001809 return ERR_ALERT | ERR_ABORT;
1810 }
1811
1812 return 0;
1813}
1814
1815#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1816static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1817 struct server *srv, struct proxy *proxy)
1818{
1819 int ret;
1820 char *err = NULL;
1821
1822 if (!srv->sni_expr)
1823 return 0;
1824
1825 ret = server_parse_sni_expr(srv, proxy, &err);
1826 if (!ret)
1827 return 0;
1828
1829 display_parser_err(file, linenum, args, cur_arg, &err);
1830 free(err);
1831
1832 return ret;
1833}
1834#endif
1835
1836/*
1837 * Server initializations finalization.
1838 * Initialize health check, agent check and SNI expression if enabled.
1839 * Must not be called for a default server instance.
1840 */
1841static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1842 struct server *srv, struct proxy *px)
1843{
1844 int ret;
1845
1846 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1847 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1848 return ret;
1849 }
1850
1851#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1852 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1853 return ret;
1854#endif
1855
1856 if (srv->flags & SRV_F_BACKUP)
1857 px->srv_bck++;
1858 else
1859 px->srv_act++;
1860 srv_lb_commit_status(srv);
1861
1862 return 0;
1863}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001864
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001865/*
1866 * Parse as much as possible such a range string argument: low[-high]
1867 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1868 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1869 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1870 * Returns 0 if succeeded, -1 if not.
1871 */
1872static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1873{
1874 char *nb_high_arg;
1875
1876 *nb_high = 0;
1877 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001878 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001879
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001880 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001881 *nb_high_arg++ = '\0';
1882 *nb_high = atoi(nb_high_arg);
1883 }
1884 else {
1885 *nb_high += *nb_low;
1886 *nb_low = 1;
1887 }
1888
1889 if (*nb_low < 0 || *nb_high < *nb_low)
1890 return -1;
1891
1892 return 0;
1893}
1894
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001895static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1896{
1897 chunk_printf(&trash, "%s%d", prefix, nb);
1898 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001899 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001900}
1901
1902/*
1903 * Initialize as much as possible servers from <srv> server template.
1904 * Note that a server template is a special server with
1905 * a few different parameters than a server which has
1906 * been parsed mostly the same way as a server.
1907 * Returns the number of servers succesfully allocated,
1908 * 'srv' template included.
1909 */
1910static int server_template_init(struct server *srv, struct proxy *px)
1911{
1912 int i;
1913 struct server *newsrv;
1914
1915 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1916 int check_init_state;
1917 int agent_init_state;
1918
1919 newsrv = new_server(px);
1920 if (!newsrv)
1921 goto err;
1922
1923 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001924 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001925#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1926 if (newsrv->sni_expr) {
1927 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1928 if (!newsrv->ssl_ctx.sni)
1929 goto err;
1930 }
1931#endif
1932 /* Set this new server ID. */
1933 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1934
1935 /* Initial checks states. */
1936 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1937 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1938
1939 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1940 do_server_agent_check_init(newsrv, agent_init_state))
1941 goto err;
1942
1943 /* Linked backwards first. This will be restablished after parsing. */
1944 newsrv->next = px->srv;
1945 px->srv = newsrv;
1946 }
1947 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1948
1949 return i - srv->tmpl_info.nb_low;
1950
1951 err:
1952 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1953 if (newsrv) {
1954#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1955 release_sample_expr(newsrv->ssl_ctx.sni);
1956#endif
1957 free_check(&newsrv->agent);
1958 free_check(&newsrv->check);
1959 }
1960 free(newsrv);
1961 return i - srv->tmpl_info.nb_low;
1962}
1963
Willy Tarreau272adea2014-03-31 10:39:59 +02001964int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1965{
1966 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001967 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001968 char *errmsg = NULL;
1969 int err_code = 0;
1970 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001971 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001972
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001973 if (!strcmp(args[0], "server") ||
1974 !strcmp(args[0], "default-server") ||
1975 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001976 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001977 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001978 int srv = !defsrv && !strcmp(args[0], "server");
1979 int srv_tmpl = !defsrv && !srv;
1980 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001981
1982 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001983 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001984 err_code |= ERR_ALERT | ERR_FATAL;
1985 goto out;
1986 }
1987 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02001988 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02001989
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001990 /* There is no mandatory first arguments for default server. */
1991 if (srv) {
1992 if (!*args[2]) {
1993 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001994 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001995 file, linenum, args[0]);
1996 err_code |= ERR_ALERT | ERR_FATAL;
1997 goto out;
1998 }
1999
2000 err = invalid_char(args[1]);
2001 }
2002 else if (srv_tmpl) {
2003 if (!*args[3]) {
2004 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002005 ha_alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002006 file, linenum, args[0]);
2007 err_code |= ERR_ALERT | ERR_FATAL;
2008 goto out;
2009 }
2010
2011 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002012 }
2013
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002014 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002015 ha_alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002016 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002017 err_code |= ERR_ALERT | ERR_FATAL;
2018 goto out;
2019 }
2020
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002021 cur_arg = 2;
2022 if (srv_tmpl) {
2023 /* Parse server-template <nb | range> arg. */
2024 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002025 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002026 file, linenum, args[0], args[cur_arg]);
2027 err_code |= ERR_ALERT | ERR_FATAL;
2028 goto out;
2029 }
2030 cur_arg++;
2031 }
2032
Willy Tarreau272adea2014-03-31 10:39:59 +02002033 if (!defsrv) {
2034 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002035 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002036 struct protocol *proto;
2037
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002038 newsrv = new_server(curproxy);
2039 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002040 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002041 err_code |= ERR_ALERT | ERR_ABORT;
2042 goto out;
2043 }
2044
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002045 if (srv_tmpl) {
2046 newsrv->tmpl_info.nb_low = tmpl_range_low;
2047 newsrv->tmpl_info.nb_high = tmpl_range_high;
2048 }
2049
Willy Tarreau272adea2014-03-31 10:39:59 +02002050 /* the servers are linked backwards first */
2051 newsrv->next = curproxy->srv;
2052 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002053 newsrv->conf.file = strdup(file);
2054 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002055 /* Note: for a server template, its id is its prefix.
2056 * This is a temporary id which will be used for server allocations to come
2057 * after parsing.
2058 */
2059 if (srv)
2060 newsrv->id = strdup(args[1]);
2061 else
2062 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002063
2064 /* several ways to check the port component :
2065 * - IP => port=+0, relative (IPv4 only)
2066 * - IP: => port=+0, relative
2067 * - IP:N => port=N, absolute
2068 * - IP:+N => port=+N, relative
2069 * - IP:-N => port=-N, relative
2070 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002071 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002072 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002073 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002074 err_code |= ERR_ALERT | ERR_FATAL;
2075 goto out;
2076 }
2077
2078 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002079 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002080 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002081 file, linenum, args[0], args[1]);
2082 err_code |= ERR_ALERT | ERR_FATAL;
2083 goto out;
2084 }
2085
2086 if (!port1 || !port2) {
2087 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002088 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002089 }
2090 else if (port1 != port2) {
2091 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002092 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002093 file, linenum, args[0], args[1], args[2]);
2094 err_code |= ERR_ALERT | ERR_FATAL;
2095 goto out;
2096 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002097
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002098 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002099 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002100 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002101 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002102 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2103 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2104 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002105 err_code |= ERR_ALERT | ERR_FATAL;
2106 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002107 }
2108 }
2109 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002110 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002111 file, linenum, newsrv->id);
2112 err_code |= ERR_ALERT | ERR_FATAL;
2113 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002114 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002115 }
2116
Willy Tarreau272adea2014-03-31 10:39:59 +02002117 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002118 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002119
Olivier Houchard8da5f982017-08-04 18:35:36 +02002120 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002121 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002122 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002123 err_code |= ERR_ALERT | ERR_FATAL;
2124 goto out;
2125 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002126
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002127 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002128 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002129 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002130 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002131 } else {
2132 newsrv = &curproxy->defsrv;
2133 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002134 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002135 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002136 }
2137
2138 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002139 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002140 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2141 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002142 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002143 file, linenum, *err, newsrv->id);
2144 err_code |= ERR_ALERT | ERR_FATAL;
2145 goto out;
2146 }
2147 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002148 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002149 file, linenum, val, args[cur_arg], newsrv->id);
2150 err_code |= ERR_ALERT | ERR_FATAL;
2151 goto out;
2152 }
2153 newsrv->agent.inter = val;
2154 cur_arg += 2;
2155 }
Misiekea849332017-01-09 09:39:51 +01002156 else if (!strcmp(args[cur_arg], "agent-addr")) {
2157 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002158 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002159 goto out;
2160 }
2161
2162 cur_arg += 2;
2163 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002164 else if (!strcmp(args[cur_arg], "agent-port")) {
2165 global.maxsock++;
2166 newsrv->agent.port = atol(args[cur_arg + 1]);
2167 cur_arg += 2;
2168 }
James Brown55f9ff12015-10-21 18:19:05 -07002169 else if (!strcmp(args[cur_arg], "agent-send")) {
2170 global.maxsock++;
2171 free(newsrv->agent.send_string);
2172 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2173 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2174 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2175 cur_arg += 2;
2176 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002177 else if (!strcmp(args[cur_arg], "init-addr")) {
2178 char *p, *end;
2179 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002180 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002181
2182 newsrv->init_addr_methods = 0;
2183 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2184
2185 for (p = args[cur_arg + 1]; *p; p = end) {
2186 /* cut on next comma */
2187 for (end = p; *end && *end != ','; end++);
2188 if (*end)
2189 *(end++) = 0;
2190
Willy Tarreau4310d362016-11-02 15:05:56 +01002191 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002192 if (!strcmp(p, "libc")) {
2193 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2194 }
2195 else if (!strcmp(p, "last")) {
2196 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2197 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002198 else if (!strcmp(p, "none")) {
2199 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2200 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002201 else if (str2ip2(p, &sa, 0)) {
2202 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002203 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002204 file, linenum, args[cur_arg], p);
2205 err_code |= ERR_ALERT | ERR_FATAL;
2206 goto out;
2207 }
2208 newsrv->init_addr = sa;
2209 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2210 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002211 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002212 ha_alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002213 file, linenum, args[cur_arg], p);
2214 err_code |= ERR_ALERT | ERR_FATAL;
2215 goto out;
2216 }
2217 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002218 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002219 file, linenum, args[cur_arg], p);
2220 err_code |= ERR_ALERT | ERR_FATAL;
2221 goto out;
2222 }
2223 }
2224 cur_arg += 2;
2225 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002226 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002227 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002228 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2229 cur_arg += 2;
2230 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002231 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2232 char *p, *end;
2233
2234 for (p = args[cur_arg + 1]; *p; p = end) {
2235 /* cut on next comma */
2236 for (end = p; *end && *end != ','; end++);
2237 if (*end)
2238 *(end++) = 0;
2239
2240 if (!strcmp(p, "allow-dup-ip")) {
2241 newsrv->dns_opts.accept_duplicate_ip = 1;
2242 }
2243 else if (!strcmp(p, "prevent-dup-ip")) {
2244 newsrv->dns_opts.accept_duplicate_ip = 0;
2245 }
2246 else {
2247 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2248 file, linenum, args[cur_arg], p);
2249 err_code |= ERR_ALERT | ERR_FATAL;
2250 goto out;
2251 }
2252 }
2253
2254 cur_arg += 2;
2255 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002256 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2257 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002258 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002259 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002260 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002261 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002262 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002263 file, linenum, args[cur_arg]);
2264 err_code |= ERR_ALERT | ERR_FATAL;
2265 goto out;
2266 }
2267 cur_arg += 2;
2268 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002269 else if (!strcmp(args[cur_arg], "resolve-net")) {
2270 char *p, *e;
2271 unsigned char mask;
2272 struct dns_options *opt;
2273
2274 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002275 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002276 file, linenum, args[cur_arg]);
2277 err_code |= ERR_ALERT | ERR_FATAL;
2278 goto out;
2279 }
2280
2281 opt = &newsrv->dns_opts;
2282
2283 /* Split arguments by comma, and convert it from ipv4 or ipv6
2284 * string network in in_addr or in6_addr.
2285 */
2286 p = args[cur_arg + 1];
2287 e = p;
2288 while (*p != '\0') {
2289 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002290 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002291 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002292 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2293 err_code |= ERR_ALERT | ERR_FATAL;
2294 goto out;
2295 }
2296 /* look for end or comma. */
2297 while (*e != ',' && *e != '\0')
2298 e++;
2299 if (*e == ',') {
2300 *e = '\0';
2301 e++;
2302 }
2303 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2304 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2305 /* Try to convert input string from ipv4 or ipv6 network. */
2306 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2307 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2308 &mask)) {
2309 /* Try to convert input string from ipv6 network. */
2310 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2311 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2312 } else {
2313 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002314 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002315 file, linenum, args[cur_arg], p);
2316 err_code |= ERR_ALERT | ERR_FATAL;
2317 goto out;
2318 }
2319 opt->pref_net_nb++;
2320 p = e;
2321 }
2322
2323 cur_arg += 2;
2324 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002325 else if (!strcmp(args[cur_arg], "rise")) {
2326 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002327 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002328 file, linenum, args[cur_arg]);
2329 err_code |= ERR_ALERT | ERR_FATAL;
2330 goto out;
2331 }
2332
2333 newsrv->check.rise = atol(args[cur_arg + 1]);
2334 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002335 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002336 file, linenum, args[cur_arg]);
2337 err_code |= ERR_ALERT | ERR_FATAL;
2338 goto out;
2339 }
2340
2341 if (newsrv->check.health)
2342 newsrv->check.health = newsrv->check.rise;
2343 cur_arg += 2;
2344 }
2345 else if (!strcmp(args[cur_arg], "fall")) {
2346 newsrv->check.fall = atol(args[cur_arg + 1]);
2347
2348 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002349 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002350 file, linenum, args[cur_arg]);
2351 err_code |= ERR_ALERT | ERR_FATAL;
2352 goto out;
2353 }
2354
2355 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002356 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002357 file, linenum, args[cur_arg]);
2358 err_code |= ERR_ALERT | ERR_FATAL;
2359 goto out;
2360 }
2361
2362 cur_arg += 2;
2363 }
2364 else if (!strcmp(args[cur_arg], "inter")) {
2365 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2366 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002367 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002368 file, linenum, *err, newsrv->id);
2369 err_code |= ERR_ALERT | ERR_FATAL;
2370 goto out;
2371 }
2372 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002373 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002374 file, linenum, val, args[cur_arg], newsrv->id);
2375 err_code |= ERR_ALERT | ERR_FATAL;
2376 goto out;
2377 }
2378 newsrv->check.inter = val;
2379 cur_arg += 2;
2380 }
2381 else if (!strcmp(args[cur_arg], "fastinter")) {
2382 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2383 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002384 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002385 file, linenum, *err, newsrv->id);
2386 err_code |= ERR_ALERT | ERR_FATAL;
2387 goto out;
2388 }
2389 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002390 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002391 file, linenum, val, args[cur_arg], newsrv->id);
2392 err_code |= ERR_ALERT | ERR_FATAL;
2393 goto out;
2394 }
2395 newsrv->check.fastinter = val;
2396 cur_arg += 2;
2397 }
2398 else if (!strcmp(args[cur_arg], "downinter")) {
2399 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2400 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002401 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002402 file, linenum, *err, newsrv->id);
2403 err_code |= ERR_ALERT | ERR_FATAL;
2404 goto out;
2405 }
2406 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002407 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002408 file, linenum, val, args[cur_arg], newsrv->id);
2409 err_code |= ERR_ALERT | ERR_FATAL;
2410 goto out;
2411 }
2412 newsrv->check.downinter = val;
2413 cur_arg += 2;
2414 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002415 else if (!strcmp(args[cur_arg], "port")) {
2416 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002417 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002418 cur_arg += 2;
2419 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002420 else if (!strcmp(args[cur_arg], "weight")) {
2421 int w;
2422 w = atol(args[cur_arg + 1]);
2423 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002424 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002425 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2426 err_code |= ERR_ALERT | ERR_FATAL;
2427 goto out;
2428 }
2429 newsrv->uweight = newsrv->iweight = w;
2430 cur_arg += 2;
2431 }
2432 else if (!strcmp(args[cur_arg], "minconn")) {
2433 newsrv->minconn = atol(args[cur_arg + 1]);
2434 cur_arg += 2;
2435 }
2436 else if (!strcmp(args[cur_arg], "maxconn")) {
2437 newsrv->maxconn = atol(args[cur_arg + 1]);
2438 cur_arg += 2;
2439 }
2440 else if (!strcmp(args[cur_arg], "maxqueue")) {
2441 newsrv->maxqueue = atol(args[cur_arg + 1]);
2442 cur_arg += 2;
2443 }
2444 else if (!strcmp(args[cur_arg], "slowstart")) {
2445 /* slowstart is stored in seconds */
2446 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2447 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002448 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002449 file, linenum, *err, newsrv->id);
2450 err_code |= ERR_ALERT | ERR_FATAL;
2451 goto out;
2452 }
2453 newsrv->slowstart = (val + 999) / 1000;
2454 cur_arg += 2;
2455 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002456 else if (!strcmp(args[cur_arg], "on-error")) {
2457 if (!strcmp(args[cur_arg + 1], "fastinter"))
2458 newsrv->onerror = HANA_ONERR_FASTINTER;
2459 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2460 newsrv->onerror = HANA_ONERR_FAILCHK;
2461 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2462 newsrv->onerror = HANA_ONERR_SUDDTH;
2463 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2464 newsrv->onerror = HANA_ONERR_MARKDWN;
2465 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002466 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002467 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2468 file, linenum, args[cur_arg], args[cur_arg + 1]);
2469 err_code |= ERR_ALERT | ERR_FATAL;
2470 goto out;
2471 }
2472
2473 cur_arg += 2;
2474 }
2475 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2476 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2477 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2478 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002479 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002480 file, linenum, args[cur_arg], args[cur_arg + 1]);
2481 err_code |= ERR_ALERT | ERR_FATAL;
2482 goto out;
2483 }
2484
2485 cur_arg += 2;
2486 }
2487 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2488 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2489 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2490 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002491 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002492 file, linenum, args[cur_arg], args[cur_arg + 1]);
2493 err_code |= ERR_ALERT | ERR_FATAL;
2494 goto out;
2495 }
2496
2497 cur_arg += 2;
2498 }
2499 else if (!strcmp(args[cur_arg], "error-limit")) {
2500 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002501 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002502 file, linenum, args[cur_arg]);
2503 err_code |= ERR_ALERT | ERR_FATAL;
2504 goto out;
2505 }
2506
2507 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2508
2509 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002510 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002511 file, linenum, args[cur_arg]);
2512 err_code |= ERR_ALERT | ERR_FATAL;
2513 goto out;
2514 }
2515 cur_arg += 2;
2516 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002517 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002518 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002519 file, linenum, "usesrc", "source");
2520 err_code |= ERR_ALERT | ERR_FATAL;
2521 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002522 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002523 else {
2524 static int srv_dumped;
2525 struct srv_kw *kw;
2526 char *err;
2527
2528 kw = srv_find_kw(args[cur_arg]);
2529 if (kw) {
2530 char *err = NULL;
2531 int code;
2532
2533 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002534 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002535 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002536 if (kw->skip != -1)
2537 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002538 err_code |= ERR_ALERT | ERR_FATAL;
2539 goto out;
2540 }
2541
2542 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002543 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002544 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002545 if (kw->skip != -1)
2546 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002547 err_code |= ERR_ALERT;
2548 continue;
2549 }
2550
2551 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2552 err_code |= code;
2553
2554 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002555 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002556 if (code & ERR_FATAL) {
2557 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002558 if (kw->skip != -1)
2559 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002560 goto out;
2561 }
2562 }
2563 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002564 if (kw->skip != -1)
2565 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002566 continue;
2567 }
2568
2569 err = NULL;
2570 if (!srv_dumped) {
2571 srv_dump_kws(&err);
2572 indent_msg(&err, 4);
2573 srv_dumped = 1;
2574 }
2575
Christopher Faulet767a84b2017-11-24 16:50:31 +01002576 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002577 file, linenum, args[0], args[1], args[cur_arg],
2578 err ? " Registered keywords :" : "", err ? err : "");
2579 free(err);
2580
2581 err_code |= ERR_ALERT | ERR_FATAL;
2582 goto out;
2583 }
2584 }
2585
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002586 if (!defsrv)
2587 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2588 if (err_code & ERR_FATAL)
2589 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002590 if (srv_tmpl)
2591 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002592 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002593 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002594 return 0;
2595
2596 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002597 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002598 free(errmsg);
2599 return err_code;
2600}
2601
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002602/* Returns a pointer to the first server matching either id <id>.
2603 * NULL is returned if no match is found.
2604 * the lookup is performed in the backend <bk>
2605 */
2606struct server *server_find_by_id(struct proxy *bk, int id)
2607{
2608 struct eb32_node *eb32;
2609 struct server *curserver;
2610
2611 if (!bk || (id ==0))
2612 return NULL;
2613
2614 /* <bk> has no backend capabilities, so it can't have a server */
2615 if (!(bk->cap & PR_CAP_BE))
2616 return NULL;
2617
2618 curserver = NULL;
2619
2620 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2621 if (eb32)
2622 curserver = container_of(eb32, struct server, conf.id);
2623
2624 return curserver;
2625}
2626
2627/* Returns a pointer to the first server matching either name <name>, or id
2628 * if <name> starts with a '#'. NULL is returned if no match is found.
2629 * the lookup is performed in the backend <bk>
2630 */
2631struct server *server_find_by_name(struct proxy *bk, const char *name)
2632{
2633 struct server *curserver;
2634
2635 if (!bk || !name)
2636 return NULL;
2637
2638 /* <bk> has no backend capabilities, so it can't have a server */
2639 if (!(bk->cap & PR_CAP_BE))
2640 return NULL;
2641
2642 curserver = NULL;
2643 if (*name == '#') {
2644 curserver = server_find_by_id(bk, atoi(name + 1));
2645 if (curserver)
2646 return curserver;
2647 }
2648 else {
2649 curserver = bk->srv;
2650
2651 while (curserver && (strcmp(curserver->id, name) != 0))
2652 curserver = curserver->next;
2653
2654 if (curserver)
2655 return curserver;
2656 }
2657
2658 return NULL;
2659}
2660
2661struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2662{
2663 struct server *byname;
2664 struct server *byid;
2665
2666 if (!name && !id)
2667 return NULL;
2668
2669 if (diff)
2670 *diff = 0;
2671
2672 byname = byid = NULL;
2673
2674 if (name) {
2675 byname = server_find_by_name(bk, name);
2676 if (byname && (!id || byname->puid == id))
2677 return byname;
2678 }
2679
2680 /* remaining possibilities :
2681 * - name not set
2682 * - name set but not found
2683 * - name found but ID doesn't match
2684 */
2685 if (id) {
2686 byid = server_find_by_id(bk, id);
2687 if (byid) {
2688 if (byname) {
2689 /* use id only if forced by configuration */
2690 if (byid->flags & SRV_F_FORCED_ID) {
2691 if (diff)
2692 *diff |= 2;
2693 return byid;
2694 }
2695 else {
2696 if (diff)
2697 *diff |= 1;
2698 return byname;
2699 }
2700 }
2701
2702 /* remaining possibilities:
2703 * - name not set
2704 * - name set but not found
2705 */
2706 if (name && diff)
2707 *diff |= 2;
2708 return byid;
2709 }
2710
2711 /* id bot found */
2712 if (byname) {
2713 if (diff)
2714 *diff |= 1;
2715 return byname;
2716 }
2717 }
2718
2719 return NULL;
2720}
2721
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002722/* Update a server state using the parameters available in the params list */
2723static void srv_update_state(struct server *srv, int version, char **params)
2724{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002725 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002726 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002727
2728 /* fields since version 1
2729 * and common to all other upcoming versions
2730 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002731 enum srv_state srv_op_state;
2732 enum srv_admin srv_admin_state;
2733 unsigned srv_uweight, srv_iweight;
2734 unsigned long srv_last_time_change;
2735 short srv_check_status;
2736 enum chk_result srv_check_result;
2737 int srv_check_health;
2738 int srv_check_state, srv_agent_state;
2739 int bk_f_forced_id;
2740 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002741 int fqdn_set_by_cli;
2742 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002743 const char *port_str;
2744 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002745
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002746 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002747 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002748 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002749 switch (version) {
2750 case 1:
2751 /*
2752 * now we can proceed with server's state update:
2753 * srv_addr: params[0]
2754 * srv_op_state: params[1]
2755 * srv_admin_state: params[2]
2756 * srv_uweight: params[3]
2757 * srv_iweight: params[4]
2758 * srv_last_time_change: params[5]
2759 * srv_check_status: params[6]
2760 * srv_check_result: params[7]
2761 * srv_check_health: params[8]
2762 * srv_check_state: params[9]
2763 * srv_agent_state: params[10]
2764 * bk_f_forced_id: params[11]
2765 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002766 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002767 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002768 */
2769
2770 /* validating srv_op_state */
2771 p = NULL;
2772 errno = 0;
2773 srv_op_state = strtol(params[1], &p, 10);
2774 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2775 (srv_op_state != SRV_ST_STOPPED &&
2776 srv_op_state != SRV_ST_STARTING &&
2777 srv_op_state != SRV_ST_RUNNING &&
2778 srv_op_state != SRV_ST_STOPPING)) {
2779 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2780 }
2781
2782 /* validating srv_admin_state */
2783 p = NULL;
2784 errno = 0;
2785 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002786 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002787
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002788 /* inherited statuses will be recomputed later.
2789 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2790 */
2791 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002792
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002793 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2794 (srv_admin_state != 0 &&
2795 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002796 srv_admin_state != SRV_ADMF_CMAINT &&
2797 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002798 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002799 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002800 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2801 }
2802
2803 /* validating srv_uweight */
2804 p = NULL;
2805 errno = 0;
2806 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002807 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002808 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2809
2810 /* validating srv_iweight */
2811 p = NULL;
2812 errno = 0;
2813 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002814 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002815 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2816
2817 /* validating srv_last_time_change */
2818 p = NULL;
2819 errno = 0;
2820 srv_last_time_change = strtol(params[5], &p, 10);
2821 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2822 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2823
2824 /* validating srv_check_status */
2825 p = NULL;
2826 errno = 0;
2827 srv_check_status = strtol(params[6], &p, 10);
2828 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2829 (srv_check_status >= HCHK_STATUS_SIZE))
2830 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2831
2832 /* validating srv_check_result */
2833 p = NULL;
2834 errno = 0;
2835 srv_check_result = strtol(params[7], &p, 10);
2836 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2837 (srv_check_result != CHK_RES_UNKNOWN &&
2838 srv_check_result != CHK_RES_NEUTRAL &&
2839 srv_check_result != CHK_RES_FAILED &&
2840 srv_check_result != CHK_RES_PASSED &&
2841 srv_check_result != CHK_RES_CONDPASS)) {
2842 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2843 }
2844
2845 /* validating srv_check_health */
2846 p = NULL;
2847 errno = 0;
2848 srv_check_health = strtol(params[8], &p, 10);
2849 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2850 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2851
2852 /* validating srv_check_state */
2853 p = NULL;
2854 errno = 0;
2855 srv_check_state = strtol(params[9], &p, 10);
2856 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2857 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2858 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2859
2860 /* validating srv_agent_state */
2861 p = NULL;
2862 errno = 0;
2863 srv_agent_state = strtol(params[10], &p, 10);
2864 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2865 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2866 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2867
2868 /* validating bk_f_forced_id */
2869 p = NULL;
2870 errno = 0;
2871 bk_f_forced_id = strtol(params[11], &p, 10);
2872 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2873 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2874
2875 /* validating srv_f_forced_id */
2876 p = NULL;
2877 errno = 0;
2878 srv_f_forced_id = strtol(params[12], &p, 10);
2879 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2880 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2881
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002882 /* validating srv_fqdn */
2883 fqdn = params[13];
2884 if (fqdn && *fqdn == '-')
2885 fqdn = NULL;
2886 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2887 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2888 fqdn = NULL;
2889 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002890
Frédéric Lécaille31694712017-08-01 08:47:19 +02002891 port_str = params[14];
2892 if (port_str) {
2893 port = strl2uic(port_str, strlen(port_str));
2894 if (port > USHRT_MAX) {
2895 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2896 port_str = NULL;
2897 }
2898 }
2899
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002900 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002901 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002902 goto out;
2903
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002904 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002905 /* recover operational state and apply it to this server
2906 * and all servers tracking this one */
2907 switch (srv_op_state) {
2908 case SRV_ST_STOPPED:
2909 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002910 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002911 break;
2912 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002913 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002914 break;
2915 case SRV_ST_STOPPING:
2916 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002917 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002918 break;
2919 case SRV_ST_RUNNING:
2920 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002921 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002922 break;
2923 }
2924
2925 /* When applying server state, the following rules apply:
2926 * - in case of a configuration change, we apply the setting from the new
2927 * configuration, regardless of old running state
2928 * - if no configuration change, we apply old running state only if old running
2929 * state is different from new configuration state
2930 */
2931 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002932 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2933 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002934 srv_adm_set_maint(srv);
2935 else
2936 srv_adm_set_ready(srv);
2937 }
2938 /* configuration is the same, let's compate old running state and new conf state */
2939 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002940 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002941 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002942 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002943 srv_adm_set_ready(srv);
2944 }
2945 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002946 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002947 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002948 * (srv->iweight is the weight set up in configuration).
2949 * There are two possible reasons for FDRAIN to have been present :
2950 * - previous config weight was zero
2951 * - "set server b/s drain" was sent to the CLI
2952 *
2953 * In the first case, we simply want to drop this drain state
2954 * if the new weight is not zero anymore, meaning the administrator
2955 * has intentionally turned the weight back to a positive value to
2956 * enable the server again after an operation. In the second case,
2957 * the drain state was forced on the CLI regardless of the config's
2958 * weight so we don't want a change to the config weight to lose this
2959 * status. What this means is :
2960 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2961 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002962 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002963 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002964 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002965 }
2966
2967 srv->last_change = date.tv_sec - srv_last_time_change;
2968 srv->check.status = srv_check_status;
2969 srv->check.result = srv_check_result;
2970 srv->check.health = srv_check_health;
2971
2972 /* Only case we want to apply is removing ENABLED flag which could have been
2973 * done by the "disable health" command over the stats socket
2974 */
2975 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2976 (srv_check_state & CHK_ST_CONFIGURED) &&
2977 !(srv_check_state & CHK_ST_ENABLED))
2978 srv->check.state &= ~CHK_ST_ENABLED;
2979
2980 /* Only case we want to apply is removing ENABLED flag which could have been
2981 * done by the "disable agent" command over the stats socket
2982 */
2983 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2984 (srv_agent_state & CHK_ST_CONFIGURED) &&
2985 !(srv_agent_state & CHK_ST_ENABLED))
2986 srv->agent.state &= ~CHK_ST_ENABLED;
2987
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002988 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2989 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002990 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002991 * It means that a configuration file change has precedence over a unix socket change
2992 * for server's weight
2993 *
2994 * by default, HAProxy applies the following weight when parsing the configuration
2995 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002996 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002997 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002998 srv->uweight = srv_uweight;
2999 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003000 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003001
Willy Tarreaue5a60682016-11-09 14:54:53 +01003002 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003003 if (strcmp(params[0], "-"))
3004 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003005
3006 if (fqdn && srv->hostname) {
3007 if (!strcmp(srv->hostname, fqdn)) {
3008 /* Here we reset the 'set from stats socket FQDN' flag
3009 * to support such transitions:
3010 * Let's say initial FQDN value is foo1 (in configuration file).
3011 * - FQDN changed from stats socket, from foo1 to foo2 value,
3012 * - FQDN changed again from file configuration (with the same previous value
3013 set from stats socket, from foo1 to foo2 value),
3014 * - reload for any other reason than a FQDN modification,
3015 * the configuration file FQDN matches the fqdn server state file value.
3016 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
3017 * any futher FQDN modification.
3018 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003019 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003020 }
3021 else {
3022 /* If the FDQN has been changed from stats socket,
3023 * apply fqdn state file value (which is the value set
3024 * from stats socket).
3025 */
3026 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003027 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003028 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003029 }
3030 }
3031 }
3032
Frédéric Lécaille31694712017-08-01 08:47:19 +02003033 if (port_str)
3034 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003035 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003036
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003037 break;
3038 default:
3039 chunk_appendf(msg, ", version '%d' not supported", version);
3040 }
3041
3042 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003043 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003044 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003045 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003046 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003047 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003048}
3049
3050/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3051 * For each proxy, it does the following:
3052 * - opens its server state file (either one or local one)
3053 * - read whole file, line by line
3054 * - analyse each line to check if it matches our current backend:
3055 * - backend name matches
3056 * - backend id matches if id is forced and name doesn't match
3057 * - if the server pointed by the line is found, then state is applied
3058 *
3059 * If the running backend uuid or id differs from the state file, then HAProxy reports
3060 * a warning.
3061 */
3062void apply_server_state(void)
3063{
3064 char *cur, *end;
3065 char mybuf[SRV_STATE_LINE_MAXLEN];
3066 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003067 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3068 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003069 int arg, srv_arg, version, diff;
3070 FILE *f;
3071 char *filepath;
3072 char globalfilepath[MAXPATHLEN + 1];
3073 char localfilepath[MAXPATHLEN + 1];
3074 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003075 struct proxy *curproxy, *bk;
3076 struct server *srv;
3077
3078 globalfilepathlen = 0;
3079 /* create the globalfilepath variable */
3080 if (global.server_state_file) {
3081 /* absolute path or no base directory provided */
3082 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3083 len = strlen(global.server_state_file);
3084 if (len > MAXPATHLEN) {
3085 globalfilepathlen = 0;
3086 goto globalfileerror;
3087 }
3088 memcpy(globalfilepath, global.server_state_file, len);
3089 globalfilepath[len] = '\0';
3090 globalfilepathlen = len;
3091 }
3092 else if (global.server_state_base) {
3093 len = strlen(global.server_state_base);
3094 globalfilepathlen += len;
3095
3096 if (globalfilepathlen > MAXPATHLEN) {
3097 globalfilepathlen = 0;
3098 goto globalfileerror;
3099 }
3100 strncpy(globalfilepath, global.server_state_base, len);
3101 globalfilepath[globalfilepathlen] = 0;
3102
3103 /* append a slash if needed */
3104 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3105 if (globalfilepathlen + 1 > MAXPATHLEN) {
3106 globalfilepathlen = 0;
3107 goto globalfileerror;
3108 }
3109 globalfilepath[globalfilepathlen++] = '/';
3110 }
3111
3112 len = strlen(global.server_state_file);
3113 if (globalfilepathlen + len > MAXPATHLEN) {
3114 globalfilepathlen = 0;
3115 goto globalfileerror;
3116 }
3117 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3118 globalfilepathlen += len;
3119 globalfilepath[globalfilepathlen++] = 0;
3120 }
3121 }
3122 globalfileerror:
3123 if (globalfilepathlen == 0)
3124 globalfilepath[0] = '\0';
3125
3126 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003127 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003128 /* servers are only in backends */
3129 if (!(curproxy->cap & PR_CAP_BE))
3130 continue;
3131 fileopenerr = 0;
3132 filepath = NULL;
3133
3134 /* search server state file path and name */
3135 switch (curproxy->load_server_state_from_file) {
3136 /* read servers state from global file */
3137 case PR_SRV_STATE_FILE_GLOBAL:
3138 /* there was an error while generating global server state file path */
3139 if (globalfilepathlen == 0)
3140 continue;
3141 filepath = globalfilepath;
3142 fileopenerr = 1;
3143 break;
3144 /* this backend has its own file */
3145 case PR_SRV_STATE_FILE_LOCAL:
3146 localfilepathlen = 0;
3147 localfilepath[0] = '\0';
3148 len = 0;
3149 /* create the localfilepath variable */
3150 /* absolute path or no base directory provided */
3151 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3152 len = strlen(curproxy->server_state_file_name);
3153 if (len > MAXPATHLEN) {
3154 localfilepathlen = 0;
3155 goto localfileerror;
3156 }
3157 memcpy(localfilepath, curproxy->server_state_file_name, len);
3158 localfilepath[len] = '\0';
3159 localfilepathlen = len;
3160 }
3161 else if (global.server_state_base) {
3162 len = strlen(global.server_state_base);
3163 localfilepathlen += len;
3164
3165 if (localfilepathlen > MAXPATHLEN) {
3166 localfilepathlen = 0;
3167 goto localfileerror;
3168 }
3169 strncpy(localfilepath, global.server_state_base, len);
3170 localfilepath[localfilepathlen] = 0;
3171
3172 /* append a slash if needed */
3173 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3174 if (localfilepathlen + 1 > MAXPATHLEN) {
3175 localfilepathlen = 0;
3176 goto localfileerror;
3177 }
3178 localfilepath[localfilepathlen++] = '/';
3179 }
3180
3181 len = strlen(curproxy->server_state_file_name);
3182 if (localfilepathlen + len > MAXPATHLEN) {
3183 localfilepathlen = 0;
3184 goto localfileerror;
3185 }
3186 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3187 localfilepathlen += len;
3188 localfilepath[localfilepathlen++] = 0;
3189 }
3190 filepath = localfilepath;
3191 localfileerror:
3192 if (localfilepathlen == 0)
3193 localfilepath[0] = '\0';
3194
3195 break;
3196 case PR_SRV_STATE_FILE_NONE:
3197 default:
3198 continue;
3199 }
3200
3201 /* preload global state file */
3202 errno = 0;
3203 f = fopen(filepath, "r");
3204 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003205 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003206 if (!f)
3207 continue;
3208
3209 mybuf[0] = '\0';
3210 mybuflen = 0;
3211 version = 0;
3212
3213 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003214 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003215 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003216 goto fileclose;
3217 }
3218
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003219 cur = mybuf;
3220 version = atoi(cur);
3221 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3222 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003223 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003224
3225 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3226 int bk_f_forced_id = 0;
3227 int check_id = 0;
3228 int check_name = 0;
3229
3230 mybuflen = strlen(mybuf);
3231 cur = mybuf;
3232 end = cur + mybuflen;
3233
3234 bk = NULL;
3235 srv = NULL;
3236
3237 /* we need at least one character */
3238 if (mybuflen == 0)
3239 continue;
3240
3241 /* ignore blank characters at the beginning of the line */
3242 while (isspace(*cur))
3243 ++cur;
3244
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003245 /* Ignore empty or commented lines */
3246 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003247 continue;
3248
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003249 /* truncated lines */
3250 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003251 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003252 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003253 }
3254
3255 /* Removes trailing '\n' */
3256 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003257
3258 /* we're now ready to move the line into *srv_params[] */
3259 params[0] = cur;
3260 arg = 1;
3261 srv_arg = 0;
3262 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3263 if (isspace(*cur)) {
3264 *cur = '\0';
3265 ++cur;
3266 while (isspace(*cur))
3267 ++cur;
3268 switch (version) {
3269 case 1:
3270 /*
3271 * srv_addr: params[4] => srv_params[0]
3272 * srv_op_state: params[5] => srv_params[1]
3273 * srv_admin_state: params[6] => srv_params[2]
3274 * srv_uweight: params[7] => srv_params[3]
3275 * srv_iweight: params[8] => srv_params[4]
3276 * srv_last_time_change: params[9] => srv_params[5]
3277 * srv_check_status: params[10] => srv_params[6]
3278 * srv_check_result: params[11] => srv_params[7]
3279 * srv_check_health: params[12] => srv_params[8]
3280 * srv_check_state: params[13] => srv_params[9]
3281 * srv_agent_state: params[14] => srv_params[10]
3282 * bk_f_forced_id: params[15] => srv_params[11]
3283 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003284 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003285 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003286 */
3287 if (arg >= 4) {
3288 srv_params[srv_arg] = cur;
3289 ++srv_arg;
3290 }
3291 break;
3292 }
3293
3294 params[arg] = cur;
3295 ++arg;
3296 }
3297 else {
3298 ++cur;
3299 }
3300 }
3301
3302 /* if line is incomplete line, then ignore it.
3303 * otherwise, update useful flags */
3304 switch (version) {
3305 case 1:
3306 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3307 continue;
3308 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3309 check_id = (atoi(params[0]) == curproxy->uuid);
3310 check_name = (strcmp(curproxy->id, params[1]) == 0);
3311 break;
3312 }
3313
3314 diff = 0;
3315 bk = curproxy;
3316
3317 /* if backend can't be found, let's continue */
3318 if (!check_id && !check_name)
3319 continue;
3320 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003321 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003322 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3323 }
3324 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003325 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003326 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3327 /* if name doesn't match, we still want to update curproxy if the backend id
3328 * was forced in previous the previous configuration */
3329 if (!bk_f_forced_id)
3330 continue;
3331 }
3332
3333 /* look for the server by its id: param[2] */
3334 /* else look for the server by its name: param[3] */
3335 diff = 0;
3336 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3337
3338 if (!srv) {
3339 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003340 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3341 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003342 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3343 params[3], params[2], params[0], params[1]);
3344 continue;
3345 }
3346 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003347 ha_warning("In backend '%s' (id: '%d'): server ID mismatch: from server state file: '%s', from running config %d\n", bk->id, bk->uuid, params[2], srv->puid);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003348 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 +02003349 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003350 }
3351 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003352 ha_warning("In backend '%s' (id: %d): server name mismatch: from server state file: '%s', from running config '%s'\n", bk->id, bk->uuid, params[3], srv->id);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003353 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 +02003354 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003355 }
3356
3357 /* now we can proceed with server's state update */
3358 srv_update_state(srv, version, srv_params);
3359 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003360fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003361 fclose(f);
3362 }
3363}
3364
Simon Horman7d09b9a2013-02-12 10:45:51 +09003365/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003366 * update a server's current IP address.
3367 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3368 * ip is in network format.
3369 * updater is a string which contains an information about the requester of the update.
3370 * updater is used if not NULL.
3371 *
3372 * A log line and a stderr warning message is generated based on server's backend options.
3373 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003374int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003375{
3376 /* generates a log line and a warning on stderr */
3377 if (1) {
3378 /* book enough space for both IPv4 and IPv6 */
3379 char oldip[INET6_ADDRSTRLEN];
3380 char newip[INET6_ADDRSTRLEN];
3381
3382 memset(oldip, '\0', INET6_ADDRSTRLEN);
3383 memset(newip, '\0', INET6_ADDRSTRLEN);
3384
3385 /* copy old IP address in a string */
3386 switch (s->addr.ss_family) {
3387 case AF_INET:
3388 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3389 break;
3390 case AF_INET6:
3391 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3392 break;
3393 };
3394
3395 /* copy new IP address in a string */
3396 switch (ip_sin_family) {
3397 case AF_INET:
3398 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3399 break;
3400 case AF_INET6:
3401 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3402 break;
3403 };
3404
3405 /* save log line into a buffer */
3406 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3407 s->proxy->id, s->id, oldip, newip, updater);
3408
3409 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003410 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003411
3412 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003413 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003414 }
3415
3416 /* save the new IP family */
3417 s->addr.ss_family = ip_sin_family;
3418 /* save the new IP address */
3419 switch (ip_sin_family) {
3420 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003421 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003422 break;
3423 case AF_INET6:
3424 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3425 break;
3426 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003427 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003428
3429 return 0;
3430}
3431
3432/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003433 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3434 *
3435 * Caller can pass its name through <updater> to get it integrated in the response message
3436 * returned by the function.
3437 *
3438 * The function first does the following, in that order:
3439 * - validates the new addr and/or port
3440 * - checks if an update is required (new IP or port is different than current ones)
3441 * - checks the update is allowed:
3442 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3443 * - allow all changes if no CHECKS are configured
3444 * - if CHECK is configured:
3445 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3446 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3447 * conditions are met
3448 */
3449const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3450{
3451 struct sockaddr_storage sa;
3452 int ret, port_change_required;
3453 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003454 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003455 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003456 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003457
3458 msg = get_trash_chunk();
3459 chunk_reset(msg);
3460
3461 if (addr) {
3462 memset(&sa, 0, sizeof(struct sockaddr_storage));
3463 if (str2ip2(addr, &sa, 0) == NULL) {
3464 chunk_printf(msg, "Invalid addr '%s'", addr);
3465 goto out;
3466 }
3467
3468 /* changes are allowed on AF_INET* families only */
3469 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3470 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3471 goto out;
3472 }
3473
3474 /* collecting data currently setup */
3475 memset(current_addr, '\0', sizeof(current_addr));
3476 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3477 /* changes are allowed on AF_INET* families only */
3478 if ((ret != AF_INET) && (ret != AF_INET6)) {
3479 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3480 goto out;
3481 }
3482
3483 /* applying ADDR changes if required and allowed
3484 * ipcmp returns 0 when both ADDR are the same
3485 */
3486 if (ipcmp(&s->addr, &sa) == 0) {
3487 chunk_appendf(msg, "no need to change the addr");
3488 goto port;
3489 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003490 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003491 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003492
3493 /* we also need to update check's ADDR only if it uses the server's one */
3494 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003495 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003496 }
3497
3498 /* we also need to update agent ADDR only if it use the server's one */
3499 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003500 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003501 }
3502
3503 /* update report for caller */
3504 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3505 }
3506
3507 port:
3508 if (port) {
3509 char sign = '\0';
3510 char *endptr;
3511
3512 if (addr)
3513 chunk_appendf(msg, ", ");
3514
3515 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003516 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003517
3518 /* check if PORT change is required */
3519 port_change_required = 0;
3520
3521 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003522 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003523 new_port = strtol(port, &endptr, 10);
3524 if ((errno != 0) || (port == endptr)) {
3525 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3526 goto out;
3527 }
3528
3529 /* check if caller triggers a port mapped or offset */
3530 if (sign == '-' || (sign == '+')) {
3531 /* check if server currently uses port map */
3532 if (!(s->flags & SRV_F_MAPPORTS)) {
3533 /* switch from fixed port to port map mandatorily triggers
3534 * a port change */
3535 port_change_required = 1;
3536 /* check is configured
3537 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3538 * prevent PORT change if check doesn't have it's dedicated port while switching
3539 * to port mapping */
3540 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3541 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.");
3542 goto out;
3543 }
3544 }
3545 /* we're already using port maps */
3546 else {
3547 port_change_required = current_port != new_port;
3548 }
3549 }
3550 /* fixed port */
3551 else {
3552 port_change_required = current_port != new_port;
3553 }
3554
3555 /* applying PORT changes if required and update response message */
3556 if (port_change_required) {
3557 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003558 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003559 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003560
3561 /* prepare message */
3562 chunk_appendf(msg, "port changed from '");
3563 if (s->flags & SRV_F_MAPPORTS)
3564 chunk_appendf(msg, "+");
3565 chunk_appendf(msg, "%d' to '", current_port);
3566
3567 if (sign == '-') {
3568 s->flags |= SRV_F_MAPPORTS;
3569 chunk_appendf(msg, "%c", sign);
3570 /* just use for result output */
3571 new_port = -new_port;
3572 }
3573 else if (sign == '+') {
3574 s->flags |= SRV_F_MAPPORTS;
3575 chunk_appendf(msg, "%c", sign);
3576 }
3577 else {
3578 s->flags &= ~SRV_F_MAPPORTS;
3579 }
3580
3581 chunk_appendf(msg, "%d'", new_port);
3582
3583 /* we also need to update health checks port only if it uses server's realport */
3584 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3585 s->check.port = new_port;
3586 }
3587 }
3588 else {
3589 chunk_appendf(msg, "no need to change the port");
3590 }
3591 }
3592
3593out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003594 if (changed)
3595 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003596 if (updater)
3597 chunk_appendf(msg, " by '%s'", updater);
3598 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003599 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003600}
3601
3602
3603/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003604 * update server status based on result of name resolution
3605 * returns:
3606 * 0 if server status is updated
3607 * 1 if server status has not changed
3608 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003609int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003610{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003611 struct dns_resolvers *resolvers = s->resolvers;
3612 struct dns_resolution *resolution = s->dns_requester->resolution;
3613 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003614
3615 switch (resolution->status) {
3616 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003617 /* status when HAProxy has just (re)started.
3618 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003619 break;
3620
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003621 case RSLV_STATUS_VALID:
3622 /*
3623 * resume health checks
3624 * server will be turned back on if health check is safe
3625 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003626 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003627 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003628 return 1;
3629 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3630 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003631 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003632 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003633
Emeric Brun52a91d32017-08-31 14:41:55 +02003634 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003635 return 1;
3636 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3637 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3638 s->proxy->id, s->id);
3639
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003640 ha_warning("%s.\n", trash.area);
3641 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003642 return 0;
3643
3644 case RSLV_STATUS_NX:
3645 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003646 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3647 if (!tick_is_expired(exp, now_ms))
3648 break;
3649
3650 if (s->next_admin & SRV_ADMF_RMAINT)
3651 return 1;
3652 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3653 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003654
3655 case RSLV_STATUS_TIMEOUT:
3656 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003657 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3658 if (!tick_is_expired(exp, now_ms))
3659 break;
3660
3661 if (s->next_admin & SRV_ADMF_RMAINT)
3662 return 1;
3663 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3664 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003665
3666 case RSLV_STATUS_REFUSED:
3667 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003668 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3669 if (!tick_is_expired(exp, now_ms))
3670 break;
3671
3672 if (s->next_admin & SRV_ADMF_RMAINT)
3673 return 1;
3674 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3675 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003676
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003677 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003678 /* stop server if resolution failed for a long enough period */
3679 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3680 if (!tick_is_expired(exp, now_ms))
3681 break;
3682
3683 if (s->next_admin & SRV_ADMF_RMAINT)
3684 return 1;
3685 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3686 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003687 }
3688
3689 return 1;
3690}
3691
3692/*
3693 * Server Name Resolution valid response callback
3694 * It expects:
3695 * - <nameserver>: the name server which answered the valid response
3696 * - <response>: buffer containing a valid DNS response
3697 * - <response_len>: size of <response>
3698 * It performs the following actions:
3699 * - ignore response if current ip found and server family not met
3700 * - update with first new ip found if family is met and current IP is not found
3701 * returns:
3702 * 0 on error
3703 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003704 *
3705 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003706 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003707int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003708{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003709 struct server *s = NULL;
3710 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003711 void *serverip, *firstip;
3712 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003713 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003714 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003715 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003716
Christopher Faulet67957bd2017-09-27 11:00:59 +02003717 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003718 if (!s)
3719 return 1;
3720
Christopher Faulet67957bd2017-09-27 11:00:59 +02003721 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003722
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003723 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003724 firstip = NULL; /* pointer to the first valid response found */
3725 /* it will be used as the new IP if a change is required */
3726 firstip_sin_family = AF_UNSPEC;
3727 serverip = NULL; /* current server IP address */
3728
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003729 /* initializing server IP pointer */
3730 server_sin_family = s->addr.ss_family;
3731 switch (server_sin_family) {
3732 case AF_INET:
3733 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3734 break;
3735
3736 case AF_INET6:
3737 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3738 break;
3739
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003740 case AF_UNSPEC:
3741 break;
3742
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003743 default:
3744 goto invalid;
3745 }
3746
Baptiste Assmann729c9012017-05-22 15:13:10 +02003747 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003748 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003749 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003750
3751 switch (ret) {
3752 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003753 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003754
3755 case DNS_UPD_SRVIP_NOT_FOUND:
3756 goto save_ip;
3757
3758 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003759 goto invalid;
3760
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003761 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003762 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003763 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003764
Baptiste Assmannfad03182015-10-28 02:03:32 +01003765 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003766 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003767 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003768 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003769
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003770 default:
3771 goto invalid;
3772
3773 }
3774
3775 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003776 if (nameserver) {
3777 nameserver->counters.update++;
3778 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003779 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003780 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003781 else
3782 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003783 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003784
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003785 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003786 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003787 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003788
3789 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003790 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003791 nameserver->counters.invalid++;
3792 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003793 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003794 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003795 return 0;
3796}
3797
3798/*
3799 * Server Name Resolution error management callback
3800 * returns:
3801 * 0 on error
3802 * 1 when no error or safe ignore
3803 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003804int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003805{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003806 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003807
Christopher Faulet67957bd2017-09-27 11:00:59 +02003808 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003809 if (!s)
3810 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003811 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003812 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003813 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003814 return 1;
3815}
3816
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003817/*
3818 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003819 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003820 * It returns a pointer to the first server found or NULL if <ip> is not yet
3821 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003822 *
3823 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003824 */
3825struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3826{
3827 struct server *tmpsrv;
3828 struct proxy *be;
3829
3830 if (!srv)
3831 return NULL;
3832
3833 be = srv->proxy;
3834 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003835 /* we found the current server is the same, ignore it */
3836 if (srv == tmpsrv)
3837 continue;
3838
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003839 /* We want to compare the IP in the record with the IP of the servers in the
3840 * same backend, only if:
3841 * * DNS resolution is enabled on the server
3842 * * the hostname used for the resolution by our server is the same than the
3843 * one used for the server found in the backend
3844 * * the server found in the backend is not our current server
3845 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003846 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003847 if ((tmpsrv->hostname_dn == NULL) ||
3848 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3849 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003850 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003851 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003852 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003853 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003854
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003855 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003856 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003857 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003858 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003859 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003860
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003861 /* At this point, we have 2 different servers using the same DNS hostname
3862 * for their respective resolution.
3863 */
3864 if (*ip_family == tmpsrv->addr.ss_family &&
3865 ((tmpsrv->addr.ss_family == AF_INET &&
3866 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3867 (tmpsrv->addr.ss_family == AF_INET6 &&
3868 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003869 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003870 return tmpsrv;
3871 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003872 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003873 }
3874
Emeric Brune9fd6b52017-11-02 17:20:39 +01003875
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003876 return NULL;
3877}
3878
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003879/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3880 * resolver. This is suited for initial address configuration. Returns 0 on
3881 * success otherwise a non-zero error code. In case of error, *err_code, if
3882 * not NULL, is filled up.
3883 */
3884int srv_set_addr_via_libc(struct server *srv, int *err_code)
3885{
3886 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003887 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003888 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003889 return 1;
3890 }
3891 return 0;
3892}
3893
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003894/* Set the server's FDQN (->hostname) from <hostname>.
3895 * Returns -1 if failed, 0 if not.
3896 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003897int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003898{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003899 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003900 char *hostname_dn;
3901 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003902
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003903 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003904 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003905 /* run time DNS resolution was not active for this server
3906 * and we can't enable it at run time for now.
3907 */
3908 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003909 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003910
3911 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003912 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003913 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003914 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3915 hostname_dn, trash.size);
3916 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003917 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003918
Christopher Faulet67957bd2017-09-27 11:00:59 +02003919 resolution = srv->dns_requester->resolution;
3920 if (resolution &&
3921 resolution->hostname_dn &&
3922 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02003923 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003924
Christopher Faulet67957bd2017-09-27 11:00:59 +02003925 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003926
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003927 free(srv->hostname);
3928 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003929 srv->hostname = strdup(hostname);
3930 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003931 srv->hostname_dn_len = hostname_dn_len;
3932 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003933 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003934
Olivier Houchard55dcdf42017-11-06 15:15:04 +01003935 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003936 goto err;
3937
3938 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003939 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003940 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003941 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003942
3943 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003944 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003945 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003946 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003947}
3948
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003949/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3950 * from the state file. This is suited for initial address configuration.
3951 * Returns 0 on success otherwise a non-zero error code. In case of error,
3952 * *err_code, if not NULL, is filled up.
3953 */
3954static int srv_apply_lastaddr(struct server *srv, int *err_code)
3955{
3956 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3957 if (err_code)
3958 *err_code |= ERR_WARN;
3959 return 1;
3960 }
3961 return 0;
3962}
3963
Willy Tarreau25e51522016-11-04 15:10:17 +01003964/* returns 0 if no error, otherwise a combination of ERR_* flags */
3965static int srv_iterate_initaddr(struct server *srv)
3966{
3967 int return_code = 0;
3968 int err_code;
3969 unsigned int methods;
3970
3971 methods = srv->init_addr_methods;
3972 if (!methods) { // default to "last,libc"
3973 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3974 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3975 }
3976
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003977 /* "-dr" : always append "none" so that server addresses resolution
3978 * failures are silently ignored, this is convenient to validate some
3979 * configs out of their environment.
3980 */
3981 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3982 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3983
Willy Tarreau25e51522016-11-04 15:10:17 +01003984 while (methods) {
3985 err_code = 0;
3986 switch (srv_get_next_initaddr(&methods)) {
3987 case SRV_IADDR_LAST:
3988 if (!srv->lastaddr)
3989 continue;
3990 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003991 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003992 return_code |= err_code;
3993 break;
3994
3995 case SRV_IADDR_LIBC:
3996 if (!srv->hostname)
3997 continue;
3998 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003999 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004000 return_code |= err_code;
4001 break;
4002
Willy Tarreau37ebe122016-11-04 15:17:58 +01004003 case SRV_IADDR_NONE:
4004 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004005 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004006 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4007 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004008 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004009 return return_code;
4010
Willy Tarreau4310d362016-11-02 15:05:56 +01004011 case SRV_IADDR_IP:
4012 ipcpy(&srv->init_addr, &srv->addr);
4013 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004014 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4015 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004016 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004017 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004018
Willy Tarreau25e51522016-11-04 15:10:17 +01004019 default: /* unhandled method */
4020 break;
4021 }
4022 }
4023
4024 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004025 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004026 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4027 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004028 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004029 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004030 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4031 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004032
4033 return_code |= ERR_ALERT | ERR_FATAL;
4034 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004035out:
4036 srv_set_dyncookie(srv);
4037 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004038}
4039
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004040/*
4041 * This function parses all backends and all servers within each backend
4042 * and performs servers' addr resolution based on information provided by:
4043 * - configuration file
4044 * - server-state file (states provided by an 'old' haproxy process)
4045 *
4046 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4047 */
4048int srv_init_addr(void)
4049{
4050 struct proxy *curproxy;
4051 int return_code = 0;
4052
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004053 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004054 while (curproxy) {
4055 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004056
4057 /* servers are in backend only */
4058 if (!(curproxy->cap & PR_CAP_BE))
4059 goto srv_init_addr_next;
4060
Willy Tarreau25e51522016-11-04 15:10:17 +01004061 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004062 if (srv->hostname)
4063 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004064
4065 srv_init_addr_next:
4066 curproxy = curproxy->next;
4067 }
4068
4069 return return_code;
4070}
4071
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004072const 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 +02004073{
4074
Willy Tarreau83061a82018-07-13 11:56:34 +02004075 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004076
4077 msg = get_trash_chunk();
4078 chunk_reset(msg);
4079
Olivier Houchard8da5f982017-08-04 18:35:36 +02004080 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004081 chunk_appendf(msg, "no need to change the FDQN");
4082 goto out;
4083 }
4084
4085 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4086 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4087 goto out;
4088 }
4089
4090 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4091 server->proxy->id, server->id, server->hostname, fqdn);
4092
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004093 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004094 chunk_reset(msg);
4095 chunk_appendf(msg, "could not update %s/%s FQDN",
4096 server->proxy->id, server->id);
4097 goto out;
4098 }
4099
4100 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004101 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004102
4103 out:
4104 if (updater)
4105 chunk_appendf(msg, " by '%s'", updater);
4106 chunk_appendf(msg, "\n");
4107
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004108 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004109}
4110
4111
Willy Tarreau21b069d2016-11-23 17:15:08 +01004112/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4113 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004114 * 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 +01004115 * used for CLI commands requiring a server name.
4116 * Important: the <arg> is modified to remove the '/'.
4117 */
4118struct server *cli_find_server(struct appctx *appctx, char *arg)
4119{
4120 struct proxy *px;
4121 struct server *sv;
4122 char *line;
4123
4124 /* split "backend/server" and make <line> point to server */
4125 for (line = arg; *line; line++)
4126 if (*line == '/') {
4127 *line++ = '\0';
4128 break;
4129 }
4130
4131 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004132 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004133 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004134 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004135 return NULL;
4136 }
4137
4138 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004139 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004140 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004141 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004142 return NULL;
4143 }
4144
4145 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004146 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004147 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004148 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004149 return NULL;
4150 }
4151
4152 return sv;
4153}
4154
William Lallemand222baf22016-11-19 02:00:33 +01004155
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004156static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004157{
4158 struct server *sv;
4159 const char *warning;
4160
4161 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4162 return 1;
4163
4164 sv = cli_find_server(appctx, args[2]);
4165 if (!sv)
4166 return 1;
4167
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004168 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004169
William Lallemand222baf22016-11-19 02:00:33 +01004170 if (strcmp(args[3], "weight") == 0) {
4171 warning = server_parse_weight_change_request(sv, args[4]);
4172 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004173 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004174 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004175 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004176 }
4177 }
4178 else if (strcmp(args[3], "state") == 0) {
4179 if (strcmp(args[4], "ready") == 0)
4180 srv_adm_set_ready(sv);
4181 else if (strcmp(args[4], "drain") == 0)
4182 srv_adm_set_drain(sv);
4183 else if (strcmp(args[4], "maint") == 0)
4184 srv_adm_set_maint(sv);
4185 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004186 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004187 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004188 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004189 }
4190 }
4191 else if (strcmp(args[3], "health") == 0) {
4192 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004193 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004194 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004195 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004196 }
4197 else if (strcmp(args[4], "up") == 0) {
4198 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004199 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004200 }
4201 else if (strcmp(args[4], "stopping") == 0) {
4202 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004203 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004204 }
4205 else if (strcmp(args[4], "down") == 0) {
4206 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004207 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004208 }
4209 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004210 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004211 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004212 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004213 }
4214 }
4215 else if (strcmp(args[3], "agent") == 0) {
4216 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004217 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004218 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004219 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004220 }
4221 else if (strcmp(args[4], "up") == 0) {
4222 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004223 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004224 }
4225 else if (strcmp(args[4], "down") == 0) {
4226 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004227 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004228 }
4229 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004230 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004231 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004232 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004233 }
4234 }
Misiek2da082d2017-01-09 09:40:42 +01004235 else if (strcmp(args[3], "agent-addr") == 0) {
4236 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004237 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004238 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4239 appctx->st0 = CLI_ST_PRINT;
4240 } else {
4241 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004242 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004243 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4244 appctx->st0 = CLI_ST_PRINT;
4245 }
4246 }
4247 }
4248 else if (strcmp(args[3], "agent-send") == 0) {
4249 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004250 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004251 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4252 appctx->st0 = CLI_ST_PRINT;
4253 } else {
4254 char *nss = strdup(args[4]);
4255 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004256 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004257 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4258 appctx->st0 = CLI_ST_PRINT;
4259 } else {
4260 free(sv->agent.send_string);
4261 sv->agent.send_string = nss;
4262 sv->agent.send_string_len = strlen(args[4]);
4263 }
4264 }
4265 }
William Lallemand222baf22016-11-19 02:00:33 +01004266 else if (strcmp(args[3], "check-port") == 0) {
4267 int i = 0;
4268 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004269 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004270 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004271 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004272 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004273 }
4274 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004275 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004276 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004277 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004278 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004279 }
4280 /* prevent the update of port to 0 if MAPPORTS are in use */
4281 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004282 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004283 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004284 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004285 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004286 }
4287 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004288 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004289 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004290 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004291 }
4292 else if (strcmp(args[3], "addr") == 0) {
4293 char *addr = NULL;
4294 char *port = NULL;
4295 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004296 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004297 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004298 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004299 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004300 }
4301 else {
4302 addr = args[4];
4303 }
4304 if (strcmp(args[5], "port") == 0) {
4305 port = args[6];
4306 }
4307 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4308 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004309 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004310 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004311 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004312 }
4313 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4314 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004315 else if (strcmp(args[3], "fqdn") == 0) {
4316 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004317 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004318 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4319 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004320 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004321 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004322 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004323 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004324 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004325 appctx->ctx.cli.msg = warning;
4326 appctx->st0 = CLI_ST_PRINT;
4327 }
4328 }
William Lallemand222baf22016-11-19 02:00:33 +01004329 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004330 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004331 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 +01004332 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004333 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004334 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004335 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004336 return 1;
4337}
4338
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004339static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004340{
4341 struct stream_interface *si = appctx->owner;
4342 struct proxy *px;
4343 struct server *sv;
4344 char *line;
4345
4346
4347 /* split "backend/server" and make <line> point to server */
4348 for (line = args[2]; *line; line++)
4349 if (*line == '/') {
4350 *line++ = '\0';
4351 break;
4352 }
4353
4354 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004355 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004356 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004357 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004358 return 1;
4359 }
4360
4361 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004362 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004363 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004364 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004365 return 1;
4366 }
4367
4368 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004369 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4370 sv->iweight);
4371 if (ci_putstr(si_ic(si), trash.area) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004372 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004373 return 0;
4374 }
William Lallemand6b160942016-11-22 12:34:35 +01004375 return 1;
4376}
4377
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004378static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004379{
4380 struct server *sv;
4381 const char *warning;
4382
4383 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4384 return 1;
4385
4386 sv = cli_find_server(appctx, args[2]);
4387 if (!sv)
4388 return 1;
4389
4390 warning = server_parse_weight_change_request(sv, args[3]);
4391 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004392 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004393 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004394 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004395 }
4396 return 1;
4397}
4398
Willy Tarreaub8026272016-11-23 11:26:56 +01004399/* parse a "set maxconn server" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004400static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004401{
4402 struct server *sv;
4403 const char *warning;
4404
4405 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4406 return 1;
4407
4408 sv = cli_find_server(appctx, args[3]);
4409 if (!sv)
4410 return 1;
4411
4412 warning = server_parse_maxconn_change_request(sv, args[4]);
4413 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004414 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004415 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004416 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004417 }
4418 return 1;
4419}
William Lallemand6b160942016-11-22 12:34:35 +01004420
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004421/* parse a "disable agent" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004422static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004423{
4424 struct server *sv;
4425
4426 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4427 return 1;
4428
4429 sv = cli_find_server(appctx, args[2]);
4430 if (!sv)
4431 return 1;
4432
4433 sv->agent.state &= ~CHK_ST_ENABLED;
4434 return 1;
4435}
4436
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004437/* parse a "disable health" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004438static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004439{
4440 struct server *sv;
4441
4442 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4443 return 1;
4444
4445 sv = cli_find_server(appctx, args[2]);
4446 if (!sv)
4447 return 1;
4448
4449 sv->check.state &= ~CHK_ST_ENABLED;
4450 return 1;
4451}
4452
Willy Tarreauffb4d582016-11-24 12:47:00 +01004453/* parse a "disable server" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004454static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004455{
4456 struct server *sv;
4457
4458 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4459 return 1;
4460
4461 sv = cli_find_server(appctx, args[2]);
4462 if (!sv)
4463 return 1;
4464
4465 srv_adm_set_maint(sv);
4466 return 1;
4467}
4468
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004469/* parse a "enable agent" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004470static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004471{
4472 struct server *sv;
4473
4474 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4475 return 1;
4476
4477 sv = cli_find_server(appctx, args[2]);
4478 if (!sv)
4479 return 1;
4480
4481 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004482 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004483 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004484 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004485 return 1;
4486 }
4487
4488 sv->agent.state |= CHK_ST_ENABLED;
4489 return 1;
4490}
4491
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004492/* parse a "enable health" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004493static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004494{
4495 struct server *sv;
4496
4497 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4498 return 1;
4499
4500 sv = cli_find_server(appctx, args[2]);
4501 if (!sv)
4502 return 1;
4503
4504 sv->check.state |= CHK_ST_ENABLED;
4505 return 1;
4506}
4507
Willy Tarreauffb4d582016-11-24 12:47:00 +01004508/* parse a "enable server" command. It always returns 1. */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004509static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004510{
4511 struct server *sv;
4512
4513 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4514 return 1;
4515
4516 sv = cli_find_server(appctx, args[2]);
4517 if (!sv)
4518 return 1;
4519
4520 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004521 if (!(sv->flags & SRV_F_COOKIESET)
4522 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4523 sv->cookie)
4524 srv_check_for_dup_dyncookie(sv);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004525 return 1;
4526}
4527
William Lallemand222baf22016-11-19 02:00:33 +01004528/* register cli keywords */
4529static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004530 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004531 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004532 { { "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 +01004533 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004534 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004535 { { "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 +01004536 { { "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 +01004537 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004538 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4539 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4540
William Lallemand222baf22016-11-19 02:00:33 +01004541 {{},}
4542}};
4543
4544__attribute__((constructor))
4545static void __server_init(void)
4546{
4547 cli_register_kw(&cli_kws);
4548}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004549
Emeric Brun64cc49c2017-10-03 14:46:45 +02004550/*
4551 * This function applies server's status changes, it is
4552 * is designed to be called asynchronously.
4553 *
4554 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004555static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004556{
4557 struct check *check = &s->check;
4558 int xferred;
4559 struct proxy *px = s->proxy;
4560 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4561 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4562 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004563 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004564
Emeric Brun64cc49c2017-10-03 14:46:45 +02004565 /* If currently main is not set we try to apply pending state changes */
4566 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4567 int next_admin;
4568
4569 /* Backup next admin */
4570 next_admin = s->next_admin;
4571
4572 /* restore current admin state */
4573 s->next_admin = s->cur_admin;
4574
4575 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4576 s->last_change = now.tv_sec;
4577 if (s->proxy->lbprm.set_server_status_down)
4578 s->proxy->lbprm.set_server_status_down(s);
4579
4580 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4581 srv_shutdown_streams(s, SF_ERR_DOWN);
4582
4583 /* we might have streams queued on this server and waiting for
4584 * a connection. Those which are redispatchable will be queued
4585 * to another server or to the proxy itself.
4586 */
4587 xferred = pendconn_redistribute(s);
4588
4589 tmptrash = alloc_trash_chunk();
4590 if (tmptrash) {
4591 chunk_printf(tmptrash,
4592 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4593 s->proxy->id, s->id);
4594
Emeric Brun5a133512017-10-19 14:42:30 +02004595 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004596 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004597
4598 /* we don't send an alert if the server was previously paused */
4599 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004600 send_log(s->proxy, log_level, "%s.\n",
4601 tmptrash->area);
4602 send_email_alert(s, log_level, "%s",
4603 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004604 free_trash_chunk(tmptrash);
4605 tmptrash = NULL;
4606 }
4607 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4608 set_backend_down(s->proxy);
4609
4610 s->counters.down_trans++;
4611 }
4612 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4613 s->last_change = now.tv_sec;
4614 if (s->proxy->lbprm.set_server_status_down)
4615 s->proxy->lbprm.set_server_status_down(s);
4616
4617 /* we might have streams queued on this server and waiting for
4618 * a connection. Those which are redispatchable will be queued
4619 * to another server or to the proxy itself.
4620 */
4621 xferred = pendconn_redistribute(s);
4622
4623 tmptrash = alloc_trash_chunk();
4624 if (tmptrash) {
4625 chunk_printf(tmptrash,
4626 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4627 s->proxy->id, s->id);
4628
Emeric Brun5a133512017-10-19 14:42:30 +02004629 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004630
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004631 ha_warning("%s.\n", tmptrash->area);
4632 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4633 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004634 free_trash_chunk(tmptrash);
4635 tmptrash = NULL;
4636 }
4637
4638 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4639 set_backend_down(s->proxy);
4640 }
4641 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4642 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4643 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4644 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4645 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4646 s->proxy->last_change = now.tv_sec;
4647 }
4648
4649 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4650 s->down_time += now.tv_sec - s->last_change;
4651
4652 s->last_change = now.tv_sec;
4653 if (s->next_state == SRV_ST_STARTING)
4654 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4655
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004656 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004657 /* now propagate the status change to any LB algorithms */
4658 if (px->lbprm.update_server_eweight)
4659 px->lbprm.update_server_eweight(s);
4660 else if (srv_willbe_usable(s)) {
4661 if (px->lbprm.set_server_status_up)
4662 px->lbprm.set_server_status_up(s);
4663 }
4664 else {
4665 if (px->lbprm.set_server_status_down)
4666 px->lbprm.set_server_status_down(s);
4667 }
4668
4669 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4670 * and it's not a backup server and its effective weight is > 0,
4671 * then it can accept new connections, so we shut down all streams
4672 * on all backup servers.
4673 */
4674 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4675 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4676 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4677
4678 /* check if we can handle some connections queued at the proxy. We
4679 * will take as many as we can handle.
4680 */
4681 xferred = pendconn_grab_from_px(s);
4682
4683 tmptrash = alloc_trash_chunk();
4684 if (tmptrash) {
4685 chunk_printf(tmptrash,
4686 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4687 s->proxy->id, s->id);
4688
Emeric Brun5a133512017-10-19 14:42:30 +02004689 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004690 ha_warning("%s.\n", tmptrash->area);
4691 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4692 tmptrash->area);
4693 send_email_alert(s, LOG_NOTICE, "%s",
4694 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004695 free_trash_chunk(tmptrash);
4696 tmptrash = NULL;
4697 }
4698
4699 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4700 set_backend_down(s->proxy);
4701 }
4702 else if (s->cur_eweight != s->next_eweight) {
4703 /* now propagate the status change to any LB algorithms */
4704 if (px->lbprm.update_server_eweight)
4705 px->lbprm.update_server_eweight(s);
4706 else if (srv_willbe_usable(s)) {
4707 if (px->lbprm.set_server_status_up)
4708 px->lbprm.set_server_status_up(s);
4709 }
4710 else {
4711 if (px->lbprm.set_server_status_down)
4712 px->lbprm.set_server_status_down(s);
4713 }
4714
4715 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4716 set_backend_down(s->proxy);
4717 }
4718
4719 s->next_admin = next_admin;
4720 }
4721
Emeric Brun5a133512017-10-19 14:42:30 +02004722 /* reset operational state change */
4723 *s->op_st_chg.reason = 0;
4724 s->op_st_chg.status = s->op_st_chg.code = -1;
4725 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004726
4727 /* Now we try to apply pending admin changes */
4728
4729 /* Maintenance must also disable health checks */
4730 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4731 if (s->check.state & CHK_ST_ENABLED) {
4732 s->check.state |= CHK_ST_PAUSED;
4733 check->health = 0;
4734 }
4735
4736 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004737 tmptrash = alloc_trash_chunk();
4738 if (tmptrash) {
4739 chunk_printf(tmptrash,
4740 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4741 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4742 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004743
Olivier Houchard796a2b32017-10-24 17:42:47 +02004744 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004745
Olivier Houchard796a2b32017-10-24 17:42:47 +02004746 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004747 ha_warning("%s.\n", tmptrash->area);
4748 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4749 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004750 }
4751 free_trash_chunk(tmptrash);
4752 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004753 }
Emeric Brun8f298292017-12-06 16:47:17 +01004754 /* commit new admin status */
4755
4756 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004757 }
4758 else { /* server was still running */
4759 check->health = 0; /* failure */
4760 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004761
4762 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004763 if (s->proxy->lbprm.set_server_status_down)
4764 s->proxy->lbprm.set_server_status_down(s);
4765
Emeric Brun64cc49c2017-10-03 14:46:45 +02004766 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4767 srv_shutdown_streams(s, SF_ERR_DOWN);
4768
4769 /* we might have streams queued on this server and waiting for
4770 * a connection. Those which are redispatchable will be queued
4771 * to another server or to the proxy itself.
4772 */
4773 xferred = pendconn_redistribute(s);
4774
4775 tmptrash = alloc_trash_chunk();
4776 if (tmptrash) {
4777 chunk_printf(tmptrash,
4778 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4779 s->flags & SRV_F_BACKUP ? "Backup " : "",
4780 s->proxy->id, s->id,
4781 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4782
4783 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4784
4785 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004786 ha_warning("%s.\n", tmptrash->area);
4787 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4788 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004789 }
4790 free_trash_chunk(tmptrash);
4791 tmptrash = NULL;
4792 }
4793 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4794 set_backend_down(s->proxy);
4795
4796 s->counters.down_trans++;
4797 }
4798 }
4799 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4800 /* OK here we're leaving maintenance, we have many things to check,
4801 * because the server might possibly be coming back up depending on
4802 * its state. In practice, leaving maintenance means that we should
4803 * immediately turn to UP (more or less the slowstart) under the
4804 * following conditions :
4805 * - server is neither checked nor tracked
4806 * - server tracks another server which is not checked
4807 * - server tracks another server which is already up
4808 * Which sums up as something simpler :
4809 * "either the tracking server is up or the server's checks are disabled
4810 * or up". Otherwise we only re-enable health checks. There's a special
4811 * case associated to the stopping state which can be inherited. Note
4812 * that the server might still be in drain mode, which is naturally dealt
4813 * with by the lower level functions.
4814 */
4815
4816 if (s->check.state & CHK_ST_ENABLED) {
4817 s->check.state &= ~CHK_ST_PAUSED;
4818 check->health = check->rise; /* start OK but check immediately */
4819 }
4820
4821 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4822 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4823 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4824 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4825 s->next_state = SRV_ST_STOPPING;
4826 }
4827 else {
4828 s->next_state = SRV_ST_STARTING;
4829 if (s->slowstart > 0)
4830 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4831 else
4832 s->next_state = SRV_ST_RUNNING;
4833 }
4834
4835 }
4836
4837 tmptrash = alloc_trash_chunk();
4838 if (tmptrash) {
4839 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4840 chunk_printf(tmptrash,
4841 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4842 s->flags & SRV_F_BACKUP ? "Backup " : "",
4843 s->proxy->id, s->id,
4844 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4845 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4846 }
4847 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4848 chunk_printf(tmptrash,
4849 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4850 s->flags & SRV_F_BACKUP ? "Backup " : "",
4851 s->proxy->id, s->id, s->hostname,
4852 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4853 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4854 }
4855 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4856 chunk_printf(tmptrash,
4857 "%sServer %s/%s is %s/%s (leaving maintenance)",
4858 s->flags & SRV_F_BACKUP ? "Backup " : "",
4859 s->proxy->id, s->id,
4860 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4861 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4862 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004863 ha_warning("%s.\n", tmptrash->area);
4864 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4865 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004866 free_trash_chunk(tmptrash);
4867 tmptrash = NULL;
4868 }
4869
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004870 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004871 /* now propagate the status change to any LB algorithms */
4872 if (px->lbprm.update_server_eweight)
4873 px->lbprm.update_server_eweight(s);
4874 else if (srv_willbe_usable(s)) {
4875 if (px->lbprm.set_server_status_up)
4876 px->lbprm.set_server_status_up(s);
4877 }
4878 else {
4879 if (px->lbprm.set_server_status_down)
4880 px->lbprm.set_server_status_down(s);
4881 }
4882
4883 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4884 set_backend_down(s->proxy);
4885
Willy Tarreau6a78e612018-08-07 10:14:53 +02004886 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4887 * and it's not a backup server and its effective weight is > 0,
4888 * then it can accept new connections, so we shut down all streams
4889 * on all backup servers.
4890 */
4891 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4892 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4893 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4894
4895 /* check if we can handle some connections queued at the proxy. We
4896 * will take as many as we can handle.
4897 */
4898 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004899 }
4900 else if (s->next_admin & SRV_ADMF_MAINT) {
4901 /* remaining in maintenance mode, let's inform precisely about the
4902 * situation.
4903 */
4904 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4905 tmptrash = alloc_trash_chunk();
4906 if (tmptrash) {
4907 chunk_printf(tmptrash,
4908 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4909 s->flags & SRV_F_BACKUP ? "Backup " : "",
4910 s->proxy->id, s->id);
4911
4912 if (s->track) /* normally it's mandatory here */
4913 chunk_appendf(tmptrash, " via %s/%s",
4914 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004915 ha_warning("%s.\n", tmptrash->area);
4916 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4917 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004918 free_trash_chunk(tmptrash);
4919 tmptrash = NULL;
4920 }
4921 }
4922 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4923 tmptrash = alloc_trash_chunk();
4924 if (tmptrash) {
4925 chunk_printf(tmptrash,
4926 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4927 s->flags & SRV_F_BACKUP ? "Backup " : "",
4928 s->proxy->id, s->id, s->hostname);
4929
4930 if (s->track) /* normally it's mandatory here */
4931 chunk_appendf(tmptrash, " via %s/%s",
4932 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004933 ha_warning("%s.\n", tmptrash->area);
4934 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4935 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004936 free_trash_chunk(tmptrash);
4937 tmptrash = NULL;
4938 }
4939 }
4940 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4941 tmptrash = alloc_trash_chunk();
4942 if (tmptrash) {
4943 chunk_printf(tmptrash,
4944 "%sServer %s/%s remains in forced maintenance",
4945 s->flags & SRV_F_BACKUP ? "Backup " : "",
4946 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004947 ha_warning("%s.\n", tmptrash->area);
4948 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4949 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004950 free_trash_chunk(tmptrash);
4951 tmptrash = NULL;
4952 }
4953 }
4954 /* don't report anything when leaving drain mode and remaining in maintenance */
4955
4956 s->cur_admin = s->next_admin;
4957 }
4958
4959 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4960 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4961 /* drain state is applied only if not yet in maint */
4962
4963 s->last_change = now.tv_sec;
4964 if (px->lbprm.set_server_status_down)
4965 px->lbprm.set_server_status_down(s);
4966
4967 /* we might have streams queued on this server and waiting for
4968 * a connection. Those which are redispatchable will be queued
4969 * to another server or to the proxy itself.
4970 */
4971 xferred = pendconn_redistribute(s);
4972
4973 tmptrash = alloc_trash_chunk();
4974 if (tmptrash) {
4975 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4976 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4977 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4978
4979 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4980
4981 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004982 ha_warning("%s.\n", tmptrash->area);
4983 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4984 tmptrash->area);
4985 send_email_alert(s, LOG_NOTICE, "%s",
4986 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004987 }
4988 free_trash_chunk(tmptrash);
4989 tmptrash = NULL;
4990 }
4991
4992 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4993 set_backend_down(s->proxy);
4994 }
4995 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4996 /* OK completely leaving drain mode */
4997 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4998 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4999 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5000 s->proxy->last_change = now.tv_sec;
5001 }
5002
5003 if (s->last_change < now.tv_sec) // ignore negative times
5004 s->down_time += now.tv_sec - s->last_change;
5005 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005006 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005007
5008 tmptrash = alloc_trash_chunk();
5009 if (tmptrash) {
5010 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5011 chunk_printf(tmptrash,
5012 "%sServer %s/%s is %s (leaving forced drain)",
5013 s->flags & SRV_F_BACKUP ? "Backup " : "",
5014 s->proxy->id, s->id,
5015 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5016 }
5017 else {
5018 chunk_printf(tmptrash,
5019 "%sServer %s/%s is %s (leaving drain)",
5020 s->flags & SRV_F_BACKUP ? "Backup " : "",
5021 s->proxy->id, s->id,
5022 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5023 if (s->track) /* normally it's mandatory here */
5024 chunk_appendf(tmptrash, " via %s/%s",
5025 s->track->proxy->id, s->track->id);
5026 }
5027
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005028 ha_warning("%s.\n", tmptrash->area);
5029 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5030 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005031 free_trash_chunk(tmptrash);
5032 tmptrash = NULL;
5033 }
5034
5035 /* now propagate the status change to any LB algorithms */
5036 if (px->lbprm.update_server_eweight)
5037 px->lbprm.update_server_eweight(s);
5038 else if (srv_willbe_usable(s)) {
5039 if (px->lbprm.set_server_status_up)
5040 px->lbprm.set_server_status_up(s);
5041 }
5042 else {
5043 if (px->lbprm.set_server_status_down)
5044 px->lbprm.set_server_status_down(s);
5045 }
5046 }
5047 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5048 /* remaining in drain mode after removing one of its flags */
5049
5050 tmptrash = alloc_trash_chunk();
5051 if (tmptrash) {
5052 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5053 chunk_printf(tmptrash,
5054 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5055 s->flags & SRV_F_BACKUP ? "Backup " : "",
5056 s->proxy->id, s->id);
5057
5058 if (s->track) /* normally it's mandatory here */
5059 chunk_appendf(tmptrash, " via %s/%s",
5060 s->track->proxy->id, s->track->id);
5061 }
5062 else {
5063 chunk_printf(tmptrash,
5064 "%sServer %s/%s remains in forced drain mode",
5065 s->flags & SRV_F_BACKUP ? "Backup " : "",
5066 s->proxy->id, s->id);
5067 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005068 ha_warning("%s.\n", tmptrash->area);
5069 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5070 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005071 free_trash_chunk(tmptrash);
5072 tmptrash = NULL;
5073 }
5074
5075 /* commit new admin status */
5076
5077 s->cur_admin = s->next_admin;
5078 }
5079 }
5080
5081 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005082 *s->adm_st_chg_cause = 0;
5083}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005084
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005085/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005086 * Local variables:
5087 * c-indent-level: 8
5088 * c-basic-offset: 8
5089 * End:
5090 */