blob: af0069668e0df976d8d7074cf21664db94a89904 [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 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100392
Olivier Houchard006e3102018-12-10 18:30:32 +0100393 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100394 if ((int)newsrv->max_idle_conns < -1) {
395 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
396 return ERR_ALERT | ERR_FATAL;
397 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100398
399 return 0;
400}
401
Willy Tarreaudff55432012-10-10 17:51:05 +0200402/* parse the "id" server keyword */
403static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
404{
405 struct eb32_node *node;
406
407 if (!*args[*cur_arg + 1]) {
408 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
409 return ERR_ALERT | ERR_FATAL;
410 }
411
412 newsrv->puid = atol(args[*cur_arg + 1]);
413 newsrv->conf.id.key = newsrv->puid;
414
415 if (newsrv->puid <= 0) {
416 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
417 return ERR_ALERT | ERR_FATAL;
418 }
419
420 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
421 if (node) {
422 struct server *target = container_of(node, struct server, conf.id);
423 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
424 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
425 target->id);
426 return ERR_ALERT | ERR_FATAL;
427 }
428
429 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200430 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200431 return 0;
432}
433
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100434/* Parse the "namespace" server keyword */
435static int srv_parse_namespace(char **args, int *cur_arg,
436 struct proxy *curproxy, struct server *newsrv, char **err)
437{
438#ifdef CONFIG_HAP_NS
439 char *arg;
440
441 arg = args[*cur_arg + 1];
442 if (!*arg) {
443 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
444 return ERR_ALERT | ERR_FATAL;
445 }
446
447 if (!strcmp(arg, "*")) {
448 /* Use the namespace associated with the connection (if present). */
449 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
450 return 0;
451 }
452
453 /*
454 * As this parser may be called several times for the same 'default-server'
455 * object, or for a new 'server' instance deriving from a 'default-server'
456 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
457 */
458 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
459
460 newsrv->netns = netns_store_lookup(arg, strlen(arg));
461 if (!newsrv->netns)
462 newsrv->netns = netns_store_insert(arg);
463
464 if (!newsrv->netns) {
465 memprintf(err, "Cannot open namespace '%s'", arg);
466 return ERR_ALERT | ERR_FATAL;
467 }
468
469 return 0;
470#else
471 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
472 return ERR_ALERT | ERR_FATAL;
473#endif
474}
475
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100476/* Parse the "no-agent-check" server keyword */
477static int srv_parse_no_agent_check(char **args, int *cur_arg,
478 struct proxy *curproxy, struct server *newsrv, char **err)
479{
480 free_check(&newsrv->agent);
481 newsrv->agent.inter = 0;
482 newsrv->agent.port = 0;
483 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
484 newsrv->do_agent = 0;
485 return 0;
486}
487
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100488/* Parse the "no-backup" server keyword */
489static int srv_parse_no_backup(char **args, int *cur_arg,
490 struct proxy *curproxy, struct server *newsrv, char **err)
491{
492 newsrv->flags &= ~SRV_F_BACKUP;
493 return 0;
494}
495
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100496/* Parse the "no-check" server keyword */
497static int srv_parse_no_check(char **args, int *cur_arg,
498 struct proxy *curproxy, struct server *newsrv, char **err)
499{
500 free_check(&newsrv->check);
501 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
502 newsrv->do_check = 0;
503 return 0;
504}
505
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100506/* Parse the "no-check-send-proxy" server keyword */
507static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
508 struct proxy *curproxy, struct server *newsrv, char **err)
509{
510 newsrv->check.send_proxy = 0;
511 return 0;
512}
513
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100514/* Disable server PROXY protocol flags. */
515static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
516{
517 srv->pp_opts &= ~flags;
518 return 0;
519}
520
521/* Parse the "no-send-proxy" server keyword */
522static int srv_parse_no_send_proxy(char **args, int *cur_arg,
523 struct proxy *curproxy, struct server *newsrv, char **err)
524{
525 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
526}
527
528/* Parse the "no-send-proxy-v2" server keyword */
529static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
530 struct proxy *curproxy, struct server *newsrv, char **err)
531{
532 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
533}
534
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100535/* Parse the "non-stick" server keyword */
536static int srv_parse_non_stick(char **args, int *cur_arg,
537 struct proxy *curproxy, struct server *newsrv, char **err)
538{
539 newsrv->flags |= SRV_F_NON_STICK;
540 return 0;
541}
542
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100543/* Enable server PROXY protocol flags. */
544static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
545{
546 srv->pp_opts |= flags;
547 return 0;
548}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200549/* parse the "proto" server keyword */
550static int srv_parse_proto(char **args, int *cur_arg,
551 struct proxy *px, struct server *newsrv, char **err)
552{
553 struct ist proto;
554
555 if (!*args[*cur_arg + 1]) {
556 memprintf(err, "'%s' : missing value", args[*cur_arg]);
557 return ERR_ALERT | ERR_FATAL;
558 }
559 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
560 newsrv->mux_proto = get_mux_proto(proto);
561 if (!newsrv->mux_proto) {
562 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
563 return ERR_ALERT | ERR_FATAL;
564 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200565 return 0;
566}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100567
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100568/* parse the "proxy-v2-options" */
569static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
570 struct proxy *px, struct server *newsrv, char **err)
571{
572 char *p, *n;
573 for (p = args[*cur_arg+1]; p; p = n) {
574 n = strchr(p, ',');
575 if (n)
576 *n++ = '\0';
577 if (!strcmp(p, "ssl")) {
578 newsrv->pp_opts |= SRV_PP_V2_SSL;
579 } else if (!strcmp(p, "cert-cn")) {
580 newsrv->pp_opts |= SRV_PP_V2_SSL;
581 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100582 } else if (!strcmp(p, "cert-key")) {
583 newsrv->pp_opts |= SRV_PP_V2_SSL;
584 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
585 } else if (!strcmp(p, "cert-sig")) {
586 newsrv->pp_opts |= SRV_PP_V2_SSL;
587 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
588 } else if (!strcmp(p, "ssl-cipher")) {
589 newsrv->pp_opts |= SRV_PP_V2_SSL;
590 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100591 } else if (!strcmp(p, "authority")) {
592 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100593 } else if (!strcmp(p, "crc32c")) {
594 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100595 } else
596 goto fail;
597 }
598 return 0;
599 fail:
600 if (err)
601 memprintf(err, "'%s' : proxy v2 option not implemented", p);
602 return ERR_ALERT | ERR_FATAL;
603}
604
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100605/* Parse the "observe" server keyword */
606static int srv_parse_observe(char **args, int *cur_arg,
607 struct proxy *curproxy, struct server *newsrv, char **err)
608{
609 char *arg;
610
611 arg = args[*cur_arg + 1];
612 if (!*arg) {
613 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
614 return ERR_ALERT | ERR_FATAL;
615 }
616
617 if (!strcmp(arg, "none")) {
618 newsrv->observe = HANA_OBS_NONE;
619 }
620 else if (!strcmp(arg, "layer4")) {
621 newsrv->observe = HANA_OBS_LAYER4;
622 }
623 else if (!strcmp(arg, "layer7")) {
624 if (curproxy->mode != PR_MODE_HTTP) {
625 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
626 return ERR_ALERT;
627 }
628 newsrv->observe = HANA_OBS_LAYER7;
629 }
630 else {
631 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
632 "but got '%s'\n", args[*cur_arg], arg);
633 return ERR_ALERT | ERR_FATAL;
634 }
635
636 return 0;
637}
638
Frédéric Lécaille16186232017-03-14 16:42:49 +0100639/* Parse the "redir" server keyword */
640static int srv_parse_redir(char **args, int *cur_arg,
641 struct proxy *curproxy, struct server *newsrv, char **err)
642{
643 char *arg;
644
645 arg = args[*cur_arg + 1];
646 if (!*arg) {
647 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
648 return ERR_ALERT | ERR_FATAL;
649 }
650
651 free(newsrv->rdr_pfx);
652 newsrv->rdr_pfx = strdup(arg);
653 newsrv->rdr_len = strlen(arg);
654
655 return 0;
656}
657
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100658/* Parse the "send-proxy" server keyword */
659static int srv_parse_send_proxy(char **args, int *cur_arg,
660 struct proxy *curproxy, struct server *newsrv, char **err)
661{
662 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
663}
664
665/* Parse the "send-proxy-v2" server keyword */
666static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
667 struct proxy *curproxy, struct server *newsrv, char **err)
668{
669 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
670}
671
Frédéric Lécailledba97072017-03-17 15:33:50 +0100672
673/* Parse the "source" server keyword */
674static int srv_parse_source(char **args, int *cur_arg,
675 struct proxy *curproxy, struct server *newsrv, char **err)
676{
677 char *errmsg;
678 int port_low, port_high;
679 struct sockaddr_storage *sk;
680 struct protocol *proto;
681
682 errmsg = NULL;
683
684 if (!*args[*cur_arg + 1]) {
685 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
686 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
687 goto err;
688 }
689
690 /* 'sk' is statically allocated (no need to be freed). */
691 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
692 if (!sk) {
693 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
694 goto err;
695 }
696
697 proto = protocol_by_family(sk->ss_family);
698 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100699 ha_alert("'%s %s' : connect() not supported for this address family.\n",
700 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100701 goto err;
702 }
703
704 newsrv->conn_src.opts |= CO_SRC_BIND;
705 newsrv->conn_src.source_addr = *sk;
706
707 if (port_low != port_high) {
708 int i;
709
710 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100711 ha_alert("'%s' does not support port offsets (found '%s').\n",
712 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100713 goto err;
714 }
715
716 if (port_low <= 0 || port_low > 65535 ||
717 port_high <= 0 || port_high > 65535 ||
718 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100719 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 +0100720 goto err;
721 }
722 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
723 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
724 newsrv->conn_src.sport_range->ports[i] = port_low + i;
725 }
726
727 *cur_arg += 2;
728 while (*(args[*cur_arg])) {
729 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
730#if defined(CONFIG_HAP_TRANSPARENT)
731 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100732 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
733 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100734 goto err;
735 }
736 if (!strcmp(args[*cur_arg + 1], "client")) {
737 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
738 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
739 }
740 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
741 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
742 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
743 }
744 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
745 char *name, *end;
746
747 name = args[*cur_arg + 1] + 7;
748 while (isspace(*name))
749 name++;
750
751 end = name;
752 while (*end && !isspace(*end) && *end != ',' && *end != ')')
753 end++;
754
755 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
756 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
757 free(newsrv->conn_src.bind_hdr_name);
758 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
759 newsrv->conn_src.bind_hdr_len = end - name;
760 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
761 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
762 newsrv->conn_src.bind_hdr_occ = -1;
763
764 /* now look for an occurrence number */
765 while (isspace(*end))
766 end++;
767 if (*end == ',') {
768 end++;
769 name = end;
770 if (*end == '-')
771 end++;
772 while (isdigit((int)*end))
773 end++;
774 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
775 }
776
777 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100778 ha_alert("usesrc hdr_ip(name,num) does not support negative"
779 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100780 goto err;
781 }
782 }
783 else {
784 struct sockaddr_storage *sk;
785 int port1, port2;
786
787 /* 'sk' is statically allocated (no need to be freed). */
788 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
789 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100790 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100791 goto err;
792 }
793
794 proto = protocol_by_family(sk->ss_family);
795 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100796 ha_alert("'%s %s' : connect() not supported for this address family.\n",
797 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100798 goto err;
799 }
800
801 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100802 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
803 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100804 goto err;
805 }
806 newsrv->conn_src.tproxy_addr = *sk;
807 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
808 }
809 global.last_checks |= LSTCHK_NETADM;
810 *cur_arg += 2;
811 continue;
812#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100813 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 +0100814 goto err;
815#endif /* defined(CONFIG_HAP_TRANSPARENT) */
816 } /* "usesrc" */
817
818 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
819#ifdef SO_BINDTODEVICE
820 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100821 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100822 goto err;
823 }
824 free(newsrv->conn_src.iface_name);
825 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
826 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
827 global.last_checks |= LSTCHK_NETADM;
828#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100829 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100830 goto err;
831#endif
832 *cur_arg += 2;
833 continue;
834 }
835 /* this keyword in not an option of "source" */
836 break;
837 } /* while */
838
839 return 0;
840
841 err:
842 free(errmsg);
843 return ERR_ALERT | ERR_FATAL;
844}
845
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100846/* Parse the "stick" server keyword */
847static int srv_parse_stick(char **args, int *cur_arg,
848 struct proxy *curproxy, struct server *newsrv, char **err)
849{
850 newsrv->flags &= ~SRV_F_NON_STICK;
851 return 0;
852}
853
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100854/* Parse the "track" server keyword */
855static int srv_parse_track(char **args, int *cur_arg,
856 struct proxy *curproxy, struct server *newsrv, char **err)
857{
858 char *arg;
859
860 arg = args[*cur_arg + 1];
861 if (!*arg) {
862 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
863 return ERR_ALERT | ERR_FATAL;
864 }
865
866 free(newsrv->trackit);
867 newsrv->trackit = strdup(arg);
868
869 return 0;
870}
871
Frédéric Lécailledba97072017-03-17 15:33:50 +0100872
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200873/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200874 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200875 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200876 *
877 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200878 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200879void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200880{
Willy Tarreau87b09662015-04-03 00:22:06 +0200881 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200882
Willy Tarreau87b09662015-04-03 00:22:06 +0200883 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
884 if (stream->srv_conn == srv)
885 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200886}
887
888/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200889 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200890 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200891 *
892 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200893 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200894void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200895{
896 struct server *srv;
897
898 for (srv = px->srv; srv != NULL; srv = srv->next)
899 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200900 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200901}
902
Willy Tarreaubda92272014-05-20 21:55:30 +0200903/* Appends some information to a message string related to a server going UP or
904 * DOWN. If both <forced> and <reason> are null and the server tracks another
905 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200906 * If <check> is non-null, an entire string describing the check result will be
907 * appended after a comma and a space (eg: to report some information from the
908 * check that changed the state). In the other case, the string will be built
909 * using the check results stored into the struct server if present.
910 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200911 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200912 *
913 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200914 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200915void srv_append_status(struct buffer *msg, struct server *s,
916 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200917{
Emeric Brun5a133512017-10-19 14:42:30 +0200918 short status = s->op_st_chg.status;
919 short code = s->op_st_chg.code;
920 long duration = s->op_st_chg.duration;
921 char *desc = s->op_st_chg.reason;
922
923 if (check) {
924 status = check->status;
925 code = check->code;
926 duration = check->duration;
927 desc = check->desc;
928 }
929
930 if (status != -1) {
931 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
932
933 if (status >= HCHK_STATUS_L57DATA)
934 chunk_appendf(msg, ", code: %d", code);
935
936 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200937 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200938
939 chunk_appendf(msg, ", info: \"");
940
941 chunk_initlen(&src, desc, 0, strlen(desc));
942 chunk_asciiencode(msg, &src, '"');
943
944 chunk_appendf(msg, "\"");
945 }
946
947 if (duration >= 0)
948 chunk_appendf(msg, ", check duration: %ldms", duration);
949 }
950 else if (desc && *desc) {
951 chunk_appendf(msg, ", %s", desc);
952 }
953 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200954 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200955 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200956
957 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200958 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200959 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
960 " %d sessions active, %d requeued, %d remaining in queue",
961 s->proxy->srv_act, s->proxy->srv_bck,
962 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
963 s->cur_sess, xferred, s->nbpend);
964 else
965 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
966 " %d sessions requeued, %d total in queue",
967 s->proxy->srv_act, s->proxy->srv_bck,
968 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
969 xferred, s->nbpend);
970 }
971}
972
Emeric Brun5a133512017-10-19 14:42:30 +0200973/* Marks server <s> down, regardless of its checks' statuses. The server is
974 * registered in a list to postpone the counting of the remaining servers on
975 * the proxy and transfers queued streams whenever possible to other servers at
976 * a sync point. Maintenance servers are ignored. It stores the <reason> if
977 * non-null as the reason for going down or the available data from the check
978 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200979 *
980 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200981 */
Emeric Brun5a133512017-10-19 14:42:30 +0200982void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200983{
984 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200985
Emeric Brun64cc49c2017-10-03 14:46:45 +0200986 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200987 return;
988
Emeric Brun52a91d32017-08-31 14:41:55 +0200989 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200990 *s->op_st_chg.reason = 0;
991 s->op_st_chg.status = -1;
992 if (reason) {
993 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
994 }
995 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100996 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200997 s->op_st_chg.code = check->code;
998 s->op_st_chg.status = check->status;
999 s->op_st_chg.duration = check->duration;
1000 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001001
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001002 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001003 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001004
Emeric Brun9f0b4582017-10-23 14:39:51 +02001005 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001006 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001007 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001008 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001009 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001010}
1011
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001012/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001013 * in maintenance. The server is registered in a list to postpone the counting
1014 * of the remaining servers on the proxy and tries to grab requests from the
1015 * proxy at a sync point. Maintenance servers are ignored. It stores the
1016 * <reason> if non-null as the reason for going down or the available data
1017 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001018 *
1019 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001020 */
Emeric Brun5a133512017-10-19 14:42:30 +02001021void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001022{
1023 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001024
Emeric Brun64cc49c2017-10-03 14:46:45 +02001025 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001026 return;
1027
Emeric Brun52a91d32017-08-31 14:41:55 +02001028 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001029 return;
1030
Emeric Brun52a91d32017-08-31 14:41:55 +02001031 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001032 *s->op_st_chg.reason = 0;
1033 s->op_st_chg.status = -1;
1034 if (reason) {
1035 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1036 }
1037 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001038 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001039 s->op_st_chg.code = check->code;
1040 s->op_st_chg.status = check->status;
1041 s->op_st_chg.duration = check->duration;
1042 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001043
Emeric Brun64cc49c2017-10-03 14:46:45 +02001044 if (s->slowstart <= 0)
1045 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001046
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001047 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001048 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001049
Emeric Brun9f0b4582017-10-23 14:39:51 +02001050 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001051 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001052 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001053 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001054 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001055}
1056
Willy Tarreau8eb77842014-05-21 13:54:57 +02001057/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001058 * isn't in maintenance. The server is registered in a list to postpone the
1059 * counting of the remaining servers on the proxy and tries to grab requests
1060 * from the proxy. Maintenance servers are ignored. It stores the
1061 * <reason> if non-null as the reason for going down or the available data
1062 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001063 * up. Note that it makes use of the trash to build the log strings, so <reason>
1064 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001065 *
1066 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001067 */
Emeric Brun5a133512017-10-19 14:42:30 +02001068void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001069{
1070 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001071
Emeric Brun64cc49c2017-10-03 14:46:45 +02001072 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001073 return;
1074
Emeric Brun52a91d32017-08-31 14:41:55 +02001075 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001076 return;
1077
Emeric Brun52a91d32017-08-31 14:41:55 +02001078 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001079 *s->op_st_chg.reason = 0;
1080 s->op_st_chg.status = -1;
1081 if (reason) {
1082 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1083 }
1084 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001085 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001086 s->op_st_chg.code = check->code;
1087 s->op_st_chg.status = check->status;
1088 s->op_st_chg.duration = check->duration;
1089 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001090
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001091 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001092 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001093
Emeric Brun9f0b4582017-10-23 14:39:51 +02001094 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001095 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001096 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001097 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001098 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001099}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001100
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001101/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1102 * enforce either maint mode or drain mode. It is not allowed to set more than
1103 * one flag at once. The equivalent "inherited" flag is propagated to all
1104 * tracking servers. Maintenance mode disables health checks (but not agent
1105 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001106 * is done. If <cause> is non-null, it will be displayed at the end of the log
1107 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001108 *
1109 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001110 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001111void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001112{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001113 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001114
1115 if (!mode)
1116 return;
1117
1118 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001119 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001120 return;
1121
Emeric Brun52a91d32017-08-31 14:41:55 +02001122 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001123 if (cause)
1124 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1125
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001126 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001127 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001128
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001129 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001130 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1131 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001132 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001133
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001134 /* compute the inherited flag to propagate */
1135 if (mode & SRV_ADMF_MAINT)
1136 mode = SRV_ADMF_IMAINT;
1137 else if (mode & SRV_ADMF_DRAIN)
1138 mode = SRV_ADMF_IDRAIN;
1139
Emeric Brun9f0b4582017-10-23 14:39:51 +02001140 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001141 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001142 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001143 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001144 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001145}
1146
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001147/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1148 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1149 * than one flag at once. The equivalent "inherited" flag is propagated to all
1150 * tracking servers. Leaving maintenance mode re-enables health checks. When
1151 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001152 *
1153 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001154 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001155void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001156{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001157 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001158
1159 if (!mode)
1160 return;
1161
1162 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001163 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001164 return;
1165
Emeric Brun52a91d32017-08-31 14:41:55 +02001166 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001167
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001168 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001169 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001170
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001171 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001172 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1173 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001174 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001175
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001176 if (mode & SRV_ADMF_MAINT)
1177 mode = SRV_ADMF_IMAINT;
1178 else if (mode & SRV_ADMF_DRAIN)
1179 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001180
Emeric Brun9f0b4582017-10-23 14:39:51 +02001181 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001182 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001183 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001184 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001185 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001186}
1187
Willy Tarreau757478e2016-11-03 19:22:19 +01001188/* principle: propagate maint and drain to tracking servers. This is useful
1189 * upon startup so that inherited states are correct.
1190 */
1191static void srv_propagate_admin_state(struct server *srv)
1192{
1193 struct server *srv2;
1194
1195 if (!srv->trackers)
1196 return;
1197
1198 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001199 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001200 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001201 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001202
Emeric Brun52a91d32017-08-31 14:41:55 +02001203 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001204 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001205 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001206 }
1207}
1208
1209/* Compute and propagate the admin states for all servers in proxy <px>.
1210 * Only servers *not* tracking another one are considered, because other
1211 * ones will be handled when the server they track is visited.
1212 */
1213void srv_compute_all_admin_states(struct proxy *px)
1214{
1215 struct server *srv;
1216
1217 for (srv = px->srv; srv; srv = srv->next) {
1218 if (srv->track)
1219 continue;
1220 srv_propagate_admin_state(srv);
1221 }
1222}
1223
Willy Tarreaudff55432012-10-10 17:51:05 +02001224/* Note: must not be declared <const> as its list will be overwritten.
1225 * Please take care of keeping this list alphabetically sorted, doing so helps
1226 * all code contributors.
1227 * Optional keywords are also declared with a NULL ->parse() function so that
1228 * the config parser can report an appropriate error when a known keyword was
1229 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001230 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001231 */
1232static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001233 { "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 +01001234 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001235 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001236 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001237 { "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 +01001238 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001239 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1240 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001241 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001242 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001243 { "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 +01001244 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001245 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001246 { "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 +01001247 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1248 { "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 +01001249 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001250 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001251 { "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 +01001252 { "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 +02001253 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001254 { "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 +01001255 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001256 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1257 { "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 +02001258 { "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 +01001259 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001260 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001261 { NULL, NULL, 0 },
1262}};
1263
Willy Tarreau0108d902018-11-25 19:14:37 +01001264INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001265
Willy Tarreau004e0452013-11-21 11:22:01 +01001266/* Recomputes the server's eweight based on its state, uweight, the current time,
1267 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001268 * state is automatically disabled if the time is elapsed. If <must_update> is
1269 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001270 *
1271 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001272 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001273void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001274{
1275 struct proxy *px = sv->proxy;
1276 unsigned w;
1277
1278 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1279 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001280 if (sv->next_state == SRV_ST_STARTING)
1281 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001282 }
1283
1284 /* We must take care of not pushing the server to full throttle during slow starts.
1285 * It must also start immediately, at least at the minimal step when leaving maintenance.
1286 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001287 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001288 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1289 else
1290 w = px->lbprm.wdiv;
1291
Emeric Brun52a91d32017-08-31 14:41:55 +02001292 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001293
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001294 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001295 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001296 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001297}
1298
Willy Tarreaubaaee002006-06-26 02:48:02 +02001299/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001300 * Parses weight_str and configures sv accordingly.
1301 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001302 *
1303 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001304 */
1305const char *server_parse_weight_change_request(struct server *sv,
1306 const char *weight_str)
1307{
1308 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001309 long int w;
1310 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001311
1312 px = sv->proxy;
1313
1314 /* if the weight is terminated with '%', it is set relative to
1315 * the initial weight, otherwise it is absolute.
1316 */
1317 if (!*weight_str)
1318 return "Require <weight> or <weight%>.\n";
1319
Simon Hormanb796afa2013-02-12 10:45:53 +09001320 w = strtol(weight_str, &end, 10);
1321 if (end == weight_str)
1322 return "Empty weight string empty or preceded by garbage";
1323 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001324 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001325 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001326 /* Avoid integer overflow */
1327 if (w > 25600)
1328 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001329 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001330 if (w > 256)
1331 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001332 }
1333 else if (w < 0 || w > 256)
1334 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001335 else if (end[0] != '\0')
1336 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001337
1338 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1339 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1340
1341 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001342 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001343
1344 return NULL;
1345}
1346
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001347/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001348 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1349 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001350 * Returns:
1351 * - error string on error
1352 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001353 *
1354 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001355 */
1356const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001357 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001358{
1359 unsigned char ip[INET6_ADDRSTRLEN];
1360
1361 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001362 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001363 return NULL;
1364 }
1365 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001366 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001367 return NULL;
1368 }
1369
1370 return "Could not understand IP address format.\n";
1371}
1372
Willy Tarreau46b7f532018-08-21 11:54:26 +02001373/*
1374 * Must be called with the server lock held.
1375 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001376const char *server_parse_maxconn_change_request(struct server *sv,
1377 const char *maxconn_str)
1378{
1379 long int v;
1380 char *end;
1381
1382 if (!*maxconn_str)
1383 return "Require <maxconn>.\n";
1384
1385 v = strtol(maxconn_str, &end, 10);
1386 if (end == maxconn_str)
1387 return "maxconn string empty or preceded by garbage";
1388 else if (end[0] != '\0')
1389 return "Trailing garbage in maxconn string";
1390
1391 if (sv->maxconn == sv->minconn) { // static maxconn
1392 sv->maxconn = sv->minconn = v;
1393 } else { // dynamic maxconn
1394 sv->maxconn = v;
1395 }
1396
1397 if (may_dequeue_tasks(sv, sv->proxy))
1398 process_srv_queue(sv);
1399
1400 return NULL;
1401}
1402
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001403#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001404static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1405 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001406{
1407 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001408 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001409 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001410 NULL,
1411 };
1412
1413 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001414 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001415
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001416 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1417}
1418
1419static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1420{
1421 struct sample_expr *expr;
1422
1423 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 +01001424 if (!expr) {
1425 memprintf(err, "error detected while parsing sni expression : %s", *err);
1426 return ERR_ALERT | ERR_FATAL;
1427 }
1428
1429 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1430 memprintf(err, "error detected while parsing sni expression : "
1431 " fetch method '%s' extracts information from '%s', "
1432 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001433 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001434 return ERR_ALERT | ERR_FATAL;
1435 }
1436
1437 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1438 release_sample_expr(newsrv->ssl_ctx.sni);
1439 newsrv->ssl_ctx.sni = expr;
1440
1441 return 0;
1442}
1443#endif
1444
1445static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1446{
1447 if (err && *err) {
1448 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001449 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 +01001450 }
1451 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001452 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1453 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001454}
1455
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001456static void srv_conn_src_sport_range_cpy(struct server *srv,
1457 struct server *src)
1458{
1459 int range_sz;
1460
1461 range_sz = src->conn_src.sport_range->size;
1462 if (range_sz > 0) {
1463 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1464 if (srv->conn_src.sport_range != NULL) {
1465 int i;
1466
1467 for (i = 0; i < range_sz; i++) {
1468 srv->conn_src.sport_range->ports[i] =
1469 src->conn_src.sport_range->ports[i];
1470 }
1471 }
1472 }
1473}
1474
1475/*
1476 * Copy <src> server connection source settings to <srv> server everything needed.
1477 */
1478static void srv_conn_src_cpy(struct server *srv, struct server *src)
1479{
1480 srv->conn_src.opts = src->conn_src.opts;
1481 srv->conn_src.source_addr = src->conn_src.source_addr;
1482
1483 /* Source port range copy. */
1484 if (src->conn_src.sport_range != NULL)
1485 srv_conn_src_sport_range_cpy(srv, src);
1486
1487#ifdef CONFIG_HAP_TRANSPARENT
1488 if (src->conn_src.bind_hdr_name != NULL) {
1489 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1490 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1491 }
1492 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1493 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1494#endif
1495 if (src->conn_src.iface_name != NULL)
1496 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1497}
1498
1499/*
1500 * Copy <src> server SSL settings to <srv> server allocating
1501 * everything needed.
1502 */
1503#if defined(USE_OPENSSL)
1504static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1505{
1506 if (src->ssl_ctx.ca_file != NULL)
1507 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1508 if (src->ssl_ctx.crl_file != NULL)
1509 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1510 if (src->ssl_ctx.client_crt != NULL)
1511 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1512
1513 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1514
1515 if (src->ssl_ctx.verify_host != NULL)
1516 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1517 if (src->ssl_ctx.ciphers != NULL)
1518 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001519#if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
1520 if (src->ssl_ctx.ciphersuites != NULL)
1521 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1522#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001523 if (src->sni_expr != NULL)
1524 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001525
1526#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1527 if (src->ssl_ctx.alpn_str) {
1528 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1529 if (srv->ssl_ctx.alpn_str) {
1530 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1531 src->ssl_ctx.alpn_len);
1532 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1533 }
1534 }
1535#endif
1536#ifdef OPENSSL_NPN_NEGOTIATED
1537 if (src->ssl_ctx.npn_str) {
1538 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1539 if (srv->ssl_ctx.npn_str) {
1540 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1541 src->ssl_ctx.npn_len);
1542 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1543 }
1544 }
1545#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001546}
1547#endif
1548
1549/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001550 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001551 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001552 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001553 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001554static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001555{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001556 char *hostname_dn;
1557 int hostname_len, hostname_dn_len;
1558
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001559 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001560 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001561
Christopher Faulet67957bd2017-09-27 11:00:59 +02001562 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001563 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001564 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1565 hostname_dn, trash.size);
1566 if (hostname_dn_len == -1)
1567 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001568
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001569
Christopher Faulet67957bd2017-09-27 11:00:59 +02001570 free(srv->hostname);
1571 free(srv->hostname_dn);
1572 srv->hostname = strdup(hostname);
1573 srv->hostname_dn = strdup(hostname_dn);
1574 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001575 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001576 goto err;
1577
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001578 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001579
1580 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001581 free(srv->hostname); srv->hostname = NULL;
1582 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001583 return -1;
1584}
1585
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001586/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001587 * Copy <src> server settings to <srv> server allocating
1588 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001589 * This function is not supposed to be called at any time, but only
1590 * during server settings parsing or during server allocations from
1591 * a server template, and just after having calloc()'ed a new server.
1592 * So, <src> may only be a default server (when parsing server settings)
1593 * or a server template (during server allocations from a server template).
1594 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1595 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001596 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001597static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001598{
1599 /* Connection source settings copy */
1600 srv_conn_src_cpy(srv, src);
1601
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001602 if (srv_tmpl) {
1603 srv->addr = src->addr;
1604 srv->svc_port = src->svc_port;
1605 }
1606
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001607 srv->pp_opts = src->pp_opts;
1608 if (src->rdr_pfx != NULL) {
1609 srv->rdr_pfx = strdup(src->rdr_pfx);
1610 srv->rdr_len = src->rdr_len;
1611 }
1612 if (src->cookie != NULL) {
1613 srv->cookie = strdup(src->cookie);
1614 srv->cklen = src->cklen;
1615 }
1616 srv->use_ssl = src->use_ssl;
1617 srv->check.addr = srv->agent.addr = src->check.addr;
1618 srv->check.use_ssl = src->check.use_ssl;
1619 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001620 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001621 srv->check.alpn_str = src->check.alpn_str;
1622 srv->check.alpn_len = srv->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001623 /* Note: 'flags' field has potentially been already initialized. */
1624 srv->flags |= src->flags;
1625 srv->do_check = src->do_check;
1626 srv->do_agent = src->do_agent;
1627 if (srv->check.port)
1628 srv->flags |= SRV_F_CHECKPORT;
1629 srv->check.inter = src->check.inter;
1630 srv->check.fastinter = src->check.fastinter;
1631 srv->check.downinter = src->check.downinter;
1632 srv->agent.use_ssl = src->agent.use_ssl;
1633 srv->agent.port = src->agent.port;
1634 if (src->agent.send_string != NULL)
1635 srv->agent.send_string = strdup(src->agent.send_string);
1636 srv->agent.send_string_len = src->agent.send_string_len;
1637 srv->agent.inter = src->agent.inter;
1638 srv->agent.fastinter = src->agent.fastinter;
1639 srv->agent.downinter = src->agent.downinter;
1640 srv->maxqueue = src->maxqueue;
1641 srv->minconn = src->minconn;
1642 srv->maxconn = src->maxconn;
1643 srv->slowstart = src->slowstart;
1644 srv->observe = src->observe;
1645 srv->onerror = src->onerror;
1646 srv->onmarkeddown = src->onmarkeddown;
1647 srv->onmarkedup = src->onmarkedup;
1648 if (src->trackit != NULL)
1649 srv->trackit = strdup(src->trackit);
1650 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1651 srv->uweight = srv->iweight = src->iweight;
1652
1653 srv->check.send_proxy = src->check.send_proxy;
1654 /* health: up, but will fall down at first failure */
1655 srv->check.rise = srv->check.health = src->check.rise;
1656 srv->check.fall = src->check.fall;
1657
1658 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001659 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1660 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1661 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001662 srv->check.state |= CHK_ST_PAUSED;
1663 srv->check.health = 0;
1664 }
1665
1666 /* health: up but will fall down at first failure */
1667 srv->agent.rise = srv->agent.health = src->agent.rise;
1668 srv->agent.fall = src->agent.fall;
1669
1670 if (src->resolvers_id != NULL)
1671 srv->resolvers_id = strdup(src->resolvers_id);
1672 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001673 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001674 if (srv->dns_opts.family_prio == AF_UNSPEC)
1675 srv->dns_opts.family_prio = AF_INET6;
1676 memcpy(srv->dns_opts.pref_net,
1677 src->dns_opts.pref_net,
1678 sizeof srv->dns_opts.pref_net);
1679 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1680
1681 srv->init_addr_methods = src->init_addr_methods;
1682 srv->init_addr = src->init_addr;
1683#if defined(USE_OPENSSL)
1684 srv_ssl_settings_cpy(srv, src);
1685#endif
1686#ifdef TCP_USER_TIMEOUT
1687 srv->tcp_ut = src->tcp_ut;
1688#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001689 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001690 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001691 srv->max_idle_conns = src->max_idle_conns;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001692
Olivier Houchard8da5f982017-08-04 18:35:36 +02001693 if (srv_tmpl)
1694 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001695}
1696
William Lallemand313bfd12018-10-26 14:47:32 +02001697struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001698{
1699 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001700 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001701
1702 srv = calloc(1, sizeof *srv);
1703 if (!srv)
1704 return NULL;
1705
1706 srv->obj_type = OBJ_TYPE_SERVER;
1707 srv->proxy = proxy;
1708 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001709 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001710
1711 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1712 goto free_srv;
1713 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1714 goto free_priv_conns;
1715 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1716 goto free_idle_conns;
1717
1718 for (i = 0; i < global.nbthread; i++) {
1719 LIST_INIT(&srv->priv_conns[i]);
1720 LIST_INIT(&srv->idle_conns[i]);
1721 LIST_INIT(&srv->safe_conns[i]);
1722 }
1723
Emeric Brun52a91d32017-08-31 14:41:55 +02001724 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001725 srv->last_change = now.tv_sec;
1726
1727 srv->check.status = HCHK_STATUS_INI;
1728 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001729 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001730 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1731
1732 srv->agent.status = HCHK_STATUS_INI;
1733 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001734 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001735 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1736
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001737 srv->pool_purge_delay = 1000;
Olivier Houchard006e3102018-12-10 18:30:32 +01001738 srv->max_idle_conns = -1;
1739
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001740 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001741
1742 free_idle_conns:
1743 free(srv->idle_conns);
1744 free_priv_conns:
1745 free(srv->priv_conns);
1746 free_srv:
1747 free(srv);
1748 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001749}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001750
1751/*
1752 * Validate <srv> server health-check settings.
1753 * Returns 0 if everything is OK, -1 if not.
1754 */
1755static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1756{
1757 struct tcpcheck_rule *r = NULL;
1758 struct list *l;
1759
1760 /*
1761 * We need at least a service port, a check port or the first tcp-check rule must
1762 * be a 'connect' one when checking an IPv4/IPv6 server.
1763 */
1764 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1765 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1766 return 0;
1767
1768 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1769 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001770 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1771 "Check has been disabled.\n",
1772 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001773 return -1;
1774 }
1775
1776 /* search the first action (connect / send / expect) in the list */
1777 l = &srv->proxy->tcpcheck_rules;
1778 list_for_each_entry(r, l, list) {
1779 if (r->action != TCPCHK_ACT_COMMENT)
1780 break;
1781 }
1782
1783 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001784 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1785 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1786 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001787 return -1;
1788 }
1789
1790 /* scan the tcp-check ruleset to ensure a port has been configured */
1791 l = &srv->proxy->tcpcheck_rules;
1792 list_for_each_entry(r, l, list) {
1793 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001794 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1795 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1796 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001797 return -1;
1798 }
1799 }
1800
1801 return 0;
1802}
1803
1804/*
1805 * Initialize <srv> health-check structure.
1806 * Returns the error string in case of memory allocation failure, NULL if not.
1807 */
1808static const char *do_health_check_init(struct server *srv, int check_type, int state)
1809{
1810 const char *ret;
1811
1812 if (!srv->do_check)
1813 return NULL;
1814
1815 ret = init_check(&srv->check, check_type);
1816 if (ret)
1817 return ret;
1818
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001819 srv->check.state |= state;
1820 global.maxsock++;
1821
1822 return NULL;
1823}
1824
1825static int server_health_check_init(const char *file, int linenum,
1826 struct server *srv, struct proxy *curproxy)
1827{
1828 const char *ret;
1829
1830 if (!srv->do_check)
1831 return 0;
1832
1833 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001834 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1835 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001836 return ERR_ALERT | ERR_FATAL;
1837 }
1838
1839 if (server_healthcheck_validate(file, linenum, srv) < 0)
1840 return ERR_ALERT | ERR_ABORT;
1841
1842 /* note: check type will be set during the config review phase */
1843 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1844 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001845 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001846 return ERR_ALERT | ERR_ABORT;
1847 }
1848
1849 return 0;
1850}
1851
1852/*
1853 * Initialize <srv> agent check structure.
1854 * Returns the error string in case of memory allocation failure, NULL if not.
1855 */
1856static const char *do_server_agent_check_init(struct server *srv, int state)
1857{
1858 const char *ret;
1859
1860 if (!srv->do_agent)
1861 return NULL;
1862
1863 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1864 if (ret)
1865 return ret;
1866
1867 if (!srv->agent.inter)
1868 srv->agent.inter = srv->check.inter;
1869
1870 srv->agent.state |= state;
1871 global.maxsock++;
1872
1873 return NULL;
1874}
1875
1876static int server_agent_check_init(const char *file, int linenum,
1877 struct server *srv, struct proxy *curproxy)
1878{
1879 const char *ret;
1880
1881 if (!srv->do_agent)
1882 return 0;
1883
1884 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001885 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 +02001886 file, linenum, srv->id);
1887 return ERR_ALERT | ERR_FATAL;
1888 }
1889
1890 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1891 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001892 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001893 return ERR_ALERT | ERR_ABORT;
1894 }
1895
1896 return 0;
1897}
1898
1899#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1900static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1901 struct server *srv, struct proxy *proxy)
1902{
1903 int ret;
1904 char *err = NULL;
1905
1906 if (!srv->sni_expr)
1907 return 0;
1908
1909 ret = server_parse_sni_expr(srv, proxy, &err);
1910 if (!ret)
1911 return 0;
1912
1913 display_parser_err(file, linenum, args, cur_arg, &err);
1914 free(err);
1915
1916 return ret;
1917}
1918#endif
1919
1920/*
1921 * Server initializations finalization.
1922 * Initialize health check, agent check and SNI expression if enabled.
1923 * Must not be called for a default server instance.
1924 */
1925static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1926 struct server *srv, struct proxy *px)
1927{
1928 int ret;
1929
1930 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1931 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1932 return ret;
1933 }
1934
1935#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1936 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1937 return ret;
1938#endif
1939
1940 if (srv->flags & SRV_F_BACKUP)
1941 px->srv_bck++;
1942 else
1943 px->srv_act++;
1944 srv_lb_commit_status(srv);
1945
Daniel Corbett43bb8422019-01-09 08:13:29 -05001946 if (srv->max_idle_conns != 0) {
Willy Tarreau15c120d2019-01-23 10:41:19 +01001947 int i;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001948
Willy Tarreau15c120d2019-01-23 10:41:19 +01001949 srv->idle_orphan_conns = calloc(global.nbthread, sizeof(*srv->idle_orphan_conns));
1950 if (!srv->idle_orphan_conns)
1951 goto err;
1952 srv->idle_task = calloc(global.nbthread, sizeof(*srv->idle_task));
1953 if (!srv->idle_task)
1954 goto err;
1955 for (i = 0; i < global.nbthread; i++) {
1956 LIST_INIT(&srv->idle_orphan_conns[i]);
1957 srv->idle_task[i] = task_new(1 << i);
1958 if (!srv->idle_task[i])
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001959 goto err;
Willy Tarreau15c120d2019-01-23 10:41:19 +01001960 srv->idle_task[i]->process = cleanup_idle_connections;
1961 srv->idle_task[i]->context = srv;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001962 }
Willy Tarreau15c120d2019-01-23 10:41:19 +01001963 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001964
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001965 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001966err:
1967 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001968}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001969
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001970/*
1971 * Parse as much as possible such a range string argument: low[-high]
1972 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1973 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1974 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1975 * Returns 0 if succeeded, -1 if not.
1976 */
1977static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1978{
1979 char *nb_high_arg;
1980
1981 *nb_high = 0;
1982 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001983 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001984
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001985 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001986 *nb_high_arg++ = '\0';
1987 *nb_high = atoi(nb_high_arg);
1988 }
1989 else {
1990 *nb_high += *nb_low;
1991 *nb_low = 1;
1992 }
1993
1994 if (*nb_low < 0 || *nb_high < *nb_low)
1995 return -1;
1996
1997 return 0;
1998}
1999
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002000static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2001{
2002 chunk_printf(&trash, "%s%d", prefix, nb);
2003 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002004 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002005}
2006
2007/*
2008 * Initialize as much as possible servers from <srv> server template.
2009 * Note that a server template is a special server with
2010 * a few different parameters than a server which has
2011 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002012 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002013 * 'srv' template included.
2014 */
2015static int server_template_init(struct server *srv, struct proxy *px)
2016{
2017 int i;
2018 struct server *newsrv;
2019
2020 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2021 int check_init_state;
2022 int agent_init_state;
2023
2024 newsrv = new_server(px);
2025 if (!newsrv)
2026 goto err;
2027
2028 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002029 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002030#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2031 if (newsrv->sni_expr) {
2032 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2033 if (!newsrv->ssl_ctx.sni)
2034 goto err;
2035 }
2036#endif
2037 /* Set this new server ID. */
2038 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2039
2040 /* Initial checks states. */
2041 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2042 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2043
2044 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2045 do_server_agent_check_init(newsrv, agent_init_state))
2046 goto err;
2047
2048 /* Linked backwards first. This will be restablished after parsing. */
2049 newsrv->next = px->srv;
2050 px->srv = newsrv;
Olivier Houchard006e3102018-12-10 18:30:32 +01002051 if (newsrv->max_idle_conns != 0) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002052 int i;
2053
2054 newsrv->idle_orphan_conns = calloc(global.nbthread, sizeof(*newsrv->idle_orphan_conns));
2055 if (!newsrv->idle_orphan_conns)
2056 goto err;
2057 newsrv->idle_task = calloc(global.nbthread, sizeof(*newsrv->idle_task));
2058 if (!newsrv->idle_task)
2059 goto err;
2060 for (i = 0; i < global.nbthread; i++) {
2061 LIST_INIT(&newsrv->idle_orphan_conns[i]);
2062 newsrv->idle_task[i] = task_new(1 << i);
2063 if (!newsrv->idle_task[i])
2064 goto err;
2065 newsrv->idle_task[i]->process = cleanup_idle_connections;
2066 newsrv->idle_task[i]->context = newsrv;
2067 }
2068 }
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002069 }
2070 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2071
2072 return i - srv->tmpl_info.nb_low;
2073
2074 err:
2075 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2076 if (newsrv) {
2077#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2078 release_sample_expr(newsrv->ssl_ctx.sni);
2079#endif
2080 free_check(&newsrv->agent);
2081 free_check(&newsrv->check);
2082 }
2083 free(newsrv);
2084 return i - srv->tmpl_info.nb_low;
2085}
2086
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002087int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy, int parse_addr)
Willy Tarreau272adea2014-03-31 10:39:59 +02002088{
2089 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002090 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002091 char *errmsg = NULL;
2092 int err_code = 0;
2093 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002094 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002095
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002096 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002097 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002098 !strcmp(args[0], "default-server") ||
2099 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002100 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002101 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002102 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002103 int srv_tmpl = !defsrv && !srv;
2104 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002105
2106 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002107 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002108 err_code |= ERR_ALERT | ERR_FATAL;
2109 goto out;
2110 }
2111 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002112 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002113
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002114 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002115 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002116 if (!*args[2]) {
2117 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002118 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002119 file, linenum, args[0]);
2120 err_code |= ERR_ALERT | ERR_FATAL;
2121 goto out;
2122 }
2123
2124 err = invalid_char(args[1]);
2125 }
2126 else if (srv_tmpl) {
2127 if (!*args[3]) {
2128 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002129 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 +02002130 file, linenum, args[0]);
2131 err_code |= ERR_ALERT | ERR_FATAL;
2132 goto out;
2133 }
2134
2135 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002136 }
2137
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002138 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002139 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 +02002140 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002141 err_code |= ERR_ALERT | ERR_FATAL;
2142 goto out;
2143 }
2144
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002145 cur_arg = 2;
2146 if (srv_tmpl) {
2147 /* Parse server-template <nb | range> arg. */
2148 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002149 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002150 file, linenum, args[0], args[cur_arg]);
2151 err_code |= ERR_ALERT | ERR_FATAL;
2152 goto out;
2153 }
2154 cur_arg++;
2155 }
2156
Willy Tarreau272adea2014-03-31 10:39:59 +02002157 if (!defsrv) {
2158 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002159 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002160 struct protocol *proto;
2161
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002162 newsrv = new_server(curproxy);
2163 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002164 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002165 err_code |= ERR_ALERT | ERR_ABORT;
2166 goto out;
2167 }
2168
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002169 if (srv_tmpl) {
2170 newsrv->tmpl_info.nb_low = tmpl_range_low;
2171 newsrv->tmpl_info.nb_high = tmpl_range_high;
2172 }
2173
Willy Tarreau272adea2014-03-31 10:39:59 +02002174 /* the servers are linked backwards first */
2175 newsrv->next = curproxy->srv;
2176 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002177 newsrv->conf.file = strdup(file);
2178 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002179 /* Note: for a server template, its id is its prefix.
2180 * This is a temporary id which will be used for server allocations to come
2181 * after parsing.
2182 */
2183 if (srv)
2184 newsrv->id = strdup(args[1]);
2185 else
2186 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002187
2188 /* several ways to check the port component :
2189 * - IP => port=+0, relative (IPv4 only)
2190 * - IP: => port=+0, relative
2191 * - IP:N => port=N, absolute
2192 * - IP:+N => port=+N, relative
2193 * - IP:-N => port=-N, relative
2194 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002195 if (!parse_addr)
2196 goto skip_addr;
2197
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002198 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002199 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002200 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002201 err_code |= ERR_ALERT | ERR_FATAL;
2202 goto out;
2203 }
2204
2205 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002206 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002207 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002208 file, linenum, args[0], args[1]);
2209 err_code |= ERR_ALERT | ERR_FATAL;
2210 goto out;
2211 }
2212
2213 if (!port1 || !port2) {
2214 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002215 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002216 }
2217 else if (port1 != port2) {
2218 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002219 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002220 file, linenum, args[0], args[1], args[2]);
2221 err_code |= ERR_ALERT | ERR_FATAL;
2222 goto out;
2223 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002224
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002225 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002226 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002227 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002228 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002229 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2230 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2231 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002232 err_code |= ERR_ALERT | ERR_FATAL;
2233 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002234 }
2235 }
2236 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002237 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002238 file, linenum, newsrv->id);
2239 err_code |= ERR_ALERT | ERR_FATAL;
2240 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002241 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002242 }
2243
Willy Tarreau272adea2014-03-31 10:39:59 +02002244 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002245 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002246
Olivier Houchard8da5f982017-08-04 18:35:36 +02002247 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002248 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002249 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002250 err_code |= ERR_ALERT | ERR_FATAL;
2251 goto out;
2252 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002253
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002254 cur_arg++;
2255 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002256 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002257 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002258 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002259 } else {
2260 newsrv = &curproxy->defsrv;
2261 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002262 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002263 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002264 }
2265
2266 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002267 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002268 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2269 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002270 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002271 file, linenum, *err, newsrv->id);
2272 err_code |= ERR_ALERT | ERR_FATAL;
2273 goto out;
2274 }
2275 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002276 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002277 file, linenum, val, args[cur_arg], newsrv->id);
2278 err_code |= ERR_ALERT | ERR_FATAL;
2279 goto out;
2280 }
2281 newsrv->agent.inter = val;
2282 cur_arg += 2;
2283 }
Misiekea849332017-01-09 09:39:51 +01002284 else if (!strcmp(args[cur_arg], "agent-addr")) {
2285 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002286 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002287 goto out;
2288 }
2289
2290 cur_arg += 2;
2291 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002292 else if (!strcmp(args[cur_arg], "agent-port")) {
2293 global.maxsock++;
2294 newsrv->agent.port = atol(args[cur_arg + 1]);
2295 cur_arg += 2;
2296 }
James Brown55f9ff12015-10-21 18:19:05 -07002297 else if (!strcmp(args[cur_arg], "agent-send")) {
2298 global.maxsock++;
2299 free(newsrv->agent.send_string);
2300 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2301 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2302 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2303 cur_arg += 2;
2304 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002305 else if (!strcmp(args[cur_arg], "init-addr")) {
2306 char *p, *end;
2307 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002308 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002309
2310 newsrv->init_addr_methods = 0;
2311 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2312
2313 for (p = args[cur_arg + 1]; *p; p = end) {
2314 /* cut on next comma */
2315 for (end = p; *end && *end != ','; end++);
2316 if (*end)
2317 *(end++) = 0;
2318
Willy Tarreau4310d362016-11-02 15:05:56 +01002319 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002320 if (!strcmp(p, "libc")) {
2321 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2322 }
2323 else if (!strcmp(p, "last")) {
2324 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2325 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002326 else if (!strcmp(p, "none")) {
2327 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2328 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002329 else if (str2ip2(p, &sa, 0)) {
2330 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002331 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002332 file, linenum, args[cur_arg], p);
2333 err_code |= ERR_ALERT | ERR_FATAL;
2334 goto out;
2335 }
2336 newsrv->init_addr = sa;
2337 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2338 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002339 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002340 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 +02002341 file, linenum, args[cur_arg], p);
2342 err_code |= ERR_ALERT | ERR_FATAL;
2343 goto out;
2344 }
2345 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002346 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002347 file, linenum, args[cur_arg], p);
2348 err_code |= ERR_ALERT | ERR_FATAL;
2349 goto out;
2350 }
2351 }
2352 cur_arg += 2;
2353 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002354 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002355 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002356 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2357 cur_arg += 2;
2358 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002359 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2360 char *p, *end;
2361
2362 for (p = args[cur_arg + 1]; *p; p = end) {
2363 /* cut on next comma */
2364 for (end = p; *end && *end != ','; end++);
2365 if (*end)
2366 *(end++) = 0;
2367
2368 if (!strcmp(p, "allow-dup-ip")) {
2369 newsrv->dns_opts.accept_duplicate_ip = 1;
2370 }
2371 else if (!strcmp(p, "prevent-dup-ip")) {
2372 newsrv->dns_opts.accept_duplicate_ip = 0;
2373 }
2374 else {
2375 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2376 file, linenum, args[cur_arg], p);
2377 err_code |= ERR_ALERT | ERR_FATAL;
2378 goto out;
2379 }
2380 }
2381
2382 cur_arg += 2;
2383 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002384 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2385 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002386 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002387 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002388 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002389 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002390 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002391 file, linenum, args[cur_arg]);
2392 err_code |= ERR_ALERT | ERR_FATAL;
2393 goto out;
2394 }
2395 cur_arg += 2;
2396 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002397 else if (!strcmp(args[cur_arg], "resolve-net")) {
2398 char *p, *e;
2399 unsigned char mask;
2400 struct dns_options *opt;
2401
2402 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002403 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002404 file, linenum, args[cur_arg]);
2405 err_code |= ERR_ALERT | ERR_FATAL;
2406 goto out;
2407 }
2408
2409 opt = &newsrv->dns_opts;
2410
2411 /* Split arguments by comma, and convert it from ipv4 or ipv6
2412 * string network in in_addr or in6_addr.
2413 */
2414 p = args[cur_arg + 1];
2415 e = p;
2416 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002417 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002418 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002419 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002420 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2421 err_code |= ERR_ALERT | ERR_FATAL;
2422 goto out;
2423 }
2424 /* look for end or comma. */
2425 while (*e != ',' && *e != '\0')
2426 e++;
2427 if (*e == ',') {
2428 *e = '\0';
2429 e++;
2430 }
2431 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2432 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2433 /* Try to convert input string from ipv4 or ipv6 network. */
2434 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2435 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2436 &mask)) {
2437 /* Try to convert input string from ipv6 network. */
2438 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2439 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2440 } else {
2441 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002442 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002443 file, linenum, args[cur_arg], p);
2444 err_code |= ERR_ALERT | ERR_FATAL;
2445 goto out;
2446 }
2447 opt->pref_net_nb++;
2448 p = e;
2449 }
2450
2451 cur_arg += 2;
2452 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002453 else if (!strcmp(args[cur_arg], "rise")) {
2454 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002455 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002456 file, linenum, args[cur_arg]);
2457 err_code |= ERR_ALERT | ERR_FATAL;
2458 goto out;
2459 }
2460
2461 newsrv->check.rise = atol(args[cur_arg + 1]);
2462 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002463 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002464 file, linenum, args[cur_arg]);
2465 err_code |= ERR_ALERT | ERR_FATAL;
2466 goto out;
2467 }
2468
2469 if (newsrv->check.health)
2470 newsrv->check.health = newsrv->check.rise;
2471 cur_arg += 2;
2472 }
2473 else if (!strcmp(args[cur_arg], "fall")) {
2474 newsrv->check.fall = atol(args[cur_arg + 1]);
2475
2476 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002477 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002478 file, linenum, args[cur_arg]);
2479 err_code |= ERR_ALERT | ERR_FATAL;
2480 goto out;
2481 }
2482
2483 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002484 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002485 file, linenum, args[cur_arg]);
2486 err_code |= ERR_ALERT | ERR_FATAL;
2487 goto out;
2488 }
2489
2490 cur_arg += 2;
2491 }
2492 else if (!strcmp(args[cur_arg], "inter")) {
2493 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2494 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002495 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002496 file, linenum, *err, newsrv->id);
2497 err_code |= ERR_ALERT | ERR_FATAL;
2498 goto out;
2499 }
2500 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002501 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002502 file, linenum, val, args[cur_arg], newsrv->id);
2503 err_code |= ERR_ALERT | ERR_FATAL;
2504 goto out;
2505 }
2506 newsrv->check.inter = val;
2507 cur_arg += 2;
2508 }
2509 else if (!strcmp(args[cur_arg], "fastinter")) {
2510 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2511 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002512 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002513 file, linenum, *err, newsrv->id);
2514 err_code |= ERR_ALERT | ERR_FATAL;
2515 goto out;
2516 }
2517 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002518 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002519 file, linenum, val, args[cur_arg], newsrv->id);
2520 err_code |= ERR_ALERT | ERR_FATAL;
2521 goto out;
2522 }
2523 newsrv->check.fastinter = val;
2524 cur_arg += 2;
2525 }
2526 else if (!strcmp(args[cur_arg], "downinter")) {
2527 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2528 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002529 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002530 file, linenum, *err, newsrv->id);
2531 err_code |= ERR_ALERT | ERR_FATAL;
2532 goto out;
2533 }
2534 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002535 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002536 file, linenum, val, args[cur_arg], newsrv->id);
2537 err_code |= ERR_ALERT | ERR_FATAL;
2538 goto out;
2539 }
2540 newsrv->check.downinter = val;
2541 cur_arg += 2;
2542 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002543 else if (!strcmp(args[cur_arg], "port")) {
2544 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002545 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002546 cur_arg += 2;
2547 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002548 else if (!strcmp(args[cur_arg], "weight")) {
2549 int w;
2550 w = atol(args[cur_arg + 1]);
2551 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002552 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002553 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2554 err_code |= ERR_ALERT | ERR_FATAL;
2555 goto out;
2556 }
2557 newsrv->uweight = newsrv->iweight = w;
2558 cur_arg += 2;
2559 }
2560 else if (!strcmp(args[cur_arg], "minconn")) {
2561 newsrv->minconn = atol(args[cur_arg + 1]);
2562 cur_arg += 2;
2563 }
2564 else if (!strcmp(args[cur_arg], "maxconn")) {
2565 newsrv->maxconn = atol(args[cur_arg + 1]);
2566 cur_arg += 2;
2567 }
2568 else if (!strcmp(args[cur_arg], "maxqueue")) {
2569 newsrv->maxqueue = atol(args[cur_arg + 1]);
2570 cur_arg += 2;
2571 }
2572 else if (!strcmp(args[cur_arg], "slowstart")) {
2573 /* slowstart is stored in seconds */
2574 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2575 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002576 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002577 file, linenum, *err, newsrv->id);
2578 err_code |= ERR_ALERT | ERR_FATAL;
2579 goto out;
2580 }
2581 newsrv->slowstart = (val + 999) / 1000;
2582 cur_arg += 2;
2583 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002584 else if (!strcmp(args[cur_arg], "on-error")) {
2585 if (!strcmp(args[cur_arg + 1], "fastinter"))
2586 newsrv->onerror = HANA_ONERR_FASTINTER;
2587 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2588 newsrv->onerror = HANA_ONERR_FAILCHK;
2589 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2590 newsrv->onerror = HANA_ONERR_SUDDTH;
2591 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2592 newsrv->onerror = HANA_ONERR_MARKDWN;
2593 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002594 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002595 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2596 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-down")) {
2604 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2605 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2606 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002607 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-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], "on-marked-up")) {
2616 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2617 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2618 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002619 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002620 file, linenum, args[cur_arg], args[cur_arg + 1]);
2621 err_code |= ERR_ALERT | ERR_FATAL;
2622 goto out;
2623 }
2624
2625 cur_arg += 2;
2626 }
2627 else if (!strcmp(args[cur_arg], "error-limit")) {
2628 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002629 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002630 file, linenum, args[cur_arg]);
2631 err_code |= ERR_ALERT | ERR_FATAL;
2632 goto out;
2633 }
2634
2635 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2636
2637 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002638 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002639 file, linenum, args[cur_arg]);
2640 err_code |= ERR_ALERT | ERR_FATAL;
2641 goto out;
2642 }
2643 cur_arg += 2;
2644 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002645 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002646 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002647 file, linenum, "usesrc", "source");
2648 err_code |= ERR_ALERT | ERR_FATAL;
2649 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002650 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002651 else {
2652 static int srv_dumped;
2653 struct srv_kw *kw;
2654 char *err;
2655
2656 kw = srv_find_kw(args[cur_arg]);
2657 if (kw) {
2658 char *err = NULL;
2659 int code;
2660
2661 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002662 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 +02002663 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002664 if (kw->skip != -1)
2665 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002666 err_code |= ERR_ALERT | ERR_FATAL;
2667 goto out;
2668 }
2669
2670 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002671 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002672 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002673 if (kw->skip != -1)
2674 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002675 err_code |= ERR_ALERT;
2676 continue;
2677 }
2678
2679 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2680 err_code |= code;
2681
2682 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002683 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002684 if (code & ERR_FATAL) {
2685 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002686 if (kw->skip != -1)
2687 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002688 goto out;
2689 }
2690 }
2691 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002692 if (kw->skip != -1)
2693 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002694 continue;
2695 }
2696
2697 err = NULL;
2698 if (!srv_dumped) {
2699 srv_dump_kws(&err);
2700 indent_msg(&err, 4);
2701 srv_dumped = 1;
2702 }
2703
Christopher Faulet767a84b2017-11-24 16:50:31 +01002704 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002705 file, linenum, args[0], args[1], args[cur_arg],
2706 err ? " Registered keywords :" : "", err ? err : "");
2707 free(err);
2708
2709 err_code |= ERR_ALERT | ERR_FATAL;
2710 goto out;
2711 }
2712 }
2713
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002714 if (!defsrv)
2715 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2716 if (err_code & ERR_FATAL)
2717 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002718 if (srv_tmpl)
2719 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002720 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002721 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002722 return 0;
2723
2724 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002725 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002726 free(errmsg);
2727 return err_code;
2728}
2729
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002730/* Returns a pointer to the first server matching either id <id>.
2731 * NULL is returned if no match is found.
2732 * the lookup is performed in the backend <bk>
2733 */
2734struct server *server_find_by_id(struct proxy *bk, int id)
2735{
2736 struct eb32_node *eb32;
2737 struct server *curserver;
2738
2739 if (!bk || (id ==0))
2740 return NULL;
2741
2742 /* <bk> has no backend capabilities, so it can't have a server */
2743 if (!(bk->cap & PR_CAP_BE))
2744 return NULL;
2745
2746 curserver = NULL;
2747
2748 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2749 if (eb32)
2750 curserver = container_of(eb32, struct server, conf.id);
2751
2752 return curserver;
2753}
2754
2755/* Returns a pointer to the first server matching either name <name>, or id
2756 * if <name> starts with a '#'. NULL is returned if no match is found.
2757 * the lookup is performed in the backend <bk>
2758 */
2759struct server *server_find_by_name(struct proxy *bk, const char *name)
2760{
2761 struct server *curserver;
2762
2763 if (!bk || !name)
2764 return NULL;
2765
2766 /* <bk> has no backend capabilities, so it can't have a server */
2767 if (!(bk->cap & PR_CAP_BE))
2768 return NULL;
2769
2770 curserver = NULL;
2771 if (*name == '#') {
2772 curserver = server_find_by_id(bk, atoi(name + 1));
2773 if (curserver)
2774 return curserver;
2775 }
2776 else {
2777 curserver = bk->srv;
2778
2779 while (curserver && (strcmp(curserver->id, name) != 0))
2780 curserver = curserver->next;
2781
2782 if (curserver)
2783 return curserver;
2784 }
2785
2786 return NULL;
2787}
2788
2789struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2790{
2791 struct server *byname;
2792 struct server *byid;
2793
2794 if (!name && !id)
2795 return NULL;
2796
2797 if (diff)
2798 *diff = 0;
2799
2800 byname = byid = NULL;
2801
2802 if (name) {
2803 byname = server_find_by_name(bk, name);
2804 if (byname && (!id || byname->puid == id))
2805 return byname;
2806 }
2807
2808 /* remaining possibilities :
2809 * - name not set
2810 * - name set but not found
2811 * - name found but ID doesn't match
2812 */
2813 if (id) {
2814 byid = server_find_by_id(bk, id);
2815 if (byid) {
2816 if (byname) {
2817 /* use id only if forced by configuration */
2818 if (byid->flags & SRV_F_FORCED_ID) {
2819 if (diff)
2820 *diff |= 2;
2821 return byid;
2822 }
2823 else {
2824 if (diff)
2825 *diff |= 1;
2826 return byname;
2827 }
2828 }
2829
2830 /* remaining possibilities:
2831 * - name not set
2832 * - name set but not found
2833 */
2834 if (name && diff)
2835 *diff |= 2;
2836 return byid;
2837 }
2838
2839 /* id bot found */
2840 if (byname) {
2841 if (diff)
2842 *diff |= 1;
2843 return byname;
2844 }
2845 }
2846
2847 return NULL;
2848}
2849
Willy Tarreau46b7f532018-08-21 11:54:26 +02002850/* Update a server state using the parameters available in the params list.
2851 *
2852 * Grabs the server lock during operation.
2853 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002854static void srv_update_state(struct server *srv, int version, char **params)
2855{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002856 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002857 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002858
2859 /* fields since version 1
2860 * and common to all other upcoming versions
2861 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002862 enum srv_state srv_op_state;
2863 enum srv_admin srv_admin_state;
2864 unsigned srv_uweight, srv_iweight;
2865 unsigned long srv_last_time_change;
2866 short srv_check_status;
2867 enum chk_result srv_check_result;
2868 int srv_check_health;
2869 int srv_check_state, srv_agent_state;
2870 int bk_f_forced_id;
2871 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002872 int fqdn_set_by_cli;
2873 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002874 const char *port_str;
2875 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002876 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002877
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002878 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002879 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002880 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002881 switch (version) {
2882 case 1:
2883 /*
2884 * now we can proceed with server's state update:
2885 * srv_addr: params[0]
2886 * srv_op_state: params[1]
2887 * srv_admin_state: params[2]
2888 * srv_uweight: params[3]
2889 * srv_iweight: params[4]
2890 * srv_last_time_change: params[5]
2891 * srv_check_status: params[6]
2892 * srv_check_result: params[7]
2893 * srv_check_health: params[8]
2894 * srv_check_state: params[9]
2895 * srv_agent_state: params[10]
2896 * bk_f_forced_id: params[11]
2897 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002898 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002899 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002900 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002901 */
2902
2903 /* validating srv_op_state */
2904 p = NULL;
2905 errno = 0;
2906 srv_op_state = strtol(params[1], &p, 10);
2907 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2908 (srv_op_state != SRV_ST_STOPPED &&
2909 srv_op_state != SRV_ST_STARTING &&
2910 srv_op_state != SRV_ST_RUNNING &&
2911 srv_op_state != SRV_ST_STOPPING)) {
2912 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2913 }
2914
2915 /* validating srv_admin_state */
2916 p = NULL;
2917 errno = 0;
2918 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002919 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002920
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002921 /* inherited statuses will be recomputed later.
2922 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2923 */
2924 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002925
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002926 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2927 (srv_admin_state != 0 &&
2928 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002929 srv_admin_state != SRV_ADMF_CMAINT &&
2930 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002931 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002932 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002933 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2934 }
2935
2936 /* validating srv_uweight */
2937 p = NULL;
2938 errno = 0;
2939 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002940 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002941 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2942
2943 /* validating srv_iweight */
2944 p = NULL;
2945 errno = 0;
2946 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002947 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002948 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2949
2950 /* validating srv_last_time_change */
2951 p = NULL;
2952 errno = 0;
2953 srv_last_time_change = strtol(params[5], &p, 10);
2954 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2955 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2956
2957 /* validating srv_check_status */
2958 p = NULL;
2959 errno = 0;
2960 srv_check_status = strtol(params[6], &p, 10);
2961 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2962 (srv_check_status >= HCHK_STATUS_SIZE))
2963 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2964
2965 /* validating srv_check_result */
2966 p = NULL;
2967 errno = 0;
2968 srv_check_result = strtol(params[7], &p, 10);
2969 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2970 (srv_check_result != CHK_RES_UNKNOWN &&
2971 srv_check_result != CHK_RES_NEUTRAL &&
2972 srv_check_result != CHK_RES_FAILED &&
2973 srv_check_result != CHK_RES_PASSED &&
2974 srv_check_result != CHK_RES_CONDPASS)) {
2975 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2976 }
2977
2978 /* validating srv_check_health */
2979 p = NULL;
2980 errno = 0;
2981 srv_check_health = strtol(params[8], &p, 10);
2982 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2983 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2984
2985 /* validating srv_check_state */
2986 p = NULL;
2987 errno = 0;
2988 srv_check_state = strtol(params[9], &p, 10);
2989 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2990 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2991 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2992
2993 /* validating srv_agent_state */
2994 p = NULL;
2995 errno = 0;
2996 srv_agent_state = strtol(params[10], &p, 10);
2997 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2998 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2999 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3000
3001 /* validating bk_f_forced_id */
3002 p = NULL;
3003 errno = 0;
3004 bk_f_forced_id = strtol(params[11], &p, 10);
3005 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3006 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3007
3008 /* validating srv_f_forced_id */
3009 p = NULL;
3010 errno = 0;
3011 srv_f_forced_id = strtol(params[12], &p, 10);
3012 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3013 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3014
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003015 /* validating srv_fqdn */
3016 fqdn = params[13];
3017 if (fqdn && *fqdn == '-')
3018 fqdn = NULL;
3019 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3020 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3021 fqdn = NULL;
3022 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003023
Frédéric Lécaille31694712017-08-01 08:47:19 +02003024 port_str = params[14];
3025 if (port_str) {
3026 port = strl2uic(port_str, strlen(port_str));
3027 if (port > USHRT_MAX) {
3028 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3029 port_str = NULL;
3030 }
3031 }
3032
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003033 /* SRV record
3034 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3035 */
3036 srvrecord = params[15];
3037 if (srvrecord && *srvrecord != '_')
3038 srvrecord = NULL;
3039
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003040 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003041 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003042 goto out;
3043
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003044 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003045 /* recover operational state and apply it to this server
3046 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003047 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003048 switch (srv_op_state) {
3049 case SRV_ST_STOPPED:
3050 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003051 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003052 break;
3053 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003054 /* If rise == 1 there is no STARTING state, let's switch to
3055 * RUNNING
3056 */
3057 if (srv->check.rise == 1) {
3058 srv->check.health = srv->check.rise + srv->check.fall - 1;
3059 srv_set_running(srv, "", NULL);
3060 break;
3061 }
3062 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3063 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003064 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003065 break;
3066 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003067 /* If fall == 1 there is no STOPPING state, let's switch to
3068 * STOPPED
3069 */
3070 if (srv->check.fall == 1) {
3071 srv->check.health = 0;
3072 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3073 break;
3074 }
3075 if (srv->check.health < srv->check.rise ||
3076 srv->check.health > srv->check.rise + srv->check.fall - 2)
3077 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003078 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003079 break;
3080 case SRV_ST_RUNNING:
3081 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003082 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003083 break;
3084 }
3085
3086 /* When applying server state, the following rules apply:
3087 * - in case of a configuration change, we apply the setting from the new
3088 * configuration, regardless of old running state
3089 * - if no configuration change, we apply old running state only if old running
3090 * state is different from new configuration state
3091 */
3092 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003093 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3094 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003095 srv_adm_set_maint(srv);
3096 else
3097 srv_adm_set_ready(srv);
3098 }
3099 /* configuration is the same, let's compate old running state and new conf state */
3100 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003101 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003102 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003103 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003104 srv_adm_set_ready(srv);
3105 }
3106 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003107 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003108 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003109 * (srv->iweight is the weight set up in configuration).
3110 * There are two possible reasons for FDRAIN to have been present :
3111 * - previous config weight was zero
3112 * - "set server b/s drain" was sent to the CLI
3113 *
3114 * In the first case, we simply want to drop this drain state
3115 * if the new weight is not zero anymore, meaning the administrator
3116 * has intentionally turned the weight back to a positive value to
3117 * enable the server again after an operation. In the second case,
3118 * the drain state was forced on the CLI regardless of the config's
3119 * weight so we don't want a change to the config weight to lose this
3120 * status. What this means is :
3121 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3122 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003123 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003124 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003125 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003126 }
3127
3128 srv->last_change = date.tv_sec - srv_last_time_change;
3129 srv->check.status = srv_check_status;
3130 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003131
3132 /* Only case we want to apply is removing ENABLED flag which could have been
3133 * done by the "disable health" command over the stats socket
3134 */
3135 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3136 (srv_check_state & CHK_ST_CONFIGURED) &&
3137 !(srv_check_state & CHK_ST_ENABLED))
3138 srv->check.state &= ~CHK_ST_ENABLED;
3139
3140 /* Only case we want to apply is removing ENABLED flag which could have been
3141 * done by the "disable agent" command over the stats socket
3142 */
3143 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3144 (srv_agent_state & CHK_ST_CONFIGURED) &&
3145 !(srv_agent_state & CHK_ST_ENABLED))
3146 srv->agent.state &= ~CHK_ST_ENABLED;
3147
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003148 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3149 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003150 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003151 * It means that a configuration file change has precedence over a unix socket change
3152 * for server's weight
3153 *
3154 * by default, HAProxy applies the following weight when parsing the configuration
3155 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003156 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003157 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003158 srv->uweight = srv_uweight;
3159 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003160 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003161
Willy Tarreaue5a60682016-11-09 14:54:53 +01003162 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003163 if (strcmp(params[0], "-"))
3164 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003165
3166 if (fqdn && srv->hostname) {
3167 if (!strcmp(srv->hostname, fqdn)) {
3168 /* Here we reset the 'set from stats socket FQDN' flag
3169 * to support such transitions:
3170 * Let's say initial FQDN value is foo1 (in configuration file).
3171 * - FQDN changed from stats socket, from foo1 to foo2 value,
3172 * - FQDN changed again from file configuration (with the same previous value
3173 set from stats socket, from foo1 to foo2 value),
3174 * - reload for any other reason than a FQDN modification,
3175 * the configuration file FQDN matches the fqdn server state file value.
3176 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003177 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003178 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003179 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003180 }
3181 else {
3182 /* If the FDQN has been changed from stats socket,
3183 * apply fqdn state file value (which is the value set
3184 * from stats socket).
3185 */
3186 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003187 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003188 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003189 }
3190 }
3191 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003192 /* If all the conditions below are validated, this means
3193 * we're evaluating a server managed by SRV resolution
3194 */
3195 else if (fqdn && !srv->hostname && srvrecord) {
3196 int res;
3197
3198 /* we can't apply previous state if SRV record has changed */
3199 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3200 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);
3201 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3202 goto out;
3203 }
3204
3205 /* create or find a SRV resolution for this srv record */
3206 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3207 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3208 if (srv->srvrq == NULL) {
3209 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3210 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3211 goto out;
3212 }
3213
3214 /* prepare DNS resolution for this server */
3215 res = srv_prepare_for_resolution(srv, fqdn);
3216 if (res == -1) {
3217 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3218 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3219 goto out;
3220 }
3221
3222 /* configure check.port accordingly */
3223 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3224 !(srv->flags & SRV_F_CHECKPORT))
3225 srv->check.port = port;
3226
3227 /* Unset SRV_F_MAPPORTS for SRV records.
3228 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3229 * because no ports are provided in the configuration file.
3230 * This is because HAProxy will use the port found into the SRV record.
3231 */
3232 srv->flags &= ~SRV_F_MAPPORTS;
3233 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003234
Frédéric Lécaille31694712017-08-01 08:47:19 +02003235 if (port_str)
3236 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003237 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003238
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003239 break;
3240 default:
3241 chunk_appendf(msg, ", version '%d' not supported", version);
3242 }
3243
3244 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003245 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003246 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003247 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003248 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003249 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003250}
3251
3252/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3253 * For each proxy, it does the following:
3254 * - opens its server state file (either one or local one)
3255 * - read whole file, line by line
3256 * - analyse each line to check if it matches our current backend:
3257 * - backend name matches
3258 * - backend id matches if id is forced and name doesn't match
3259 * - if the server pointed by the line is found, then state is applied
3260 *
3261 * If the running backend uuid or id differs from the state file, then HAProxy reports
3262 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003263 *
3264 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003265 */
3266void apply_server_state(void)
3267{
3268 char *cur, *end;
3269 char mybuf[SRV_STATE_LINE_MAXLEN];
3270 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003271 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3272 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003273 int arg, srv_arg, version, diff;
3274 FILE *f;
3275 char *filepath;
3276 char globalfilepath[MAXPATHLEN + 1];
3277 char localfilepath[MAXPATHLEN + 1];
3278 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003279 struct proxy *curproxy, *bk;
3280 struct server *srv;
3281
3282 globalfilepathlen = 0;
3283 /* create the globalfilepath variable */
3284 if (global.server_state_file) {
3285 /* absolute path or no base directory provided */
3286 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3287 len = strlen(global.server_state_file);
3288 if (len > MAXPATHLEN) {
3289 globalfilepathlen = 0;
3290 goto globalfileerror;
3291 }
3292 memcpy(globalfilepath, global.server_state_file, len);
3293 globalfilepath[len] = '\0';
3294 globalfilepathlen = len;
3295 }
3296 else if (global.server_state_base) {
3297 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003298 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003299 globalfilepathlen = 0;
3300 goto globalfileerror;
3301 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003302 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003303 globalfilepath[len] = 0;
3304 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003305
3306 /* append a slash if needed */
3307 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3308 if (globalfilepathlen + 1 > MAXPATHLEN) {
3309 globalfilepathlen = 0;
3310 goto globalfileerror;
3311 }
3312 globalfilepath[globalfilepathlen++] = '/';
3313 }
3314
3315 len = strlen(global.server_state_file);
3316 if (globalfilepathlen + len > MAXPATHLEN) {
3317 globalfilepathlen = 0;
3318 goto globalfileerror;
3319 }
3320 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3321 globalfilepathlen += len;
3322 globalfilepath[globalfilepathlen++] = 0;
3323 }
3324 }
3325 globalfileerror:
3326 if (globalfilepathlen == 0)
3327 globalfilepath[0] = '\0';
3328
3329 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003330 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003331 /* servers are only in backends */
3332 if (!(curproxy->cap & PR_CAP_BE))
3333 continue;
3334 fileopenerr = 0;
3335 filepath = NULL;
3336
3337 /* search server state file path and name */
3338 switch (curproxy->load_server_state_from_file) {
3339 /* read servers state from global file */
3340 case PR_SRV_STATE_FILE_GLOBAL:
3341 /* there was an error while generating global server state file path */
3342 if (globalfilepathlen == 0)
3343 continue;
3344 filepath = globalfilepath;
3345 fileopenerr = 1;
3346 break;
3347 /* this backend has its own file */
3348 case PR_SRV_STATE_FILE_LOCAL:
3349 localfilepathlen = 0;
3350 localfilepath[0] = '\0';
3351 len = 0;
3352 /* create the localfilepath variable */
3353 /* absolute path or no base directory provided */
3354 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3355 len = strlen(curproxy->server_state_file_name);
3356 if (len > MAXPATHLEN) {
3357 localfilepathlen = 0;
3358 goto localfileerror;
3359 }
3360 memcpy(localfilepath, curproxy->server_state_file_name, len);
3361 localfilepath[len] = '\0';
3362 localfilepathlen = len;
3363 }
3364 else if (global.server_state_base) {
3365 len = strlen(global.server_state_base);
3366 localfilepathlen += len;
3367
3368 if (localfilepathlen > MAXPATHLEN) {
3369 localfilepathlen = 0;
3370 goto localfileerror;
3371 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003372 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003373 localfilepath[localfilepathlen] = 0;
3374
3375 /* append a slash if needed */
3376 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3377 if (localfilepathlen + 1 > MAXPATHLEN) {
3378 localfilepathlen = 0;
3379 goto localfileerror;
3380 }
3381 localfilepath[localfilepathlen++] = '/';
3382 }
3383
3384 len = strlen(curproxy->server_state_file_name);
3385 if (localfilepathlen + len > MAXPATHLEN) {
3386 localfilepathlen = 0;
3387 goto localfileerror;
3388 }
3389 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3390 localfilepathlen += len;
3391 localfilepath[localfilepathlen++] = 0;
3392 }
3393 filepath = localfilepath;
3394 localfileerror:
3395 if (localfilepathlen == 0)
3396 localfilepath[0] = '\0';
3397
3398 break;
3399 case PR_SRV_STATE_FILE_NONE:
3400 default:
3401 continue;
3402 }
3403
3404 /* preload global state file */
3405 errno = 0;
3406 f = fopen(filepath, "r");
3407 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003408 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003409 if (!f)
3410 continue;
3411
3412 mybuf[0] = '\0';
3413 mybuflen = 0;
3414 version = 0;
3415
3416 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003417 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003418 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003419 goto fileclose;
3420 }
3421
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003422 cur = mybuf;
3423 version = atoi(cur);
3424 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3425 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003426 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003427
3428 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3429 int bk_f_forced_id = 0;
3430 int check_id = 0;
3431 int check_name = 0;
3432
3433 mybuflen = strlen(mybuf);
3434 cur = mybuf;
3435 end = cur + mybuflen;
3436
3437 bk = NULL;
3438 srv = NULL;
3439
3440 /* we need at least one character */
3441 if (mybuflen == 0)
3442 continue;
3443
3444 /* ignore blank characters at the beginning of the line */
3445 while (isspace(*cur))
3446 ++cur;
3447
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003448 /* Ignore empty or commented lines */
3449 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003450 continue;
3451
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003452 /* truncated lines */
3453 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003454 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003455 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003456 }
3457
3458 /* Removes trailing '\n' */
3459 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003460
3461 /* we're now ready to move the line into *srv_params[] */
3462 params[0] = cur;
3463 arg = 1;
3464 srv_arg = 0;
3465 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3466 if (isspace(*cur)) {
3467 *cur = '\0';
3468 ++cur;
3469 while (isspace(*cur))
3470 ++cur;
3471 switch (version) {
3472 case 1:
3473 /*
3474 * srv_addr: params[4] => srv_params[0]
3475 * srv_op_state: params[5] => srv_params[1]
3476 * srv_admin_state: params[6] => srv_params[2]
3477 * srv_uweight: params[7] => srv_params[3]
3478 * srv_iweight: params[8] => srv_params[4]
3479 * srv_last_time_change: params[9] => srv_params[5]
3480 * srv_check_status: params[10] => srv_params[6]
3481 * srv_check_result: params[11] => srv_params[7]
3482 * srv_check_health: params[12] => srv_params[8]
3483 * srv_check_state: params[13] => srv_params[9]
3484 * srv_agent_state: params[14] => srv_params[10]
3485 * bk_f_forced_id: params[15] => srv_params[11]
3486 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003487 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003488 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003489 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003490 */
3491 if (arg >= 4) {
3492 srv_params[srv_arg] = cur;
3493 ++srv_arg;
3494 }
3495 break;
3496 }
3497
3498 params[arg] = cur;
3499 ++arg;
3500 }
3501 else {
3502 ++cur;
3503 }
3504 }
3505
3506 /* if line is incomplete line, then ignore it.
3507 * otherwise, update useful flags */
3508 switch (version) {
3509 case 1:
3510 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3511 continue;
3512 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3513 check_id = (atoi(params[0]) == curproxy->uuid);
3514 check_name = (strcmp(curproxy->id, params[1]) == 0);
3515 break;
3516 }
3517
3518 diff = 0;
3519 bk = curproxy;
3520
3521 /* if backend can't be found, let's continue */
3522 if (!check_id && !check_name)
3523 continue;
3524 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003525 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 +02003526 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3527 }
3528 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003529 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 +02003530 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3531 /* if name doesn't match, we still want to update curproxy if the backend id
3532 * was forced in previous the previous configuration */
3533 if (!bk_f_forced_id)
3534 continue;
3535 }
3536
3537 /* look for the server by its id: param[2] */
3538 /* else look for the server by its name: param[3] */
3539 diff = 0;
3540 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3541
3542 if (!srv) {
3543 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003544 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3545 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003546 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3547 params[3], params[2], params[0], params[1]);
3548 continue;
3549 }
3550 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003551 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 +02003552 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 +02003553 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003554 }
3555 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003556 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 +02003557 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 +02003558 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003559 }
3560
3561 /* now we can proceed with server's state update */
3562 srv_update_state(srv, version, srv_params);
3563 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003564fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003565 fclose(f);
3566 }
3567}
3568
Simon Horman7d09b9a2013-02-12 10:45:51 +09003569/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003570 * update a server's current IP address.
3571 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3572 * ip is in network format.
3573 * updater is a string which contains an information about the requester of the update.
3574 * updater is used if not NULL.
3575 *
3576 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003577 *
3578 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003579 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003580int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003581{
3582 /* generates a log line and a warning on stderr */
3583 if (1) {
3584 /* book enough space for both IPv4 and IPv6 */
3585 char oldip[INET6_ADDRSTRLEN];
3586 char newip[INET6_ADDRSTRLEN];
3587
3588 memset(oldip, '\0', INET6_ADDRSTRLEN);
3589 memset(newip, '\0', INET6_ADDRSTRLEN);
3590
3591 /* copy old IP address in a string */
3592 switch (s->addr.ss_family) {
3593 case AF_INET:
3594 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3595 break;
3596 case AF_INET6:
3597 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3598 break;
3599 };
3600
3601 /* copy new IP address in a string */
3602 switch (ip_sin_family) {
3603 case AF_INET:
3604 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3605 break;
3606 case AF_INET6:
3607 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3608 break;
3609 };
3610
3611 /* save log line into a buffer */
3612 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3613 s->proxy->id, s->id, oldip, newip, updater);
3614
3615 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003616 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003617
3618 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003619 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003620 }
3621
3622 /* save the new IP family */
3623 s->addr.ss_family = ip_sin_family;
3624 /* save the new IP address */
3625 switch (ip_sin_family) {
3626 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003627 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003628 break;
3629 case AF_INET6:
3630 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3631 break;
3632 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003633 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003634
3635 return 0;
3636}
3637
3638/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003639 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3640 *
3641 * Caller can pass its name through <updater> to get it integrated in the response message
3642 * returned by the function.
3643 *
3644 * The function first does the following, in that order:
3645 * - validates the new addr and/or port
3646 * - checks if an update is required (new IP or port is different than current ones)
3647 * - checks the update is allowed:
3648 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3649 * - allow all changes if no CHECKS are configured
3650 * - if CHECK is configured:
3651 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3652 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3653 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003654 *
3655 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003656 */
3657const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3658{
3659 struct sockaddr_storage sa;
3660 int ret, port_change_required;
3661 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003662 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003663 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003664 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003665
3666 msg = get_trash_chunk();
3667 chunk_reset(msg);
3668
3669 if (addr) {
3670 memset(&sa, 0, sizeof(struct sockaddr_storage));
3671 if (str2ip2(addr, &sa, 0) == NULL) {
3672 chunk_printf(msg, "Invalid addr '%s'", addr);
3673 goto out;
3674 }
3675
3676 /* changes are allowed on AF_INET* families only */
3677 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3678 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3679 goto out;
3680 }
3681
3682 /* collecting data currently setup */
3683 memset(current_addr, '\0', sizeof(current_addr));
3684 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3685 /* changes are allowed on AF_INET* families only */
3686 if ((ret != AF_INET) && (ret != AF_INET6)) {
3687 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3688 goto out;
3689 }
3690
3691 /* applying ADDR changes if required and allowed
3692 * ipcmp returns 0 when both ADDR are the same
3693 */
3694 if (ipcmp(&s->addr, &sa) == 0) {
3695 chunk_appendf(msg, "no need to change the addr");
3696 goto port;
3697 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003698 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003699 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003700
3701 /* we also need to update check's ADDR only if it uses the server's one */
3702 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003703 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003704 }
3705
3706 /* we also need to update agent ADDR only if it use the server's one */
3707 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003708 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003709 }
3710
3711 /* update report for caller */
3712 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3713 }
3714
3715 port:
3716 if (port) {
3717 char sign = '\0';
3718 char *endptr;
3719
3720 if (addr)
3721 chunk_appendf(msg, ", ");
3722
3723 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003724 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003725
3726 /* check if PORT change is required */
3727 port_change_required = 0;
3728
3729 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003730 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003731 new_port = strtol(port, &endptr, 10);
3732 if ((errno != 0) || (port == endptr)) {
3733 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3734 goto out;
3735 }
3736
3737 /* check if caller triggers a port mapped or offset */
3738 if (sign == '-' || (sign == '+')) {
3739 /* check if server currently uses port map */
3740 if (!(s->flags & SRV_F_MAPPORTS)) {
3741 /* switch from fixed port to port map mandatorily triggers
3742 * a port change */
3743 port_change_required = 1;
3744 /* check is configured
3745 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3746 * prevent PORT change if check doesn't have it's dedicated port while switching
3747 * to port mapping */
3748 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3749 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.");
3750 goto out;
3751 }
3752 }
3753 /* we're already using port maps */
3754 else {
3755 port_change_required = current_port != new_port;
3756 }
3757 }
3758 /* fixed port */
3759 else {
3760 port_change_required = current_port != new_port;
3761 }
3762
3763 /* applying PORT changes if required and update response message */
3764 if (port_change_required) {
3765 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003766 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003767 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003768
3769 /* prepare message */
3770 chunk_appendf(msg, "port changed from '");
3771 if (s->flags & SRV_F_MAPPORTS)
3772 chunk_appendf(msg, "+");
3773 chunk_appendf(msg, "%d' to '", current_port);
3774
3775 if (sign == '-') {
3776 s->flags |= SRV_F_MAPPORTS;
3777 chunk_appendf(msg, "%c", sign);
3778 /* just use for result output */
3779 new_port = -new_port;
3780 }
3781 else if (sign == '+') {
3782 s->flags |= SRV_F_MAPPORTS;
3783 chunk_appendf(msg, "%c", sign);
3784 }
3785 else {
3786 s->flags &= ~SRV_F_MAPPORTS;
3787 }
3788
3789 chunk_appendf(msg, "%d'", new_port);
3790
3791 /* we also need to update health checks port only if it uses server's realport */
3792 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3793 s->check.port = new_port;
3794 }
3795 }
3796 else {
3797 chunk_appendf(msg, "no need to change the port");
3798 }
3799 }
3800
3801out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003802 if (changed)
3803 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003804 if (updater)
3805 chunk_appendf(msg, " by '%s'", updater);
3806 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003807 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003808}
3809
3810
3811/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003812 * update server status based on result of name resolution
3813 * returns:
3814 * 0 if server status is updated
3815 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003816 *
3817 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003818 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003819int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003820{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003821 struct dns_resolvers *resolvers = s->resolvers;
3822 struct dns_resolution *resolution = s->dns_requester->resolution;
3823 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003824
3825 switch (resolution->status) {
3826 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003827 /* status when HAProxy has just (re)started.
3828 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003829 break;
3830
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003831 case RSLV_STATUS_VALID:
3832 /*
3833 * resume health checks
3834 * server will be turned back on if health check is safe
3835 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003836 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003837 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003838 return 1;
3839 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3840 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003841 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003842 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003843
Emeric Brun52a91d32017-08-31 14:41:55 +02003844 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003845 return 1;
3846 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3847 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3848 s->proxy->id, s->id);
3849
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003850 ha_warning("%s.\n", trash.area);
3851 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003852 return 0;
3853
3854 case RSLV_STATUS_NX:
3855 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003856 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3857 if (!tick_is_expired(exp, now_ms))
3858 break;
3859
3860 if (s->next_admin & SRV_ADMF_RMAINT)
3861 return 1;
3862 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3863 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003864
3865 case RSLV_STATUS_TIMEOUT:
3866 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003867 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3868 if (!tick_is_expired(exp, now_ms))
3869 break;
3870
3871 if (s->next_admin & SRV_ADMF_RMAINT)
3872 return 1;
3873 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3874 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003875
3876 case RSLV_STATUS_REFUSED:
3877 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003878 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3879 if (!tick_is_expired(exp, now_ms))
3880 break;
3881
3882 if (s->next_admin & SRV_ADMF_RMAINT)
3883 return 1;
3884 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3885 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003886
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003887 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003888 /* stop server if resolution failed for a long enough period */
3889 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3890 if (!tick_is_expired(exp, now_ms))
3891 break;
3892
3893 if (s->next_admin & SRV_ADMF_RMAINT)
3894 return 1;
3895 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3896 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003897 }
3898
3899 return 1;
3900}
3901
3902/*
3903 * Server Name Resolution valid response callback
3904 * It expects:
3905 * - <nameserver>: the name server which answered the valid response
3906 * - <response>: buffer containing a valid DNS response
3907 * - <response_len>: size of <response>
3908 * It performs the following actions:
3909 * - ignore response if current ip found and server family not met
3910 * - update with first new ip found if family is met and current IP is not found
3911 * returns:
3912 * 0 on error
3913 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003914 *
3915 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003916 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003917int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003918{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003919 struct server *s = NULL;
3920 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003921 void *serverip, *firstip;
3922 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003923 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003924 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003925 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003926
Christopher Faulet67957bd2017-09-27 11:00:59 +02003927 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003928 if (!s)
3929 return 1;
3930
Christopher Faulet67957bd2017-09-27 11:00:59 +02003931 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003932
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003933 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003934 firstip = NULL; /* pointer to the first valid response found */
3935 /* it will be used as the new IP if a change is required */
3936 firstip_sin_family = AF_UNSPEC;
3937 serverip = NULL; /* current server IP address */
3938
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003939 /* initializing server IP pointer */
3940 server_sin_family = s->addr.ss_family;
3941 switch (server_sin_family) {
3942 case AF_INET:
3943 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3944 break;
3945
3946 case AF_INET6:
3947 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3948 break;
3949
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003950 case AF_UNSPEC:
3951 break;
3952
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003953 default:
3954 goto invalid;
3955 }
3956
Baptiste Assmann729c9012017-05-22 15:13:10 +02003957 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003958 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003959 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003960
3961 switch (ret) {
3962 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003963 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003964
3965 case DNS_UPD_SRVIP_NOT_FOUND:
3966 goto save_ip;
3967
3968 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003969 goto invalid;
3970
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003971 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003972 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003973 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003974
Baptiste Assmannfad03182015-10-28 02:03:32 +01003975 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003976 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003977 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003978 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003979
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003980 default:
3981 goto invalid;
3982
3983 }
3984
3985 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003986 if (nameserver) {
3987 nameserver->counters.update++;
3988 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003989 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003990 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003991 else
3992 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003993 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003994
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003995 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003996 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003997 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003998
3999 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004000 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02004001 nameserver->counters.invalid++;
4002 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004003 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004004 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004005 return 0;
4006}
4007
4008/*
4009 * Server Name Resolution error management callback
4010 * returns:
4011 * 0 on error
4012 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004013 *
4014 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004015 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004016int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004017{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004018 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004019
Christopher Faulet67957bd2017-09-27 11:00:59 +02004020 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004021 if (!s)
4022 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004023 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004024 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004025 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004026 return 1;
4027}
4028
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004029/*
4030 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004031 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004032 * It returns a pointer to the first server found or NULL if <ip> is not yet
4033 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004034 *
4035 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004036 */
4037struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4038{
4039 struct server *tmpsrv;
4040 struct proxy *be;
4041
4042 if (!srv)
4043 return NULL;
4044
4045 be = srv->proxy;
4046 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004047 /* we found the current server is the same, ignore it */
4048 if (srv == tmpsrv)
4049 continue;
4050
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004051 /* We want to compare the IP in the record with the IP of the servers in the
4052 * same backend, only if:
4053 * * DNS resolution is enabled on the server
4054 * * the hostname used for the resolution by our server is the same than the
4055 * one used for the server found in the backend
4056 * * the server found in the backend is not our current server
4057 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004058 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004059 if ((tmpsrv->hostname_dn == NULL) ||
4060 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4061 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004062 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004063 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004064 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004065 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004066
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004067 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004068 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004069 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004070 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004071 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004072
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004073 /* At this point, we have 2 different servers using the same DNS hostname
4074 * for their respective resolution.
4075 */
4076 if (*ip_family == tmpsrv->addr.ss_family &&
4077 ((tmpsrv->addr.ss_family == AF_INET &&
4078 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4079 (tmpsrv->addr.ss_family == AF_INET6 &&
4080 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004081 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004082 return tmpsrv;
4083 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004084 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004085 }
4086
Emeric Brune9fd6b52017-11-02 17:20:39 +01004087
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004088 return NULL;
4089}
4090
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004091/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4092 * resolver. This is suited for initial address configuration. Returns 0 on
4093 * success otherwise a non-zero error code. In case of error, *err_code, if
4094 * not NULL, is filled up.
4095 */
4096int srv_set_addr_via_libc(struct server *srv, int *err_code)
4097{
4098 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004099 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004100 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004101 return 1;
4102 }
4103 return 0;
4104}
4105
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004106/* Set the server's FDQN (->hostname) from <hostname>.
4107 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004108 *
4109 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004110 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004111int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004112{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004113 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004114 char *hostname_dn;
4115 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004116
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004117 /* Note that the server lock is already held. */
4118 if (!srv->resolvers)
4119 return -1;
4120
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004121 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004122 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004123 /* run time DNS resolution was not active for this server
4124 * and we can't enable it at run time for now.
4125 */
4126 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004127 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004128
4129 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004130 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004131 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004132 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4133 hostname_dn, trash.size);
4134 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004135 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004136
Christopher Faulet67957bd2017-09-27 11:00:59 +02004137 resolution = srv->dns_requester->resolution;
4138 if (resolution &&
4139 resolution->hostname_dn &&
4140 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004141 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004142
Christopher Faulet67957bd2017-09-27 11:00:59 +02004143 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004144
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004145 free(srv->hostname);
4146 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004147 srv->hostname = strdup(hostname);
4148 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004149 srv->hostname_dn_len = hostname_dn_len;
4150 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004151 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004152
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004153 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004154 goto err;
4155
4156 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004157 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004158 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004159 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004160
4161 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004162 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004163 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004164 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004165}
4166
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004167/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4168 * from the state file. This is suited for initial address configuration.
4169 * Returns 0 on success otherwise a non-zero error code. In case of error,
4170 * *err_code, if not NULL, is filled up.
4171 */
4172static int srv_apply_lastaddr(struct server *srv, int *err_code)
4173{
4174 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4175 if (err_code)
4176 *err_code |= ERR_WARN;
4177 return 1;
4178 }
4179 return 0;
4180}
4181
Willy Tarreau25e51522016-11-04 15:10:17 +01004182/* returns 0 if no error, otherwise a combination of ERR_* flags */
4183static int srv_iterate_initaddr(struct server *srv)
4184{
4185 int return_code = 0;
4186 int err_code;
4187 unsigned int methods;
4188
4189 methods = srv->init_addr_methods;
4190 if (!methods) { // default to "last,libc"
4191 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4192 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4193 }
4194
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004195 /* "-dr" : always append "none" so that server addresses resolution
4196 * failures are silently ignored, this is convenient to validate some
4197 * configs out of their environment.
4198 */
4199 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4200 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4201
Willy Tarreau25e51522016-11-04 15:10:17 +01004202 while (methods) {
4203 err_code = 0;
4204 switch (srv_get_next_initaddr(&methods)) {
4205 case SRV_IADDR_LAST:
4206 if (!srv->lastaddr)
4207 continue;
4208 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004209 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004210 return_code |= err_code;
4211 break;
4212
4213 case SRV_IADDR_LIBC:
4214 if (!srv->hostname)
4215 continue;
4216 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004217 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004218 return_code |= err_code;
4219 break;
4220
Willy Tarreau37ebe122016-11-04 15:17:58 +01004221 case SRV_IADDR_NONE:
4222 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004223 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004224 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4225 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004226 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004227 return return_code;
4228
Willy Tarreau4310d362016-11-02 15:05:56 +01004229 case SRV_IADDR_IP:
4230 ipcpy(&srv->init_addr, &srv->addr);
4231 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004232 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4233 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004234 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004235 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004236
Willy Tarreau25e51522016-11-04 15:10:17 +01004237 default: /* unhandled method */
4238 break;
4239 }
4240 }
4241
4242 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004243 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004244 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4245 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004246 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004247 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004248 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4249 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004250
4251 return_code |= ERR_ALERT | ERR_FATAL;
4252 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004253out:
4254 srv_set_dyncookie(srv);
4255 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004256}
4257
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004258/*
4259 * This function parses all backends and all servers within each backend
4260 * and performs servers' addr resolution based on information provided by:
4261 * - configuration file
4262 * - server-state file (states provided by an 'old' haproxy process)
4263 *
4264 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4265 */
4266int srv_init_addr(void)
4267{
4268 struct proxy *curproxy;
4269 int return_code = 0;
4270
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004271 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004272 while (curproxy) {
4273 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004274
4275 /* servers are in backend only */
4276 if (!(curproxy->cap & PR_CAP_BE))
4277 goto srv_init_addr_next;
4278
Willy Tarreau25e51522016-11-04 15:10:17 +01004279 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004280 if (srv->hostname)
4281 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004282
4283 srv_init_addr_next:
4284 curproxy = curproxy->next;
4285 }
4286
4287 return return_code;
4288}
4289
Willy Tarreau46b7f532018-08-21 11:54:26 +02004290/*
4291 * Must be called with the server lock held.
4292 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004293const 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 +02004294{
4295
Willy Tarreau83061a82018-07-13 11:56:34 +02004296 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004297
4298 msg = get_trash_chunk();
4299 chunk_reset(msg);
4300
Olivier Houchard8da5f982017-08-04 18:35:36 +02004301 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004302 chunk_appendf(msg, "no need to change the FDQN");
4303 goto out;
4304 }
4305
4306 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4307 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4308 goto out;
4309 }
4310
4311 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4312 server->proxy->id, server->id, server->hostname, fqdn);
4313
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004314 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004315 chunk_reset(msg);
4316 chunk_appendf(msg, "could not update %s/%s FQDN",
4317 server->proxy->id, server->id);
4318 goto out;
4319 }
4320
4321 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004322 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004323
4324 out:
4325 if (updater)
4326 chunk_appendf(msg, " by '%s'", updater);
4327 chunk_appendf(msg, "\n");
4328
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004329 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004330}
4331
4332
Willy Tarreau21b069d2016-11-23 17:15:08 +01004333/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4334 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004335 * 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 +01004336 * used for CLI commands requiring a server name.
4337 * Important: the <arg> is modified to remove the '/'.
4338 */
4339struct server *cli_find_server(struct appctx *appctx, char *arg)
4340{
4341 struct proxy *px;
4342 struct server *sv;
4343 char *line;
4344
4345 /* split "backend/server" and make <line> point to server */
4346 for (line = arg; *line; line++)
4347 if (*line == '/') {
4348 *line++ = '\0';
4349 break;
4350 }
4351
4352 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004353 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004354 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004355 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004356 return NULL;
4357 }
4358
4359 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004360 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004361 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004362 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004363 return NULL;
4364 }
4365
4366 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004367 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004368 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004369 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004370 return NULL;
4371 }
4372
4373 return sv;
4374}
4375
William Lallemand222baf22016-11-19 02:00:33 +01004376
Willy Tarreau46b7f532018-08-21 11:54:26 +02004377/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004378static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004379{
4380 struct server *sv;
4381 const char *warning;
4382
4383 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4384 return 1;
4385
4386 sv = cli_find_server(appctx, args[2]);
4387 if (!sv)
4388 return 1;
4389
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004390 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004391
William Lallemand222baf22016-11-19 02:00:33 +01004392 if (strcmp(args[3], "weight") == 0) {
4393 warning = server_parse_weight_change_request(sv, args[4]);
4394 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004395 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004396 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004397 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004398 }
4399 }
4400 else if (strcmp(args[3], "state") == 0) {
4401 if (strcmp(args[4], "ready") == 0)
4402 srv_adm_set_ready(sv);
4403 else if (strcmp(args[4], "drain") == 0)
4404 srv_adm_set_drain(sv);
4405 else if (strcmp(args[4], "maint") == 0)
4406 srv_adm_set_maint(sv);
4407 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004408 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004409 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004410 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004411 }
4412 }
4413 else if (strcmp(args[3], "health") == 0) {
4414 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004415 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004416 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004417 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004418 }
4419 else if (strcmp(args[4], "up") == 0) {
4420 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004421 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004422 }
4423 else if (strcmp(args[4], "stopping") == 0) {
4424 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004425 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004426 }
4427 else if (strcmp(args[4], "down") == 0) {
4428 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004429 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004430 }
4431 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004432 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004433 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004434 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004435 }
4436 }
4437 else if (strcmp(args[3], "agent") == 0) {
4438 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004439 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004440 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004441 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004442 }
4443 else if (strcmp(args[4], "up") == 0) {
4444 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004445 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004446 }
4447 else if (strcmp(args[4], "down") == 0) {
4448 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004449 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004450 }
4451 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004452 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004453 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004454 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004455 }
4456 }
Misiek2da082d2017-01-09 09:40:42 +01004457 else if (strcmp(args[3], "agent-addr") == 0) {
4458 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004459 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004460 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4461 appctx->st0 = CLI_ST_PRINT;
4462 } else {
4463 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004464 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004465 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4466 appctx->st0 = CLI_ST_PRINT;
4467 }
4468 }
4469 }
4470 else if (strcmp(args[3], "agent-send") == 0) {
4471 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004472 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004473 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4474 appctx->st0 = CLI_ST_PRINT;
4475 } else {
4476 char *nss = strdup(args[4]);
4477 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004478 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004479 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4480 appctx->st0 = CLI_ST_PRINT;
4481 } else {
4482 free(sv->agent.send_string);
4483 sv->agent.send_string = nss;
4484 sv->agent.send_string_len = strlen(args[4]);
4485 }
4486 }
4487 }
William Lallemand222baf22016-11-19 02:00:33 +01004488 else if (strcmp(args[3], "check-port") == 0) {
4489 int i = 0;
4490 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004491 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004492 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004493 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004494 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004495 }
4496 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004497 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004498 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004499 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004500 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004501 }
4502 /* prevent the update of port to 0 if MAPPORTS are in use */
4503 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004504 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004505 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004506 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004507 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004508 }
4509 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004510 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004511 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004512 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004513 }
4514 else if (strcmp(args[3], "addr") == 0) {
4515 char *addr = NULL;
4516 char *port = NULL;
4517 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004518 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004519 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004520 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004521 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004522 }
4523 else {
4524 addr = args[4];
4525 }
4526 if (strcmp(args[5], "port") == 0) {
4527 port = args[6];
4528 }
4529 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4530 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004531 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004532 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004533 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004534 }
4535 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4536 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004537 else if (strcmp(args[3], "fqdn") == 0) {
4538 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004539 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004540 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4541 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004542 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004543 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004544 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004545 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004546 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004547 appctx->ctx.cli.msg = warning;
4548 appctx->st0 = CLI_ST_PRINT;
4549 }
4550 }
William Lallemand222baf22016-11-19 02:00:33 +01004551 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004552 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004553 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 +01004554 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004555 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004556 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004557 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004558 return 1;
4559}
4560
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004561static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004562{
4563 struct stream_interface *si = appctx->owner;
4564 struct proxy *px;
4565 struct server *sv;
4566 char *line;
4567
4568
4569 /* split "backend/server" and make <line> point to server */
4570 for (line = args[2]; *line; line++)
4571 if (*line == '/') {
4572 *line++ = '\0';
4573 break;
4574 }
4575
4576 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004577 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004578 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004579 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004580 return 1;
4581 }
4582
4583 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004584 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004585 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004586 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004587 return 1;
4588 }
4589
4590 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004591 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4592 sv->iweight);
4593 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004594 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004595 return 0;
4596 }
William Lallemand6b160942016-11-22 12:34:35 +01004597 return 1;
4598}
4599
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004600/* Parse a "set weight" command.
4601 *
4602 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004603 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004604static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004605{
4606 struct server *sv;
4607 const char *warning;
4608
4609 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4610 return 1;
4611
4612 sv = cli_find_server(appctx, args[2]);
4613 if (!sv)
4614 return 1;
4615
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004616 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4617
William Lallemand6b160942016-11-22 12:34:35 +01004618 warning = server_parse_weight_change_request(sv, args[3]);
4619 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004620 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004621 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004622 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004623 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004624
4625 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4626
William Lallemand6b160942016-11-22 12:34:35 +01004627 return 1;
4628}
4629
Willy Tarreau46b7f532018-08-21 11:54:26 +02004630/* parse a "set maxconn server" command. It always returns 1.
4631 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004632 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004633 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004634static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004635{
4636 struct server *sv;
4637 const char *warning;
4638
4639 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4640 return 1;
4641
4642 sv = cli_find_server(appctx, args[3]);
4643 if (!sv)
4644 return 1;
4645
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004646 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4647
Willy Tarreaub8026272016-11-23 11:26:56 +01004648 warning = server_parse_maxconn_change_request(sv, args[4]);
4649 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004650 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004651 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004652 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004653 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004654
4655 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4656
Willy Tarreaub8026272016-11-23 11:26:56 +01004657 return 1;
4658}
William Lallemand6b160942016-11-22 12:34:35 +01004659
Willy Tarreau46b7f532018-08-21 11:54:26 +02004660/* parse a "disable agent" command. It always returns 1.
4661 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004662 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004663 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004664static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004665{
4666 struct server *sv;
4667
4668 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4669 return 1;
4670
4671 sv = cli_find_server(appctx, args[2]);
4672 if (!sv)
4673 return 1;
4674
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004675 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004676 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004677 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004678 return 1;
4679}
4680
Willy Tarreau46b7f532018-08-21 11:54:26 +02004681/* parse a "disable health" command. It always returns 1.
4682 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004683 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004684 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004685static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004686{
4687 struct server *sv;
4688
4689 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4690 return 1;
4691
4692 sv = cli_find_server(appctx, args[2]);
4693 if (!sv)
4694 return 1;
4695
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004696 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004697 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004698 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004699 return 1;
4700}
4701
Willy Tarreau46b7f532018-08-21 11:54:26 +02004702/* parse a "disable server" command. It always returns 1.
4703 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004704 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004705 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004706static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004707{
4708 struct server *sv;
4709
4710 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4711 return 1;
4712
4713 sv = cli_find_server(appctx, args[2]);
4714 if (!sv)
4715 return 1;
4716
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004717 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004718 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004719 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004720 return 1;
4721}
4722
Willy Tarreau46b7f532018-08-21 11:54:26 +02004723/* parse a "enable agent" command. It always returns 1.
4724 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004725 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004726 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004727static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004728{
4729 struct server *sv;
4730
4731 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4732 return 1;
4733
4734 sv = cli_find_server(appctx, args[2]);
4735 if (!sv)
4736 return 1;
4737
4738 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004739 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004740 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004741 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004742 return 1;
4743 }
4744
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004745 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004746 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004747 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004748 return 1;
4749}
4750
Willy Tarreau46b7f532018-08-21 11:54:26 +02004751/* parse a "enable health" command. It always returns 1.
4752 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004753 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004754 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004755static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004756{
4757 struct server *sv;
4758
4759 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4760 return 1;
4761
4762 sv = cli_find_server(appctx, args[2]);
4763 if (!sv)
4764 return 1;
4765
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004766 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004767 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004768 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004769 return 1;
4770}
4771
Willy Tarreau46b7f532018-08-21 11:54:26 +02004772/* parse a "enable server" command. It always returns 1.
4773 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004774 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004775 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004776static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004777{
4778 struct server *sv;
4779
4780 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4781 return 1;
4782
4783 sv = cli_find_server(appctx, args[2]);
4784 if (!sv)
4785 return 1;
4786
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004787 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004788 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004789 if (!(sv->flags & SRV_F_COOKIESET)
4790 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4791 sv->cookie)
4792 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004793 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004794 return 1;
4795}
4796
William Lallemand222baf22016-11-19 02:00:33 +01004797/* register cli keywords */
4798static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004799 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004800 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004801 { { "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 +01004802 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004803 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004804 { { "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 +01004805 { { "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 +01004806 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004807 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4808 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4809
William Lallemand222baf22016-11-19 02:00:33 +01004810 {{},}
4811}};
4812
Willy Tarreau0108d902018-11-25 19:14:37 +01004813INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004814
Emeric Brun64cc49c2017-10-03 14:46:45 +02004815/*
4816 * This function applies server's status changes, it is
4817 * is designed to be called asynchronously.
4818 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004819 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004820 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004821static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004822{
4823 struct check *check = &s->check;
4824 int xferred;
4825 struct proxy *px = s->proxy;
4826 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4827 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4828 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004829 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004830
Emeric Brun64cc49c2017-10-03 14:46:45 +02004831 /* If currently main is not set we try to apply pending state changes */
4832 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4833 int next_admin;
4834
4835 /* Backup next admin */
4836 next_admin = s->next_admin;
4837
4838 /* restore current admin state */
4839 s->next_admin = s->cur_admin;
4840
4841 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4842 s->last_change = now.tv_sec;
4843 if (s->proxy->lbprm.set_server_status_down)
4844 s->proxy->lbprm.set_server_status_down(s);
4845
4846 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4847 srv_shutdown_streams(s, SF_ERR_DOWN);
4848
4849 /* we might have streams queued on this server and waiting for
4850 * a connection. Those which are redispatchable will be queued
4851 * to another server or to the proxy itself.
4852 */
4853 xferred = pendconn_redistribute(s);
4854
4855 tmptrash = alloc_trash_chunk();
4856 if (tmptrash) {
4857 chunk_printf(tmptrash,
4858 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4859 s->proxy->id, s->id);
4860
Emeric Brun5a133512017-10-19 14:42:30 +02004861 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004862 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004863
4864 /* we don't send an alert if the server was previously paused */
4865 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004866 send_log(s->proxy, log_level, "%s.\n",
4867 tmptrash->area);
4868 send_email_alert(s, log_level, "%s",
4869 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004870 free_trash_chunk(tmptrash);
4871 tmptrash = NULL;
4872 }
4873 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4874 set_backend_down(s->proxy);
4875
4876 s->counters.down_trans++;
4877 }
4878 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4879 s->last_change = now.tv_sec;
4880 if (s->proxy->lbprm.set_server_status_down)
4881 s->proxy->lbprm.set_server_status_down(s);
4882
4883 /* we might have streams queued on this server and waiting for
4884 * a connection. Those which are redispatchable will be queued
4885 * to another server or to the proxy itself.
4886 */
4887 xferred = pendconn_redistribute(s);
4888
4889 tmptrash = alloc_trash_chunk();
4890 if (tmptrash) {
4891 chunk_printf(tmptrash,
4892 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4893 s->proxy->id, s->id);
4894
Emeric Brun5a133512017-10-19 14:42:30 +02004895 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004896
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004897 ha_warning("%s.\n", tmptrash->area);
4898 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4899 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004900 free_trash_chunk(tmptrash);
4901 tmptrash = NULL;
4902 }
4903
4904 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4905 set_backend_down(s->proxy);
4906 }
4907 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4908 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4909 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4910 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4911 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4912 s->proxy->last_change = now.tv_sec;
4913 }
4914
4915 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4916 s->down_time += now.tv_sec - s->last_change;
4917
4918 s->last_change = now.tv_sec;
4919 if (s->next_state == SRV_ST_STARTING)
4920 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4921
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004922 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004923 /* now propagate the status change to any LB algorithms */
4924 if (px->lbprm.update_server_eweight)
4925 px->lbprm.update_server_eweight(s);
4926 else if (srv_willbe_usable(s)) {
4927 if (px->lbprm.set_server_status_up)
4928 px->lbprm.set_server_status_up(s);
4929 }
4930 else {
4931 if (px->lbprm.set_server_status_down)
4932 px->lbprm.set_server_status_down(s);
4933 }
4934
4935 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4936 * and it's not a backup server and its effective weight is > 0,
4937 * then it can accept new connections, so we shut down all streams
4938 * on all backup servers.
4939 */
4940 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4941 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4942 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4943
4944 /* check if we can handle some connections queued at the proxy. We
4945 * will take as many as we can handle.
4946 */
4947 xferred = pendconn_grab_from_px(s);
4948
4949 tmptrash = alloc_trash_chunk();
4950 if (tmptrash) {
4951 chunk_printf(tmptrash,
4952 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4953 s->proxy->id, s->id);
4954
Emeric Brun5a133512017-10-19 14:42:30 +02004955 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004956 ha_warning("%s.\n", tmptrash->area);
4957 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4958 tmptrash->area);
4959 send_email_alert(s, LOG_NOTICE, "%s",
4960 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004961 free_trash_chunk(tmptrash);
4962 tmptrash = NULL;
4963 }
4964
4965 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4966 set_backend_down(s->proxy);
4967 }
4968 else if (s->cur_eweight != s->next_eweight) {
4969 /* now propagate the status change to any LB algorithms */
4970 if (px->lbprm.update_server_eweight)
4971 px->lbprm.update_server_eweight(s);
4972 else if (srv_willbe_usable(s)) {
4973 if (px->lbprm.set_server_status_up)
4974 px->lbprm.set_server_status_up(s);
4975 }
4976 else {
4977 if (px->lbprm.set_server_status_down)
4978 px->lbprm.set_server_status_down(s);
4979 }
4980
4981 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4982 set_backend_down(s->proxy);
4983 }
4984
4985 s->next_admin = next_admin;
4986 }
4987
Emeric Brun5a133512017-10-19 14:42:30 +02004988 /* reset operational state change */
4989 *s->op_st_chg.reason = 0;
4990 s->op_st_chg.status = s->op_st_chg.code = -1;
4991 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004992
4993 /* Now we try to apply pending admin changes */
4994
4995 /* Maintenance must also disable health checks */
4996 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4997 if (s->check.state & CHK_ST_ENABLED) {
4998 s->check.state |= CHK_ST_PAUSED;
4999 check->health = 0;
5000 }
5001
5002 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005003 tmptrash = alloc_trash_chunk();
5004 if (tmptrash) {
5005 chunk_printf(tmptrash,
5006 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5007 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5008 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005009
Olivier Houchard796a2b32017-10-24 17:42:47 +02005010 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005011
Olivier Houchard796a2b32017-10-24 17:42:47 +02005012 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005013 ha_warning("%s.\n", tmptrash->area);
5014 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5015 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005016 }
5017 free_trash_chunk(tmptrash);
5018 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005019 }
Emeric Brun8f298292017-12-06 16:47:17 +01005020 /* commit new admin status */
5021
5022 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005023 }
5024 else { /* server was still running */
5025 check->health = 0; /* failure */
5026 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005027
5028 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005029 if (s->proxy->lbprm.set_server_status_down)
5030 s->proxy->lbprm.set_server_status_down(s);
5031
Emeric Brun64cc49c2017-10-03 14:46:45 +02005032 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5033 srv_shutdown_streams(s, SF_ERR_DOWN);
5034
5035 /* we might have streams queued on this server and waiting for
5036 * a connection. Those which are redispatchable will be queued
5037 * to another server or to the proxy itself.
5038 */
5039 xferred = pendconn_redistribute(s);
5040
5041 tmptrash = alloc_trash_chunk();
5042 if (tmptrash) {
5043 chunk_printf(tmptrash,
5044 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5045 s->flags & SRV_F_BACKUP ? "Backup " : "",
5046 s->proxy->id, s->id,
5047 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5048
5049 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5050
5051 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005052 ha_warning("%s.\n", tmptrash->area);
5053 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5054 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005055 }
5056 free_trash_chunk(tmptrash);
5057 tmptrash = NULL;
5058 }
5059 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5060 set_backend_down(s->proxy);
5061
5062 s->counters.down_trans++;
5063 }
5064 }
5065 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5066 /* OK here we're leaving maintenance, we have many things to check,
5067 * because the server might possibly be coming back up depending on
5068 * its state. In practice, leaving maintenance means that we should
5069 * immediately turn to UP (more or less the slowstart) under the
5070 * following conditions :
5071 * - server is neither checked nor tracked
5072 * - server tracks another server which is not checked
5073 * - server tracks another server which is already up
5074 * Which sums up as something simpler :
5075 * "either the tracking server is up or the server's checks are disabled
5076 * or up". Otherwise we only re-enable health checks. There's a special
5077 * case associated to the stopping state which can be inherited. Note
5078 * that the server might still be in drain mode, which is naturally dealt
5079 * with by the lower level functions.
5080 */
5081
5082 if (s->check.state & CHK_ST_ENABLED) {
5083 s->check.state &= ~CHK_ST_PAUSED;
5084 check->health = check->rise; /* start OK but check immediately */
5085 }
5086
5087 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5088 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5089 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5090 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5091 s->next_state = SRV_ST_STOPPING;
5092 }
5093 else {
5094 s->next_state = SRV_ST_STARTING;
5095 if (s->slowstart > 0)
5096 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5097 else
5098 s->next_state = SRV_ST_RUNNING;
5099 }
5100
5101 }
5102
5103 tmptrash = alloc_trash_chunk();
5104 if (tmptrash) {
5105 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5106 chunk_printf(tmptrash,
5107 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5108 s->flags & SRV_F_BACKUP ? "Backup " : "",
5109 s->proxy->id, s->id,
5110 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5111 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5112 }
5113 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5114 chunk_printf(tmptrash,
5115 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5116 s->flags & SRV_F_BACKUP ? "Backup " : "",
5117 s->proxy->id, s->id, s->hostname,
5118 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5119 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5120 }
5121 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5122 chunk_printf(tmptrash,
5123 "%sServer %s/%s is %s/%s (leaving maintenance)",
5124 s->flags & SRV_F_BACKUP ? "Backup " : "",
5125 s->proxy->id, s->id,
5126 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5127 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5128 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005129 ha_warning("%s.\n", tmptrash->area);
5130 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5131 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005132 free_trash_chunk(tmptrash);
5133 tmptrash = NULL;
5134 }
5135
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005136 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005137 /* now propagate the status change to any LB algorithms */
5138 if (px->lbprm.update_server_eweight)
5139 px->lbprm.update_server_eweight(s);
5140 else if (srv_willbe_usable(s)) {
5141 if (px->lbprm.set_server_status_up)
5142 px->lbprm.set_server_status_up(s);
5143 }
5144 else {
5145 if (px->lbprm.set_server_status_down)
5146 px->lbprm.set_server_status_down(s);
5147 }
5148
5149 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5150 set_backend_down(s->proxy);
5151
Willy Tarreau6a78e612018-08-07 10:14:53 +02005152 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5153 * and it's not a backup server and its effective weight is > 0,
5154 * then it can accept new connections, so we shut down all streams
5155 * on all backup servers.
5156 */
5157 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5158 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5159 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5160
5161 /* check if we can handle some connections queued at the proxy. We
5162 * will take as many as we can handle.
5163 */
5164 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005165 }
5166 else if (s->next_admin & SRV_ADMF_MAINT) {
5167 /* remaining in maintenance mode, let's inform precisely about the
5168 * situation.
5169 */
5170 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5171 tmptrash = alloc_trash_chunk();
5172 if (tmptrash) {
5173 chunk_printf(tmptrash,
5174 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5175 s->flags & SRV_F_BACKUP ? "Backup " : "",
5176 s->proxy->id, s->id);
5177
5178 if (s->track) /* normally it's mandatory here */
5179 chunk_appendf(tmptrash, " via %s/%s",
5180 s->track->proxy->id, s->track->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 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5189 tmptrash = alloc_trash_chunk();
5190 if (tmptrash) {
5191 chunk_printf(tmptrash,
5192 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5193 s->flags & SRV_F_BACKUP ? "Backup " : "",
5194 s->proxy->id, s->id, s->hostname);
5195
5196 if (s->track) /* normally it's mandatory here */
5197 chunk_appendf(tmptrash, " via %s/%s",
5198 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005199 ha_warning("%s.\n", tmptrash->area);
5200 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5201 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005202 free_trash_chunk(tmptrash);
5203 tmptrash = NULL;
5204 }
5205 }
5206 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5207 tmptrash = alloc_trash_chunk();
5208 if (tmptrash) {
5209 chunk_printf(tmptrash,
5210 "%sServer %s/%s remains in forced maintenance",
5211 s->flags & SRV_F_BACKUP ? "Backup " : "",
5212 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005213 ha_warning("%s.\n", tmptrash->area);
5214 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5215 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005216 free_trash_chunk(tmptrash);
5217 tmptrash = NULL;
5218 }
5219 }
5220 /* don't report anything when leaving drain mode and remaining in maintenance */
5221
5222 s->cur_admin = s->next_admin;
5223 }
5224
5225 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5226 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5227 /* drain state is applied only if not yet in maint */
5228
5229 s->last_change = now.tv_sec;
5230 if (px->lbprm.set_server_status_down)
5231 px->lbprm.set_server_status_down(s);
5232
5233 /* we might have streams queued on this server and waiting for
5234 * a connection. Those which are redispatchable will be queued
5235 * to another server or to the proxy itself.
5236 */
5237 xferred = pendconn_redistribute(s);
5238
5239 tmptrash = alloc_trash_chunk();
5240 if (tmptrash) {
5241 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5242 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5243 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5244
5245 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5246
5247 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005248 ha_warning("%s.\n", tmptrash->area);
5249 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5250 tmptrash->area);
5251 send_email_alert(s, LOG_NOTICE, "%s",
5252 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005253 }
5254 free_trash_chunk(tmptrash);
5255 tmptrash = NULL;
5256 }
5257
5258 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5259 set_backend_down(s->proxy);
5260 }
5261 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5262 /* OK completely leaving drain mode */
5263 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5264 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5265 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5266 s->proxy->last_change = now.tv_sec;
5267 }
5268
5269 if (s->last_change < now.tv_sec) // ignore negative times
5270 s->down_time += now.tv_sec - s->last_change;
5271 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005272 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005273
5274 tmptrash = alloc_trash_chunk();
5275 if (tmptrash) {
5276 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5277 chunk_printf(tmptrash,
5278 "%sServer %s/%s is %s (leaving forced drain)",
5279 s->flags & SRV_F_BACKUP ? "Backup " : "",
5280 s->proxy->id, s->id,
5281 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5282 }
5283 else {
5284 chunk_printf(tmptrash,
5285 "%sServer %s/%s is %s (leaving drain)",
5286 s->flags & SRV_F_BACKUP ? "Backup " : "",
5287 s->proxy->id, s->id,
5288 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5289 if (s->track) /* normally it's mandatory here */
5290 chunk_appendf(tmptrash, " via %s/%s",
5291 s->track->proxy->id, s->track->id);
5292 }
5293
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005294 ha_warning("%s.\n", tmptrash->area);
5295 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5296 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005297 free_trash_chunk(tmptrash);
5298 tmptrash = NULL;
5299 }
5300
5301 /* now propagate the status change to any LB algorithms */
5302 if (px->lbprm.update_server_eweight)
5303 px->lbprm.update_server_eweight(s);
5304 else if (srv_willbe_usable(s)) {
5305 if (px->lbprm.set_server_status_up)
5306 px->lbprm.set_server_status_up(s);
5307 }
5308 else {
5309 if (px->lbprm.set_server_status_down)
5310 px->lbprm.set_server_status_down(s);
5311 }
5312 }
5313 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5314 /* remaining in drain mode after removing one of its flags */
5315
5316 tmptrash = alloc_trash_chunk();
5317 if (tmptrash) {
5318 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5319 chunk_printf(tmptrash,
5320 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5321 s->flags & SRV_F_BACKUP ? "Backup " : "",
5322 s->proxy->id, s->id);
5323
5324 if (s->track) /* normally it's mandatory here */
5325 chunk_appendf(tmptrash, " via %s/%s",
5326 s->track->proxy->id, s->track->id);
5327 }
5328 else {
5329 chunk_printf(tmptrash,
5330 "%sServer %s/%s remains in forced drain mode",
5331 s->flags & SRV_F_BACKUP ? "Backup " : "",
5332 s->proxy->id, s->id);
5333 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005334 ha_warning("%s.\n", tmptrash->area);
5335 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5336 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005337 free_trash_chunk(tmptrash);
5338 tmptrash = NULL;
5339 }
5340
5341 /* commit new admin status */
5342
5343 s->cur_admin = s->next_admin;
5344 }
5345 }
5346
5347 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005348 *s->adm_st_chg_cause = 0;
5349}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005350
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005351static struct task *cleanup_idle_connections(struct task *task, void *context, unsigned short state)
5352{
5353 struct server *srv = context;
5354 struct connection *conn, *conn_back;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005355 unsigned int to_destroy = srv->curr_idle_conns / 2 + (srv->curr_idle_conns & 1);
5356 unsigned int i = 0;
5357
5358
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005359
5360 list_for_each_entry_safe(conn, conn_back, &srv->idle_orphan_conns[tid], list) {
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005361 if (i == to_destroy)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005362 break;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005363 conn->mux->destroy(conn);
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005364 i++;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005365 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005366 if (!LIST_ISEMPTY(&srv->idle_orphan_conns[tid]))
5367 task_schedule(task, tick_add(now_ms, srv->pool_purge_delay));
5368 else
5369 task->expire = TICK_ETERNITY;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005370 return task;
5371}
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005372/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005373 * Local variables:
5374 * c-indent-level: 8
5375 * c-basic-offset: 8
5376 * End:
5377 */