blob: 4cd87849d8bca882e58f62dade23e0f30fecbfe5 [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>
Willy Tarreau0108d902018-11-25 19:14:37 +010022#include <common/initcall.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010023#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020024#include <common/time.h>
25
William Lallemand222baf22016-11-19 02:00:33 +010026#include <types/applet.h>
27#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020028#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010029#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020030#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010031#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020032
William Lallemand222baf22016-11-19 02:00:33 +010033#include <proto/applet.h>
34#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090035#include <proto/checks.h>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020036#include <proto/connection.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020037#include <proto/port_range.h>
38#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020039#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010040#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020041#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020042#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010043#include <proto/stream_interface.h>
44#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020045#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020046#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010047#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020048
Willy Tarreau3ff577e2018-08-02 11:48:52 +020049static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020050static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010051static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010052static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Olivier Houchard0c18a6f2018-12-02 14:11:41 +010053static struct task *cleanup_idle_connections(struct task *task, void *ctx, unsigned short state);
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
Willy Tarreau21faa912012-10-10 08:27:36 +020055/* List head of all known server keywords */
56static struct srv_kw_list srv_keywords = {
57 .list = LIST_HEAD_INIT(srv_keywords.list)
58};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020059
Simon Hormana3608442013-11-01 16:46:15 +090060int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020061{
Emeric Brun52a91d32017-08-31 14:41:55 +020062 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020063 return s->down_time;
64
65 return now.tv_sec - s->last_change + s->down_time;
66}
Willy Tarreaubaaee002006-06-26 02:48:02 +020067
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050068int srv_lastsession(const struct server *s)
69{
70 if (s->counters.last_sess)
71 return now.tv_sec - s->counters.last_sess;
72
73 return -1;
74}
75
Simon Horman4a741432013-02-23 15:35:38 +090076int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020077{
Simon Horman4a741432013-02-23 15:35:38 +090078 const struct server *s = check->server;
79
Willy Tarreauff5ae352013-12-11 20:36:34 +010080 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090081 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010082
Emeric Brun52a91d32017-08-31 14:41:55 +020083 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090084 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010085
Simon Horman4a741432013-02-23 15:35:38 +090086 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010087}
88
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010089/*
90 * Check that we did not get a hash collision.
91 * Unlikely, but it can happen.
92 */
93static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +010094{
95 struct proxy *p = s->proxy;
96 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010097
98 for (tmpserv = p->srv; tmpserv != NULL;
99 tmpserv = tmpserv->next) {
100 if (tmpserv == s)
101 continue;
102 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
103 continue;
104 if (tmpserv->cookie &&
105 strcmp(tmpserv->cookie, s->cookie) == 0) {
106 ha_warning("We generated two equal cookies for two different servers.\n"
107 "Please change the secret key for '%s'.\n",
108 s->proxy->id);
109 }
110 }
111
112}
113
Willy Tarreau46b7f532018-08-21 11:54:26 +0200114/*
115 * Must be called with the server lock held.
116 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100117void srv_set_dyncookie(struct server *s)
118{
119 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100120 char *tmpbuf;
121 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100122 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100123 size_t buffer_len;
124 int addr_len;
125 int port;
126
127 if ((s->flags & SRV_F_COOKIESET) ||
128 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
129 s->proxy->dyncookie_key == NULL)
130 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100131 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100132
133 if (s->addr.ss_family != AF_INET &&
134 s->addr.ss_family != AF_INET6)
135 return;
136 /*
137 * Buffer to calculate the cookie value.
138 * The buffer contains the secret key + the server IP address
139 * + the TCP port.
140 */
141 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
142 /*
143 * The TCP port should use only 2 bytes, but is stored in
144 * an unsigned int in struct server, so let's use 4, to be
145 * on the safe side.
146 */
147 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200148 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100149 memcpy(tmpbuf, p->dyncookie_key, key_len);
150 memcpy(&(tmpbuf[key_len]),
151 s->addr.ss_family == AF_INET ?
152 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
153 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
154 addr_len);
155 /*
156 * Make sure it's the same across all the load balancers,
157 * no matter their endianness.
158 */
159 port = htonl(s->svc_port);
160 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
161 hash_value = XXH64(tmpbuf, buffer_len, 0);
162 memprintf(&s->cookie, "%016llx", hash_value);
163 if (!s->cookie)
164 return;
165 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100166
167 /* Don't bother checking if the dyncookie is duplicated if
168 * the server is marked as "disabled", maybe it doesn't have
169 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100170 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100171 if (!(s->next_admin & SRV_ADMF_FMAINT))
172 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100173}
174
Willy Tarreau21faa912012-10-10 08:27:36 +0200175/*
176 * Registers the server keyword list <kwl> as a list of valid keywords for next
177 * parsing sessions.
178 */
179void srv_register_keywords(struct srv_kw_list *kwl)
180{
181 LIST_ADDQ(&srv_keywords.list, &kwl->list);
182}
183
184/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
185 * keyword is found with a NULL ->parse() function, then an attempt is made to
186 * find one with a valid ->parse() function. This way it is possible to declare
187 * platform-dependant, known keywords as NULL, then only declare them as valid
188 * if some options are met. Note that if the requested keyword contains an
189 * opening parenthesis, everything from this point is ignored.
190 */
191struct srv_kw *srv_find_kw(const char *kw)
192{
193 int index;
194 const char *kwend;
195 struct srv_kw_list *kwl;
196 struct srv_kw *ret = NULL;
197
198 kwend = strchr(kw, '(');
199 if (!kwend)
200 kwend = kw + strlen(kw);
201
202 list_for_each_entry(kwl, &srv_keywords.list, list) {
203 for (index = 0; kwl->kw[index].kw != NULL; index++) {
204 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
205 kwl->kw[index].kw[kwend-kw] == 0) {
206 if (kwl->kw[index].parse)
207 return &kwl->kw[index]; /* found it !*/
208 else
209 ret = &kwl->kw[index]; /* may be OK */
210 }
211 }
212 }
213 return ret;
214}
215
216/* Dumps all registered "server" keywords to the <out> string pointer. The
217 * unsupported keywords are only dumped if their supported form was not
218 * found.
219 */
220void srv_dump_kws(char **out)
221{
222 struct srv_kw_list *kwl;
223 int index;
224
225 *out = NULL;
226 list_for_each_entry(kwl, &srv_keywords.list, list) {
227 for (index = 0; kwl->kw[index].kw != NULL; index++) {
228 if (kwl->kw[index].parse ||
229 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
230 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
231 kwl->scope,
232 kwl->kw[index].kw,
233 kwl->kw[index].skip ? " <arg>" : "",
234 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
235 kwl->kw[index].parse ? "" : " (not supported)");
236 }
237 }
238 }
239}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100240
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100241/* Parse the "addr" server keyword */
242static int srv_parse_addr(char **args, int *cur_arg,
243 struct proxy *curproxy, struct server *newsrv, char **err)
244{
245 char *errmsg, *arg;
246 struct sockaddr_storage *sk;
247 int port1, port2;
248 struct protocol *proto;
249
250 errmsg = NULL;
251 arg = args[*cur_arg + 1];
252
253 if (!*arg) {
254 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
255 goto err;
256 }
257
258 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
259 if (!sk) {
260 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
261 goto err;
262 }
263
264 proto = protocol_by_family(sk->ss_family);
265 if (!proto || !proto->connect) {
266 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
267 args[*cur_arg], arg);
268 goto err;
269 }
270
271 if (port1 != port2) {
272 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
273 args[*cur_arg], arg);
274 goto err;
275 }
276
277 newsrv->check.addr = newsrv->agent.addr = *sk;
278 newsrv->flags |= SRV_F_CHECKADDR;
279 newsrv->flags |= SRV_F_AGENTADDR;
280
281 return 0;
282
283 err:
284 free(errmsg);
285 return ERR_ALERT | ERR_FATAL;
286}
287
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100288/* Parse the "agent-check" server keyword */
289static int srv_parse_agent_check(char **args, int *cur_arg,
290 struct proxy *curproxy, struct server *newsrv, char **err)
291{
292 newsrv->do_agent = 1;
293 return 0;
294}
295
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100296/* Parse the "backup" server keyword */
297static int srv_parse_backup(char **args, int *cur_arg,
298 struct proxy *curproxy, struct server *newsrv, char **err)
299{
300 newsrv->flags |= SRV_F_BACKUP;
301 return 0;
302}
303
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100304/* Parse the "check" server keyword */
305static int srv_parse_check(char **args, int *cur_arg,
306 struct proxy *curproxy, struct server *newsrv, char **err)
307{
308 newsrv->do_check = 1;
309 return 0;
310}
311
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100312/* Parse the "check-send-proxy" server keyword */
313static int srv_parse_check_send_proxy(char **args, int *cur_arg,
314 struct proxy *curproxy, struct server *newsrv, char **err)
315{
316 newsrv->check.send_proxy = 1;
317 return 0;
318}
319
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100320/* Parse the "cookie" server keyword */
321static int srv_parse_cookie(char **args, int *cur_arg,
322 struct proxy *curproxy, struct server *newsrv, char **err)
323{
324 char *arg;
325
326 arg = args[*cur_arg + 1];
327 if (!*arg) {
328 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
329 return ERR_ALERT | ERR_FATAL;
330 }
331
332 free(newsrv->cookie);
333 newsrv->cookie = strdup(arg);
334 newsrv->cklen = strlen(arg);
335 newsrv->flags |= SRV_F_COOKIESET;
336 return 0;
337}
338
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100339/* Parse the "disabled" server keyword */
340static int srv_parse_disabled(char **args, int *cur_arg,
341 struct proxy *curproxy, struct server *newsrv, char **err)
342{
Emeric Brun52a91d32017-08-31 14:41:55 +0200343 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
344 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100345 newsrv->check.state |= CHK_ST_PAUSED;
346 newsrv->check.health = 0;
347 return 0;
348}
349
350/* Parse the "enabled" server keyword */
351static int srv_parse_enabled(char **args, int *cur_arg,
352 struct proxy *curproxy, struct server *newsrv, char **err)
353{
Emeric Brun52a91d32017-08-31 14:41:55 +0200354 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
355 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100356 newsrv->check.state &= ~CHK_ST_PAUSED;
357 newsrv->check.health = newsrv->check.rise;
358 return 0;
359}
360
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100361static int srv_parse_pool_purge_delay(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100362{
363 const char *res;
364 char *arg;
365 unsigned int time;
366
367 arg = args[*cur_arg + 1];
368 if (!*arg) {
369 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
370 return ERR_ALERT | ERR_FATAL;
371 }
372 res = parse_time_err(arg, &time, TIME_UNIT_MS);
373 if (res) {
374 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
375 *res, args[*cur_arg]);
376 return ERR_ALERT | ERR_FATAL;
377 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100378 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100379
380 return 0;
381}
382
Olivier Houchard006e3102018-12-10 18:30:32 +0100383static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
384{
385 char *arg;
386
387 arg = args[*cur_arg + 1];
388 if (!*arg) {
389 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
390 return ERR_ALERT | ERR_FATAL;
391 }
392 newsrv->max_idle_conns = atoi(arg);
393
394 return 0;
395}
396
Willy Tarreaudff55432012-10-10 17:51:05 +0200397/* parse the "id" server keyword */
398static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
399{
400 struct eb32_node *node;
401
402 if (!*args[*cur_arg + 1]) {
403 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
404 return ERR_ALERT | ERR_FATAL;
405 }
406
407 newsrv->puid = atol(args[*cur_arg + 1]);
408 newsrv->conf.id.key = newsrv->puid;
409
410 if (newsrv->puid <= 0) {
411 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
412 return ERR_ALERT | ERR_FATAL;
413 }
414
415 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
416 if (node) {
417 struct server *target = container_of(node, struct server, conf.id);
418 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
419 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
420 target->id);
421 return ERR_ALERT | ERR_FATAL;
422 }
423
424 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200425 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200426 return 0;
427}
428
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100429/* Parse the "namespace" server keyword */
430static int srv_parse_namespace(char **args, int *cur_arg,
431 struct proxy *curproxy, struct server *newsrv, char **err)
432{
433#ifdef CONFIG_HAP_NS
434 char *arg;
435
436 arg = args[*cur_arg + 1];
437 if (!*arg) {
438 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
439 return ERR_ALERT | ERR_FATAL;
440 }
441
442 if (!strcmp(arg, "*")) {
443 /* Use the namespace associated with the connection (if present). */
444 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
445 return 0;
446 }
447
448 /*
449 * As this parser may be called several times for the same 'default-server'
450 * object, or for a new 'server' instance deriving from a 'default-server'
451 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
452 */
453 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
454
455 newsrv->netns = netns_store_lookup(arg, strlen(arg));
456 if (!newsrv->netns)
457 newsrv->netns = netns_store_insert(arg);
458
459 if (!newsrv->netns) {
460 memprintf(err, "Cannot open namespace '%s'", arg);
461 return ERR_ALERT | ERR_FATAL;
462 }
463
464 return 0;
465#else
466 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
467 return ERR_ALERT | ERR_FATAL;
468#endif
469}
470
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100471/* Parse the "no-agent-check" server keyword */
472static int srv_parse_no_agent_check(char **args, int *cur_arg,
473 struct proxy *curproxy, struct server *newsrv, char **err)
474{
475 free_check(&newsrv->agent);
476 newsrv->agent.inter = 0;
477 newsrv->agent.port = 0;
478 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
479 newsrv->do_agent = 0;
480 return 0;
481}
482
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100483/* Parse the "no-backup" server keyword */
484static int srv_parse_no_backup(char **args, int *cur_arg,
485 struct proxy *curproxy, struct server *newsrv, char **err)
486{
487 newsrv->flags &= ~SRV_F_BACKUP;
488 return 0;
489}
490
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100491/* Parse the "no-check" server keyword */
492static int srv_parse_no_check(char **args, int *cur_arg,
493 struct proxy *curproxy, struct server *newsrv, char **err)
494{
495 free_check(&newsrv->check);
496 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
497 newsrv->do_check = 0;
498 return 0;
499}
500
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100501/* Parse the "no-check-send-proxy" server keyword */
502static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
503 struct proxy *curproxy, struct server *newsrv, char **err)
504{
505 newsrv->check.send_proxy = 0;
506 return 0;
507}
508
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100509/* Disable server PROXY protocol flags. */
510static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
511{
512 srv->pp_opts &= ~flags;
513 return 0;
514}
515
516/* Parse the "no-send-proxy" server keyword */
517static int srv_parse_no_send_proxy(char **args, int *cur_arg,
518 struct proxy *curproxy, struct server *newsrv, char **err)
519{
520 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
521}
522
523/* Parse the "no-send-proxy-v2" server keyword */
524static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
525 struct proxy *curproxy, struct server *newsrv, char **err)
526{
527 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
528}
529
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100530/* Parse the "non-stick" server keyword */
531static int srv_parse_non_stick(char **args, int *cur_arg,
532 struct proxy *curproxy, struct server *newsrv, char **err)
533{
534 newsrv->flags |= SRV_F_NON_STICK;
535 return 0;
536}
537
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100538/* Enable server PROXY protocol flags. */
539static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
540{
541 srv->pp_opts |= flags;
542 return 0;
543}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200544/* parse the "proto" server keyword */
545static int srv_parse_proto(char **args, int *cur_arg,
546 struct proxy *px, struct server *newsrv, char **err)
547{
548 struct ist proto;
549
550 if (!*args[*cur_arg + 1]) {
551 memprintf(err, "'%s' : missing value", args[*cur_arg]);
552 return ERR_ALERT | ERR_FATAL;
553 }
554 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
555 newsrv->mux_proto = get_mux_proto(proto);
556 if (!newsrv->mux_proto) {
557 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
558 return ERR_ALERT | ERR_FATAL;
559 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200560 return 0;
561}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100562
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100563/* parse the "proxy-v2-options" */
564static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
565 struct proxy *px, struct server *newsrv, char **err)
566{
567 char *p, *n;
568 for (p = args[*cur_arg+1]; p; p = n) {
569 n = strchr(p, ',');
570 if (n)
571 *n++ = '\0';
572 if (!strcmp(p, "ssl")) {
573 newsrv->pp_opts |= SRV_PP_V2_SSL;
574 } else if (!strcmp(p, "cert-cn")) {
575 newsrv->pp_opts |= SRV_PP_V2_SSL;
576 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100577 } else if (!strcmp(p, "cert-key")) {
578 newsrv->pp_opts |= SRV_PP_V2_SSL;
579 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
580 } else if (!strcmp(p, "cert-sig")) {
581 newsrv->pp_opts |= SRV_PP_V2_SSL;
582 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
583 } else if (!strcmp(p, "ssl-cipher")) {
584 newsrv->pp_opts |= SRV_PP_V2_SSL;
585 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100586 } else if (!strcmp(p, "authority")) {
587 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100588 } else if (!strcmp(p, "crc32c")) {
589 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100590 } else
591 goto fail;
592 }
593 return 0;
594 fail:
595 if (err)
596 memprintf(err, "'%s' : proxy v2 option not implemented", p);
597 return ERR_ALERT | ERR_FATAL;
598}
599
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100600/* Parse the "observe" server keyword */
601static int srv_parse_observe(char **args, int *cur_arg,
602 struct proxy *curproxy, struct server *newsrv, char **err)
603{
604 char *arg;
605
606 arg = args[*cur_arg + 1];
607 if (!*arg) {
608 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
609 return ERR_ALERT | ERR_FATAL;
610 }
611
612 if (!strcmp(arg, "none")) {
613 newsrv->observe = HANA_OBS_NONE;
614 }
615 else if (!strcmp(arg, "layer4")) {
616 newsrv->observe = HANA_OBS_LAYER4;
617 }
618 else if (!strcmp(arg, "layer7")) {
619 if (curproxy->mode != PR_MODE_HTTP) {
620 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
621 return ERR_ALERT;
622 }
623 newsrv->observe = HANA_OBS_LAYER7;
624 }
625 else {
626 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
627 "but got '%s'\n", args[*cur_arg], arg);
628 return ERR_ALERT | ERR_FATAL;
629 }
630
631 return 0;
632}
633
Frédéric Lécaille16186232017-03-14 16:42:49 +0100634/* Parse the "redir" server keyword */
635static int srv_parse_redir(char **args, int *cur_arg,
636 struct proxy *curproxy, struct server *newsrv, char **err)
637{
638 char *arg;
639
640 arg = args[*cur_arg + 1];
641 if (!*arg) {
642 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
643 return ERR_ALERT | ERR_FATAL;
644 }
645
646 free(newsrv->rdr_pfx);
647 newsrv->rdr_pfx = strdup(arg);
648 newsrv->rdr_len = strlen(arg);
649
650 return 0;
651}
652
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100653/* Parse the "send-proxy" server keyword */
654static int srv_parse_send_proxy(char **args, int *cur_arg,
655 struct proxy *curproxy, struct server *newsrv, char **err)
656{
657 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
658}
659
660/* Parse the "send-proxy-v2" server keyword */
661static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
662 struct proxy *curproxy, struct server *newsrv, char **err)
663{
664 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
665}
666
Frédéric Lécailledba97072017-03-17 15:33:50 +0100667
668/* Parse the "source" server keyword */
669static int srv_parse_source(char **args, int *cur_arg,
670 struct proxy *curproxy, struct server *newsrv, char **err)
671{
672 char *errmsg;
673 int port_low, port_high;
674 struct sockaddr_storage *sk;
675 struct protocol *proto;
676
677 errmsg = NULL;
678
679 if (!*args[*cur_arg + 1]) {
680 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
681 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
682 goto err;
683 }
684
685 /* 'sk' is statically allocated (no need to be freed). */
686 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
687 if (!sk) {
688 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
689 goto err;
690 }
691
692 proto = protocol_by_family(sk->ss_family);
693 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100694 ha_alert("'%s %s' : connect() not supported for this address family.\n",
695 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100696 goto err;
697 }
698
699 newsrv->conn_src.opts |= CO_SRC_BIND;
700 newsrv->conn_src.source_addr = *sk;
701
702 if (port_low != port_high) {
703 int i;
704
705 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100706 ha_alert("'%s' does not support port offsets (found '%s').\n",
707 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100708 goto err;
709 }
710
711 if (port_low <= 0 || port_low > 65535 ||
712 port_high <= 0 || port_high > 65535 ||
713 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100714 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 +0100715 goto err;
716 }
717 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
718 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
719 newsrv->conn_src.sport_range->ports[i] = port_low + i;
720 }
721
722 *cur_arg += 2;
723 while (*(args[*cur_arg])) {
724 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
725#if defined(CONFIG_HAP_TRANSPARENT)
726 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100727 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
728 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100729 goto err;
730 }
731 if (!strcmp(args[*cur_arg + 1], "client")) {
732 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
733 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
734 }
735 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
736 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
737 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
738 }
739 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
740 char *name, *end;
741
742 name = args[*cur_arg + 1] + 7;
743 while (isspace(*name))
744 name++;
745
746 end = name;
747 while (*end && !isspace(*end) && *end != ',' && *end != ')')
748 end++;
749
750 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
751 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
752 free(newsrv->conn_src.bind_hdr_name);
753 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
754 newsrv->conn_src.bind_hdr_len = end - name;
755 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
756 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
757 newsrv->conn_src.bind_hdr_occ = -1;
758
759 /* now look for an occurrence number */
760 while (isspace(*end))
761 end++;
762 if (*end == ',') {
763 end++;
764 name = end;
765 if (*end == '-')
766 end++;
767 while (isdigit((int)*end))
768 end++;
769 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
770 }
771
772 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100773 ha_alert("usesrc hdr_ip(name,num) does not support negative"
774 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100775 goto err;
776 }
777 }
778 else {
779 struct sockaddr_storage *sk;
780 int port1, port2;
781
782 /* 'sk' is statically allocated (no need to be freed). */
783 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
784 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100785 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100786 goto err;
787 }
788
789 proto = protocol_by_family(sk->ss_family);
790 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100791 ha_alert("'%s %s' : connect() not supported for this address family.\n",
792 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100793 goto err;
794 }
795
796 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100797 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
798 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100799 goto err;
800 }
801 newsrv->conn_src.tproxy_addr = *sk;
802 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
803 }
804 global.last_checks |= LSTCHK_NETADM;
805 *cur_arg += 2;
806 continue;
807#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100808 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 +0100809 goto err;
810#endif /* defined(CONFIG_HAP_TRANSPARENT) */
811 } /* "usesrc" */
812
813 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
814#ifdef SO_BINDTODEVICE
815 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100816 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100817 goto err;
818 }
819 free(newsrv->conn_src.iface_name);
820 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
821 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
822 global.last_checks |= LSTCHK_NETADM;
823#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100824 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100825 goto err;
826#endif
827 *cur_arg += 2;
828 continue;
829 }
830 /* this keyword in not an option of "source" */
831 break;
832 } /* while */
833
834 return 0;
835
836 err:
837 free(errmsg);
838 return ERR_ALERT | ERR_FATAL;
839}
840
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100841/* Parse the "stick" server keyword */
842static int srv_parse_stick(char **args, int *cur_arg,
843 struct proxy *curproxy, struct server *newsrv, char **err)
844{
845 newsrv->flags &= ~SRV_F_NON_STICK;
846 return 0;
847}
848
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100849/* Parse the "track" server keyword */
850static int srv_parse_track(char **args, int *cur_arg,
851 struct proxy *curproxy, struct server *newsrv, char **err)
852{
853 char *arg;
854
855 arg = args[*cur_arg + 1];
856 if (!*arg) {
857 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
858 return ERR_ALERT | ERR_FATAL;
859 }
860
861 free(newsrv->trackit);
862 newsrv->trackit = strdup(arg);
863
864 return 0;
865}
866
Frédéric Lécailledba97072017-03-17 15:33:50 +0100867
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200868/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200869 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200870 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200871 *
872 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200873 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200874void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200875{
Willy Tarreau87b09662015-04-03 00:22:06 +0200876 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200877
Willy Tarreau87b09662015-04-03 00:22:06 +0200878 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
879 if (stream->srv_conn == srv)
880 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200881}
882
883/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200884 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200885 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200886 *
887 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200888 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200889void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200890{
891 struct server *srv;
892
893 for (srv = px->srv; srv != NULL; srv = srv->next)
894 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200895 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200896}
897
Willy Tarreaubda92272014-05-20 21:55:30 +0200898/* Appends some information to a message string related to a server going UP or
899 * DOWN. If both <forced> and <reason> are null and the server tracks another
900 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200901 * If <check> is non-null, an entire string describing the check result will be
902 * appended after a comma and a space (eg: to report some information from the
903 * check that changed the state). In the other case, the string will be built
904 * using the check results stored into the struct server if present.
905 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200906 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200907 *
908 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200909 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200910void srv_append_status(struct buffer *msg, struct server *s,
911 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200912{
Emeric Brun5a133512017-10-19 14:42:30 +0200913 short status = s->op_st_chg.status;
914 short code = s->op_st_chg.code;
915 long duration = s->op_st_chg.duration;
916 char *desc = s->op_st_chg.reason;
917
918 if (check) {
919 status = check->status;
920 code = check->code;
921 duration = check->duration;
922 desc = check->desc;
923 }
924
925 if (status != -1) {
926 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
927
928 if (status >= HCHK_STATUS_L57DATA)
929 chunk_appendf(msg, ", code: %d", code);
930
931 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200932 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200933
934 chunk_appendf(msg, ", info: \"");
935
936 chunk_initlen(&src, desc, 0, strlen(desc));
937 chunk_asciiencode(msg, &src, '"');
938
939 chunk_appendf(msg, "\"");
940 }
941
942 if (duration >= 0)
943 chunk_appendf(msg, ", check duration: %ldms", duration);
944 }
945 else if (desc && *desc) {
946 chunk_appendf(msg, ", %s", desc);
947 }
948 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200949 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200950 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200951
952 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200953 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200954 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
955 " %d sessions active, %d requeued, %d remaining in queue",
956 s->proxy->srv_act, s->proxy->srv_bck,
957 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
958 s->cur_sess, xferred, s->nbpend);
959 else
960 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
961 " %d sessions requeued, %d total in queue",
962 s->proxy->srv_act, s->proxy->srv_bck,
963 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
964 xferred, s->nbpend);
965 }
966}
967
Emeric Brun5a133512017-10-19 14:42:30 +0200968/* Marks server <s> down, regardless of its checks' statuses. The server is
969 * registered in a list to postpone the counting of the remaining servers on
970 * the proxy and transfers queued streams whenever possible to other servers at
971 * a sync point. Maintenance servers are ignored. It stores the <reason> if
972 * non-null as the reason for going down or the available data from the check
973 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200974 *
975 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200976 */
Emeric Brun5a133512017-10-19 14:42:30 +0200977void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200978{
979 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200980
Emeric Brun64cc49c2017-10-03 14:46:45 +0200981 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200982 return;
983
Emeric Brun52a91d32017-08-31 14:41:55 +0200984 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200985 *s->op_st_chg.reason = 0;
986 s->op_st_chg.status = -1;
987 if (reason) {
988 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
989 }
990 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100991 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200992 s->op_st_chg.code = check->code;
993 s->op_st_chg.status = check->status;
994 s->op_st_chg.duration = check->duration;
995 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200996
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200997 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200998 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200999
Emeric Brun9f0b4582017-10-23 14:39:51 +02001000 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001001 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001002 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001003 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001004 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001005}
1006
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001007/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001008 * in maintenance. The server is registered in a list to postpone the counting
1009 * of the remaining servers on the proxy and tries to grab requests from the
1010 * proxy at a sync point. Maintenance servers are ignored. It stores the
1011 * <reason> if non-null as the reason for going down or the available data
1012 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001013 *
1014 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001015 */
Emeric Brun5a133512017-10-19 14:42:30 +02001016void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001017{
1018 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001019
Emeric Brun64cc49c2017-10-03 14:46:45 +02001020 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001021 return;
1022
Emeric Brun52a91d32017-08-31 14:41:55 +02001023 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001024 return;
1025
Emeric Brun52a91d32017-08-31 14:41:55 +02001026 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001027 *s->op_st_chg.reason = 0;
1028 s->op_st_chg.status = -1;
1029 if (reason) {
1030 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1031 }
1032 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001033 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001034 s->op_st_chg.code = check->code;
1035 s->op_st_chg.status = check->status;
1036 s->op_st_chg.duration = check->duration;
1037 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001038
Emeric Brun64cc49c2017-10-03 14:46:45 +02001039 if (s->slowstart <= 0)
1040 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001041
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001042 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001043 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001044
Emeric Brun9f0b4582017-10-23 14:39:51 +02001045 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001046 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001047 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001048 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001049 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001050}
1051
Willy Tarreau8eb77842014-05-21 13:54:57 +02001052/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001053 * isn't in maintenance. The server is registered in a list to postpone the
1054 * counting of the remaining servers on the proxy and tries to grab requests
1055 * from the proxy. Maintenance servers are ignored. It stores the
1056 * <reason> if non-null as the reason for going down or the available data
1057 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001058 * up. Note that it makes use of the trash to build the log strings, so <reason>
1059 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001060 *
1061 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001062 */
Emeric Brun5a133512017-10-19 14:42:30 +02001063void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001064{
1065 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001066
Emeric Brun64cc49c2017-10-03 14:46:45 +02001067 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001068 return;
1069
Emeric Brun52a91d32017-08-31 14:41:55 +02001070 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001071 return;
1072
Emeric Brun52a91d32017-08-31 14:41:55 +02001073 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001074 *s->op_st_chg.reason = 0;
1075 s->op_st_chg.status = -1;
1076 if (reason) {
1077 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1078 }
1079 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001080 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001081 s->op_st_chg.code = check->code;
1082 s->op_st_chg.status = check->status;
1083 s->op_st_chg.duration = check->duration;
1084 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001085
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001086 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001087 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001088
Emeric Brun9f0b4582017-10-23 14:39:51 +02001089 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001090 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001091 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001092 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001093 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001094}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001095
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001096/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1097 * enforce either maint mode or drain mode. It is not allowed to set more than
1098 * one flag at once. The equivalent "inherited" flag is propagated to all
1099 * tracking servers. Maintenance mode disables health checks (but not agent
1100 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001101 * is done. If <cause> is non-null, it will be displayed at the end of the log
1102 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001103 *
1104 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001105 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001106void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001107{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001108 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001109
1110 if (!mode)
1111 return;
1112
1113 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001114 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001115 return;
1116
Emeric Brun52a91d32017-08-31 14:41:55 +02001117 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001118 if (cause)
1119 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1120
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001121 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001122 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001123
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001124 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001125 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1126 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001127 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001128
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001129 /* compute the inherited flag to propagate */
1130 if (mode & SRV_ADMF_MAINT)
1131 mode = SRV_ADMF_IMAINT;
1132 else if (mode & SRV_ADMF_DRAIN)
1133 mode = SRV_ADMF_IDRAIN;
1134
Emeric Brun9f0b4582017-10-23 14:39:51 +02001135 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001136 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001137 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001138 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001139 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001140}
1141
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001142/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1143 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1144 * than one flag at once. The equivalent "inherited" flag is propagated to all
1145 * tracking servers. Leaving maintenance mode re-enables health checks. When
1146 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001147 *
1148 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001149 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001150void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001151{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001152 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001153
1154 if (!mode)
1155 return;
1156
1157 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001158 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001159 return;
1160
Emeric Brun52a91d32017-08-31 14:41:55 +02001161 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001162
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001163 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001164 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001165
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001166 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001167 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1168 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001169 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001170
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001171 if (mode & SRV_ADMF_MAINT)
1172 mode = SRV_ADMF_IMAINT;
1173 else if (mode & SRV_ADMF_DRAIN)
1174 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001175
Emeric Brun9f0b4582017-10-23 14:39:51 +02001176 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001177 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001178 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001179 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001180 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001181}
1182
Willy Tarreau757478e2016-11-03 19:22:19 +01001183/* principle: propagate maint and drain to tracking servers. This is useful
1184 * upon startup so that inherited states are correct.
1185 */
1186static void srv_propagate_admin_state(struct server *srv)
1187{
1188 struct server *srv2;
1189
1190 if (!srv->trackers)
1191 return;
1192
1193 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001194 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001195 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001196 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001197
Emeric Brun52a91d32017-08-31 14:41:55 +02001198 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001199 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001200 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001201 }
1202}
1203
1204/* Compute and propagate the admin states for all servers in proxy <px>.
1205 * Only servers *not* tracking another one are considered, because other
1206 * ones will be handled when the server they track is visited.
1207 */
1208void srv_compute_all_admin_states(struct proxy *px)
1209{
1210 struct server *srv;
1211
1212 for (srv = px->srv; srv; srv = srv->next) {
1213 if (srv->track)
1214 continue;
1215 srv_propagate_admin_state(srv);
1216 }
1217}
1218
Willy Tarreaudff55432012-10-10 17:51:05 +02001219/* Note: must not be declared <const> as its list will be overwritten.
1220 * Please take care of keeping this list alphabetically sorted, doing so helps
1221 * all code contributors.
1222 * Optional keywords are also declared with a NULL ->parse() function so that
1223 * the config parser can report an appropriate error when a known keyword was
1224 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001225 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001226 */
1227static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001228 { "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 +01001229 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001230 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001231 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001232 { "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 +01001233 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001234 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1235 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001236 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001237 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001238 { "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 +01001239 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001240 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001241 { "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 +01001242 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1243 { "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 +01001244 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001245 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001246 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1 }, /* Set the max number of orphan idle connections, 0 means unlimited */
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001247 { "pool-purge-delay", srv_parse_pool_purge_delay, 1, 1 }, /* Set the time before we destroy orphan idle connections, defaults to 1s */
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001248 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001249 { "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 +01001250 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001251 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1252 { "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 +02001253 { "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 +01001254 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001255 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001256 { NULL, NULL, 0 },
1257}};
1258
Willy Tarreau0108d902018-11-25 19:14:37 +01001259INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001260
Willy Tarreau004e0452013-11-21 11:22:01 +01001261/* Recomputes the server's eweight based on its state, uweight, the current time,
1262 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001263 * state is automatically disabled if the time is elapsed. If <must_update> is
1264 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001265 *
1266 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001267 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001268void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001269{
1270 struct proxy *px = sv->proxy;
1271 unsigned w;
1272
1273 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1274 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001275 if (sv->next_state == SRV_ST_STARTING)
1276 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001277 }
1278
1279 /* We must take care of not pushing the server to full throttle during slow starts.
1280 * It must also start immediately, at least at the minimal step when leaving maintenance.
1281 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001282 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001283 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1284 else
1285 w = px->lbprm.wdiv;
1286
Emeric Brun52a91d32017-08-31 14:41:55 +02001287 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001288
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001289 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001290 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001291 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001292}
1293
Willy Tarreaubaaee002006-06-26 02:48:02 +02001294/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001295 * Parses weight_str and configures sv accordingly.
1296 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001297 *
1298 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001299 */
1300const char *server_parse_weight_change_request(struct server *sv,
1301 const char *weight_str)
1302{
1303 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001304 long int w;
1305 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001306
1307 px = sv->proxy;
1308
1309 /* if the weight is terminated with '%', it is set relative to
1310 * the initial weight, otherwise it is absolute.
1311 */
1312 if (!*weight_str)
1313 return "Require <weight> or <weight%>.\n";
1314
Simon Hormanb796afa2013-02-12 10:45:53 +09001315 w = strtol(weight_str, &end, 10);
1316 if (end == weight_str)
1317 return "Empty weight string empty or preceded by garbage";
1318 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001319 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001320 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001321 /* Avoid integer overflow */
1322 if (w > 25600)
1323 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001324 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001325 if (w > 256)
1326 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001327 }
1328 else if (w < 0 || w > 256)
1329 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001330 else if (end[0] != '\0')
1331 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001332
1333 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1334 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1335
1336 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001337 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001338
1339 return NULL;
1340}
1341
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001342/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001343 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1344 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001345 * Returns:
1346 * - error string on error
1347 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001348 *
1349 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001350 */
1351const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001352 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001353{
1354 unsigned char ip[INET6_ADDRSTRLEN];
1355
1356 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001357 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001358 return NULL;
1359 }
1360 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001361 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001362 return NULL;
1363 }
1364
1365 return "Could not understand IP address format.\n";
1366}
1367
Willy Tarreau46b7f532018-08-21 11:54:26 +02001368/*
1369 * Must be called with the server lock held.
1370 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001371const char *server_parse_maxconn_change_request(struct server *sv,
1372 const char *maxconn_str)
1373{
1374 long int v;
1375 char *end;
1376
1377 if (!*maxconn_str)
1378 return "Require <maxconn>.\n";
1379
1380 v = strtol(maxconn_str, &end, 10);
1381 if (end == maxconn_str)
1382 return "maxconn string empty or preceded by garbage";
1383 else if (end[0] != '\0')
1384 return "Trailing garbage in maxconn string";
1385
1386 if (sv->maxconn == sv->minconn) { // static maxconn
1387 sv->maxconn = sv->minconn = v;
1388 } else { // dynamic maxconn
1389 sv->maxconn = v;
1390 }
1391
1392 if (may_dequeue_tasks(sv, sv->proxy))
1393 process_srv_queue(sv);
1394
1395 return NULL;
1396}
1397
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001398#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001399static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1400 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001401{
1402 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001403 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001404 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001405 NULL,
1406 };
1407
1408 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001409 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001410
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001411 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1412}
1413
1414static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1415{
1416 struct sample_expr *expr;
1417
1418 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 +01001419 if (!expr) {
1420 memprintf(err, "error detected while parsing sni expression : %s", *err);
1421 return ERR_ALERT | ERR_FATAL;
1422 }
1423
1424 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1425 memprintf(err, "error detected while parsing sni expression : "
1426 " fetch method '%s' extracts information from '%s', "
1427 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001428 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001429 return ERR_ALERT | ERR_FATAL;
1430 }
1431
1432 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1433 release_sample_expr(newsrv->ssl_ctx.sni);
1434 newsrv->ssl_ctx.sni = expr;
1435
1436 return 0;
1437}
1438#endif
1439
1440static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1441{
1442 if (err && *err) {
1443 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001444 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 +01001445 }
1446 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001447 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1448 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001449}
1450
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001451static void srv_conn_src_sport_range_cpy(struct server *srv,
1452 struct server *src)
1453{
1454 int range_sz;
1455
1456 range_sz = src->conn_src.sport_range->size;
1457 if (range_sz > 0) {
1458 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1459 if (srv->conn_src.sport_range != NULL) {
1460 int i;
1461
1462 for (i = 0; i < range_sz; i++) {
1463 srv->conn_src.sport_range->ports[i] =
1464 src->conn_src.sport_range->ports[i];
1465 }
1466 }
1467 }
1468}
1469
1470/*
1471 * Copy <src> server connection source settings to <srv> server everything needed.
1472 */
1473static void srv_conn_src_cpy(struct server *srv, struct server *src)
1474{
1475 srv->conn_src.opts = src->conn_src.opts;
1476 srv->conn_src.source_addr = src->conn_src.source_addr;
1477
1478 /* Source port range copy. */
1479 if (src->conn_src.sport_range != NULL)
1480 srv_conn_src_sport_range_cpy(srv, src);
1481
1482#ifdef CONFIG_HAP_TRANSPARENT
1483 if (src->conn_src.bind_hdr_name != NULL) {
1484 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1485 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1486 }
1487 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1488 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1489#endif
1490 if (src->conn_src.iface_name != NULL)
1491 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1492}
1493
1494/*
1495 * Copy <src> server SSL settings to <srv> server allocating
1496 * everything needed.
1497 */
1498#if defined(USE_OPENSSL)
1499static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1500{
1501 if (src->ssl_ctx.ca_file != NULL)
1502 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1503 if (src->ssl_ctx.crl_file != NULL)
1504 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1505 if (src->ssl_ctx.client_crt != NULL)
1506 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1507
1508 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1509
1510 if (src->ssl_ctx.verify_host != NULL)
1511 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1512 if (src->ssl_ctx.ciphers != NULL)
1513 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001514#if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
1515 if (src->ssl_ctx.ciphersuites != NULL)
1516 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1517#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001518 if (src->sni_expr != NULL)
1519 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001520
1521#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1522 if (src->ssl_ctx.alpn_str) {
1523 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1524 if (srv->ssl_ctx.alpn_str) {
1525 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1526 src->ssl_ctx.alpn_len);
1527 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1528 }
1529 }
1530#endif
1531#ifdef OPENSSL_NPN_NEGOTIATED
1532 if (src->ssl_ctx.npn_str) {
1533 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1534 if (srv->ssl_ctx.npn_str) {
1535 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1536 src->ssl_ctx.npn_len);
1537 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1538 }
1539 }
1540#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001541}
1542#endif
1543
1544/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001545 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001546 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001547 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001548 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001549static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001550{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001551 char *hostname_dn;
1552 int hostname_len, hostname_dn_len;
1553
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001554 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001555 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001556
Christopher Faulet67957bd2017-09-27 11:00:59 +02001557 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001558 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001559 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1560 hostname_dn, trash.size);
1561 if (hostname_dn_len == -1)
1562 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001563
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001564
Christopher Faulet67957bd2017-09-27 11:00:59 +02001565 free(srv->hostname);
1566 free(srv->hostname_dn);
1567 srv->hostname = strdup(hostname);
1568 srv->hostname_dn = strdup(hostname_dn);
1569 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001570 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001571 goto err;
1572
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001573 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001574
1575 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001576 free(srv->hostname); srv->hostname = NULL;
1577 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001578 return -1;
1579}
1580
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001581/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001582 * Copy <src> server settings to <srv> server allocating
1583 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001584 * This function is not supposed to be called at any time, but only
1585 * during server settings parsing or during server allocations from
1586 * a server template, and just after having calloc()'ed a new server.
1587 * So, <src> may only be a default server (when parsing server settings)
1588 * or a server template (during server allocations from a server template).
1589 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1590 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001591 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001592static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001593{
1594 /* Connection source settings copy */
1595 srv_conn_src_cpy(srv, src);
1596
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001597 if (srv_tmpl) {
1598 srv->addr = src->addr;
1599 srv->svc_port = src->svc_port;
1600 }
1601
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001602 srv->pp_opts = src->pp_opts;
1603 if (src->rdr_pfx != NULL) {
1604 srv->rdr_pfx = strdup(src->rdr_pfx);
1605 srv->rdr_len = src->rdr_len;
1606 }
1607 if (src->cookie != NULL) {
1608 srv->cookie = strdup(src->cookie);
1609 srv->cklen = src->cklen;
1610 }
1611 srv->use_ssl = src->use_ssl;
1612 srv->check.addr = srv->agent.addr = src->check.addr;
1613 srv->check.use_ssl = src->check.use_ssl;
1614 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001615 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001616 srv->check.alpn_str = src->check.alpn_str;
1617 srv->check.alpn_len = srv->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001618 /* Note: 'flags' field has potentially been already initialized. */
1619 srv->flags |= src->flags;
1620 srv->do_check = src->do_check;
1621 srv->do_agent = src->do_agent;
1622 if (srv->check.port)
1623 srv->flags |= SRV_F_CHECKPORT;
1624 srv->check.inter = src->check.inter;
1625 srv->check.fastinter = src->check.fastinter;
1626 srv->check.downinter = src->check.downinter;
1627 srv->agent.use_ssl = src->agent.use_ssl;
1628 srv->agent.port = src->agent.port;
1629 if (src->agent.send_string != NULL)
1630 srv->agent.send_string = strdup(src->agent.send_string);
1631 srv->agent.send_string_len = src->agent.send_string_len;
1632 srv->agent.inter = src->agent.inter;
1633 srv->agent.fastinter = src->agent.fastinter;
1634 srv->agent.downinter = src->agent.downinter;
1635 srv->maxqueue = src->maxqueue;
1636 srv->minconn = src->minconn;
1637 srv->maxconn = src->maxconn;
1638 srv->slowstart = src->slowstart;
1639 srv->observe = src->observe;
1640 srv->onerror = src->onerror;
1641 srv->onmarkeddown = src->onmarkeddown;
1642 srv->onmarkedup = src->onmarkedup;
1643 if (src->trackit != NULL)
1644 srv->trackit = strdup(src->trackit);
1645 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1646 srv->uweight = srv->iweight = src->iweight;
1647
1648 srv->check.send_proxy = src->check.send_proxy;
1649 /* health: up, but will fall down at first failure */
1650 srv->check.rise = srv->check.health = src->check.rise;
1651 srv->check.fall = src->check.fall;
1652
1653 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001654 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1655 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1656 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001657 srv->check.state |= CHK_ST_PAUSED;
1658 srv->check.health = 0;
1659 }
1660
1661 /* health: up but will fall down at first failure */
1662 srv->agent.rise = srv->agent.health = src->agent.rise;
1663 srv->agent.fall = src->agent.fall;
1664
1665 if (src->resolvers_id != NULL)
1666 srv->resolvers_id = strdup(src->resolvers_id);
1667 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001668 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001669 if (srv->dns_opts.family_prio == AF_UNSPEC)
1670 srv->dns_opts.family_prio = AF_INET6;
1671 memcpy(srv->dns_opts.pref_net,
1672 src->dns_opts.pref_net,
1673 sizeof srv->dns_opts.pref_net);
1674 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1675
1676 srv->init_addr_methods = src->init_addr_methods;
1677 srv->init_addr = src->init_addr;
1678#if defined(USE_OPENSSL)
1679 srv_ssl_settings_cpy(srv, src);
1680#endif
1681#ifdef TCP_USER_TIMEOUT
1682 srv->tcp_ut = src->tcp_ut;
1683#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001684 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001685 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001686 srv->max_idle_conns = src->max_idle_conns;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001687
Olivier Houchard8da5f982017-08-04 18:35:36 +02001688 if (srv_tmpl)
1689 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001690}
1691
William Lallemand313bfd12018-10-26 14:47:32 +02001692struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001693{
1694 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001695 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001696
1697 srv = calloc(1, sizeof *srv);
1698 if (!srv)
1699 return NULL;
1700
1701 srv->obj_type = OBJ_TYPE_SERVER;
1702 srv->proxy = proxy;
1703 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001704 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001705
1706 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1707 goto free_srv;
1708 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1709 goto free_priv_conns;
1710 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1711 goto free_idle_conns;
1712
1713 for (i = 0; i < global.nbthread; i++) {
1714 LIST_INIT(&srv->priv_conns[i]);
1715 LIST_INIT(&srv->idle_conns[i]);
1716 LIST_INIT(&srv->safe_conns[i]);
1717 }
1718
Emeric Brun52a91d32017-08-31 14:41:55 +02001719 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001720 srv->last_change = now.tv_sec;
1721
1722 srv->check.status = HCHK_STATUS_INI;
1723 srv->check.server = srv;
1724 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1725
1726 srv->agent.status = HCHK_STATUS_INI;
1727 srv->agent.server = srv;
1728 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1729
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001730 srv->pool_purge_delay = 1000;
Olivier Houchard006e3102018-12-10 18:30:32 +01001731 srv->max_idle_conns = -1;
1732
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001733 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001734
1735 free_idle_conns:
1736 free(srv->idle_conns);
1737 free_priv_conns:
1738 free(srv->priv_conns);
1739 free_srv:
1740 free(srv);
1741 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001742}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001743
1744/*
1745 * Validate <srv> server health-check settings.
1746 * Returns 0 if everything is OK, -1 if not.
1747 */
1748static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1749{
1750 struct tcpcheck_rule *r = NULL;
1751 struct list *l;
1752
1753 /*
1754 * We need at least a service port, a check port or the first tcp-check rule must
1755 * be a 'connect' one when checking an IPv4/IPv6 server.
1756 */
1757 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1758 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1759 return 0;
1760
1761 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1762 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001763 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1764 "Check has been disabled.\n",
1765 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001766 return -1;
1767 }
1768
1769 /* search the first action (connect / send / expect) in the list */
1770 l = &srv->proxy->tcpcheck_rules;
1771 list_for_each_entry(r, l, list) {
1772 if (r->action != TCPCHK_ACT_COMMENT)
1773 break;
1774 }
1775
1776 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001777 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1778 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1779 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001780 return -1;
1781 }
1782
1783 /* scan the tcp-check ruleset to ensure a port has been configured */
1784 l = &srv->proxy->tcpcheck_rules;
1785 list_for_each_entry(r, l, list) {
1786 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001787 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1788 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1789 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001790 return -1;
1791 }
1792 }
1793
1794 return 0;
1795}
1796
1797/*
1798 * Initialize <srv> health-check structure.
1799 * Returns the error string in case of memory allocation failure, NULL if not.
1800 */
1801static const char *do_health_check_init(struct server *srv, int check_type, int state)
1802{
1803 const char *ret;
1804
1805 if (!srv->do_check)
1806 return NULL;
1807
1808 ret = init_check(&srv->check, check_type);
1809 if (ret)
1810 return ret;
1811
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001812 srv->check.state |= state;
1813 global.maxsock++;
1814
1815 return NULL;
1816}
1817
1818static int server_health_check_init(const char *file, int linenum,
1819 struct server *srv, struct proxy *curproxy)
1820{
1821 const char *ret;
1822
1823 if (!srv->do_check)
1824 return 0;
1825
1826 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001827 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1828 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001829 return ERR_ALERT | ERR_FATAL;
1830 }
1831
1832 if (server_healthcheck_validate(file, linenum, srv) < 0)
1833 return ERR_ALERT | ERR_ABORT;
1834
1835 /* note: check type will be set during the config review phase */
1836 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1837 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001838 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001839 return ERR_ALERT | ERR_ABORT;
1840 }
1841
1842 return 0;
1843}
1844
1845/*
1846 * Initialize <srv> agent check structure.
1847 * Returns the error string in case of memory allocation failure, NULL if not.
1848 */
1849static const char *do_server_agent_check_init(struct server *srv, int state)
1850{
1851 const char *ret;
1852
1853 if (!srv->do_agent)
1854 return NULL;
1855
1856 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1857 if (ret)
1858 return ret;
1859
1860 if (!srv->agent.inter)
1861 srv->agent.inter = srv->check.inter;
1862
1863 srv->agent.state |= state;
1864 global.maxsock++;
1865
1866 return NULL;
1867}
1868
1869static int server_agent_check_init(const char *file, int linenum,
1870 struct server *srv, struct proxy *curproxy)
1871{
1872 const char *ret;
1873
1874 if (!srv->do_agent)
1875 return 0;
1876
1877 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001878 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 +02001879 file, linenum, srv->id);
1880 return ERR_ALERT | ERR_FATAL;
1881 }
1882
1883 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1884 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001885 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001886 return ERR_ALERT | ERR_ABORT;
1887 }
1888
1889 return 0;
1890}
1891
1892#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1893static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1894 struct server *srv, struct proxy *proxy)
1895{
1896 int ret;
1897 char *err = NULL;
1898
1899 if (!srv->sni_expr)
1900 return 0;
1901
1902 ret = server_parse_sni_expr(srv, proxy, &err);
1903 if (!ret)
1904 return 0;
1905
1906 display_parser_err(file, linenum, args, cur_arg, &err);
1907 free(err);
1908
1909 return ret;
1910}
1911#endif
1912
1913/*
1914 * Server initializations finalization.
1915 * Initialize health check, agent check and SNI expression if enabled.
1916 * Must not be called for a default server instance.
1917 */
1918static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1919 struct server *srv, struct proxy *px)
1920{
1921 int ret;
1922
1923 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1924 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1925 return ret;
1926 }
1927
1928#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1929 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1930 return ret;
1931#endif
1932
1933 if (srv->flags & SRV_F_BACKUP)
1934 px->srv_bck++;
1935 else
1936 px->srv_act++;
1937 srv_lb_commit_status(srv);
1938
Olivier Houchard006e3102018-12-10 18:30:32 +01001939 if (!srv->tmpl_info.prefix && srv->max_idle_conns != 0) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001940 int i;
1941
1942 srv->idle_orphan_conns = calloc(global.nbthread, sizeof(*srv->idle_orphan_conns));
1943 if (!srv->idle_orphan_conns)
1944 goto err;
1945 srv->idle_task = calloc(global.nbthread, sizeof(*srv->idle_task));
1946 if (!srv->idle_task)
1947 goto err;
1948 for (i = 0; i < global.nbthread; i++) {
1949 LIST_INIT(&srv->idle_orphan_conns[i]);
1950 srv->idle_task[i] = task_new(1 << i);
1951 if (!srv->idle_task[i])
1952 goto err;
1953 srv->idle_task[i]->process = cleanup_idle_connections;
1954 srv->idle_task[i]->context = srv;
1955 }
1956 }
1957
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001958 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001959err:
1960 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001961}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001962
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001963/*
1964 * Parse as much as possible such a range string argument: low[-high]
1965 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1966 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1967 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1968 * Returns 0 if succeeded, -1 if not.
1969 */
1970static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1971{
1972 char *nb_high_arg;
1973
1974 *nb_high = 0;
1975 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001976 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001977
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001978 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001979 *nb_high_arg++ = '\0';
1980 *nb_high = atoi(nb_high_arg);
1981 }
1982 else {
1983 *nb_high += *nb_low;
1984 *nb_low = 1;
1985 }
1986
1987 if (*nb_low < 0 || *nb_high < *nb_low)
1988 return -1;
1989
1990 return 0;
1991}
1992
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001993static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1994{
1995 chunk_printf(&trash, "%s%d", prefix, nb);
1996 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001997 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001998}
1999
2000/*
2001 * Initialize as much as possible servers from <srv> server template.
2002 * Note that a server template is a special server with
2003 * a few different parameters than a server which has
2004 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002005 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002006 * 'srv' template included.
2007 */
2008static int server_template_init(struct server *srv, struct proxy *px)
2009{
2010 int i;
2011 struct server *newsrv;
2012
2013 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2014 int check_init_state;
2015 int agent_init_state;
2016
2017 newsrv = new_server(px);
2018 if (!newsrv)
2019 goto err;
2020
2021 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002022 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002023#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2024 if (newsrv->sni_expr) {
2025 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2026 if (!newsrv->ssl_ctx.sni)
2027 goto err;
2028 }
2029#endif
2030 /* Set this new server ID. */
2031 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2032
2033 /* Initial checks states. */
2034 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2035 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2036
2037 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2038 do_server_agent_check_init(newsrv, agent_init_state))
2039 goto err;
2040
2041 /* Linked backwards first. This will be restablished after parsing. */
2042 newsrv->next = px->srv;
2043 px->srv = newsrv;
Olivier Houchard006e3102018-12-10 18:30:32 +01002044 if (newsrv->max_idle_conns != 0) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002045 int i;
2046
2047 newsrv->idle_orphan_conns = calloc(global.nbthread, sizeof(*newsrv->idle_orphan_conns));
2048 if (!newsrv->idle_orphan_conns)
2049 goto err;
2050 newsrv->idle_task = calloc(global.nbthread, sizeof(*newsrv->idle_task));
2051 if (!newsrv->idle_task)
2052 goto err;
2053 for (i = 0; i < global.nbthread; i++) {
2054 LIST_INIT(&newsrv->idle_orphan_conns[i]);
2055 newsrv->idle_task[i] = task_new(1 << i);
2056 if (!newsrv->idle_task[i])
2057 goto err;
2058 newsrv->idle_task[i]->process = cleanup_idle_connections;
2059 newsrv->idle_task[i]->context = newsrv;
2060 }
2061 }
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002062 }
2063 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2064
2065 return i - srv->tmpl_info.nb_low;
2066
2067 err:
2068 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2069 if (newsrv) {
2070#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2071 release_sample_expr(newsrv->ssl_ctx.sni);
2072#endif
2073 free_check(&newsrv->agent);
2074 free_check(&newsrv->check);
2075 }
2076 free(newsrv);
2077 return i - srv->tmpl_info.nb_low;
2078}
2079
Willy Tarreau272adea2014-03-31 10:39:59 +02002080int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
2081{
2082 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002083 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002084 char *errmsg = NULL;
2085 int err_code = 0;
2086 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002087 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002088
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002089 if (!strcmp(args[0], "server") ||
2090 !strcmp(args[0], "default-server") ||
2091 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002092 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002093 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002094 int srv = !defsrv && !strcmp(args[0], "server");
2095 int srv_tmpl = !defsrv && !srv;
2096 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002097
2098 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002099 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002100 err_code |= ERR_ALERT | ERR_FATAL;
2101 goto out;
2102 }
2103 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002104 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002105
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002106 /* There is no mandatory first arguments for default server. */
2107 if (srv) {
2108 if (!*args[2]) {
2109 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002110 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002111 file, linenum, args[0]);
2112 err_code |= ERR_ALERT | ERR_FATAL;
2113 goto out;
2114 }
2115
2116 err = invalid_char(args[1]);
2117 }
2118 else if (srv_tmpl) {
2119 if (!*args[3]) {
2120 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002121 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 +02002122 file, linenum, args[0]);
2123 err_code |= ERR_ALERT | ERR_FATAL;
2124 goto out;
2125 }
2126
2127 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002128 }
2129
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002130 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002131 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 +02002132 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002133 err_code |= ERR_ALERT | ERR_FATAL;
2134 goto out;
2135 }
2136
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002137 cur_arg = 2;
2138 if (srv_tmpl) {
2139 /* Parse server-template <nb | range> arg. */
2140 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002141 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002142 file, linenum, args[0], args[cur_arg]);
2143 err_code |= ERR_ALERT | ERR_FATAL;
2144 goto out;
2145 }
2146 cur_arg++;
2147 }
2148
Willy Tarreau272adea2014-03-31 10:39:59 +02002149 if (!defsrv) {
2150 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002151 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002152 struct protocol *proto;
2153
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002154 newsrv = new_server(curproxy);
2155 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002156 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002157 err_code |= ERR_ALERT | ERR_ABORT;
2158 goto out;
2159 }
2160
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002161 if (srv_tmpl) {
2162 newsrv->tmpl_info.nb_low = tmpl_range_low;
2163 newsrv->tmpl_info.nb_high = tmpl_range_high;
2164 }
2165
Willy Tarreau272adea2014-03-31 10:39:59 +02002166 /* the servers are linked backwards first */
2167 newsrv->next = curproxy->srv;
2168 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002169 newsrv->conf.file = strdup(file);
2170 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002171 /* Note: for a server template, its id is its prefix.
2172 * This is a temporary id which will be used for server allocations to come
2173 * after parsing.
2174 */
2175 if (srv)
2176 newsrv->id = strdup(args[1]);
2177 else
2178 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002179
2180 /* several ways to check the port component :
2181 * - IP => port=+0, relative (IPv4 only)
2182 * - IP: => port=+0, relative
2183 * - IP:N => port=N, absolute
2184 * - IP:+N => port=+N, relative
2185 * - IP:-N => port=-N, relative
2186 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002187 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002188 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002189 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002190 err_code |= ERR_ALERT | ERR_FATAL;
2191 goto out;
2192 }
2193
2194 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002195 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002196 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002197 file, linenum, args[0], args[1]);
2198 err_code |= ERR_ALERT | ERR_FATAL;
2199 goto out;
2200 }
2201
2202 if (!port1 || !port2) {
2203 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002204 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002205 }
2206 else if (port1 != port2) {
2207 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002208 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002209 file, linenum, args[0], args[1], args[2]);
2210 err_code |= ERR_ALERT | ERR_FATAL;
2211 goto out;
2212 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002213
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002214 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002215 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002216 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002217 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002218 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2219 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2220 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002221 err_code |= ERR_ALERT | ERR_FATAL;
2222 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002223 }
2224 }
2225 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002226 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002227 file, linenum, newsrv->id);
2228 err_code |= ERR_ALERT | ERR_FATAL;
2229 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002230 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002231 }
2232
Willy Tarreau272adea2014-03-31 10:39:59 +02002233 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002234 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002235
Olivier Houchard8da5f982017-08-04 18:35:36 +02002236 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002237 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002238 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002239 err_code |= ERR_ALERT | ERR_FATAL;
2240 goto out;
2241 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002242
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002243 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002244 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002245 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002246 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002247 } else {
2248 newsrv = &curproxy->defsrv;
2249 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002250 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002251 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002252 }
2253
2254 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002255 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002256 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2257 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002258 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002259 file, linenum, *err, newsrv->id);
2260 err_code |= ERR_ALERT | ERR_FATAL;
2261 goto out;
2262 }
2263 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002264 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002265 file, linenum, val, args[cur_arg], newsrv->id);
2266 err_code |= ERR_ALERT | ERR_FATAL;
2267 goto out;
2268 }
2269 newsrv->agent.inter = val;
2270 cur_arg += 2;
2271 }
Misiekea849332017-01-09 09:39:51 +01002272 else if (!strcmp(args[cur_arg], "agent-addr")) {
2273 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002274 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002275 goto out;
2276 }
2277
2278 cur_arg += 2;
2279 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002280 else if (!strcmp(args[cur_arg], "agent-port")) {
2281 global.maxsock++;
2282 newsrv->agent.port = atol(args[cur_arg + 1]);
2283 cur_arg += 2;
2284 }
James Brown55f9ff12015-10-21 18:19:05 -07002285 else if (!strcmp(args[cur_arg], "agent-send")) {
2286 global.maxsock++;
2287 free(newsrv->agent.send_string);
2288 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2289 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2290 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2291 cur_arg += 2;
2292 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002293 else if (!strcmp(args[cur_arg], "init-addr")) {
2294 char *p, *end;
2295 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002296 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002297
2298 newsrv->init_addr_methods = 0;
2299 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2300
2301 for (p = args[cur_arg + 1]; *p; p = end) {
2302 /* cut on next comma */
2303 for (end = p; *end && *end != ','; end++);
2304 if (*end)
2305 *(end++) = 0;
2306
Willy Tarreau4310d362016-11-02 15:05:56 +01002307 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002308 if (!strcmp(p, "libc")) {
2309 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2310 }
2311 else if (!strcmp(p, "last")) {
2312 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2313 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002314 else if (!strcmp(p, "none")) {
2315 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2316 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002317 else if (str2ip2(p, &sa, 0)) {
2318 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002319 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002320 file, linenum, args[cur_arg], p);
2321 err_code |= ERR_ALERT | ERR_FATAL;
2322 goto out;
2323 }
2324 newsrv->init_addr = sa;
2325 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2326 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002327 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002328 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 +02002329 file, linenum, args[cur_arg], p);
2330 err_code |= ERR_ALERT | ERR_FATAL;
2331 goto out;
2332 }
2333 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002334 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002335 file, linenum, args[cur_arg], p);
2336 err_code |= ERR_ALERT | ERR_FATAL;
2337 goto out;
2338 }
2339 }
2340 cur_arg += 2;
2341 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002342 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002343 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002344 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2345 cur_arg += 2;
2346 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002347 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2348 char *p, *end;
2349
2350 for (p = args[cur_arg + 1]; *p; p = end) {
2351 /* cut on next comma */
2352 for (end = p; *end && *end != ','; end++);
2353 if (*end)
2354 *(end++) = 0;
2355
2356 if (!strcmp(p, "allow-dup-ip")) {
2357 newsrv->dns_opts.accept_duplicate_ip = 1;
2358 }
2359 else if (!strcmp(p, "prevent-dup-ip")) {
2360 newsrv->dns_opts.accept_duplicate_ip = 0;
2361 }
2362 else {
2363 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2364 file, linenum, args[cur_arg], p);
2365 err_code |= ERR_ALERT | ERR_FATAL;
2366 goto out;
2367 }
2368 }
2369
2370 cur_arg += 2;
2371 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002372 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2373 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002374 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002375 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002376 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002377 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002378 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002379 file, linenum, args[cur_arg]);
2380 err_code |= ERR_ALERT | ERR_FATAL;
2381 goto out;
2382 }
2383 cur_arg += 2;
2384 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002385 else if (!strcmp(args[cur_arg], "resolve-net")) {
2386 char *p, *e;
2387 unsigned char mask;
2388 struct dns_options *opt;
2389
2390 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002391 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002392 file, linenum, args[cur_arg]);
2393 err_code |= ERR_ALERT | ERR_FATAL;
2394 goto out;
2395 }
2396
2397 opt = &newsrv->dns_opts;
2398
2399 /* Split arguments by comma, and convert it from ipv4 or ipv6
2400 * string network in in_addr or in6_addr.
2401 */
2402 p = args[cur_arg + 1];
2403 e = p;
2404 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002405 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002406 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002407 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002408 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2409 err_code |= ERR_ALERT | ERR_FATAL;
2410 goto out;
2411 }
2412 /* look for end or comma. */
2413 while (*e != ',' && *e != '\0')
2414 e++;
2415 if (*e == ',') {
2416 *e = '\0';
2417 e++;
2418 }
2419 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2420 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2421 /* Try to convert input string from ipv4 or ipv6 network. */
2422 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2423 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2424 &mask)) {
2425 /* Try to convert input string from ipv6 network. */
2426 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2427 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2428 } else {
2429 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002430 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002431 file, linenum, args[cur_arg], p);
2432 err_code |= ERR_ALERT | ERR_FATAL;
2433 goto out;
2434 }
2435 opt->pref_net_nb++;
2436 p = e;
2437 }
2438
2439 cur_arg += 2;
2440 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002441 else if (!strcmp(args[cur_arg], "rise")) {
2442 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002443 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002444 file, linenum, args[cur_arg]);
2445 err_code |= ERR_ALERT | ERR_FATAL;
2446 goto out;
2447 }
2448
2449 newsrv->check.rise = atol(args[cur_arg + 1]);
2450 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002451 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002452 file, linenum, args[cur_arg]);
2453 err_code |= ERR_ALERT | ERR_FATAL;
2454 goto out;
2455 }
2456
2457 if (newsrv->check.health)
2458 newsrv->check.health = newsrv->check.rise;
2459 cur_arg += 2;
2460 }
2461 else if (!strcmp(args[cur_arg], "fall")) {
2462 newsrv->check.fall = atol(args[cur_arg + 1]);
2463
2464 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002465 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002466 file, linenum, args[cur_arg]);
2467 err_code |= ERR_ALERT | ERR_FATAL;
2468 goto out;
2469 }
2470
2471 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002472 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002473 file, linenum, args[cur_arg]);
2474 err_code |= ERR_ALERT | ERR_FATAL;
2475 goto out;
2476 }
2477
2478 cur_arg += 2;
2479 }
2480 else if (!strcmp(args[cur_arg], "inter")) {
2481 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2482 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002483 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002484 file, linenum, *err, newsrv->id);
2485 err_code |= ERR_ALERT | ERR_FATAL;
2486 goto out;
2487 }
2488 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002489 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002490 file, linenum, val, args[cur_arg], newsrv->id);
2491 err_code |= ERR_ALERT | ERR_FATAL;
2492 goto out;
2493 }
2494 newsrv->check.inter = val;
2495 cur_arg += 2;
2496 }
2497 else if (!strcmp(args[cur_arg], "fastinter")) {
2498 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2499 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002500 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002501 file, linenum, *err, newsrv->id);
2502 err_code |= ERR_ALERT | ERR_FATAL;
2503 goto out;
2504 }
2505 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002506 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002507 file, linenum, val, args[cur_arg], newsrv->id);
2508 err_code |= ERR_ALERT | ERR_FATAL;
2509 goto out;
2510 }
2511 newsrv->check.fastinter = val;
2512 cur_arg += 2;
2513 }
2514 else if (!strcmp(args[cur_arg], "downinter")) {
2515 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2516 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002517 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002518 file, linenum, *err, newsrv->id);
2519 err_code |= ERR_ALERT | ERR_FATAL;
2520 goto out;
2521 }
2522 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002523 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002524 file, linenum, val, args[cur_arg], newsrv->id);
2525 err_code |= ERR_ALERT | ERR_FATAL;
2526 goto out;
2527 }
2528 newsrv->check.downinter = val;
2529 cur_arg += 2;
2530 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002531 else if (!strcmp(args[cur_arg], "port")) {
2532 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002533 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002534 cur_arg += 2;
2535 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002536 else if (!strcmp(args[cur_arg], "weight")) {
2537 int w;
2538 w = atol(args[cur_arg + 1]);
2539 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002540 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002541 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2542 err_code |= ERR_ALERT | ERR_FATAL;
2543 goto out;
2544 }
2545 newsrv->uweight = newsrv->iweight = w;
2546 cur_arg += 2;
2547 }
2548 else if (!strcmp(args[cur_arg], "minconn")) {
2549 newsrv->minconn = atol(args[cur_arg + 1]);
2550 cur_arg += 2;
2551 }
2552 else if (!strcmp(args[cur_arg], "maxconn")) {
2553 newsrv->maxconn = atol(args[cur_arg + 1]);
2554 cur_arg += 2;
2555 }
2556 else if (!strcmp(args[cur_arg], "maxqueue")) {
2557 newsrv->maxqueue = atol(args[cur_arg + 1]);
2558 cur_arg += 2;
2559 }
2560 else if (!strcmp(args[cur_arg], "slowstart")) {
2561 /* slowstart is stored in seconds */
2562 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2563 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002564 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002565 file, linenum, *err, newsrv->id);
2566 err_code |= ERR_ALERT | ERR_FATAL;
2567 goto out;
2568 }
2569 newsrv->slowstart = (val + 999) / 1000;
2570 cur_arg += 2;
2571 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002572 else if (!strcmp(args[cur_arg], "on-error")) {
2573 if (!strcmp(args[cur_arg + 1], "fastinter"))
2574 newsrv->onerror = HANA_ONERR_FASTINTER;
2575 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2576 newsrv->onerror = HANA_ONERR_FAILCHK;
2577 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2578 newsrv->onerror = HANA_ONERR_SUDDTH;
2579 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2580 newsrv->onerror = HANA_ONERR_MARKDWN;
2581 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002582 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002583 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2584 file, linenum, args[cur_arg], args[cur_arg + 1]);
2585 err_code |= ERR_ALERT | ERR_FATAL;
2586 goto out;
2587 }
2588
2589 cur_arg += 2;
2590 }
2591 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2592 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2593 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2594 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002595 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002596 file, linenum, args[cur_arg], args[cur_arg + 1]);
2597 err_code |= ERR_ALERT | ERR_FATAL;
2598 goto out;
2599 }
2600
2601 cur_arg += 2;
2602 }
2603 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2604 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2605 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2606 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002607 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002608 file, linenum, args[cur_arg], args[cur_arg + 1]);
2609 err_code |= ERR_ALERT | ERR_FATAL;
2610 goto out;
2611 }
2612
2613 cur_arg += 2;
2614 }
2615 else if (!strcmp(args[cur_arg], "error-limit")) {
2616 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002617 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002618 file, linenum, args[cur_arg]);
2619 err_code |= ERR_ALERT | ERR_FATAL;
2620 goto out;
2621 }
2622
2623 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2624
2625 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002626 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002627 file, linenum, args[cur_arg]);
2628 err_code |= ERR_ALERT | ERR_FATAL;
2629 goto out;
2630 }
2631 cur_arg += 2;
2632 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002633 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002634 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002635 file, linenum, "usesrc", "source");
2636 err_code |= ERR_ALERT | ERR_FATAL;
2637 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002638 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002639 else {
2640 static int srv_dumped;
2641 struct srv_kw *kw;
2642 char *err;
2643
2644 kw = srv_find_kw(args[cur_arg]);
2645 if (kw) {
2646 char *err = NULL;
2647 int code;
2648
2649 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002650 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 +02002651 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002652 if (kw->skip != -1)
2653 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002654 err_code |= ERR_ALERT | ERR_FATAL;
2655 goto out;
2656 }
2657
2658 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002659 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002660 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002661 if (kw->skip != -1)
2662 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002663 err_code |= ERR_ALERT;
2664 continue;
2665 }
2666
2667 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2668 err_code |= code;
2669
2670 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002671 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002672 if (code & ERR_FATAL) {
2673 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002674 if (kw->skip != -1)
2675 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002676 goto out;
2677 }
2678 }
2679 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002680 if (kw->skip != -1)
2681 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002682 continue;
2683 }
2684
2685 err = NULL;
2686 if (!srv_dumped) {
2687 srv_dump_kws(&err);
2688 indent_msg(&err, 4);
2689 srv_dumped = 1;
2690 }
2691
Christopher Faulet767a84b2017-11-24 16:50:31 +01002692 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002693 file, linenum, args[0], args[1], args[cur_arg],
2694 err ? " Registered keywords :" : "", err ? err : "");
2695 free(err);
2696
2697 err_code |= ERR_ALERT | ERR_FATAL;
2698 goto out;
2699 }
2700 }
2701
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002702 if (!defsrv)
2703 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2704 if (err_code & ERR_FATAL)
2705 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002706 if (srv_tmpl)
2707 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002708 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002709 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002710 return 0;
2711
2712 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002713 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002714 free(errmsg);
2715 return err_code;
2716}
2717
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002718/* Returns a pointer to the first server matching either id <id>.
2719 * NULL is returned if no match is found.
2720 * the lookup is performed in the backend <bk>
2721 */
2722struct server *server_find_by_id(struct proxy *bk, int id)
2723{
2724 struct eb32_node *eb32;
2725 struct server *curserver;
2726
2727 if (!bk || (id ==0))
2728 return NULL;
2729
2730 /* <bk> has no backend capabilities, so it can't have a server */
2731 if (!(bk->cap & PR_CAP_BE))
2732 return NULL;
2733
2734 curserver = NULL;
2735
2736 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2737 if (eb32)
2738 curserver = container_of(eb32, struct server, conf.id);
2739
2740 return curserver;
2741}
2742
2743/* Returns a pointer to the first server matching either name <name>, or id
2744 * if <name> starts with a '#'. NULL is returned if no match is found.
2745 * the lookup is performed in the backend <bk>
2746 */
2747struct server *server_find_by_name(struct proxy *bk, const char *name)
2748{
2749 struct server *curserver;
2750
2751 if (!bk || !name)
2752 return NULL;
2753
2754 /* <bk> has no backend capabilities, so it can't have a server */
2755 if (!(bk->cap & PR_CAP_BE))
2756 return NULL;
2757
2758 curserver = NULL;
2759 if (*name == '#') {
2760 curserver = server_find_by_id(bk, atoi(name + 1));
2761 if (curserver)
2762 return curserver;
2763 }
2764 else {
2765 curserver = bk->srv;
2766
2767 while (curserver && (strcmp(curserver->id, name) != 0))
2768 curserver = curserver->next;
2769
2770 if (curserver)
2771 return curserver;
2772 }
2773
2774 return NULL;
2775}
2776
2777struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2778{
2779 struct server *byname;
2780 struct server *byid;
2781
2782 if (!name && !id)
2783 return NULL;
2784
2785 if (diff)
2786 *diff = 0;
2787
2788 byname = byid = NULL;
2789
2790 if (name) {
2791 byname = server_find_by_name(bk, name);
2792 if (byname && (!id || byname->puid == id))
2793 return byname;
2794 }
2795
2796 /* remaining possibilities :
2797 * - name not set
2798 * - name set but not found
2799 * - name found but ID doesn't match
2800 */
2801 if (id) {
2802 byid = server_find_by_id(bk, id);
2803 if (byid) {
2804 if (byname) {
2805 /* use id only if forced by configuration */
2806 if (byid->flags & SRV_F_FORCED_ID) {
2807 if (diff)
2808 *diff |= 2;
2809 return byid;
2810 }
2811 else {
2812 if (diff)
2813 *diff |= 1;
2814 return byname;
2815 }
2816 }
2817
2818 /* remaining possibilities:
2819 * - name not set
2820 * - name set but not found
2821 */
2822 if (name && diff)
2823 *diff |= 2;
2824 return byid;
2825 }
2826
2827 /* id bot found */
2828 if (byname) {
2829 if (diff)
2830 *diff |= 1;
2831 return byname;
2832 }
2833 }
2834
2835 return NULL;
2836}
2837
Willy Tarreau46b7f532018-08-21 11:54:26 +02002838/* Update a server state using the parameters available in the params list.
2839 *
2840 * Grabs the server lock during operation.
2841 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002842static void srv_update_state(struct server *srv, int version, char **params)
2843{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002844 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002845 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002846
2847 /* fields since version 1
2848 * and common to all other upcoming versions
2849 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002850 enum srv_state srv_op_state;
2851 enum srv_admin srv_admin_state;
2852 unsigned srv_uweight, srv_iweight;
2853 unsigned long srv_last_time_change;
2854 short srv_check_status;
2855 enum chk_result srv_check_result;
2856 int srv_check_health;
2857 int srv_check_state, srv_agent_state;
2858 int bk_f_forced_id;
2859 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002860 int fqdn_set_by_cli;
2861 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002862 const char *port_str;
2863 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002864 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002865
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002866 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002867 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002868 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002869 switch (version) {
2870 case 1:
2871 /*
2872 * now we can proceed with server's state update:
2873 * srv_addr: params[0]
2874 * srv_op_state: params[1]
2875 * srv_admin_state: params[2]
2876 * srv_uweight: params[3]
2877 * srv_iweight: params[4]
2878 * srv_last_time_change: params[5]
2879 * srv_check_status: params[6]
2880 * srv_check_result: params[7]
2881 * srv_check_health: params[8]
2882 * srv_check_state: params[9]
2883 * srv_agent_state: params[10]
2884 * bk_f_forced_id: params[11]
2885 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002886 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002887 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002888 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002889 */
2890
2891 /* validating srv_op_state */
2892 p = NULL;
2893 errno = 0;
2894 srv_op_state = strtol(params[1], &p, 10);
2895 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2896 (srv_op_state != SRV_ST_STOPPED &&
2897 srv_op_state != SRV_ST_STARTING &&
2898 srv_op_state != SRV_ST_RUNNING &&
2899 srv_op_state != SRV_ST_STOPPING)) {
2900 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2901 }
2902
2903 /* validating srv_admin_state */
2904 p = NULL;
2905 errno = 0;
2906 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002907 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002908
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002909 /* inherited statuses will be recomputed later.
2910 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2911 */
2912 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002913
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002914 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2915 (srv_admin_state != 0 &&
2916 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002917 srv_admin_state != SRV_ADMF_CMAINT &&
2918 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002919 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002920 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002921 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2922 }
2923
2924 /* validating srv_uweight */
2925 p = NULL;
2926 errno = 0;
2927 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002928 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002929 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2930
2931 /* validating srv_iweight */
2932 p = NULL;
2933 errno = 0;
2934 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002935 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002936 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2937
2938 /* validating srv_last_time_change */
2939 p = NULL;
2940 errno = 0;
2941 srv_last_time_change = strtol(params[5], &p, 10);
2942 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2943 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2944
2945 /* validating srv_check_status */
2946 p = NULL;
2947 errno = 0;
2948 srv_check_status = strtol(params[6], &p, 10);
2949 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2950 (srv_check_status >= HCHK_STATUS_SIZE))
2951 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2952
2953 /* validating srv_check_result */
2954 p = NULL;
2955 errno = 0;
2956 srv_check_result = strtol(params[7], &p, 10);
2957 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2958 (srv_check_result != CHK_RES_UNKNOWN &&
2959 srv_check_result != CHK_RES_NEUTRAL &&
2960 srv_check_result != CHK_RES_FAILED &&
2961 srv_check_result != CHK_RES_PASSED &&
2962 srv_check_result != CHK_RES_CONDPASS)) {
2963 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2964 }
2965
2966 /* validating srv_check_health */
2967 p = NULL;
2968 errno = 0;
2969 srv_check_health = strtol(params[8], &p, 10);
2970 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2971 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2972
2973 /* validating srv_check_state */
2974 p = NULL;
2975 errno = 0;
2976 srv_check_state = strtol(params[9], &p, 10);
2977 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2978 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2979 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2980
2981 /* validating srv_agent_state */
2982 p = NULL;
2983 errno = 0;
2984 srv_agent_state = strtol(params[10], &p, 10);
2985 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2986 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2987 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2988
2989 /* validating bk_f_forced_id */
2990 p = NULL;
2991 errno = 0;
2992 bk_f_forced_id = strtol(params[11], &p, 10);
2993 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2994 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2995
2996 /* validating srv_f_forced_id */
2997 p = NULL;
2998 errno = 0;
2999 srv_f_forced_id = strtol(params[12], &p, 10);
3000 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3001 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3002
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003003 /* validating srv_fqdn */
3004 fqdn = params[13];
3005 if (fqdn && *fqdn == '-')
3006 fqdn = NULL;
3007 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3008 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3009 fqdn = NULL;
3010 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003011
Frédéric Lécaille31694712017-08-01 08:47:19 +02003012 port_str = params[14];
3013 if (port_str) {
3014 port = strl2uic(port_str, strlen(port_str));
3015 if (port > USHRT_MAX) {
3016 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3017 port_str = NULL;
3018 }
3019 }
3020
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003021 /* SRV record
3022 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3023 */
3024 srvrecord = params[15];
3025 if (srvrecord && *srvrecord != '_')
3026 srvrecord = NULL;
3027
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003028 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003029 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003030 goto out;
3031
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003032 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003033 /* recover operational state and apply it to this server
3034 * and all servers tracking this one */
3035 switch (srv_op_state) {
3036 case SRV_ST_STOPPED:
3037 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003038 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003039 break;
3040 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02003041 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003042 break;
3043 case SRV_ST_STOPPING:
3044 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003045 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003046 break;
3047 case SRV_ST_RUNNING:
3048 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003049 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003050 break;
3051 }
3052
3053 /* When applying server state, the following rules apply:
3054 * - in case of a configuration change, we apply the setting from the new
3055 * configuration, regardless of old running state
3056 * - if no configuration change, we apply old running state only if old running
3057 * state is different from new configuration state
3058 */
3059 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003060 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3061 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003062 srv_adm_set_maint(srv);
3063 else
3064 srv_adm_set_ready(srv);
3065 }
3066 /* configuration is the same, let's compate old running state and new conf state */
3067 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003068 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003069 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003070 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003071 srv_adm_set_ready(srv);
3072 }
3073 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003074 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003075 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003076 * (srv->iweight is the weight set up in configuration).
3077 * There are two possible reasons for FDRAIN to have been present :
3078 * - previous config weight was zero
3079 * - "set server b/s drain" was sent to the CLI
3080 *
3081 * In the first case, we simply want to drop this drain state
3082 * if the new weight is not zero anymore, meaning the administrator
3083 * has intentionally turned the weight back to a positive value to
3084 * enable the server again after an operation. In the second case,
3085 * the drain state was forced on the CLI regardless of the config's
3086 * weight so we don't want a change to the config weight to lose this
3087 * status. What this means is :
3088 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3089 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003090 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003091 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003092 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003093 }
3094
3095 srv->last_change = date.tv_sec - srv_last_time_change;
3096 srv->check.status = srv_check_status;
3097 srv->check.result = srv_check_result;
3098 srv->check.health = srv_check_health;
3099
3100 /* Only case we want to apply is removing ENABLED flag which could have been
3101 * done by the "disable health" command over the stats socket
3102 */
3103 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3104 (srv_check_state & CHK_ST_CONFIGURED) &&
3105 !(srv_check_state & CHK_ST_ENABLED))
3106 srv->check.state &= ~CHK_ST_ENABLED;
3107
3108 /* Only case we want to apply is removing ENABLED flag which could have been
3109 * done by the "disable agent" command over the stats socket
3110 */
3111 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3112 (srv_agent_state & CHK_ST_CONFIGURED) &&
3113 !(srv_agent_state & CHK_ST_ENABLED))
3114 srv->agent.state &= ~CHK_ST_ENABLED;
3115
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003116 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3117 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003118 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003119 * It means that a configuration file change has precedence over a unix socket change
3120 * for server's weight
3121 *
3122 * by default, HAProxy applies the following weight when parsing the configuration
3123 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003124 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003125 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003126 srv->uweight = srv_uweight;
3127 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003128 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003129
Willy Tarreaue5a60682016-11-09 14:54:53 +01003130 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003131 if (strcmp(params[0], "-"))
3132 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003133
3134 if (fqdn && srv->hostname) {
3135 if (!strcmp(srv->hostname, fqdn)) {
3136 /* Here we reset the 'set from stats socket FQDN' flag
3137 * to support such transitions:
3138 * Let's say initial FQDN value is foo1 (in configuration file).
3139 * - FQDN changed from stats socket, from foo1 to foo2 value,
3140 * - FQDN changed again from file configuration (with the same previous value
3141 set from stats socket, from foo1 to foo2 value),
3142 * - reload for any other reason than a FQDN modification,
3143 * the configuration file FQDN matches the fqdn server state file value.
3144 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003145 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003146 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003147 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003148 }
3149 else {
3150 /* If the FDQN has been changed from stats socket,
3151 * apply fqdn state file value (which is the value set
3152 * from stats socket).
3153 */
3154 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003155 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003156 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003157 }
3158 }
3159 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003160 /* If all the conditions below are validated, this means
3161 * we're evaluating a server managed by SRV resolution
3162 */
3163 else if (fqdn && !srv->hostname && srvrecord) {
3164 int res;
3165
3166 /* we can't apply previous state if SRV record has changed */
3167 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3168 chunk_appendf(msg, ", SRV record mismatch between configuration ('%s') and state file ('%s) for server '%s'. Previous state not applied", srv->srvrq->name, srvrecord, srv->id);
3169 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3170 goto out;
3171 }
3172
3173 /* create or find a SRV resolution for this srv record */
3174 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3175 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3176 if (srv->srvrq == NULL) {
3177 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3178 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3179 goto out;
3180 }
3181
3182 /* prepare DNS resolution for this server */
3183 res = srv_prepare_for_resolution(srv, fqdn);
3184 if (res == -1) {
3185 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3186 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3187 goto out;
3188 }
3189
3190 /* configure check.port accordingly */
3191 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3192 !(srv->flags & SRV_F_CHECKPORT))
3193 srv->check.port = port;
3194
3195 /* Unset SRV_F_MAPPORTS for SRV records.
3196 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3197 * because no ports are provided in the configuration file.
3198 * This is because HAProxy will use the port found into the SRV record.
3199 */
3200 srv->flags &= ~SRV_F_MAPPORTS;
3201 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003202
Frédéric Lécaille31694712017-08-01 08:47:19 +02003203 if (port_str)
3204 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003205 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003206
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003207 break;
3208 default:
3209 chunk_appendf(msg, ", version '%d' not supported", version);
3210 }
3211
3212 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003213 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003214 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003215 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003216 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003217 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003218}
3219
3220/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3221 * For each proxy, it does the following:
3222 * - opens its server state file (either one or local one)
3223 * - read whole file, line by line
3224 * - analyse each line to check if it matches our current backend:
3225 * - backend name matches
3226 * - backend id matches if id is forced and name doesn't match
3227 * - if the server pointed by the line is found, then state is applied
3228 *
3229 * If the running backend uuid or id differs from the state file, then HAProxy reports
3230 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003231 *
3232 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003233 */
3234void apply_server_state(void)
3235{
3236 char *cur, *end;
3237 char mybuf[SRV_STATE_LINE_MAXLEN];
3238 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003239 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3240 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003241 int arg, srv_arg, version, diff;
3242 FILE *f;
3243 char *filepath;
3244 char globalfilepath[MAXPATHLEN + 1];
3245 char localfilepath[MAXPATHLEN + 1];
3246 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003247 struct proxy *curproxy, *bk;
3248 struct server *srv;
3249
3250 globalfilepathlen = 0;
3251 /* create the globalfilepath variable */
3252 if (global.server_state_file) {
3253 /* absolute path or no base directory provided */
3254 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3255 len = strlen(global.server_state_file);
3256 if (len > MAXPATHLEN) {
3257 globalfilepathlen = 0;
3258 goto globalfileerror;
3259 }
3260 memcpy(globalfilepath, global.server_state_file, len);
3261 globalfilepath[len] = '\0';
3262 globalfilepathlen = len;
3263 }
3264 else if (global.server_state_base) {
3265 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003266 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003267 globalfilepathlen = 0;
3268 goto globalfileerror;
3269 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003270 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003271 globalfilepath[len] = 0;
3272 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003273
3274 /* append a slash if needed */
3275 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3276 if (globalfilepathlen + 1 > MAXPATHLEN) {
3277 globalfilepathlen = 0;
3278 goto globalfileerror;
3279 }
3280 globalfilepath[globalfilepathlen++] = '/';
3281 }
3282
3283 len = strlen(global.server_state_file);
3284 if (globalfilepathlen + len > MAXPATHLEN) {
3285 globalfilepathlen = 0;
3286 goto globalfileerror;
3287 }
3288 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3289 globalfilepathlen += len;
3290 globalfilepath[globalfilepathlen++] = 0;
3291 }
3292 }
3293 globalfileerror:
3294 if (globalfilepathlen == 0)
3295 globalfilepath[0] = '\0';
3296
3297 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003298 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003299 /* servers are only in backends */
3300 if (!(curproxy->cap & PR_CAP_BE))
3301 continue;
3302 fileopenerr = 0;
3303 filepath = NULL;
3304
3305 /* search server state file path and name */
3306 switch (curproxy->load_server_state_from_file) {
3307 /* read servers state from global file */
3308 case PR_SRV_STATE_FILE_GLOBAL:
3309 /* there was an error while generating global server state file path */
3310 if (globalfilepathlen == 0)
3311 continue;
3312 filepath = globalfilepath;
3313 fileopenerr = 1;
3314 break;
3315 /* this backend has its own file */
3316 case PR_SRV_STATE_FILE_LOCAL:
3317 localfilepathlen = 0;
3318 localfilepath[0] = '\0';
3319 len = 0;
3320 /* create the localfilepath variable */
3321 /* absolute path or no base directory provided */
3322 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3323 len = strlen(curproxy->server_state_file_name);
3324 if (len > MAXPATHLEN) {
3325 localfilepathlen = 0;
3326 goto localfileerror;
3327 }
3328 memcpy(localfilepath, curproxy->server_state_file_name, len);
3329 localfilepath[len] = '\0';
3330 localfilepathlen = len;
3331 }
3332 else if (global.server_state_base) {
3333 len = strlen(global.server_state_base);
3334 localfilepathlen += len;
3335
3336 if (localfilepathlen > MAXPATHLEN) {
3337 localfilepathlen = 0;
3338 goto localfileerror;
3339 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003340 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003341 localfilepath[localfilepathlen] = 0;
3342
3343 /* append a slash if needed */
3344 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3345 if (localfilepathlen + 1 > MAXPATHLEN) {
3346 localfilepathlen = 0;
3347 goto localfileerror;
3348 }
3349 localfilepath[localfilepathlen++] = '/';
3350 }
3351
3352 len = strlen(curproxy->server_state_file_name);
3353 if (localfilepathlen + len > MAXPATHLEN) {
3354 localfilepathlen = 0;
3355 goto localfileerror;
3356 }
3357 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3358 localfilepathlen += len;
3359 localfilepath[localfilepathlen++] = 0;
3360 }
3361 filepath = localfilepath;
3362 localfileerror:
3363 if (localfilepathlen == 0)
3364 localfilepath[0] = '\0';
3365
3366 break;
3367 case PR_SRV_STATE_FILE_NONE:
3368 default:
3369 continue;
3370 }
3371
3372 /* preload global state file */
3373 errno = 0;
3374 f = fopen(filepath, "r");
3375 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003376 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003377 if (!f)
3378 continue;
3379
3380 mybuf[0] = '\0';
3381 mybuflen = 0;
3382 version = 0;
3383
3384 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003385 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003386 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003387 goto fileclose;
3388 }
3389
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003390 cur = mybuf;
3391 version = atoi(cur);
3392 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3393 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003394 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003395
3396 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3397 int bk_f_forced_id = 0;
3398 int check_id = 0;
3399 int check_name = 0;
3400
3401 mybuflen = strlen(mybuf);
3402 cur = mybuf;
3403 end = cur + mybuflen;
3404
3405 bk = NULL;
3406 srv = NULL;
3407
3408 /* we need at least one character */
3409 if (mybuflen == 0)
3410 continue;
3411
3412 /* ignore blank characters at the beginning of the line */
3413 while (isspace(*cur))
3414 ++cur;
3415
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003416 /* Ignore empty or commented lines */
3417 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003418 continue;
3419
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003420 /* truncated lines */
3421 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003422 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003423 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003424 }
3425
3426 /* Removes trailing '\n' */
3427 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003428
3429 /* we're now ready to move the line into *srv_params[] */
3430 params[0] = cur;
3431 arg = 1;
3432 srv_arg = 0;
3433 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3434 if (isspace(*cur)) {
3435 *cur = '\0';
3436 ++cur;
3437 while (isspace(*cur))
3438 ++cur;
3439 switch (version) {
3440 case 1:
3441 /*
3442 * srv_addr: params[4] => srv_params[0]
3443 * srv_op_state: params[5] => srv_params[1]
3444 * srv_admin_state: params[6] => srv_params[2]
3445 * srv_uweight: params[7] => srv_params[3]
3446 * srv_iweight: params[8] => srv_params[4]
3447 * srv_last_time_change: params[9] => srv_params[5]
3448 * srv_check_status: params[10] => srv_params[6]
3449 * srv_check_result: params[11] => srv_params[7]
3450 * srv_check_health: params[12] => srv_params[8]
3451 * srv_check_state: params[13] => srv_params[9]
3452 * srv_agent_state: params[14] => srv_params[10]
3453 * bk_f_forced_id: params[15] => srv_params[11]
3454 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003455 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003456 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003457 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003458 */
3459 if (arg >= 4) {
3460 srv_params[srv_arg] = cur;
3461 ++srv_arg;
3462 }
3463 break;
3464 }
3465
3466 params[arg] = cur;
3467 ++arg;
3468 }
3469 else {
3470 ++cur;
3471 }
3472 }
3473
3474 /* if line is incomplete line, then ignore it.
3475 * otherwise, update useful flags */
3476 switch (version) {
3477 case 1:
3478 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3479 continue;
3480 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3481 check_id = (atoi(params[0]) == curproxy->uuid);
3482 check_name = (strcmp(curproxy->id, params[1]) == 0);
3483 break;
3484 }
3485
3486 diff = 0;
3487 bk = curproxy;
3488
3489 /* if backend can't be found, let's continue */
3490 if (!check_id && !check_name)
3491 continue;
3492 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003493 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 +02003494 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3495 }
3496 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003497 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 +02003498 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3499 /* if name doesn't match, we still want to update curproxy if the backend id
3500 * was forced in previous the previous configuration */
3501 if (!bk_f_forced_id)
3502 continue;
3503 }
3504
3505 /* look for the server by its id: param[2] */
3506 /* else look for the server by its name: param[3] */
3507 diff = 0;
3508 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3509
3510 if (!srv) {
3511 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003512 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3513 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003514 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3515 params[3], params[2], params[0], params[1]);
3516 continue;
3517 }
3518 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003519 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 +02003520 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 +02003521 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003522 }
3523 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003524 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 +02003525 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 +02003526 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003527 }
3528
3529 /* now we can proceed with server's state update */
3530 srv_update_state(srv, version, srv_params);
3531 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003532fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003533 fclose(f);
3534 }
3535}
3536
Simon Horman7d09b9a2013-02-12 10:45:51 +09003537/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003538 * update a server's current IP address.
3539 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3540 * ip is in network format.
3541 * updater is a string which contains an information about the requester of the update.
3542 * updater is used if not NULL.
3543 *
3544 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003545 *
3546 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003547 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003548int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003549{
3550 /* generates a log line and a warning on stderr */
3551 if (1) {
3552 /* book enough space for both IPv4 and IPv6 */
3553 char oldip[INET6_ADDRSTRLEN];
3554 char newip[INET6_ADDRSTRLEN];
3555
3556 memset(oldip, '\0', INET6_ADDRSTRLEN);
3557 memset(newip, '\0', INET6_ADDRSTRLEN);
3558
3559 /* copy old IP address in a string */
3560 switch (s->addr.ss_family) {
3561 case AF_INET:
3562 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3563 break;
3564 case AF_INET6:
3565 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3566 break;
3567 };
3568
3569 /* copy new IP address in a string */
3570 switch (ip_sin_family) {
3571 case AF_INET:
3572 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3573 break;
3574 case AF_INET6:
3575 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3576 break;
3577 };
3578
3579 /* save log line into a buffer */
3580 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3581 s->proxy->id, s->id, oldip, newip, updater);
3582
3583 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003584 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003585
3586 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003587 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003588 }
3589
3590 /* save the new IP family */
3591 s->addr.ss_family = ip_sin_family;
3592 /* save the new IP address */
3593 switch (ip_sin_family) {
3594 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003595 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003596 break;
3597 case AF_INET6:
3598 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3599 break;
3600 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003601 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003602
3603 return 0;
3604}
3605
3606/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003607 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3608 *
3609 * Caller can pass its name through <updater> to get it integrated in the response message
3610 * returned by the function.
3611 *
3612 * The function first does the following, in that order:
3613 * - validates the new addr and/or port
3614 * - checks if an update is required (new IP or port is different than current ones)
3615 * - checks the update is allowed:
3616 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3617 * - allow all changes if no CHECKS are configured
3618 * - if CHECK is configured:
3619 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3620 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3621 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003622 *
3623 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003624 */
3625const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3626{
3627 struct sockaddr_storage sa;
3628 int ret, port_change_required;
3629 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003630 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003631 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003632 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003633
3634 msg = get_trash_chunk();
3635 chunk_reset(msg);
3636
3637 if (addr) {
3638 memset(&sa, 0, sizeof(struct sockaddr_storage));
3639 if (str2ip2(addr, &sa, 0) == NULL) {
3640 chunk_printf(msg, "Invalid addr '%s'", addr);
3641 goto out;
3642 }
3643
3644 /* changes are allowed on AF_INET* families only */
3645 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3646 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3647 goto out;
3648 }
3649
3650 /* collecting data currently setup */
3651 memset(current_addr, '\0', sizeof(current_addr));
3652 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3653 /* changes are allowed on AF_INET* families only */
3654 if ((ret != AF_INET) && (ret != AF_INET6)) {
3655 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3656 goto out;
3657 }
3658
3659 /* applying ADDR changes if required and allowed
3660 * ipcmp returns 0 when both ADDR are the same
3661 */
3662 if (ipcmp(&s->addr, &sa) == 0) {
3663 chunk_appendf(msg, "no need to change the addr");
3664 goto port;
3665 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003666 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003667 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003668
3669 /* we also need to update check's ADDR only if it uses the server's one */
3670 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003671 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003672 }
3673
3674 /* we also need to update agent ADDR only if it use the server's one */
3675 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003676 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003677 }
3678
3679 /* update report for caller */
3680 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3681 }
3682
3683 port:
3684 if (port) {
3685 char sign = '\0';
3686 char *endptr;
3687
3688 if (addr)
3689 chunk_appendf(msg, ", ");
3690
3691 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003692 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003693
3694 /* check if PORT change is required */
3695 port_change_required = 0;
3696
3697 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003698 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003699 new_port = strtol(port, &endptr, 10);
3700 if ((errno != 0) || (port == endptr)) {
3701 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3702 goto out;
3703 }
3704
3705 /* check if caller triggers a port mapped or offset */
3706 if (sign == '-' || (sign == '+')) {
3707 /* check if server currently uses port map */
3708 if (!(s->flags & SRV_F_MAPPORTS)) {
3709 /* switch from fixed port to port map mandatorily triggers
3710 * a port change */
3711 port_change_required = 1;
3712 /* check is configured
3713 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3714 * prevent PORT change if check doesn't have it's dedicated port while switching
3715 * to port mapping */
3716 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3717 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.");
3718 goto out;
3719 }
3720 }
3721 /* we're already using port maps */
3722 else {
3723 port_change_required = current_port != new_port;
3724 }
3725 }
3726 /* fixed port */
3727 else {
3728 port_change_required = current_port != new_port;
3729 }
3730
3731 /* applying PORT changes if required and update response message */
3732 if (port_change_required) {
3733 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003734 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003735 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003736
3737 /* prepare message */
3738 chunk_appendf(msg, "port changed from '");
3739 if (s->flags & SRV_F_MAPPORTS)
3740 chunk_appendf(msg, "+");
3741 chunk_appendf(msg, "%d' to '", current_port);
3742
3743 if (sign == '-') {
3744 s->flags |= SRV_F_MAPPORTS;
3745 chunk_appendf(msg, "%c", sign);
3746 /* just use for result output */
3747 new_port = -new_port;
3748 }
3749 else if (sign == '+') {
3750 s->flags |= SRV_F_MAPPORTS;
3751 chunk_appendf(msg, "%c", sign);
3752 }
3753 else {
3754 s->flags &= ~SRV_F_MAPPORTS;
3755 }
3756
3757 chunk_appendf(msg, "%d'", new_port);
3758
3759 /* we also need to update health checks port only if it uses server's realport */
3760 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3761 s->check.port = new_port;
3762 }
3763 }
3764 else {
3765 chunk_appendf(msg, "no need to change the port");
3766 }
3767 }
3768
3769out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003770 if (changed)
3771 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003772 if (updater)
3773 chunk_appendf(msg, " by '%s'", updater);
3774 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003775 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003776}
3777
3778
3779/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003780 * update server status based on result of name resolution
3781 * returns:
3782 * 0 if server status is updated
3783 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003784 *
3785 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003786 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003787int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003788{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003789 struct dns_resolvers *resolvers = s->resolvers;
3790 struct dns_resolution *resolution = s->dns_requester->resolution;
3791 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003792
3793 switch (resolution->status) {
3794 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003795 /* status when HAProxy has just (re)started.
3796 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003797 break;
3798
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003799 case RSLV_STATUS_VALID:
3800 /*
3801 * resume health checks
3802 * server will be turned back on if health check is safe
3803 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003804 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003805 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003806 return 1;
3807 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3808 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003809 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003810 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003811
Emeric Brun52a91d32017-08-31 14:41:55 +02003812 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003813 return 1;
3814 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3815 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3816 s->proxy->id, s->id);
3817
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003818 ha_warning("%s.\n", trash.area);
3819 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003820 return 0;
3821
3822 case RSLV_STATUS_NX:
3823 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003824 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3825 if (!tick_is_expired(exp, now_ms))
3826 break;
3827
3828 if (s->next_admin & SRV_ADMF_RMAINT)
3829 return 1;
3830 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3831 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003832
3833 case RSLV_STATUS_TIMEOUT:
3834 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003835 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3836 if (!tick_is_expired(exp, now_ms))
3837 break;
3838
3839 if (s->next_admin & SRV_ADMF_RMAINT)
3840 return 1;
3841 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3842 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003843
3844 case RSLV_STATUS_REFUSED:
3845 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003846 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3847 if (!tick_is_expired(exp, now_ms))
3848 break;
3849
3850 if (s->next_admin & SRV_ADMF_RMAINT)
3851 return 1;
3852 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3853 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003854
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003855 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003856 /* stop server if resolution failed for a long enough period */
3857 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3858 if (!tick_is_expired(exp, now_ms))
3859 break;
3860
3861 if (s->next_admin & SRV_ADMF_RMAINT)
3862 return 1;
3863 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3864 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003865 }
3866
3867 return 1;
3868}
3869
3870/*
3871 * Server Name Resolution valid response callback
3872 * It expects:
3873 * - <nameserver>: the name server which answered the valid response
3874 * - <response>: buffer containing a valid DNS response
3875 * - <response_len>: size of <response>
3876 * It performs the following actions:
3877 * - ignore response if current ip found and server family not met
3878 * - update with first new ip found if family is met and current IP is not found
3879 * returns:
3880 * 0 on error
3881 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003882 *
3883 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003884 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003885int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003886{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003887 struct server *s = NULL;
3888 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003889 void *serverip, *firstip;
3890 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003891 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003892 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003893 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003894
Christopher Faulet67957bd2017-09-27 11:00:59 +02003895 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003896 if (!s)
3897 return 1;
3898
Christopher Faulet67957bd2017-09-27 11:00:59 +02003899 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003900
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003901 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003902 firstip = NULL; /* pointer to the first valid response found */
3903 /* it will be used as the new IP if a change is required */
3904 firstip_sin_family = AF_UNSPEC;
3905 serverip = NULL; /* current server IP address */
3906
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003907 /* initializing server IP pointer */
3908 server_sin_family = s->addr.ss_family;
3909 switch (server_sin_family) {
3910 case AF_INET:
3911 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3912 break;
3913
3914 case AF_INET6:
3915 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3916 break;
3917
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003918 case AF_UNSPEC:
3919 break;
3920
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003921 default:
3922 goto invalid;
3923 }
3924
Baptiste Assmann729c9012017-05-22 15:13:10 +02003925 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003926 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003927 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003928
3929 switch (ret) {
3930 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003931 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003932
3933 case DNS_UPD_SRVIP_NOT_FOUND:
3934 goto save_ip;
3935
3936 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003937 goto invalid;
3938
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003939 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003940 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003941 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003942
Baptiste Assmannfad03182015-10-28 02:03:32 +01003943 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003944 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003945 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003946 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003947
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003948 default:
3949 goto invalid;
3950
3951 }
3952
3953 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003954 if (nameserver) {
3955 nameserver->counters.update++;
3956 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003957 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003958 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003959 else
3960 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003961 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003962
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003963 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003964 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003965 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003966
3967 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003968 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003969 nameserver->counters.invalid++;
3970 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003971 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003972 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003973 return 0;
3974}
3975
3976/*
3977 * Server Name Resolution error management callback
3978 * returns:
3979 * 0 on error
3980 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003981 *
3982 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003983 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003984int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003985{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003986 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003987
Christopher Faulet67957bd2017-09-27 11:00:59 +02003988 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003989 if (!s)
3990 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003991 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003992 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003993 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003994 return 1;
3995}
3996
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003997/*
3998 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003999 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004000 * It returns a pointer to the first server found or NULL if <ip> is not yet
4001 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004002 *
4003 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004004 */
4005struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4006{
4007 struct server *tmpsrv;
4008 struct proxy *be;
4009
4010 if (!srv)
4011 return NULL;
4012
4013 be = srv->proxy;
4014 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004015 /* we found the current server is the same, ignore it */
4016 if (srv == tmpsrv)
4017 continue;
4018
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004019 /* We want to compare the IP in the record with the IP of the servers in the
4020 * same backend, only if:
4021 * * DNS resolution is enabled on the server
4022 * * the hostname used for the resolution by our server is the same than the
4023 * one used for the server found in the backend
4024 * * the server found in the backend is not our current server
4025 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004026 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004027 if ((tmpsrv->hostname_dn == NULL) ||
4028 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4029 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004030 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004031 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004032 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004033 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004034
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004035 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004036 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004037 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004038 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004039 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004040
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004041 /* At this point, we have 2 different servers using the same DNS hostname
4042 * for their respective resolution.
4043 */
4044 if (*ip_family == tmpsrv->addr.ss_family &&
4045 ((tmpsrv->addr.ss_family == AF_INET &&
4046 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4047 (tmpsrv->addr.ss_family == AF_INET6 &&
4048 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004049 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004050 return tmpsrv;
4051 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004052 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004053 }
4054
Emeric Brune9fd6b52017-11-02 17:20:39 +01004055
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004056 return NULL;
4057}
4058
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004059/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4060 * resolver. This is suited for initial address configuration. Returns 0 on
4061 * success otherwise a non-zero error code. In case of error, *err_code, if
4062 * not NULL, is filled up.
4063 */
4064int srv_set_addr_via_libc(struct server *srv, int *err_code)
4065{
4066 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004067 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004068 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004069 return 1;
4070 }
4071 return 0;
4072}
4073
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004074/* Set the server's FDQN (->hostname) from <hostname>.
4075 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004076 *
4077 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004078 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004079int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004080{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004081 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004082 char *hostname_dn;
4083 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004084
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004085 /* Note that the server lock is already held. */
4086 if (!srv->resolvers)
4087 return -1;
4088
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004089 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004090 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004091 /* run time DNS resolution was not active for this server
4092 * and we can't enable it at run time for now.
4093 */
4094 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004095 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004096
4097 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004098 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004099 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004100 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4101 hostname_dn, trash.size);
4102 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004103 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004104
Christopher Faulet67957bd2017-09-27 11:00:59 +02004105 resolution = srv->dns_requester->resolution;
4106 if (resolution &&
4107 resolution->hostname_dn &&
4108 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004109 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004110
Christopher Faulet67957bd2017-09-27 11:00:59 +02004111 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004112
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004113 free(srv->hostname);
4114 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004115 srv->hostname = strdup(hostname);
4116 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004117 srv->hostname_dn_len = hostname_dn_len;
4118 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004119 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004120
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004121 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004122 goto err;
4123
4124 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004125 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004126 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004127 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004128
4129 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004130 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004131 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004132 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004133}
4134
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004135/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4136 * from the state file. This is suited for initial address configuration.
4137 * Returns 0 on success otherwise a non-zero error code. In case of error,
4138 * *err_code, if not NULL, is filled up.
4139 */
4140static int srv_apply_lastaddr(struct server *srv, int *err_code)
4141{
4142 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4143 if (err_code)
4144 *err_code |= ERR_WARN;
4145 return 1;
4146 }
4147 return 0;
4148}
4149
Willy Tarreau25e51522016-11-04 15:10:17 +01004150/* returns 0 if no error, otherwise a combination of ERR_* flags */
4151static int srv_iterate_initaddr(struct server *srv)
4152{
4153 int return_code = 0;
4154 int err_code;
4155 unsigned int methods;
4156
4157 methods = srv->init_addr_methods;
4158 if (!methods) { // default to "last,libc"
4159 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4160 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4161 }
4162
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004163 /* "-dr" : always append "none" so that server addresses resolution
4164 * failures are silently ignored, this is convenient to validate some
4165 * configs out of their environment.
4166 */
4167 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4168 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4169
Willy Tarreau25e51522016-11-04 15:10:17 +01004170 while (methods) {
4171 err_code = 0;
4172 switch (srv_get_next_initaddr(&methods)) {
4173 case SRV_IADDR_LAST:
4174 if (!srv->lastaddr)
4175 continue;
4176 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004177 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004178 return_code |= err_code;
4179 break;
4180
4181 case SRV_IADDR_LIBC:
4182 if (!srv->hostname)
4183 continue;
4184 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004185 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004186 return_code |= err_code;
4187 break;
4188
Willy Tarreau37ebe122016-11-04 15:17:58 +01004189 case SRV_IADDR_NONE:
4190 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004191 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004192 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4193 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004194 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004195 return return_code;
4196
Willy Tarreau4310d362016-11-02 15:05:56 +01004197 case SRV_IADDR_IP:
4198 ipcpy(&srv->init_addr, &srv->addr);
4199 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004200 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4201 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004202 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004203 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004204
Willy Tarreau25e51522016-11-04 15:10:17 +01004205 default: /* unhandled method */
4206 break;
4207 }
4208 }
4209
4210 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004211 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004212 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4213 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004214 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004215 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004216 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4217 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004218
4219 return_code |= ERR_ALERT | ERR_FATAL;
4220 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004221out:
4222 srv_set_dyncookie(srv);
4223 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004224}
4225
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004226/*
4227 * This function parses all backends and all servers within each backend
4228 * and performs servers' addr resolution based on information provided by:
4229 * - configuration file
4230 * - server-state file (states provided by an 'old' haproxy process)
4231 *
4232 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4233 */
4234int srv_init_addr(void)
4235{
4236 struct proxy *curproxy;
4237 int return_code = 0;
4238
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004239 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004240 while (curproxy) {
4241 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004242
4243 /* servers are in backend only */
4244 if (!(curproxy->cap & PR_CAP_BE))
4245 goto srv_init_addr_next;
4246
Willy Tarreau25e51522016-11-04 15:10:17 +01004247 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004248 if (srv->hostname)
4249 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004250
4251 srv_init_addr_next:
4252 curproxy = curproxy->next;
4253 }
4254
4255 return return_code;
4256}
4257
Willy Tarreau46b7f532018-08-21 11:54:26 +02004258/*
4259 * Must be called with the server lock held.
4260 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004261const 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 +02004262{
4263
Willy Tarreau83061a82018-07-13 11:56:34 +02004264 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004265
4266 msg = get_trash_chunk();
4267 chunk_reset(msg);
4268
Olivier Houchard8da5f982017-08-04 18:35:36 +02004269 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004270 chunk_appendf(msg, "no need to change the FDQN");
4271 goto out;
4272 }
4273
4274 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4275 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4276 goto out;
4277 }
4278
4279 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4280 server->proxy->id, server->id, server->hostname, fqdn);
4281
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004282 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004283 chunk_reset(msg);
4284 chunk_appendf(msg, "could not update %s/%s FQDN",
4285 server->proxy->id, server->id);
4286 goto out;
4287 }
4288
4289 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004290 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004291
4292 out:
4293 if (updater)
4294 chunk_appendf(msg, " by '%s'", updater);
4295 chunk_appendf(msg, "\n");
4296
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004297 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004298}
4299
4300
Willy Tarreau21b069d2016-11-23 17:15:08 +01004301/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4302 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004303 * 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 +01004304 * used for CLI commands requiring a server name.
4305 * Important: the <arg> is modified to remove the '/'.
4306 */
4307struct server *cli_find_server(struct appctx *appctx, char *arg)
4308{
4309 struct proxy *px;
4310 struct server *sv;
4311 char *line;
4312
4313 /* split "backend/server" and make <line> point to server */
4314 for (line = arg; *line; line++)
4315 if (*line == '/') {
4316 *line++ = '\0';
4317 break;
4318 }
4319
4320 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004321 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004322 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004323 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004324 return NULL;
4325 }
4326
4327 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004328 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004329 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004330 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004331 return NULL;
4332 }
4333
4334 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004335 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004336 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004337 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004338 return NULL;
4339 }
4340
4341 return sv;
4342}
4343
William Lallemand222baf22016-11-19 02:00:33 +01004344
Willy Tarreau46b7f532018-08-21 11:54:26 +02004345/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004346static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004347{
4348 struct server *sv;
4349 const char *warning;
4350
4351 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4352 return 1;
4353
4354 sv = cli_find_server(appctx, args[2]);
4355 if (!sv)
4356 return 1;
4357
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004358 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004359
William Lallemand222baf22016-11-19 02:00:33 +01004360 if (strcmp(args[3], "weight") == 0) {
4361 warning = server_parse_weight_change_request(sv, args[4]);
4362 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004363 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004364 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004365 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004366 }
4367 }
4368 else if (strcmp(args[3], "state") == 0) {
4369 if (strcmp(args[4], "ready") == 0)
4370 srv_adm_set_ready(sv);
4371 else if (strcmp(args[4], "drain") == 0)
4372 srv_adm_set_drain(sv);
4373 else if (strcmp(args[4], "maint") == 0)
4374 srv_adm_set_maint(sv);
4375 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004376 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004377 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004378 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004379 }
4380 }
4381 else if (strcmp(args[3], "health") == 0) {
4382 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004383 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004384 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004385 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004386 }
4387 else if (strcmp(args[4], "up") == 0) {
4388 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004389 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004390 }
4391 else if (strcmp(args[4], "stopping") == 0) {
4392 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004393 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004394 }
4395 else if (strcmp(args[4], "down") == 0) {
4396 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004397 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004398 }
4399 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004400 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004401 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004402 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004403 }
4404 }
4405 else if (strcmp(args[3], "agent") == 0) {
4406 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004407 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004408 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004409 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004410 }
4411 else if (strcmp(args[4], "up") == 0) {
4412 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004413 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004414 }
4415 else if (strcmp(args[4], "down") == 0) {
4416 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004417 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004418 }
4419 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004420 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004421 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004422 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004423 }
4424 }
Misiek2da082d2017-01-09 09:40:42 +01004425 else if (strcmp(args[3], "agent-addr") == 0) {
4426 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004427 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004428 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4429 appctx->st0 = CLI_ST_PRINT;
4430 } else {
4431 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004432 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004433 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4434 appctx->st0 = CLI_ST_PRINT;
4435 }
4436 }
4437 }
4438 else if (strcmp(args[3], "agent-send") == 0) {
4439 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004440 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004441 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4442 appctx->st0 = CLI_ST_PRINT;
4443 } else {
4444 char *nss = strdup(args[4]);
4445 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004446 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004447 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4448 appctx->st0 = CLI_ST_PRINT;
4449 } else {
4450 free(sv->agent.send_string);
4451 sv->agent.send_string = nss;
4452 sv->agent.send_string_len = strlen(args[4]);
4453 }
4454 }
4455 }
William Lallemand222baf22016-11-19 02:00:33 +01004456 else if (strcmp(args[3], "check-port") == 0) {
4457 int i = 0;
4458 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004459 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004460 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004461 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004462 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004463 }
4464 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004465 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004466 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004467 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004468 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004469 }
4470 /* prevent the update of port to 0 if MAPPORTS are in use */
4471 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004472 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004473 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004474 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004475 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004476 }
4477 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004478 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004479 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004480 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004481 }
4482 else if (strcmp(args[3], "addr") == 0) {
4483 char *addr = NULL;
4484 char *port = NULL;
4485 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004486 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004487 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004488 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004489 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004490 }
4491 else {
4492 addr = args[4];
4493 }
4494 if (strcmp(args[5], "port") == 0) {
4495 port = args[6];
4496 }
4497 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4498 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004499 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004500 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004501 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004502 }
4503 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4504 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004505 else if (strcmp(args[3], "fqdn") == 0) {
4506 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004507 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004508 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4509 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004510 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004511 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004512 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004513 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004514 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004515 appctx->ctx.cli.msg = warning;
4516 appctx->st0 = CLI_ST_PRINT;
4517 }
4518 }
William Lallemand222baf22016-11-19 02:00:33 +01004519 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004520 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004521 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 +01004522 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004523 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004524 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004525 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004526 return 1;
4527}
4528
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004529static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004530{
4531 struct stream_interface *si = appctx->owner;
4532 struct proxy *px;
4533 struct server *sv;
4534 char *line;
4535
4536
4537 /* split "backend/server" and make <line> point to server */
4538 for (line = args[2]; *line; line++)
4539 if (*line == '/') {
4540 *line++ = '\0';
4541 break;
4542 }
4543
4544 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004545 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004546 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004547 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004548 return 1;
4549 }
4550
4551 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004552 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004553 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004554 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004555 return 1;
4556 }
4557
4558 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004559 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4560 sv->iweight);
4561 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004562 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004563 return 0;
4564 }
William Lallemand6b160942016-11-22 12:34:35 +01004565 return 1;
4566}
4567
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004568/* Parse a "set weight" command.
4569 *
4570 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004571 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004572static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004573{
4574 struct server *sv;
4575 const char *warning;
4576
4577 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4578 return 1;
4579
4580 sv = cli_find_server(appctx, args[2]);
4581 if (!sv)
4582 return 1;
4583
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004584 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4585
William Lallemand6b160942016-11-22 12:34:35 +01004586 warning = server_parse_weight_change_request(sv, args[3]);
4587 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004588 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004589 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004590 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004591 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004592
4593 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4594
William Lallemand6b160942016-11-22 12:34:35 +01004595 return 1;
4596}
4597
Willy Tarreau46b7f532018-08-21 11:54:26 +02004598/* parse a "set maxconn server" command. It always returns 1.
4599 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004600 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004601 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004602static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004603{
4604 struct server *sv;
4605 const char *warning;
4606
4607 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4608 return 1;
4609
4610 sv = cli_find_server(appctx, args[3]);
4611 if (!sv)
4612 return 1;
4613
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004614 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4615
Willy Tarreaub8026272016-11-23 11:26:56 +01004616 warning = server_parse_maxconn_change_request(sv, args[4]);
4617 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004618 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004619 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004620 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004621 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004622
4623 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4624
Willy Tarreaub8026272016-11-23 11:26:56 +01004625 return 1;
4626}
William Lallemand6b160942016-11-22 12:34:35 +01004627
Willy Tarreau46b7f532018-08-21 11:54:26 +02004628/* parse a "disable agent" command. It always returns 1.
4629 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004630 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004631 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004632static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004633{
4634 struct server *sv;
4635
4636 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4637 return 1;
4638
4639 sv = cli_find_server(appctx, args[2]);
4640 if (!sv)
4641 return 1;
4642
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004643 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004644 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004645 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004646 return 1;
4647}
4648
Willy Tarreau46b7f532018-08-21 11:54:26 +02004649/* parse a "disable health" command. It always returns 1.
4650 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004651 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004652 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004653static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004654{
4655 struct server *sv;
4656
4657 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4658 return 1;
4659
4660 sv = cli_find_server(appctx, args[2]);
4661 if (!sv)
4662 return 1;
4663
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004664 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004665 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004666 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004667 return 1;
4668}
4669
Willy Tarreau46b7f532018-08-21 11:54:26 +02004670/* parse a "disable server" command. It always returns 1.
4671 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004672 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004673 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004674static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004675{
4676 struct server *sv;
4677
4678 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4679 return 1;
4680
4681 sv = cli_find_server(appctx, args[2]);
4682 if (!sv)
4683 return 1;
4684
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004685 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004686 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004687 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004688 return 1;
4689}
4690
Willy Tarreau46b7f532018-08-21 11:54:26 +02004691/* parse a "enable agent" command. It always returns 1.
4692 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004693 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004694 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004695static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004696{
4697 struct server *sv;
4698
4699 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4700 return 1;
4701
4702 sv = cli_find_server(appctx, args[2]);
4703 if (!sv)
4704 return 1;
4705
4706 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004707 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004708 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004709 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004710 return 1;
4711 }
4712
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004713 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004714 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004715 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004716 return 1;
4717}
4718
Willy Tarreau46b7f532018-08-21 11:54:26 +02004719/* parse a "enable health" command. It always returns 1.
4720 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004721 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004722 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004723static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004724{
4725 struct server *sv;
4726
4727 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4728 return 1;
4729
4730 sv = cli_find_server(appctx, args[2]);
4731 if (!sv)
4732 return 1;
4733
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004734 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004735 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004736 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004737 return 1;
4738}
4739
Willy Tarreau46b7f532018-08-21 11:54:26 +02004740/* parse a "enable server" command. It always returns 1.
4741 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004742 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004743 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004744static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004745{
4746 struct server *sv;
4747
4748 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4749 return 1;
4750
4751 sv = cli_find_server(appctx, args[2]);
4752 if (!sv)
4753 return 1;
4754
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004755 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004756 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004757 if (!(sv->flags & SRV_F_COOKIESET)
4758 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4759 sv->cookie)
4760 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004761 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004762 return 1;
4763}
4764
William Lallemand222baf22016-11-19 02:00:33 +01004765/* register cli keywords */
4766static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004767 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004768 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004769 { { "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 +01004770 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004771 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004772 { { "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 +01004773 { { "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 +01004774 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004775 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4776 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4777
William Lallemand222baf22016-11-19 02:00:33 +01004778 {{},}
4779}};
4780
Willy Tarreau0108d902018-11-25 19:14:37 +01004781INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004782
Emeric Brun64cc49c2017-10-03 14:46:45 +02004783/*
4784 * This function applies server's status changes, it is
4785 * is designed to be called asynchronously.
4786 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004787 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004788 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004789static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004790{
4791 struct check *check = &s->check;
4792 int xferred;
4793 struct proxy *px = s->proxy;
4794 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4795 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4796 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004797 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004798
Emeric Brun64cc49c2017-10-03 14:46:45 +02004799 /* If currently main is not set we try to apply pending state changes */
4800 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4801 int next_admin;
4802
4803 /* Backup next admin */
4804 next_admin = s->next_admin;
4805
4806 /* restore current admin state */
4807 s->next_admin = s->cur_admin;
4808
4809 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4810 s->last_change = now.tv_sec;
4811 if (s->proxy->lbprm.set_server_status_down)
4812 s->proxy->lbprm.set_server_status_down(s);
4813
4814 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4815 srv_shutdown_streams(s, SF_ERR_DOWN);
4816
4817 /* we might have streams queued on this server and waiting for
4818 * a connection. Those which are redispatchable will be queued
4819 * to another server or to the proxy itself.
4820 */
4821 xferred = pendconn_redistribute(s);
4822
4823 tmptrash = alloc_trash_chunk();
4824 if (tmptrash) {
4825 chunk_printf(tmptrash,
4826 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4827 s->proxy->id, s->id);
4828
Emeric Brun5a133512017-10-19 14:42:30 +02004829 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004830 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004831
4832 /* we don't send an alert if the server was previously paused */
4833 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004834 send_log(s->proxy, log_level, "%s.\n",
4835 tmptrash->area);
4836 send_email_alert(s, log_level, "%s",
4837 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004838 free_trash_chunk(tmptrash);
4839 tmptrash = NULL;
4840 }
4841 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4842 set_backend_down(s->proxy);
4843
4844 s->counters.down_trans++;
4845 }
4846 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4847 s->last_change = now.tv_sec;
4848 if (s->proxy->lbprm.set_server_status_down)
4849 s->proxy->lbprm.set_server_status_down(s);
4850
4851 /* we might have streams queued on this server and waiting for
4852 * a connection. Those which are redispatchable will be queued
4853 * to another server or to the proxy itself.
4854 */
4855 xferred = pendconn_redistribute(s);
4856
4857 tmptrash = alloc_trash_chunk();
4858 if (tmptrash) {
4859 chunk_printf(tmptrash,
4860 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4861 s->proxy->id, s->id);
4862
Emeric Brun5a133512017-10-19 14:42:30 +02004863 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004864
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004865 ha_warning("%s.\n", tmptrash->area);
4866 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4867 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004868 free_trash_chunk(tmptrash);
4869 tmptrash = NULL;
4870 }
4871
4872 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4873 set_backend_down(s->proxy);
4874 }
4875 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4876 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4877 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4878 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4879 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4880 s->proxy->last_change = now.tv_sec;
4881 }
4882
4883 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4884 s->down_time += now.tv_sec - s->last_change;
4885
4886 s->last_change = now.tv_sec;
4887 if (s->next_state == SRV_ST_STARTING)
4888 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4889
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004890 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004891 /* now propagate the status change to any LB algorithms */
4892 if (px->lbprm.update_server_eweight)
4893 px->lbprm.update_server_eweight(s);
4894 else if (srv_willbe_usable(s)) {
4895 if (px->lbprm.set_server_status_up)
4896 px->lbprm.set_server_status_up(s);
4897 }
4898 else {
4899 if (px->lbprm.set_server_status_down)
4900 px->lbprm.set_server_status_down(s);
4901 }
4902
4903 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4904 * and it's not a backup server and its effective weight is > 0,
4905 * then it can accept new connections, so we shut down all streams
4906 * on all backup servers.
4907 */
4908 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4909 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4910 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4911
4912 /* check if we can handle some connections queued at the proxy. We
4913 * will take as many as we can handle.
4914 */
4915 xferred = pendconn_grab_from_px(s);
4916
4917 tmptrash = alloc_trash_chunk();
4918 if (tmptrash) {
4919 chunk_printf(tmptrash,
4920 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4921 s->proxy->id, s->id);
4922
Emeric Brun5a133512017-10-19 14:42:30 +02004923 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004924 ha_warning("%s.\n", tmptrash->area);
4925 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4926 tmptrash->area);
4927 send_email_alert(s, LOG_NOTICE, "%s",
4928 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004929 free_trash_chunk(tmptrash);
4930 tmptrash = NULL;
4931 }
4932
4933 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4934 set_backend_down(s->proxy);
4935 }
4936 else if (s->cur_eweight != s->next_eweight) {
4937 /* now propagate the status change to any LB algorithms */
4938 if (px->lbprm.update_server_eweight)
4939 px->lbprm.update_server_eweight(s);
4940 else if (srv_willbe_usable(s)) {
4941 if (px->lbprm.set_server_status_up)
4942 px->lbprm.set_server_status_up(s);
4943 }
4944 else {
4945 if (px->lbprm.set_server_status_down)
4946 px->lbprm.set_server_status_down(s);
4947 }
4948
4949 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4950 set_backend_down(s->proxy);
4951 }
4952
4953 s->next_admin = next_admin;
4954 }
4955
Emeric Brun5a133512017-10-19 14:42:30 +02004956 /* reset operational state change */
4957 *s->op_st_chg.reason = 0;
4958 s->op_st_chg.status = s->op_st_chg.code = -1;
4959 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004960
4961 /* Now we try to apply pending admin changes */
4962
4963 /* Maintenance must also disable health checks */
4964 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4965 if (s->check.state & CHK_ST_ENABLED) {
4966 s->check.state |= CHK_ST_PAUSED;
4967 check->health = 0;
4968 }
4969
4970 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004971 tmptrash = alloc_trash_chunk();
4972 if (tmptrash) {
4973 chunk_printf(tmptrash,
4974 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4975 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4976 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004977
Olivier Houchard796a2b32017-10-24 17:42:47 +02004978 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004979
Olivier Houchard796a2b32017-10-24 17:42:47 +02004980 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004981 ha_warning("%s.\n", tmptrash->area);
4982 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4983 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004984 }
4985 free_trash_chunk(tmptrash);
4986 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004987 }
Emeric Brun8f298292017-12-06 16:47:17 +01004988 /* commit new admin status */
4989
4990 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004991 }
4992 else { /* server was still running */
4993 check->health = 0; /* failure */
4994 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004995
4996 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004997 if (s->proxy->lbprm.set_server_status_down)
4998 s->proxy->lbprm.set_server_status_down(s);
4999
Emeric Brun64cc49c2017-10-03 14:46:45 +02005000 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5001 srv_shutdown_streams(s, SF_ERR_DOWN);
5002
5003 /* we might have streams queued on this server and waiting for
5004 * a connection. Those which are redispatchable will be queued
5005 * to another server or to the proxy itself.
5006 */
5007 xferred = pendconn_redistribute(s);
5008
5009 tmptrash = alloc_trash_chunk();
5010 if (tmptrash) {
5011 chunk_printf(tmptrash,
5012 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5013 s->flags & SRV_F_BACKUP ? "Backup " : "",
5014 s->proxy->id, s->id,
5015 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5016
5017 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5018
5019 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005020 ha_warning("%s.\n", tmptrash->area);
5021 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5022 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005023 }
5024 free_trash_chunk(tmptrash);
5025 tmptrash = NULL;
5026 }
5027 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5028 set_backend_down(s->proxy);
5029
5030 s->counters.down_trans++;
5031 }
5032 }
5033 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5034 /* OK here we're leaving maintenance, we have many things to check,
5035 * because the server might possibly be coming back up depending on
5036 * its state. In practice, leaving maintenance means that we should
5037 * immediately turn to UP (more or less the slowstart) under the
5038 * following conditions :
5039 * - server is neither checked nor tracked
5040 * - server tracks another server which is not checked
5041 * - server tracks another server which is already up
5042 * Which sums up as something simpler :
5043 * "either the tracking server is up or the server's checks are disabled
5044 * or up". Otherwise we only re-enable health checks. There's a special
5045 * case associated to the stopping state which can be inherited. Note
5046 * that the server might still be in drain mode, which is naturally dealt
5047 * with by the lower level functions.
5048 */
5049
5050 if (s->check.state & CHK_ST_ENABLED) {
5051 s->check.state &= ~CHK_ST_PAUSED;
5052 check->health = check->rise; /* start OK but check immediately */
5053 }
5054
5055 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5056 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5057 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5058 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5059 s->next_state = SRV_ST_STOPPING;
5060 }
5061 else {
5062 s->next_state = SRV_ST_STARTING;
5063 if (s->slowstart > 0)
5064 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5065 else
5066 s->next_state = SRV_ST_RUNNING;
5067 }
5068
5069 }
5070
5071 tmptrash = alloc_trash_chunk();
5072 if (tmptrash) {
5073 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5074 chunk_printf(tmptrash,
5075 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5076 s->flags & SRV_F_BACKUP ? "Backup " : "",
5077 s->proxy->id, s->id,
5078 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5079 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5080 }
5081 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5082 chunk_printf(tmptrash,
5083 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5084 s->flags & SRV_F_BACKUP ? "Backup " : "",
5085 s->proxy->id, s->id, s->hostname,
5086 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5087 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5088 }
5089 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5090 chunk_printf(tmptrash,
5091 "%sServer %s/%s is %s/%s (leaving maintenance)",
5092 s->flags & SRV_F_BACKUP ? "Backup " : "",
5093 s->proxy->id, s->id,
5094 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5095 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5096 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005097 ha_warning("%s.\n", tmptrash->area);
5098 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5099 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005100 free_trash_chunk(tmptrash);
5101 tmptrash = NULL;
5102 }
5103
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005104 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005105 /* now propagate the status change to any LB algorithms */
5106 if (px->lbprm.update_server_eweight)
5107 px->lbprm.update_server_eweight(s);
5108 else if (srv_willbe_usable(s)) {
5109 if (px->lbprm.set_server_status_up)
5110 px->lbprm.set_server_status_up(s);
5111 }
5112 else {
5113 if (px->lbprm.set_server_status_down)
5114 px->lbprm.set_server_status_down(s);
5115 }
5116
5117 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5118 set_backend_down(s->proxy);
5119
Willy Tarreau6a78e612018-08-07 10:14:53 +02005120 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5121 * and it's not a backup server and its effective weight is > 0,
5122 * then it can accept new connections, so we shut down all streams
5123 * on all backup servers.
5124 */
5125 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5126 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5127 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5128
5129 /* check if we can handle some connections queued at the proxy. We
5130 * will take as many as we can handle.
5131 */
5132 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005133 }
5134 else if (s->next_admin & SRV_ADMF_MAINT) {
5135 /* remaining in maintenance mode, let's inform precisely about the
5136 * situation.
5137 */
5138 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5139 tmptrash = alloc_trash_chunk();
5140 if (tmptrash) {
5141 chunk_printf(tmptrash,
5142 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5143 s->flags & SRV_F_BACKUP ? "Backup " : "",
5144 s->proxy->id, s->id);
5145
5146 if (s->track) /* normally it's mandatory here */
5147 chunk_appendf(tmptrash, " via %s/%s",
5148 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005149 ha_warning("%s.\n", tmptrash->area);
5150 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5151 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005152 free_trash_chunk(tmptrash);
5153 tmptrash = NULL;
5154 }
5155 }
5156 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5157 tmptrash = alloc_trash_chunk();
5158 if (tmptrash) {
5159 chunk_printf(tmptrash,
5160 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5161 s->flags & SRV_F_BACKUP ? "Backup " : "",
5162 s->proxy->id, s->id, s->hostname);
5163
5164 if (s->track) /* normally it's mandatory here */
5165 chunk_appendf(tmptrash, " via %s/%s",
5166 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005167 ha_warning("%s.\n", tmptrash->area);
5168 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5169 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005170 free_trash_chunk(tmptrash);
5171 tmptrash = NULL;
5172 }
5173 }
5174 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5175 tmptrash = alloc_trash_chunk();
5176 if (tmptrash) {
5177 chunk_printf(tmptrash,
5178 "%sServer %s/%s remains in forced maintenance",
5179 s->flags & SRV_F_BACKUP ? "Backup " : "",
5180 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005181 ha_warning("%s.\n", tmptrash->area);
5182 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5183 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005184 free_trash_chunk(tmptrash);
5185 tmptrash = NULL;
5186 }
5187 }
5188 /* don't report anything when leaving drain mode and remaining in maintenance */
5189
5190 s->cur_admin = s->next_admin;
5191 }
5192
5193 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5194 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5195 /* drain state is applied only if not yet in maint */
5196
5197 s->last_change = now.tv_sec;
5198 if (px->lbprm.set_server_status_down)
5199 px->lbprm.set_server_status_down(s);
5200
5201 /* we might have streams queued on this server and waiting for
5202 * a connection. Those which are redispatchable will be queued
5203 * to another server or to the proxy itself.
5204 */
5205 xferred = pendconn_redistribute(s);
5206
5207 tmptrash = alloc_trash_chunk();
5208 if (tmptrash) {
5209 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5210 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5211 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5212
5213 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5214
5215 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005216 ha_warning("%s.\n", tmptrash->area);
5217 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5218 tmptrash->area);
5219 send_email_alert(s, LOG_NOTICE, "%s",
5220 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005221 }
5222 free_trash_chunk(tmptrash);
5223 tmptrash = NULL;
5224 }
5225
5226 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5227 set_backend_down(s->proxy);
5228 }
5229 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5230 /* OK completely leaving drain mode */
5231 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5232 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5233 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5234 s->proxy->last_change = now.tv_sec;
5235 }
5236
5237 if (s->last_change < now.tv_sec) // ignore negative times
5238 s->down_time += now.tv_sec - s->last_change;
5239 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005240 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005241
5242 tmptrash = alloc_trash_chunk();
5243 if (tmptrash) {
5244 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5245 chunk_printf(tmptrash,
5246 "%sServer %s/%s is %s (leaving forced drain)",
5247 s->flags & SRV_F_BACKUP ? "Backup " : "",
5248 s->proxy->id, s->id,
5249 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5250 }
5251 else {
5252 chunk_printf(tmptrash,
5253 "%sServer %s/%s is %s (leaving drain)",
5254 s->flags & SRV_F_BACKUP ? "Backup " : "",
5255 s->proxy->id, s->id,
5256 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5257 if (s->track) /* normally it's mandatory here */
5258 chunk_appendf(tmptrash, " via %s/%s",
5259 s->track->proxy->id, s->track->id);
5260 }
5261
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005262 ha_warning("%s.\n", tmptrash->area);
5263 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5264 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005265 free_trash_chunk(tmptrash);
5266 tmptrash = NULL;
5267 }
5268
5269 /* now propagate the status change to any LB algorithms */
5270 if (px->lbprm.update_server_eweight)
5271 px->lbprm.update_server_eweight(s);
5272 else if (srv_willbe_usable(s)) {
5273 if (px->lbprm.set_server_status_up)
5274 px->lbprm.set_server_status_up(s);
5275 }
5276 else {
5277 if (px->lbprm.set_server_status_down)
5278 px->lbprm.set_server_status_down(s);
5279 }
5280 }
5281 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5282 /* remaining in drain mode after removing one of its flags */
5283
5284 tmptrash = alloc_trash_chunk();
5285 if (tmptrash) {
5286 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5287 chunk_printf(tmptrash,
5288 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5289 s->flags & SRV_F_BACKUP ? "Backup " : "",
5290 s->proxy->id, s->id);
5291
5292 if (s->track) /* normally it's mandatory here */
5293 chunk_appendf(tmptrash, " via %s/%s",
5294 s->track->proxy->id, s->track->id);
5295 }
5296 else {
5297 chunk_printf(tmptrash,
5298 "%sServer %s/%s remains in forced drain mode",
5299 s->flags & SRV_F_BACKUP ? "Backup " : "",
5300 s->proxy->id, s->id);
5301 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005302 ha_warning("%s.\n", tmptrash->area);
5303 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5304 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005305 free_trash_chunk(tmptrash);
5306 tmptrash = NULL;
5307 }
5308
5309 /* commit new admin status */
5310
5311 s->cur_admin = s->next_admin;
5312 }
5313 }
5314
5315 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005316 *s->adm_st_chg_cause = 0;
5317}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005318
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005319static struct task *cleanup_idle_connections(struct task *task, void *context, unsigned short state)
5320{
5321 struct server *srv = context;
5322 struct connection *conn, *conn_back;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005323 unsigned int to_destroy = srv->curr_idle_conns / 2 + (srv->curr_idle_conns & 1);
5324 unsigned int i = 0;
5325
5326
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005327
5328 list_for_each_entry_safe(conn, conn_back, &srv->idle_orphan_conns[tid], list) {
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005329 if (i == to_destroy)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005330 break;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005331 conn->mux->destroy(conn);
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005332 i++;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005333 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005334 if (!LIST_ISEMPTY(&srv->idle_orphan_conns[tid]))
5335 task_schedule(task, tick_add(now_ms, srv->pool_purge_delay));
5336 else
5337 task->expire = TICK_ETERNITY;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005338 return task;
5339}
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005340/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005341 * Local variables:
5342 * c-indent-level: 8
5343 * c-basic-offset: 8
5344 * End:
5345 */