blob: 50325de41ce1a32cab011ee8c1c5f883c907d011 [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
Willy Tarreau9c538e02019-01-23 10:21:49 +0100361static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
362{
363 char *arg;
364
365 arg = args[*cur_arg + 1];
366 if (!*arg) {
367 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
368 return ERR_ALERT | ERR_FATAL;
369 }
370 newsrv->max_reuse = atoi(arg);
371
372 return 0;
373}
374
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100375static 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 +0100376{
377 const char *res;
378 char *arg;
379 unsigned int time;
380
381 arg = args[*cur_arg + 1];
382 if (!*arg) {
383 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
384 return ERR_ALERT | ERR_FATAL;
385 }
386 res = parse_time_err(arg, &time, TIME_UNIT_MS);
387 if (res) {
388 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
389 *res, args[*cur_arg]);
390 return ERR_ALERT | ERR_FATAL;
391 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100392 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100393
394 return 0;
395}
396
Olivier Houchard006e3102018-12-10 18:30:32 +0100397static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
398{
399 char *arg;
400
401 arg = args[*cur_arg + 1];
402 if (!*arg) {
403 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
404 return ERR_ALERT | ERR_FATAL;
405 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100406
Olivier Houchard006e3102018-12-10 18:30:32 +0100407 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100408 if ((int)newsrv->max_idle_conns < -1) {
409 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
410 return ERR_ALERT | ERR_FATAL;
411 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100412
413 return 0;
414}
415
Willy Tarreaudff55432012-10-10 17:51:05 +0200416/* parse the "id" server keyword */
417static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
418{
419 struct eb32_node *node;
420
421 if (!*args[*cur_arg + 1]) {
422 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
423 return ERR_ALERT | ERR_FATAL;
424 }
425
426 newsrv->puid = atol(args[*cur_arg + 1]);
427 newsrv->conf.id.key = newsrv->puid;
428
429 if (newsrv->puid <= 0) {
430 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
431 return ERR_ALERT | ERR_FATAL;
432 }
433
434 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
435 if (node) {
436 struct server *target = container_of(node, struct server, conf.id);
437 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
438 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
439 target->id);
440 return ERR_ALERT | ERR_FATAL;
441 }
442
443 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200444 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200445 return 0;
446}
447
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100448/* Parse the "namespace" server keyword */
449static int srv_parse_namespace(char **args, int *cur_arg,
450 struct proxy *curproxy, struct server *newsrv, char **err)
451{
452#ifdef CONFIG_HAP_NS
453 char *arg;
454
455 arg = args[*cur_arg + 1];
456 if (!*arg) {
457 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
458 return ERR_ALERT | ERR_FATAL;
459 }
460
461 if (!strcmp(arg, "*")) {
462 /* Use the namespace associated with the connection (if present). */
463 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
464 return 0;
465 }
466
467 /*
468 * As this parser may be called several times for the same 'default-server'
469 * object, or for a new 'server' instance deriving from a 'default-server'
470 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
471 */
472 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
473
474 newsrv->netns = netns_store_lookup(arg, strlen(arg));
475 if (!newsrv->netns)
476 newsrv->netns = netns_store_insert(arg);
477
478 if (!newsrv->netns) {
479 memprintf(err, "Cannot open namespace '%s'", arg);
480 return ERR_ALERT | ERR_FATAL;
481 }
482
483 return 0;
484#else
485 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
486 return ERR_ALERT | ERR_FATAL;
487#endif
488}
489
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100490/* Parse the "no-agent-check" server keyword */
491static int srv_parse_no_agent_check(char **args, int *cur_arg,
492 struct proxy *curproxy, struct server *newsrv, char **err)
493{
494 free_check(&newsrv->agent);
495 newsrv->agent.inter = 0;
496 newsrv->agent.port = 0;
497 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
498 newsrv->do_agent = 0;
499 return 0;
500}
501
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100502/* Parse the "no-backup" server keyword */
503static int srv_parse_no_backup(char **args, int *cur_arg,
504 struct proxy *curproxy, struct server *newsrv, char **err)
505{
506 newsrv->flags &= ~SRV_F_BACKUP;
507 return 0;
508}
509
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100510/* Parse the "no-check" server keyword */
511static int srv_parse_no_check(char **args, int *cur_arg,
512 struct proxy *curproxy, struct server *newsrv, char **err)
513{
514 free_check(&newsrv->check);
515 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
516 newsrv->do_check = 0;
517 return 0;
518}
519
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100520/* Parse the "no-check-send-proxy" server keyword */
521static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
522 struct proxy *curproxy, struct server *newsrv, char **err)
523{
524 newsrv->check.send_proxy = 0;
525 return 0;
526}
527
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100528/* Disable server PROXY protocol flags. */
529static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
530{
531 srv->pp_opts &= ~flags;
532 return 0;
533}
534
535/* Parse the "no-send-proxy" server keyword */
536static int srv_parse_no_send_proxy(char **args, int *cur_arg,
537 struct proxy *curproxy, struct server *newsrv, char **err)
538{
539 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
540}
541
542/* Parse the "no-send-proxy-v2" server keyword */
543static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
544 struct proxy *curproxy, struct server *newsrv, char **err)
545{
546 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
547}
548
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100549/* Parse the "non-stick" server keyword */
550static int srv_parse_non_stick(char **args, int *cur_arg,
551 struct proxy *curproxy, struct server *newsrv, char **err)
552{
553 newsrv->flags |= SRV_F_NON_STICK;
554 return 0;
555}
556
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100557/* Enable server PROXY protocol flags. */
558static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
559{
560 srv->pp_opts |= flags;
561 return 0;
562}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200563/* parse the "proto" server keyword */
564static int srv_parse_proto(char **args, int *cur_arg,
565 struct proxy *px, struct server *newsrv, char **err)
566{
567 struct ist proto;
568
569 if (!*args[*cur_arg + 1]) {
570 memprintf(err, "'%s' : missing value", args[*cur_arg]);
571 return ERR_ALERT | ERR_FATAL;
572 }
573 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
574 newsrv->mux_proto = get_mux_proto(proto);
575 if (!newsrv->mux_proto) {
576 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
577 return ERR_ALERT | ERR_FATAL;
578 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200579 return 0;
580}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100581
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100582/* parse the "proxy-v2-options" */
583static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
584 struct proxy *px, struct server *newsrv, char **err)
585{
586 char *p, *n;
587 for (p = args[*cur_arg+1]; p; p = n) {
588 n = strchr(p, ',');
589 if (n)
590 *n++ = '\0';
591 if (!strcmp(p, "ssl")) {
592 newsrv->pp_opts |= SRV_PP_V2_SSL;
593 } else if (!strcmp(p, "cert-cn")) {
594 newsrv->pp_opts |= SRV_PP_V2_SSL;
595 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100596 } else if (!strcmp(p, "cert-key")) {
597 newsrv->pp_opts |= SRV_PP_V2_SSL;
598 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
599 } else if (!strcmp(p, "cert-sig")) {
600 newsrv->pp_opts |= SRV_PP_V2_SSL;
601 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
602 } else if (!strcmp(p, "ssl-cipher")) {
603 newsrv->pp_opts |= SRV_PP_V2_SSL;
604 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100605 } else if (!strcmp(p, "authority")) {
606 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100607 } else if (!strcmp(p, "crc32c")) {
608 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100609 } else
610 goto fail;
611 }
612 return 0;
613 fail:
614 if (err)
615 memprintf(err, "'%s' : proxy v2 option not implemented", p);
616 return ERR_ALERT | ERR_FATAL;
617}
618
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100619/* Parse the "observe" server keyword */
620static int srv_parse_observe(char **args, int *cur_arg,
621 struct proxy *curproxy, struct server *newsrv, char **err)
622{
623 char *arg;
624
625 arg = args[*cur_arg + 1];
626 if (!*arg) {
627 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
628 return ERR_ALERT | ERR_FATAL;
629 }
630
631 if (!strcmp(arg, "none")) {
632 newsrv->observe = HANA_OBS_NONE;
633 }
634 else if (!strcmp(arg, "layer4")) {
635 newsrv->observe = HANA_OBS_LAYER4;
636 }
637 else if (!strcmp(arg, "layer7")) {
638 if (curproxy->mode != PR_MODE_HTTP) {
639 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
640 return ERR_ALERT;
641 }
642 newsrv->observe = HANA_OBS_LAYER7;
643 }
644 else {
645 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
646 "but got '%s'\n", args[*cur_arg], arg);
647 return ERR_ALERT | ERR_FATAL;
648 }
649
650 return 0;
651}
652
Frédéric Lécaille16186232017-03-14 16:42:49 +0100653/* Parse the "redir" server keyword */
654static int srv_parse_redir(char **args, int *cur_arg,
655 struct proxy *curproxy, struct server *newsrv, char **err)
656{
657 char *arg;
658
659 arg = args[*cur_arg + 1];
660 if (!*arg) {
661 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
662 return ERR_ALERT | ERR_FATAL;
663 }
664
665 free(newsrv->rdr_pfx);
666 newsrv->rdr_pfx = strdup(arg);
667 newsrv->rdr_len = strlen(arg);
668
669 return 0;
670}
671
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100672/* Parse the "send-proxy" server keyword */
673static int srv_parse_send_proxy(char **args, int *cur_arg,
674 struct proxy *curproxy, struct server *newsrv, char **err)
675{
676 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
677}
678
679/* Parse the "send-proxy-v2" server keyword */
680static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
681 struct proxy *curproxy, struct server *newsrv, char **err)
682{
683 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
684}
685
Frédéric Lécailledba97072017-03-17 15:33:50 +0100686
687/* Parse the "source" server keyword */
688static int srv_parse_source(char **args, int *cur_arg,
689 struct proxy *curproxy, struct server *newsrv, char **err)
690{
691 char *errmsg;
692 int port_low, port_high;
693 struct sockaddr_storage *sk;
694 struct protocol *proto;
695
696 errmsg = NULL;
697
698 if (!*args[*cur_arg + 1]) {
699 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
700 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
701 goto err;
702 }
703
704 /* 'sk' is statically allocated (no need to be freed). */
705 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
706 if (!sk) {
707 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
708 goto err;
709 }
710
711 proto = protocol_by_family(sk->ss_family);
712 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100713 ha_alert("'%s %s' : connect() not supported for this address family.\n",
714 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100715 goto err;
716 }
717
718 newsrv->conn_src.opts |= CO_SRC_BIND;
719 newsrv->conn_src.source_addr = *sk;
720
721 if (port_low != port_high) {
722 int i;
723
724 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100725 ha_alert("'%s' does not support port offsets (found '%s').\n",
726 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100727 goto err;
728 }
729
730 if (port_low <= 0 || port_low > 65535 ||
731 port_high <= 0 || port_high > 65535 ||
732 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100733 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 +0100734 goto err;
735 }
736 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
737 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
738 newsrv->conn_src.sport_range->ports[i] = port_low + i;
739 }
740
741 *cur_arg += 2;
742 while (*(args[*cur_arg])) {
743 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
744#if defined(CONFIG_HAP_TRANSPARENT)
745 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100746 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
747 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100748 goto err;
749 }
750 if (!strcmp(args[*cur_arg + 1], "client")) {
751 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
752 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
753 }
754 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
755 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
756 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
757 }
758 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
759 char *name, *end;
760
761 name = args[*cur_arg + 1] + 7;
762 while (isspace(*name))
763 name++;
764
765 end = name;
766 while (*end && !isspace(*end) && *end != ',' && *end != ')')
767 end++;
768
769 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
770 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
771 free(newsrv->conn_src.bind_hdr_name);
772 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
773 newsrv->conn_src.bind_hdr_len = end - name;
774 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
775 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
776 newsrv->conn_src.bind_hdr_occ = -1;
777
778 /* now look for an occurrence number */
779 while (isspace(*end))
780 end++;
781 if (*end == ',') {
782 end++;
783 name = end;
784 if (*end == '-')
785 end++;
786 while (isdigit((int)*end))
787 end++;
788 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
789 }
790
791 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100792 ha_alert("usesrc hdr_ip(name,num) does not support negative"
793 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100794 goto err;
795 }
796 }
797 else {
798 struct sockaddr_storage *sk;
799 int port1, port2;
800
801 /* 'sk' is statically allocated (no need to be freed). */
802 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
803 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100804 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100805 goto err;
806 }
807
808 proto = protocol_by_family(sk->ss_family);
809 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100810 ha_alert("'%s %s' : connect() not supported for this address family.\n",
811 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100812 goto err;
813 }
814
815 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100816 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
817 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100818 goto err;
819 }
820 newsrv->conn_src.tproxy_addr = *sk;
821 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
822 }
823 global.last_checks |= LSTCHK_NETADM;
824 *cur_arg += 2;
825 continue;
826#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100827 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 +0100828 goto err;
829#endif /* defined(CONFIG_HAP_TRANSPARENT) */
830 } /* "usesrc" */
831
832 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
833#ifdef SO_BINDTODEVICE
834 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100835 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100836 goto err;
837 }
838 free(newsrv->conn_src.iface_name);
839 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
840 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
841 global.last_checks |= LSTCHK_NETADM;
842#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100843 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100844 goto err;
845#endif
846 *cur_arg += 2;
847 continue;
848 }
849 /* this keyword in not an option of "source" */
850 break;
851 } /* while */
852
853 return 0;
854
855 err:
856 free(errmsg);
857 return ERR_ALERT | ERR_FATAL;
858}
859
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100860/* Parse the "stick" server keyword */
861static int srv_parse_stick(char **args, int *cur_arg,
862 struct proxy *curproxy, struct server *newsrv, char **err)
863{
864 newsrv->flags &= ~SRV_F_NON_STICK;
865 return 0;
866}
867
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100868/* Parse the "track" server keyword */
869static int srv_parse_track(char **args, int *cur_arg,
870 struct proxy *curproxy, struct server *newsrv, char **err)
871{
872 char *arg;
873
874 arg = args[*cur_arg + 1];
875 if (!*arg) {
876 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
877 return ERR_ALERT | ERR_FATAL;
878 }
879
880 free(newsrv->trackit);
881 newsrv->trackit = strdup(arg);
882
883 return 0;
884}
885
Frédéric Lécailledba97072017-03-17 15:33:50 +0100886
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200887/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200888 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200889 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200890 *
891 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200892 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200893void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200894{
Willy Tarreau87b09662015-04-03 00:22:06 +0200895 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200896
Willy Tarreau87b09662015-04-03 00:22:06 +0200897 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
898 if (stream->srv_conn == srv)
899 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200900}
901
902/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200903 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200904 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200905 *
906 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200907 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200908void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200909{
910 struct server *srv;
911
912 for (srv = px->srv; srv != NULL; srv = srv->next)
913 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200914 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200915}
916
Willy Tarreaubda92272014-05-20 21:55:30 +0200917/* Appends some information to a message string related to a server going UP or
918 * DOWN. If both <forced> and <reason> are null and the server tracks another
919 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200920 * If <check> is non-null, an entire string describing the check result will be
921 * appended after a comma and a space (eg: to report some information from the
922 * check that changed the state). In the other case, the string will be built
923 * using the check results stored into the struct server if present.
924 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200925 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200926 *
927 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200928 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200929void srv_append_status(struct buffer *msg, struct server *s,
930 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200931{
Emeric Brun5a133512017-10-19 14:42:30 +0200932 short status = s->op_st_chg.status;
933 short code = s->op_st_chg.code;
934 long duration = s->op_st_chg.duration;
935 char *desc = s->op_st_chg.reason;
936
937 if (check) {
938 status = check->status;
939 code = check->code;
940 duration = check->duration;
941 desc = check->desc;
942 }
943
944 if (status != -1) {
945 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
946
947 if (status >= HCHK_STATUS_L57DATA)
948 chunk_appendf(msg, ", code: %d", code);
949
950 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200951 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200952
953 chunk_appendf(msg, ", info: \"");
954
955 chunk_initlen(&src, desc, 0, strlen(desc));
956 chunk_asciiencode(msg, &src, '"');
957
958 chunk_appendf(msg, "\"");
959 }
960
961 if (duration >= 0)
962 chunk_appendf(msg, ", check duration: %ldms", duration);
963 }
964 else if (desc && *desc) {
965 chunk_appendf(msg, ", %s", desc);
966 }
967 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200968 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200969 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200970
971 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200972 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200973 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
974 " %d sessions active, %d requeued, %d remaining in queue",
975 s->proxy->srv_act, s->proxy->srv_bck,
976 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
977 s->cur_sess, xferred, s->nbpend);
978 else
979 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
980 " %d sessions requeued, %d total in queue",
981 s->proxy->srv_act, s->proxy->srv_bck,
982 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
983 xferred, s->nbpend);
984 }
985}
986
Emeric Brun5a133512017-10-19 14:42:30 +0200987/* Marks server <s> down, regardless of its checks' statuses. The server is
988 * registered in a list to postpone the counting of the remaining servers on
989 * the proxy and transfers queued streams whenever possible to other servers at
990 * a sync point. Maintenance servers are ignored. It stores the <reason> if
991 * non-null as the reason for going down or the available data from the check
992 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200993 *
994 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200995 */
Emeric Brun5a133512017-10-19 14:42:30 +0200996void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200997{
998 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200999
Emeric Brun64cc49c2017-10-03 14:46:45 +02001000 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001001 return;
1002
Emeric Brun52a91d32017-08-31 14:41:55 +02001003 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001004 *s->op_st_chg.reason = 0;
1005 s->op_st_chg.status = -1;
1006 if (reason) {
1007 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1008 }
1009 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001010 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001011 s->op_st_chg.code = check->code;
1012 s->op_st_chg.status = check->status;
1013 s->op_st_chg.duration = check->duration;
1014 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001015
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001016 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001017 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001018
Emeric Brun9f0b4582017-10-23 14:39:51 +02001019 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001020 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001021 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001022 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001023 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001024}
1025
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001026/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001027 * in maintenance. The server is registered in a list to postpone the counting
1028 * of the remaining servers on the proxy and tries to grab requests from the
1029 * proxy at a sync point. Maintenance servers are ignored. It stores the
1030 * <reason> if non-null as the reason for going down or the available data
1031 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001032 *
1033 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001034 */
Emeric Brun5a133512017-10-19 14:42:30 +02001035void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001036{
1037 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001038
Emeric Brun64cc49c2017-10-03 14:46:45 +02001039 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001040 return;
1041
Emeric Brun52a91d32017-08-31 14:41:55 +02001042 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001043 return;
1044
Emeric Brun52a91d32017-08-31 14:41:55 +02001045 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001046 *s->op_st_chg.reason = 0;
1047 s->op_st_chg.status = -1;
1048 if (reason) {
1049 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1050 }
1051 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001052 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001053 s->op_st_chg.code = check->code;
1054 s->op_st_chg.status = check->status;
1055 s->op_st_chg.duration = check->duration;
1056 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001057
Emeric Brun64cc49c2017-10-03 14:46:45 +02001058 if (s->slowstart <= 0)
1059 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001060
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001061 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001062 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001063
Emeric Brun9f0b4582017-10-23 14:39:51 +02001064 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001065 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001066 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001067 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001068 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001069}
1070
Willy Tarreau8eb77842014-05-21 13:54:57 +02001071/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001072 * isn't in maintenance. The server is registered in a list to postpone the
1073 * counting of the remaining servers on the proxy and tries to grab requests
1074 * from the proxy. Maintenance servers are ignored. It stores the
1075 * <reason> if non-null as the reason for going down or the available data
1076 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001077 * up. Note that it makes use of the trash to build the log strings, so <reason>
1078 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001079 *
1080 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001081 */
Emeric Brun5a133512017-10-19 14:42:30 +02001082void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001083{
1084 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001085
Emeric Brun64cc49c2017-10-03 14:46:45 +02001086 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001087 return;
1088
Emeric Brun52a91d32017-08-31 14:41:55 +02001089 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001090 return;
1091
Emeric Brun52a91d32017-08-31 14:41:55 +02001092 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001093 *s->op_st_chg.reason = 0;
1094 s->op_st_chg.status = -1;
1095 if (reason) {
1096 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1097 }
1098 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001099 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001100 s->op_st_chg.code = check->code;
1101 s->op_st_chg.status = check->status;
1102 s->op_st_chg.duration = check->duration;
1103 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001104
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001105 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001106 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001107
Emeric Brun9f0b4582017-10-23 14:39:51 +02001108 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001109 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001110 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001111 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001112 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001113}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001114
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001115/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1116 * enforce either maint mode or drain mode. It is not allowed to set more than
1117 * one flag at once. The equivalent "inherited" flag is propagated to all
1118 * tracking servers. Maintenance mode disables health checks (but not agent
1119 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001120 * is done. If <cause> is non-null, it will be displayed at the end of the log
1121 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001122 *
1123 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001124 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001125void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001126{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001127 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001128
1129 if (!mode)
1130 return;
1131
1132 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001133 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001134 return;
1135
Emeric Brun52a91d32017-08-31 14:41:55 +02001136 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001137 if (cause)
1138 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1139
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001140 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001141 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001142
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001143 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001144 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1145 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001146 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001147
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001148 /* compute the inherited flag to propagate */
1149 if (mode & SRV_ADMF_MAINT)
1150 mode = SRV_ADMF_IMAINT;
1151 else if (mode & SRV_ADMF_DRAIN)
1152 mode = SRV_ADMF_IDRAIN;
1153
Emeric Brun9f0b4582017-10-23 14:39:51 +02001154 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001155 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001156 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001157 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001158 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001159}
1160
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001161/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1162 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1163 * than one flag at once. The equivalent "inherited" flag is propagated to all
1164 * tracking servers. Leaving maintenance mode re-enables health checks. When
1165 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001166 *
1167 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001168 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001169void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001170{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001171 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001172
1173 if (!mode)
1174 return;
1175
1176 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001177 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001178 return;
1179
Emeric Brun52a91d32017-08-31 14:41:55 +02001180 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001181
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001182 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001183 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001184
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001185 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001186 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1187 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001188 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001189
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001190 if (mode & SRV_ADMF_MAINT)
1191 mode = SRV_ADMF_IMAINT;
1192 else if (mode & SRV_ADMF_DRAIN)
1193 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001194
Emeric Brun9f0b4582017-10-23 14:39:51 +02001195 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001196 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001197 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001198 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001199 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001200}
1201
Willy Tarreau757478e2016-11-03 19:22:19 +01001202/* principle: propagate maint and drain to tracking servers. This is useful
1203 * upon startup so that inherited states are correct.
1204 */
1205static void srv_propagate_admin_state(struct server *srv)
1206{
1207 struct server *srv2;
1208
1209 if (!srv->trackers)
1210 return;
1211
1212 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001213 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001214 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001215 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001216
Emeric Brun52a91d32017-08-31 14:41:55 +02001217 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001218 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001219 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001220 }
1221}
1222
1223/* Compute and propagate the admin states for all servers in proxy <px>.
1224 * Only servers *not* tracking another one are considered, because other
1225 * ones will be handled when the server they track is visited.
1226 */
1227void srv_compute_all_admin_states(struct proxy *px)
1228{
1229 struct server *srv;
1230
1231 for (srv = px->srv; srv; srv = srv->next) {
1232 if (srv->track)
1233 continue;
1234 srv_propagate_admin_state(srv);
1235 }
1236}
1237
Willy Tarreaudff55432012-10-10 17:51:05 +02001238/* Note: must not be declared <const> as its list will be overwritten.
1239 * Please take care of keeping this list alphabetically sorted, doing so helps
1240 * all code contributors.
1241 * Optional keywords are also declared with a NULL ->parse() function so that
1242 * the config parser can report an appropriate error when a known keyword was
1243 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001244 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001245 */
1246static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001247 { "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 +01001248 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001249 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001250 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001251 { "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 +01001252 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001253 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1254 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001255 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001256 { "max-reuse", srv_parse_max_reuse, 1, 1 }, /* Set the max number of requests on a connection, -1 means unlimited */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001257 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001258 { "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 +01001259 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001260 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001261 { "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 +01001262 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1263 { "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 +01001264 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001265 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001266 { "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 +01001267 { "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 +02001268 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001269 { "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 +01001270 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001271 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1272 { "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 +02001273 { "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 +01001274 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001275 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001276 { NULL, NULL, 0 },
1277}};
1278
Willy Tarreau0108d902018-11-25 19:14:37 +01001279INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001280
Willy Tarreau004e0452013-11-21 11:22:01 +01001281/* Recomputes the server's eweight based on its state, uweight, the current time,
1282 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001283 * state is automatically disabled if the time is elapsed. If <must_update> is
1284 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001285 *
1286 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001287 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001288void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001289{
1290 struct proxy *px = sv->proxy;
1291 unsigned w;
1292
1293 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1294 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001295 if (sv->next_state == SRV_ST_STARTING)
1296 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001297 }
1298
1299 /* We must take care of not pushing the server to full throttle during slow starts.
1300 * It must also start immediately, at least at the minimal step when leaving maintenance.
1301 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001302 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001303 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1304 else
1305 w = px->lbprm.wdiv;
1306
Emeric Brun52a91d32017-08-31 14:41:55 +02001307 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001308
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001309 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001310 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001311 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001312}
1313
Willy Tarreaubaaee002006-06-26 02:48:02 +02001314/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001315 * Parses weight_str and configures sv accordingly.
1316 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001317 *
1318 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001319 */
1320const char *server_parse_weight_change_request(struct server *sv,
1321 const char *weight_str)
1322{
1323 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001324 long int w;
1325 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001326
1327 px = sv->proxy;
1328
1329 /* if the weight is terminated with '%', it is set relative to
1330 * the initial weight, otherwise it is absolute.
1331 */
1332 if (!*weight_str)
1333 return "Require <weight> or <weight%>.\n";
1334
Simon Hormanb796afa2013-02-12 10:45:53 +09001335 w = strtol(weight_str, &end, 10);
1336 if (end == weight_str)
1337 return "Empty weight string empty or preceded by garbage";
1338 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001339 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001340 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001341 /* Avoid integer overflow */
1342 if (w > 25600)
1343 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001344 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001345 if (w > 256)
1346 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001347 }
1348 else if (w < 0 || w > 256)
1349 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001350 else if (end[0] != '\0')
1351 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001352
1353 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1354 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1355
1356 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001357 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001358
1359 return NULL;
1360}
1361
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001362/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001363 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1364 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001365 * Returns:
1366 * - error string on error
1367 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001368 *
1369 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001370 */
1371const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001372 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001373{
1374 unsigned char ip[INET6_ADDRSTRLEN];
1375
1376 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001377 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001378 return NULL;
1379 }
1380 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001381 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001382 return NULL;
1383 }
1384
1385 return "Could not understand IP address format.\n";
1386}
1387
Willy Tarreau46b7f532018-08-21 11:54:26 +02001388/*
1389 * Must be called with the server lock held.
1390 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001391const char *server_parse_maxconn_change_request(struct server *sv,
1392 const char *maxconn_str)
1393{
1394 long int v;
1395 char *end;
1396
1397 if (!*maxconn_str)
1398 return "Require <maxconn>.\n";
1399
1400 v = strtol(maxconn_str, &end, 10);
1401 if (end == maxconn_str)
1402 return "maxconn string empty or preceded by garbage";
1403 else if (end[0] != '\0')
1404 return "Trailing garbage in maxconn string";
1405
1406 if (sv->maxconn == sv->minconn) { // static maxconn
1407 sv->maxconn = sv->minconn = v;
1408 } else { // dynamic maxconn
1409 sv->maxconn = v;
1410 }
1411
1412 if (may_dequeue_tasks(sv, sv->proxy))
1413 process_srv_queue(sv);
1414
1415 return NULL;
1416}
1417
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001418#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001419static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1420 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001421{
1422 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001423 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001424 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001425 NULL,
1426 };
1427
1428 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001429 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001430
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001431 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1432}
1433
1434static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1435{
1436 struct sample_expr *expr;
1437
1438 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 +01001439 if (!expr) {
1440 memprintf(err, "error detected while parsing sni expression : %s", *err);
1441 return ERR_ALERT | ERR_FATAL;
1442 }
1443
1444 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1445 memprintf(err, "error detected while parsing sni expression : "
1446 " fetch method '%s' extracts information from '%s', "
1447 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001448 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001449 return ERR_ALERT | ERR_FATAL;
1450 }
1451
1452 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1453 release_sample_expr(newsrv->ssl_ctx.sni);
1454 newsrv->ssl_ctx.sni = expr;
1455
1456 return 0;
1457}
1458#endif
1459
1460static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1461{
1462 if (err && *err) {
1463 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001464 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 +01001465 }
1466 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001467 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1468 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001469}
1470
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001471static void srv_conn_src_sport_range_cpy(struct server *srv,
1472 struct server *src)
1473{
1474 int range_sz;
1475
1476 range_sz = src->conn_src.sport_range->size;
1477 if (range_sz > 0) {
1478 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1479 if (srv->conn_src.sport_range != NULL) {
1480 int i;
1481
1482 for (i = 0; i < range_sz; i++) {
1483 srv->conn_src.sport_range->ports[i] =
1484 src->conn_src.sport_range->ports[i];
1485 }
1486 }
1487 }
1488}
1489
1490/*
1491 * Copy <src> server connection source settings to <srv> server everything needed.
1492 */
1493static void srv_conn_src_cpy(struct server *srv, struct server *src)
1494{
1495 srv->conn_src.opts = src->conn_src.opts;
1496 srv->conn_src.source_addr = src->conn_src.source_addr;
1497
1498 /* Source port range copy. */
1499 if (src->conn_src.sport_range != NULL)
1500 srv_conn_src_sport_range_cpy(srv, src);
1501
1502#ifdef CONFIG_HAP_TRANSPARENT
1503 if (src->conn_src.bind_hdr_name != NULL) {
1504 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1505 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1506 }
1507 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1508 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1509#endif
1510 if (src->conn_src.iface_name != NULL)
1511 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1512}
1513
1514/*
1515 * Copy <src> server SSL settings to <srv> server allocating
1516 * everything needed.
1517 */
1518#if defined(USE_OPENSSL)
1519static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1520{
1521 if (src->ssl_ctx.ca_file != NULL)
1522 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1523 if (src->ssl_ctx.crl_file != NULL)
1524 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1525 if (src->ssl_ctx.client_crt != NULL)
1526 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1527
1528 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1529
1530 if (src->ssl_ctx.verify_host != NULL)
1531 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1532 if (src->ssl_ctx.ciphers != NULL)
1533 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001534#if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
1535 if (src->ssl_ctx.ciphersuites != NULL)
1536 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1537#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001538 if (src->sni_expr != NULL)
1539 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001540
1541#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1542 if (src->ssl_ctx.alpn_str) {
1543 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1544 if (srv->ssl_ctx.alpn_str) {
1545 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1546 src->ssl_ctx.alpn_len);
1547 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1548 }
1549 }
1550#endif
1551#ifdef OPENSSL_NPN_NEGOTIATED
1552 if (src->ssl_ctx.npn_str) {
1553 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1554 if (srv->ssl_ctx.npn_str) {
1555 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1556 src->ssl_ctx.npn_len);
1557 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1558 }
1559 }
1560#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001561}
1562#endif
1563
1564/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001565 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001566 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001567 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001568 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001569static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001570{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001571 char *hostname_dn;
1572 int hostname_len, hostname_dn_len;
1573
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001574 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001575 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001576
Christopher Faulet67957bd2017-09-27 11:00:59 +02001577 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001578 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001579 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1580 hostname_dn, trash.size);
1581 if (hostname_dn_len == -1)
1582 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001583
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001584
Christopher Faulet67957bd2017-09-27 11:00:59 +02001585 free(srv->hostname);
1586 free(srv->hostname_dn);
1587 srv->hostname = strdup(hostname);
1588 srv->hostname_dn = strdup(hostname_dn);
1589 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001590 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001591 goto err;
1592
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001593 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001594
1595 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001596 free(srv->hostname); srv->hostname = NULL;
1597 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001598 return -1;
1599}
1600
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001601/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001602 * Copy <src> server settings to <srv> server allocating
1603 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001604 * This function is not supposed to be called at any time, but only
1605 * during server settings parsing or during server allocations from
1606 * a server template, and just after having calloc()'ed a new server.
1607 * So, <src> may only be a default server (when parsing server settings)
1608 * or a server template (during server allocations from a server template).
1609 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1610 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001611 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001612static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001613{
1614 /* Connection source settings copy */
1615 srv_conn_src_cpy(srv, src);
1616
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001617 if (srv_tmpl) {
1618 srv->addr = src->addr;
1619 srv->svc_port = src->svc_port;
1620 }
1621
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001622 srv->pp_opts = src->pp_opts;
1623 if (src->rdr_pfx != NULL) {
1624 srv->rdr_pfx = strdup(src->rdr_pfx);
1625 srv->rdr_len = src->rdr_len;
1626 }
1627 if (src->cookie != NULL) {
1628 srv->cookie = strdup(src->cookie);
1629 srv->cklen = src->cklen;
1630 }
1631 srv->use_ssl = src->use_ssl;
1632 srv->check.addr = srv->agent.addr = src->check.addr;
1633 srv->check.use_ssl = src->check.use_ssl;
1634 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001635 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001636 srv->check.alpn_str = src->check.alpn_str;
1637 srv->check.alpn_len = srv->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001638 /* Note: 'flags' field has potentially been already initialized. */
1639 srv->flags |= src->flags;
1640 srv->do_check = src->do_check;
1641 srv->do_agent = src->do_agent;
1642 if (srv->check.port)
1643 srv->flags |= SRV_F_CHECKPORT;
1644 srv->check.inter = src->check.inter;
1645 srv->check.fastinter = src->check.fastinter;
1646 srv->check.downinter = src->check.downinter;
1647 srv->agent.use_ssl = src->agent.use_ssl;
1648 srv->agent.port = src->agent.port;
1649 if (src->agent.send_string != NULL)
1650 srv->agent.send_string = strdup(src->agent.send_string);
1651 srv->agent.send_string_len = src->agent.send_string_len;
1652 srv->agent.inter = src->agent.inter;
1653 srv->agent.fastinter = src->agent.fastinter;
1654 srv->agent.downinter = src->agent.downinter;
1655 srv->maxqueue = src->maxqueue;
1656 srv->minconn = src->minconn;
1657 srv->maxconn = src->maxconn;
1658 srv->slowstart = src->slowstart;
1659 srv->observe = src->observe;
1660 srv->onerror = src->onerror;
1661 srv->onmarkeddown = src->onmarkeddown;
1662 srv->onmarkedup = src->onmarkedup;
1663 if (src->trackit != NULL)
1664 srv->trackit = strdup(src->trackit);
1665 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1666 srv->uweight = srv->iweight = src->iweight;
1667
1668 srv->check.send_proxy = src->check.send_proxy;
1669 /* health: up, but will fall down at first failure */
1670 srv->check.rise = srv->check.health = src->check.rise;
1671 srv->check.fall = src->check.fall;
1672
1673 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001674 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1675 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1676 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001677 srv->check.state |= CHK_ST_PAUSED;
1678 srv->check.health = 0;
1679 }
1680
1681 /* health: up but will fall down at first failure */
1682 srv->agent.rise = srv->agent.health = src->agent.rise;
1683 srv->agent.fall = src->agent.fall;
1684
1685 if (src->resolvers_id != NULL)
1686 srv->resolvers_id = strdup(src->resolvers_id);
1687 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001688 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001689 if (srv->dns_opts.family_prio == AF_UNSPEC)
1690 srv->dns_opts.family_prio = AF_INET6;
1691 memcpy(srv->dns_opts.pref_net,
1692 src->dns_opts.pref_net,
1693 sizeof srv->dns_opts.pref_net);
1694 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1695
1696 srv->init_addr_methods = src->init_addr_methods;
1697 srv->init_addr = src->init_addr;
1698#if defined(USE_OPENSSL)
1699 srv_ssl_settings_cpy(srv, src);
1700#endif
1701#ifdef TCP_USER_TIMEOUT
1702 srv->tcp_ut = src->tcp_ut;
1703#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001704 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001705 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001706 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001707 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001708
Olivier Houchard8da5f982017-08-04 18:35:36 +02001709 if (srv_tmpl)
1710 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001711}
1712
William Lallemand313bfd12018-10-26 14:47:32 +02001713struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001714{
1715 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001716 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001717
1718 srv = calloc(1, sizeof *srv);
1719 if (!srv)
1720 return NULL;
1721
1722 srv->obj_type = OBJ_TYPE_SERVER;
1723 srv->proxy = proxy;
1724 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001725 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001726
1727 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1728 goto free_srv;
1729 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1730 goto free_priv_conns;
1731 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1732 goto free_idle_conns;
1733
1734 for (i = 0; i < global.nbthread; i++) {
1735 LIST_INIT(&srv->priv_conns[i]);
1736 LIST_INIT(&srv->idle_conns[i]);
1737 LIST_INIT(&srv->safe_conns[i]);
1738 }
1739
Emeric Brun52a91d32017-08-31 14:41:55 +02001740 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001741 srv->last_change = now.tv_sec;
1742
1743 srv->check.status = HCHK_STATUS_INI;
1744 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001745 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001746 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1747
1748 srv->agent.status = HCHK_STATUS_INI;
1749 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001750 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001751 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1752
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001753 srv->pool_purge_delay = 1000;
Olivier Houchard006e3102018-12-10 18:30:32 +01001754 srv->max_idle_conns = -1;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001755 srv->max_reuse = -1;
Olivier Houchard006e3102018-12-10 18:30:32 +01001756
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001757 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001758
1759 free_idle_conns:
1760 free(srv->idle_conns);
1761 free_priv_conns:
1762 free(srv->priv_conns);
1763 free_srv:
1764 free(srv);
1765 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001766}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001767
1768/*
1769 * Validate <srv> server health-check settings.
1770 * Returns 0 if everything is OK, -1 if not.
1771 */
1772static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1773{
1774 struct tcpcheck_rule *r = NULL;
1775 struct list *l;
1776
1777 /*
1778 * We need at least a service port, a check port or the first tcp-check rule must
1779 * be a 'connect' one when checking an IPv4/IPv6 server.
1780 */
1781 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1782 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1783 return 0;
1784
1785 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1786 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001787 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1788 "Check has been disabled.\n",
1789 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001790 return -1;
1791 }
1792
1793 /* search the first action (connect / send / expect) in the list */
1794 l = &srv->proxy->tcpcheck_rules;
1795 list_for_each_entry(r, l, list) {
1796 if (r->action != TCPCHK_ACT_COMMENT)
1797 break;
1798 }
1799
1800 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001801 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1802 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1803 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001804 return -1;
1805 }
1806
1807 /* scan the tcp-check ruleset to ensure a port has been configured */
1808 l = &srv->proxy->tcpcheck_rules;
1809 list_for_each_entry(r, l, list) {
1810 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001811 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1812 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1813 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001814 return -1;
1815 }
1816 }
1817
1818 return 0;
1819}
1820
1821/*
1822 * Initialize <srv> health-check structure.
1823 * Returns the error string in case of memory allocation failure, NULL if not.
1824 */
1825static const char *do_health_check_init(struct server *srv, int check_type, int state)
1826{
1827 const char *ret;
1828
1829 if (!srv->do_check)
1830 return NULL;
1831
1832 ret = init_check(&srv->check, check_type);
1833 if (ret)
1834 return ret;
1835
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001836 srv->check.state |= state;
1837 global.maxsock++;
1838
1839 return NULL;
1840}
1841
1842static int server_health_check_init(const char *file, int linenum,
1843 struct server *srv, struct proxy *curproxy)
1844{
1845 const char *ret;
1846
1847 if (!srv->do_check)
1848 return 0;
1849
1850 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001851 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1852 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001853 return ERR_ALERT | ERR_FATAL;
1854 }
1855
1856 if (server_healthcheck_validate(file, linenum, srv) < 0)
1857 return ERR_ALERT | ERR_ABORT;
1858
1859 /* note: check type will be set during the config review phase */
1860 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1861 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001862 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001863 return ERR_ALERT | ERR_ABORT;
1864 }
1865
1866 return 0;
1867}
1868
1869/*
1870 * Initialize <srv> agent check structure.
1871 * Returns the error string in case of memory allocation failure, NULL if not.
1872 */
1873static const char *do_server_agent_check_init(struct server *srv, int state)
1874{
1875 const char *ret;
1876
1877 if (!srv->do_agent)
1878 return NULL;
1879
1880 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1881 if (ret)
1882 return ret;
1883
1884 if (!srv->agent.inter)
1885 srv->agent.inter = srv->check.inter;
1886
1887 srv->agent.state |= state;
1888 global.maxsock++;
1889
1890 return NULL;
1891}
1892
1893static int server_agent_check_init(const char *file, int linenum,
1894 struct server *srv, struct proxy *curproxy)
1895{
1896 const char *ret;
1897
1898 if (!srv->do_agent)
1899 return 0;
1900
1901 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001902 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 +02001903 file, linenum, srv->id);
1904 return ERR_ALERT | ERR_FATAL;
1905 }
1906
1907 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1908 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001909 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001910 return ERR_ALERT | ERR_ABORT;
1911 }
1912
1913 return 0;
1914}
1915
1916#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1917static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1918 struct server *srv, struct proxy *proxy)
1919{
1920 int ret;
1921 char *err = NULL;
1922
1923 if (!srv->sni_expr)
1924 return 0;
1925
1926 ret = server_parse_sni_expr(srv, proxy, &err);
1927 if (!ret)
1928 return 0;
1929
1930 display_parser_err(file, linenum, args, cur_arg, &err);
1931 free(err);
1932
1933 return ret;
1934}
1935#endif
1936
1937/*
1938 * Server initializations finalization.
1939 * Initialize health check, agent check and SNI expression if enabled.
1940 * Must not be called for a default server instance.
1941 */
1942static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1943 struct server *srv, struct proxy *px)
1944{
1945 int ret;
1946
1947 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1948 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1949 return ret;
1950 }
1951
1952#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1953 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1954 return ret;
1955#endif
1956
1957 if (srv->flags & SRV_F_BACKUP)
1958 px->srv_bck++;
1959 else
1960 px->srv_act++;
1961 srv_lb_commit_status(srv);
1962
Daniel Corbett43bb8422019-01-09 08:13:29 -05001963 if (srv->max_idle_conns != 0) {
Willy Tarreau15c120d2019-01-23 10:41:19 +01001964 int i;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001965
Willy Tarreau15c120d2019-01-23 10:41:19 +01001966 srv->idle_orphan_conns = calloc(global.nbthread, sizeof(*srv->idle_orphan_conns));
1967 if (!srv->idle_orphan_conns)
1968 goto err;
1969 srv->idle_task = calloc(global.nbthread, sizeof(*srv->idle_task));
1970 if (!srv->idle_task)
1971 goto err;
1972 for (i = 0; i < global.nbthread; i++) {
1973 LIST_INIT(&srv->idle_orphan_conns[i]);
1974 srv->idle_task[i] = task_new(1 << i);
1975 if (!srv->idle_task[i])
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001976 goto err;
Willy Tarreau15c120d2019-01-23 10:41:19 +01001977 srv->idle_task[i]->process = cleanup_idle_connections;
1978 srv->idle_task[i]->context = srv;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001979 }
Willy Tarreau15c120d2019-01-23 10:41:19 +01001980 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001981
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001982 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001983err:
1984 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001985}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001986
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001987/*
1988 * Parse as much as possible such a range string argument: low[-high]
1989 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1990 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1991 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1992 * Returns 0 if succeeded, -1 if not.
1993 */
1994static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1995{
1996 char *nb_high_arg;
1997
1998 *nb_high = 0;
1999 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002000 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002001
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002002 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002003 *nb_high_arg++ = '\0';
2004 *nb_high = atoi(nb_high_arg);
2005 }
2006 else {
2007 *nb_high += *nb_low;
2008 *nb_low = 1;
2009 }
2010
2011 if (*nb_low < 0 || *nb_high < *nb_low)
2012 return -1;
2013
2014 return 0;
2015}
2016
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002017static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2018{
2019 chunk_printf(&trash, "%s%d", prefix, nb);
2020 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002021 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002022}
2023
2024/*
2025 * Initialize as much as possible servers from <srv> server template.
2026 * Note that a server template is a special server with
2027 * a few different parameters than a server which has
2028 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002029 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002030 * 'srv' template included.
2031 */
2032static int server_template_init(struct server *srv, struct proxy *px)
2033{
2034 int i;
2035 struct server *newsrv;
2036
2037 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2038 int check_init_state;
2039 int agent_init_state;
2040
2041 newsrv = new_server(px);
2042 if (!newsrv)
2043 goto err;
2044
2045 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002046 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002047#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2048 if (newsrv->sni_expr) {
2049 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2050 if (!newsrv->ssl_ctx.sni)
2051 goto err;
2052 }
2053#endif
2054 /* Set this new server ID. */
2055 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2056
2057 /* Initial checks states. */
2058 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2059 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2060
2061 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2062 do_server_agent_check_init(newsrv, agent_init_state))
2063 goto err;
2064
2065 /* Linked backwards first. This will be restablished after parsing. */
2066 newsrv->next = px->srv;
2067 px->srv = newsrv;
Olivier Houchard006e3102018-12-10 18:30:32 +01002068 if (newsrv->max_idle_conns != 0) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002069 int i;
2070
2071 newsrv->idle_orphan_conns = calloc(global.nbthread, sizeof(*newsrv->idle_orphan_conns));
2072 if (!newsrv->idle_orphan_conns)
2073 goto err;
2074 newsrv->idle_task = calloc(global.nbthread, sizeof(*newsrv->idle_task));
2075 if (!newsrv->idle_task)
2076 goto err;
2077 for (i = 0; i < global.nbthread; i++) {
2078 LIST_INIT(&newsrv->idle_orphan_conns[i]);
2079 newsrv->idle_task[i] = task_new(1 << i);
2080 if (!newsrv->idle_task[i])
2081 goto err;
2082 newsrv->idle_task[i]->process = cleanup_idle_connections;
2083 newsrv->idle_task[i]->context = newsrv;
2084 }
2085 }
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002086 }
2087 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2088
2089 return i - srv->tmpl_info.nb_low;
2090
2091 err:
2092 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2093 if (newsrv) {
2094#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2095 release_sample_expr(newsrv->ssl_ctx.sni);
2096#endif
2097 free_check(&newsrv->agent);
2098 free_check(&newsrv->check);
2099 }
2100 free(newsrv);
2101 return i - srv->tmpl_info.nb_low;
2102}
2103
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002104int 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 +02002105{
2106 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002107 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002108 char *errmsg = NULL;
2109 int err_code = 0;
2110 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002111 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002112
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002113 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002114 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002115 !strcmp(args[0], "default-server") ||
2116 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002117 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002118 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002119 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002120 int srv_tmpl = !defsrv && !srv;
2121 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002122
2123 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002124 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002125 err_code |= ERR_ALERT | ERR_FATAL;
2126 goto out;
2127 }
2128 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002129 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002130
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002131 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002132 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002133 if (!*args[2]) {
2134 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002135 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002136 file, linenum, args[0]);
2137 err_code |= ERR_ALERT | ERR_FATAL;
2138 goto out;
2139 }
2140
2141 err = invalid_char(args[1]);
2142 }
2143 else if (srv_tmpl) {
2144 if (!*args[3]) {
2145 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002146 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 +02002147 file, linenum, args[0]);
2148 err_code |= ERR_ALERT | ERR_FATAL;
2149 goto out;
2150 }
2151
2152 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002153 }
2154
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002155 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002156 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 +02002157 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002158 err_code |= ERR_ALERT | ERR_FATAL;
2159 goto out;
2160 }
2161
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002162 cur_arg = 2;
2163 if (srv_tmpl) {
2164 /* Parse server-template <nb | range> arg. */
2165 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002166 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002167 file, linenum, args[0], args[cur_arg]);
2168 err_code |= ERR_ALERT | ERR_FATAL;
2169 goto out;
2170 }
2171 cur_arg++;
2172 }
2173
Willy Tarreau272adea2014-03-31 10:39:59 +02002174 if (!defsrv) {
2175 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002176 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002177 struct protocol *proto;
2178
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002179 newsrv = new_server(curproxy);
2180 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002181 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002182 err_code |= ERR_ALERT | ERR_ABORT;
2183 goto out;
2184 }
2185
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002186 if (srv_tmpl) {
2187 newsrv->tmpl_info.nb_low = tmpl_range_low;
2188 newsrv->tmpl_info.nb_high = tmpl_range_high;
2189 }
2190
Willy Tarreau272adea2014-03-31 10:39:59 +02002191 /* the servers are linked backwards first */
2192 newsrv->next = curproxy->srv;
2193 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002194 newsrv->conf.file = strdup(file);
2195 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002196 /* Note: for a server template, its id is its prefix.
2197 * This is a temporary id which will be used for server allocations to come
2198 * after parsing.
2199 */
2200 if (srv)
2201 newsrv->id = strdup(args[1]);
2202 else
2203 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002204
2205 /* several ways to check the port component :
2206 * - IP => port=+0, relative (IPv4 only)
2207 * - IP: => port=+0, relative
2208 * - IP:N => port=N, absolute
2209 * - IP:+N => port=+N, relative
2210 * - IP:-N => port=-N, relative
2211 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002212 if (!parse_addr)
2213 goto skip_addr;
2214
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002215 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002216 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002217 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002218 err_code |= ERR_ALERT | ERR_FATAL;
2219 goto out;
2220 }
2221
2222 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002223 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002224 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002225 file, linenum, args[0], args[1]);
2226 err_code |= ERR_ALERT | ERR_FATAL;
2227 goto out;
2228 }
2229
2230 if (!port1 || !port2) {
2231 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002232 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002233 }
2234 else if (port1 != port2) {
2235 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002236 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002237 file, linenum, args[0], args[1], args[2]);
2238 err_code |= ERR_ALERT | ERR_FATAL;
2239 goto out;
2240 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002241
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002242 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002243 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002244 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002245 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002246 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2247 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2248 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002249 err_code |= ERR_ALERT | ERR_FATAL;
2250 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002251 }
2252 }
2253 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002254 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002255 file, linenum, newsrv->id);
2256 err_code |= ERR_ALERT | ERR_FATAL;
2257 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002258 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002259 }
2260
Willy Tarreau272adea2014-03-31 10:39:59 +02002261 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002262 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002263
Olivier Houchard8da5f982017-08-04 18:35:36 +02002264 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002265 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002266 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002267 err_code |= ERR_ALERT | ERR_FATAL;
2268 goto out;
2269 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002270
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002271 cur_arg++;
2272 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002273 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002274 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002275 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002276 } else {
2277 newsrv = &curproxy->defsrv;
2278 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002279 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002280 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002281 }
2282
2283 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002284 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002285 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2286 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002287 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002288 file, linenum, *err, newsrv->id);
2289 err_code |= ERR_ALERT | ERR_FATAL;
2290 goto out;
2291 }
2292 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002293 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002294 file, linenum, val, args[cur_arg], newsrv->id);
2295 err_code |= ERR_ALERT | ERR_FATAL;
2296 goto out;
2297 }
2298 newsrv->agent.inter = val;
2299 cur_arg += 2;
2300 }
Misiekea849332017-01-09 09:39:51 +01002301 else if (!strcmp(args[cur_arg], "agent-addr")) {
2302 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002303 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002304 goto out;
2305 }
2306
2307 cur_arg += 2;
2308 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002309 else if (!strcmp(args[cur_arg], "agent-port")) {
2310 global.maxsock++;
2311 newsrv->agent.port = atol(args[cur_arg + 1]);
2312 cur_arg += 2;
2313 }
James Brown55f9ff12015-10-21 18:19:05 -07002314 else if (!strcmp(args[cur_arg], "agent-send")) {
2315 global.maxsock++;
2316 free(newsrv->agent.send_string);
2317 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2318 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2319 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2320 cur_arg += 2;
2321 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002322 else if (!strcmp(args[cur_arg], "init-addr")) {
2323 char *p, *end;
2324 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002325 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002326
2327 newsrv->init_addr_methods = 0;
2328 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2329
2330 for (p = args[cur_arg + 1]; *p; p = end) {
2331 /* cut on next comma */
2332 for (end = p; *end && *end != ','; end++);
2333 if (*end)
2334 *(end++) = 0;
2335
Willy Tarreau4310d362016-11-02 15:05:56 +01002336 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002337 if (!strcmp(p, "libc")) {
2338 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2339 }
2340 else if (!strcmp(p, "last")) {
2341 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2342 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002343 else if (!strcmp(p, "none")) {
2344 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2345 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002346 else if (str2ip2(p, &sa, 0)) {
2347 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002348 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002349 file, linenum, args[cur_arg], p);
2350 err_code |= ERR_ALERT | ERR_FATAL;
2351 goto out;
2352 }
2353 newsrv->init_addr = sa;
2354 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2355 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002356 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002357 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 +02002358 file, linenum, args[cur_arg], p);
2359 err_code |= ERR_ALERT | ERR_FATAL;
2360 goto out;
2361 }
2362 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002363 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002364 file, linenum, args[cur_arg], p);
2365 err_code |= ERR_ALERT | ERR_FATAL;
2366 goto out;
2367 }
2368 }
2369 cur_arg += 2;
2370 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002371 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002372 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002373 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2374 cur_arg += 2;
2375 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002376 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2377 char *p, *end;
2378
2379 for (p = args[cur_arg + 1]; *p; p = end) {
2380 /* cut on next comma */
2381 for (end = p; *end && *end != ','; end++);
2382 if (*end)
2383 *(end++) = 0;
2384
2385 if (!strcmp(p, "allow-dup-ip")) {
2386 newsrv->dns_opts.accept_duplicate_ip = 1;
2387 }
2388 else if (!strcmp(p, "prevent-dup-ip")) {
2389 newsrv->dns_opts.accept_duplicate_ip = 0;
2390 }
2391 else {
2392 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2393 file, linenum, args[cur_arg], p);
2394 err_code |= ERR_ALERT | ERR_FATAL;
2395 goto out;
2396 }
2397 }
2398
2399 cur_arg += 2;
2400 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002401 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2402 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002403 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002404 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002405 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002406 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002407 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002408 file, linenum, args[cur_arg]);
2409 err_code |= ERR_ALERT | ERR_FATAL;
2410 goto out;
2411 }
2412 cur_arg += 2;
2413 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002414 else if (!strcmp(args[cur_arg], "resolve-net")) {
2415 char *p, *e;
2416 unsigned char mask;
2417 struct dns_options *opt;
2418
2419 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002420 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002421 file, linenum, args[cur_arg]);
2422 err_code |= ERR_ALERT | ERR_FATAL;
2423 goto out;
2424 }
2425
2426 opt = &newsrv->dns_opts;
2427
2428 /* Split arguments by comma, and convert it from ipv4 or ipv6
2429 * string network in in_addr or in6_addr.
2430 */
2431 p = args[cur_arg + 1];
2432 e = p;
2433 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002434 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002435 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002436 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002437 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2438 err_code |= ERR_ALERT | ERR_FATAL;
2439 goto out;
2440 }
2441 /* look for end or comma. */
2442 while (*e != ',' && *e != '\0')
2443 e++;
2444 if (*e == ',') {
2445 *e = '\0';
2446 e++;
2447 }
2448 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2449 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2450 /* Try to convert input string from ipv4 or ipv6 network. */
2451 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2452 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2453 &mask)) {
2454 /* Try to convert input string from ipv6 network. */
2455 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2456 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2457 } else {
2458 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002459 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002460 file, linenum, args[cur_arg], p);
2461 err_code |= ERR_ALERT | ERR_FATAL;
2462 goto out;
2463 }
2464 opt->pref_net_nb++;
2465 p = e;
2466 }
2467
2468 cur_arg += 2;
2469 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002470 else if (!strcmp(args[cur_arg], "rise")) {
2471 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002472 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002473 file, linenum, args[cur_arg]);
2474 err_code |= ERR_ALERT | ERR_FATAL;
2475 goto out;
2476 }
2477
2478 newsrv->check.rise = atol(args[cur_arg + 1]);
2479 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002480 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002481 file, linenum, args[cur_arg]);
2482 err_code |= ERR_ALERT | ERR_FATAL;
2483 goto out;
2484 }
2485
2486 if (newsrv->check.health)
2487 newsrv->check.health = newsrv->check.rise;
2488 cur_arg += 2;
2489 }
2490 else if (!strcmp(args[cur_arg], "fall")) {
2491 newsrv->check.fall = atol(args[cur_arg + 1]);
2492
2493 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002494 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002495 file, linenum, args[cur_arg]);
2496 err_code |= ERR_ALERT | ERR_FATAL;
2497 goto out;
2498 }
2499
2500 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002501 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002502 file, linenum, args[cur_arg]);
2503 err_code |= ERR_ALERT | ERR_FATAL;
2504 goto out;
2505 }
2506
2507 cur_arg += 2;
2508 }
2509 else if (!strcmp(args[cur_arg], "inter")) {
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 'inter' 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.inter = val;
2524 cur_arg += 2;
2525 }
2526 else if (!strcmp(args[cur_arg], "fastinter")) {
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 'fastinter' 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.fastinter = val;
2541 cur_arg += 2;
2542 }
2543 else if (!strcmp(args[cur_arg], "downinter")) {
2544 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2545 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002546 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002547 file, linenum, *err, newsrv->id);
2548 err_code |= ERR_ALERT | ERR_FATAL;
2549 goto out;
2550 }
2551 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002552 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002553 file, linenum, val, args[cur_arg], newsrv->id);
2554 err_code |= ERR_ALERT | ERR_FATAL;
2555 goto out;
2556 }
2557 newsrv->check.downinter = val;
2558 cur_arg += 2;
2559 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002560 else if (!strcmp(args[cur_arg], "port")) {
2561 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002562 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002563 cur_arg += 2;
2564 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002565 else if (!strcmp(args[cur_arg], "weight")) {
2566 int w;
2567 w = atol(args[cur_arg + 1]);
2568 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002569 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002570 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2571 err_code |= ERR_ALERT | ERR_FATAL;
2572 goto out;
2573 }
2574 newsrv->uweight = newsrv->iweight = w;
2575 cur_arg += 2;
2576 }
2577 else if (!strcmp(args[cur_arg], "minconn")) {
2578 newsrv->minconn = atol(args[cur_arg + 1]);
2579 cur_arg += 2;
2580 }
2581 else if (!strcmp(args[cur_arg], "maxconn")) {
2582 newsrv->maxconn = atol(args[cur_arg + 1]);
2583 cur_arg += 2;
2584 }
2585 else if (!strcmp(args[cur_arg], "maxqueue")) {
2586 newsrv->maxqueue = atol(args[cur_arg + 1]);
2587 cur_arg += 2;
2588 }
2589 else if (!strcmp(args[cur_arg], "slowstart")) {
2590 /* slowstart is stored in seconds */
2591 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2592 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002593 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002594 file, linenum, *err, newsrv->id);
2595 err_code |= ERR_ALERT | ERR_FATAL;
2596 goto out;
2597 }
2598 newsrv->slowstart = (val + 999) / 1000;
2599 cur_arg += 2;
2600 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002601 else if (!strcmp(args[cur_arg], "on-error")) {
2602 if (!strcmp(args[cur_arg + 1], "fastinter"))
2603 newsrv->onerror = HANA_ONERR_FASTINTER;
2604 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2605 newsrv->onerror = HANA_ONERR_FAILCHK;
2606 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2607 newsrv->onerror = HANA_ONERR_SUDDTH;
2608 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2609 newsrv->onerror = HANA_ONERR_MARKDWN;
2610 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002611 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002612 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2613 file, linenum, args[cur_arg], args[cur_arg + 1]);
2614 err_code |= ERR_ALERT | ERR_FATAL;
2615 goto out;
2616 }
2617
2618 cur_arg += 2;
2619 }
2620 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2621 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2622 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2623 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002624 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002625 file, linenum, args[cur_arg], args[cur_arg + 1]);
2626 err_code |= ERR_ALERT | ERR_FATAL;
2627 goto out;
2628 }
2629
2630 cur_arg += 2;
2631 }
2632 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2633 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2634 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2635 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002636 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002637 file, linenum, args[cur_arg], args[cur_arg + 1]);
2638 err_code |= ERR_ALERT | ERR_FATAL;
2639 goto out;
2640 }
2641
2642 cur_arg += 2;
2643 }
2644 else if (!strcmp(args[cur_arg], "error-limit")) {
2645 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002646 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002647 file, linenum, args[cur_arg]);
2648 err_code |= ERR_ALERT | ERR_FATAL;
2649 goto out;
2650 }
2651
2652 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2653
2654 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002655 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002656 file, linenum, args[cur_arg]);
2657 err_code |= ERR_ALERT | ERR_FATAL;
2658 goto out;
2659 }
2660 cur_arg += 2;
2661 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002662 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002663 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002664 file, linenum, "usesrc", "source");
2665 err_code |= ERR_ALERT | ERR_FATAL;
2666 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002667 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002668 else {
2669 static int srv_dumped;
2670 struct srv_kw *kw;
2671 char *err;
2672
2673 kw = srv_find_kw(args[cur_arg]);
2674 if (kw) {
2675 char *err = NULL;
2676 int code;
2677
2678 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002679 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 +02002680 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002681 if (kw->skip != -1)
2682 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002683 err_code |= ERR_ALERT | ERR_FATAL;
2684 goto out;
2685 }
2686
2687 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002688 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002689 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002690 if (kw->skip != -1)
2691 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002692 err_code |= ERR_ALERT;
2693 continue;
2694 }
2695
2696 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2697 err_code |= code;
2698
2699 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002700 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002701 if (code & ERR_FATAL) {
2702 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002703 if (kw->skip != -1)
2704 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002705 goto out;
2706 }
2707 }
2708 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002709 if (kw->skip != -1)
2710 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002711 continue;
2712 }
2713
2714 err = NULL;
2715 if (!srv_dumped) {
2716 srv_dump_kws(&err);
2717 indent_msg(&err, 4);
2718 srv_dumped = 1;
2719 }
2720
Christopher Faulet767a84b2017-11-24 16:50:31 +01002721 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002722 file, linenum, args[0], args[1], args[cur_arg],
2723 err ? " Registered keywords :" : "", err ? err : "");
2724 free(err);
2725
2726 err_code |= ERR_ALERT | ERR_FATAL;
2727 goto out;
2728 }
2729 }
2730
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002731 if (!defsrv)
2732 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2733 if (err_code & ERR_FATAL)
2734 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002735 if (srv_tmpl)
2736 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002737 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002738 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002739 return 0;
2740
2741 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002742 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002743 free(errmsg);
2744 return err_code;
2745}
2746
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002747/* Returns a pointer to the first server matching either id <id>.
2748 * NULL is returned if no match is found.
2749 * the lookup is performed in the backend <bk>
2750 */
2751struct server *server_find_by_id(struct proxy *bk, int id)
2752{
2753 struct eb32_node *eb32;
2754 struct server *curserver;
2755
2756 if (!bk || (id ==0))
2757 return NULL;
2758
2759 /* <bk> has no backend capabilities, so it can't have a server */
2760 if (!(bk->cap & PR_CAP_BE))
2761 return NULL;
2762
2763 curserver = NULL;
2764
2765 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2766 if (eb32)
2767 curserver = container_of(eb32, struct server, conf.id);
2768
2769 return curserver;
2770}
2771
2772/* Returns a pointer to the first server matching either name <name>, or id
2773 * if <name> starts with a '#'. NULL is returned if no match is found.
2774 * the lookup is performed in the backend <bk>
2775 */
2776struct server *server_find_by_name(struct proxy *bk, const char *name)
2777{
2778 struct server *curserver;
2779
2780 if (!bk || !name)
2781 return NULL;
2782
2783 /* <bk> has no backend capabilities, so it can't have a server */
2784 if (!(bk->cap & PR_CAP_BE))
2785 return NULL;
2786
2787 curserver = NULL;
2788 if (*name == '#') {
2789 curserver = server_find_by_id(bk, atoi(name + 1));
2790 if (curserver)
2791 return curserver;
2792 }
2793 else {
2794 curserver = bk->srv;
2795
2796 while (curserver && (strcmp(curserver->id, name) != 0))
2797 curserver = curserver->next;
2798
2799 if (curserver)
2800 return curserver;
2801 }
2802
2803 return NULL;
2804}
2805
2806struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2807{
2808 struct server *byname;
2809 struct server *byid;
2810
2811 if (!name && !id)
2812 return NULL;
2813
2814 if (diff)
2815 *diff = 0;
2816
2817 byname = byid = NULL;
2818
2819 if (name) {
2820 byname = server_find_by_name(bk, name);
2821 if (byname && (!id || byname->puid == id))
2822 return byname;
2823 }
2824
2825 /* remaining possibilities :
2826 * - name not set
2827 * - name set but not found
2828 * - name found but ID doesn't match
2829 */
2830 if (id) {
2831 byid = server_find_by_id(bk, id);
2832 if (byid) {
2833 if (byname) {
2834 /* use id only if forced by configuration */
2835 if (byid->flags & SRV_F_FORCED_ID) {
2836 if (diff)
2837 *diff |= 2;
2838 return byid;
2839 }
2840 else {
2841 if (diff)
2842 *diff |= 1;
2843 return byname;
2844 }
2845 }
2846
2847 /* remaining possibilities:
2848 * - name not set
2849 * - name set but not found
2850 */
2851 if (name && diff)
2852 *diff |= 2;
2853 return byid;
2854 }
2855
2856 /* id bot found */
2857 if (byname) {
2858 if (diff)
2859 *diff |= 1;
2860 return byname;
2861 }
2862 }
2863
2864 return NULL;
2865}
2866
Willy Tarreau46b7f532018-08-21 11:54:26 +02002867/* Update a server state using the parameters available in the params list.
2868 *
2869 * Grabs the server lock during operation.
2870 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002871static void srv_update_state(struct server *srv, int version, char **params)
2872{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002873 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002874 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002875
2876 /* fields since version 1
2877 * and common to all other upcoming versions
2878 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002879 enum srv_state srv_op_state;
2880 enum srv_admin srv_admin_state;
2881 unsigned srv_uweight, srv_iweight;
2882 unsigned long srv_last_time_change;
2883 short srv_check_status;
2884 enum chk_result srv_check_result;
2885 int srv_check_health;
2886 int srv_check_state, srv_agent_state;
2887 int bk_f_forced_id;
2888 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002889 int fqdn_set_by_cli;
2890 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002891 const char *port_str;
2892 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002893 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002894
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002895 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002896 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002897 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002898 switch (version) {
2899 case 1:
2900 /*
2901 * now we can proceed with server's state update:
2902 * srv_addr: params[0]
2903 * srv_op_state: params[1]
2904 * srv_admin_state: params[2]
2905 * srv_uweight: params[3]
2906 * srv_iweight: params[4]
2907 * srv_last_time_change: params[5]
2908 * srv_check_status: params[6]
2909 * srv_check_result: params[7]
2910 * srv_check_health: params[8]
2911 * srv_check_state: params[9]
2912 * srv_agent_state: params[10]
2913 * bk_f_forced_id: params[11]
2914 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002915 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002916 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002917 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002918 */
2919
2920 /* validating srv_op_state */
2921 p = NULL;
2922 errno = 0;
2923 srv_op_state = strtol(params[1], &p, 10);
2924 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2925 (srv_op_state != SRV_ST_STOPPED &&
2926 srv_op_state != SRV_ST_STARTING &&
2927 srv_op_state != SRV_ST_RUNNING &&
2928 srv_op_state != SRV_ST_STOPPING)) {
2929 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2930 }
2931
2932 /* validating srv_admin_state */
2933 p = NULL;
2934 errno = 0;
2935 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002936 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002937
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002938 /* inherited statuses will be recomputed later.
2939 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2940 */
2941 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002942
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002943 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2944 (srv_admin_state != 0 &&
2945 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002946 srv_admin_state != SRV_ADMF_CMAINT &&
2947 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002948 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002949 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002950 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2951 }
2952
2953 /* validating srv_uweight */
2954 p = NULL;
2955 errno = 0;
2956 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002957 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002958 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2959
2960 /* validating srv_iweight */
2961 p = NULL;
2962 errno = 0;
2963 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002964 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002965 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2966
2967 /* validating srv_last_time_change */
2968 p = NULL;
2969 errno = 0;
2970 srv_last_time_change = strtol(params[5], &p, 10);
2971 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2972 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2973
2974 /* validating srv_check_status */
2975 p = NULL;
2976 errno = 0;
2977 srv_check_status = strtol(params[6], &p, 10);
2978 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2979 (srv_check_status >= HCHK_STATUS_SIZE))
2980 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2981
2982 /* validating srv_check_result */
2983 p = NULL;
2984 errno = 0;
2985 srv_check_result = strtol(params[7], &p, 10);
2986 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2987 (srv_check_result != CHK_RES_UNKNOWN &&
2988 srv_check_result != CHK_RES_NEUTRAL &&
2989 srv_check_result != CHK_RES_FAILED &&
2990 srv_check_result != CHK_RES_PASSED &&
2991 srv_check_result != CHK_RES_CONDPASS)) {
2992 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2993 }
2994
2995 /* validating srv_check_health */
2996 p = NULL;
2997 errno = 0;
2998 srv_check_health = strtol(params[8], &p, 10);
2999 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3000 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3001
3002 /* validating srv_check_state */
3003 p = NULL;
3004 errno = 0;
3005 srv_check_state = strtol(params[9], &p, 10);
3006 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3007 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3008 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3009
3010 /* validating srv_agent_state */
3011 p = NULL;
3012 errno = 0;
3013 srv_agent_state = strtol(params[10], &p, 10);
3014 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3015 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3016 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3017
3018 /* validating bk_f_forced_id */
3019 p = NULL;
3020 errno = 0;
3021 bk_f_forced_id = strtol(params[11], &p, 10);
3022 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3023 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3024
3025 /* validating srv_f_forced_id */
3026 p = NULL;
3027 errno = 0;
3028 srv_f_forced_id = strtol(params[12], &p, 10);
3029 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3030 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3031
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003032 /* validating srv_fqdn */
3033 fqdn = params[13];
3034 if (fqdn && *fqdn == '-')
3035 fqdn = NULL;
3036 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3037 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3038 fqdn = NULL;
3039 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003040
Frédéric Lécaille31694712017-08-01 08:47:19 +02003041 port_str = params[14];
3042 if (port_str) {
3043 port = strl2uic(port_str, strlen(port_str));
3044 if (port > USHRT_MAX) {
3045 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3046 port_str = NULL;
3047 }
3048 }
3049
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003050 /* SRV record
3051 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3052 */
3053 srvrecord = params[15];
3054 if (srvrecord && *srvrecord != '_')
3055 srvrecord = NULL;
3056
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003057 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003058 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003059 goto out;
3060
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003061 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003062 /* recover operational state and apply it to this server
3063 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003064 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003065 switch (srv_op_state) {
3066 case SRV_ST_STOPPED:
3067 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003068 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003069 break;
3070 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003071 /* If rise == 1 there is no STARTING state, let's switch to
3072 * RUNNING
3073 */
3074 if (srv->check.rise == 1) {
3075 srv->check.health = srv->check.rise + srv->check.fall - 1;
3076 srv_set_running(srv, "", NULL);
3077 break;
3078 }
3079 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3080 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003081 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003082 break;
3083 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003084 /* If fall == 1 there is no STOPPING state, let's switch to
3085 * STOPPED
3086 */
3087 if (srv->check.fall == 1) {
3088 srv->check.health = 0;
3089 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3090 break;
3091 }
3092 if (srv->check.health < srv->check.rise ||
3093 srv->check.health > srv->check.rise + srv->check.fall - 2)
3094 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003095 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003096 break;
3097 case SRV_ST_RUNNING:
3098 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003099 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003100 break;
3101 }
3102
3103 /* When applying server state, the following rules apply:
3104 * - in case of a configuration change, we apply the setting from the new
3105 * configuration, regardless of old running state
3106 * - if no configuration change, we apply old running state only if old running
3107 * state is different from new configuration state
3108 */
3109 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003110 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3111 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003112 srv_adm_set_maint(srv);
3113 else
3114 srv_adm_set_ready(srv);
3115 }
3116 /* configuration is the same, let's compate old running state and new conf state */
3117 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003118 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003119 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003120 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003121 srv_adm_set_ready(srv);
3122 }
3123 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003124 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003125 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003126 * (srv->iweight is the weight set up in configuration).
3127 * There are two possible reasons for FDRAIN to have been present :
3128 * - previous config weight was zero
3129 * - "set server b/s drain" was sent to the CLI
3130 *
3131 * In the first case, we simply want to drop this drain state
3132 * if the new weight is not zero anymore, meaning the administrator
3133 * has intentionally turned the weight back to a positive value to
3134 * enable the server again after an operation. In the second case,
3135 * the drain state was forced on the CLI regardless of the config's
3136 * weight so we don't want a change to the config weight to lose this
3137 * status. What this means is :
3138 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3139 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003140 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003141 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003142 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003143 }
3144
3145 srv->last_change = date.tv_sec - srv_last_time_change;
3146 srv->check.status = srv_check_status;
3147 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003148
3149 /* Only case we want to apply is removing ENABLED flag which could have been
3150 * done by the "disable health" command over the stats socket
3151 */
3152 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3153 (srv_check_state & CHK_ST_CONFIGURED) &&
3154 !(srv_check_state & CHK_ST_ENABLED))
3155 srv->check.state &= ~CHK_ST_ENABLED;
3156
3157 /* Only case we want to apply is removing ENABLED flag which could have been
3158 * done by the "disable agent" command over the stats socket
3159 */
3160 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3161 (srv_agent_state & CHK_ST_CONFIGURED) &&
3162 !(srv_agent_state & CHK_ST_ENABLED))
3163 srv->agent.state &= ~CHK_ST_ENABLED;
3164
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003165 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3166 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003167 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003168 * It means that a configuration file change has precedence over a unix socket change
3169 * for server's weight
3170 *
3171 * by default, HAProxy applies the following weight when parsing the configuration
3172 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003173 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003174 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003175 srv->uweight = srv_uweight;
3176 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003177 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003178
Willy Tarreaue5a60682016-11-09 14:54:53 +01003179 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003180 if (strcmp(params[0], "-"))
3181 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003182
3183 if (fqdn && srv->hostname) {
3184 if (!strcmp(srv->hostname, fqdn)) {
3185 /* Here we reset the 'set from stats socket FQDN' flag
3186 * to support such transitions:
3187 * Let's say initial FQDN value is foo1 (in configuration file).
3188 * - FQDN changed from stats socket, from foo1 to foo2 value,
3189 * - FQDN changed again from file configuration (with the same previous value
3190 set from stats socket, from foo1 to foo2 value),
3191 * - reload for any other reason than a FQDN modification,
3192 * the configuration file FQDN matches the fqdn server state file value.
3193 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003194 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003195 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003196 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003197 }
3198 else {
3199 /* If the FDQN has been changed from stats socket,
3200 * apply fqdn state file value (which is the value set
3201 * from stats socket).
3202 */
3203 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003204 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003205 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003206 }
3207 }
3208 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003209 /* If all the conditions below are validated, this means
3210 * we're evaluating a server managed by SRV resolution
3211 */
3212 else if (fqdn && !srv->hostname && srvrecord) {
3213 int res;
3214
3215 /* we can't apply previous state if SRV record has changed */
3216 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3217 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);
3218 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3219 goto out;
3220 }
3221
3222 /* create or find a SRV resolution for this srv record */
3223 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3224 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3225 if (srv->srvrq == NULL) {
3226 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3227 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3228 goto out;
3229 }
3230
3231 /* prepare DNS resolution for this server */
3232 res = srv_prepare_for_resolution(srv, fqdn);
3233 if (res == -1) {
3234 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3235 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3236 goto out;
3237 }
3238
3239 /* configure check.port accordingly */
3240 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3241 !(srv->flags & SRV_F_CHECKPORT))
3242 srv->check.port = port;
3243
3244 /* Unset SRV_F_MAPPORTS for SRV records.
3245 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3246 * because no ports are provided in the configuration file.
3247 * This is because HAProxy will use the port found into the SRV record.
3248 */
3249 srv->flags &= ~SRV_F_MAPPORTS;
3250 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003251
Frédéric Lécaille31694712017-08-01 08:47:19 +02003252 if (port_str)
3253 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003254 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003255
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003256 break;
3257 default:
3258 chunk_appendf(msg, ", version '%d' not supported", version);
3259 }
3260
3261 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003262 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003263 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003264 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003265 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003266 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003267}
3268
3269/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3270 * For each proxy, it does the following:
3271 * - opens its server state file (either one or local one)
3272 * - read whole file, line by line
3273 * - analyse each line to check if it matches our current backend:
3274 * - backend name matches
3275 * - backend id matches if id is forced and name doesn't match
3276 * - if the server pointed by the line is found, then state is applied
3277 *
3278 * If the running backend uuid or id differs from the state file, then HAProxy reports
3279 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003280 *
3281 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003282 */
3283void apply_server_state(void)
3284{
3285 char *cur, *end;
3286 char mybuf[SRV_STATE_LINE_MAXLEN];
3287 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003288 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3289 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003290 int arg, srv_arg, version, diff;
3291 FILE *f;
3292 char *filepath;
3293 char globalfilepath[MAXPATHLEN + 1];
3294 char localfilepath[MAXPATHLEN + 1];
3295 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003296 struct proxy *curproxy, *bk;
3297 struct server *srv;
3298
3299 globalfilepathlen = 0;
3300 /* create the globalfilepath variable */
3301 if (global.server_state_file) {
3302 /* absolute path or no base directory provided */
3303 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3304 len = strlen(global.server_state_file);
3305 if (len > MAXPATHLEN) {
3306 globalfilepathlen = 0;
3307 goto globalfileerror;
3308 }
3309 memcpy(globalfilepath, global.server_state_file, len);
3310 globalfilepath[len] = '\0';
3311 globalfilepathlen = len;
3312 }
3313 else if (global.server_state_base) {
3314 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003315 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003316 globalfilepathlen = 0;
3317 goto globalfileerror;
3318 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003319 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003320 globalfilepath[len] = 0;
3321 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003322
3323 /* append a slash if needed */
3324 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3325 if (globalfilepathlen + 1 > MAXPATHLEN) {
3326 globalfilepathlen = 0;
3327 goto globalfileerror;
3328 }
3329 globalfilepath[globalfilepathlen++] = '/';
3330 }
3331
3332 len = strlen(global.server_state_file);
3333 if (globalfilepathlen + len > MAXPATHLEN) {
3334 globalfilepathlen = 0;
3335 goto globalfileerror;
3336 }
3337 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3338 globalfilepathlen += len;
3339 globalfilepath[globalfilepathlen++] = 0;
3340 }
3341 }
3342 globalfileerror:
3343 if (globalfilepathlen == 0)
3344 globalfilepath[0] = '\0';
3345
3346 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003347 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003348 /* servers are only in backends */
3349 if (!(curproxy->cap & PR_CAP_BE))
3350 continue;
3351 fileopenerr = 0;
3352 filepath = NULL;
3353
3354 /* search server state file path and name */
3355 switch (curproxy->load_server_state_from_file) {
3356 /* read servers state from global file */
3357 case PR_SRV_STATE_FILE_GLOBAL:
3358 /* there was an error while generating global server state file path */
3359 if (globalfilepathlen == 0)
3360 continue;
3361 filepath = globalfilepath;
3362 fileopenerr = 1;
3363 break;
3364 /* this backend has its own file */
3365 case PR_SRV_STATE_FILE_LOCAL:
3366 localfilepathlen = 0;
3367 localfilepath[0] = '\0';
3368 len = 0;
3369 /* create the localfilepath variable */
3370 /* absolute path or no base directory provided */
3371 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3372 len = strlen(curproxy->server_state_file_name);
3373 if (len > MAXPATHLEN) {
3374 localfilepathlen = 0;
3375 goto localfileerror;
3376 }
3377 memcpy(localfilepath, curproxy->server_state_file_name, len);
3378 localfilepath[len] = '\0';
3379 localfilepathlen = len;
3380 }
3381 else if (global.server_state_base) {
3382 len = strlen(global.server_state_base);
3383 localfilepathlen += len;
3384
3385 if (localfilepathlen > MAXPATHLEN) {
3386 localfilepathlen = 0;
3387 goto localfileerror;
3388 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003389 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003390 localfilepath[localfilepathlen] = 0;
3391
3392 /* append a slash if needed */
3393 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3394 if (localfilepathlen + 1 > MAXPATHLEN) {
3395 localfilepathlen = 0;
3396 goto localfileerror;
3397 }
3398 localfilepath[localfilepathlen++] = '/';
3399 }
3400
3401 len = strlen(curproxy->server_state_file_name);
3402 if (localfilepathlen + len > MAXPATHLEN) {
3403 localfilepathlen = 0;
3404 goto localfileerror;
3405 }
3406 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3407 localfilepathlen += len;
3408 localfilepath[localfilepathlen++] = 0;
3409 }
3410 filepath = localfilepath;
3411 localfileerror:
3412 if (localfilepathlen == 0)
3413 localfilepath[0] = '\0';
3414
3415 break;
3416 case PR_SRV_STATE_FILE_NONE:
3417 default:
3418 continue;
3419 }
3420
3421 /* preload global state file */
3422 errno = 0;
3423 f = fopen(filepath, "r");
3424 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003425 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003426 if (!f)
3427 continue;
3428
3429 mybuf[0] = '\0';
3430 mybuflen = 0;
3431 version = 0;
3432
3433 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003434 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003435 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003436 goto fileclose;
3437 }
3438
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003439 cur = mybuf;
3440 version = atoi(cur);
3441 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3442 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003443 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003444
3445 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3446 int bk_f_forced_id = 0;
3447 int check_id = 0;
3448 int check_name = 0;
3449
3450 mybuflen = strlen(mybuf);
3451 cur = mybuf;
3452 end = cur + mybuflen;
3453
3454 bk = NULL;
3455 srv = NULL;
3456
3457 /* we need at least one character */
3458 if (mybuflen == 0)
3459 continue;
3460
3461 /* ignore blank characters at the beginning of the line */
3462 while (isspace(*cur))
3463 ++cur;
3464
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003465 /* Ignore empty or commented lines */
3466 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003467 continue;
3468
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003469 /* truncated lines */
3470 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003471 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003472 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003473 }
3474
3475 /* Removes trailing '\n' */
3476 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003477
3478 /* we're now ready to move the line into *srv_params[] */
3479 params[0] = cur;
3480 arg = 1;
3481 srv_arg = 0;
3482 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3483 if (isspace(*cur)) {
3484 *cur = '\0';
3485 ++cur;
3486 while (isspace(*cur))
3487 ++cur;
3488 switch (version) {
3489 case 1:
3490 /*
3491 * srv_addr: params[4] => srv_params[0]
3492 * srv_op_state: params[5] => srv_params[1]
3493 * srv_admin_state: params[6] => srv_params[2]
3494 * srv_uweight: params[7] => srv_params[3]
3495 * srv_iweight: params[8] => srv_params[4]
3496 * srv_last_time_change: params[9] => srv_params[5]
3497 * srv_check_status: params[10] => srv_params[6]
3498 * srv_check_result: params[11] => srv_params[7]
3499 * srv_check_health: params[12] => srv_params[8]
3500 * srv_check_state: params[13] => srv_params[9]
3501 * srv_agent_state: params[14] => srv_params[10]
3502 * bk_f_forced_id: params[15] => srv_params[11]
3503 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003504 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003505 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003506 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003507 */
3508 if (arg >= 4) {
3509 srv_params[srv_arg] = cur;
3510 ++srv_arg;
3511 }
3512 break;
3513 }
3514
3515 params[arg] = cur;
3516 ++arg;
3517 }
3518 else {
3519 ++cur;
3520 }
3521 }
3522
3523 /* if line is incomplete line, then ignore it.
3524 * otherwise, update useful flags */
3525 switch (version) {
3526 case 1:
3527 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3528 continue;
3529 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3530 check_id = (atoi(params[0]) == curproxy->uuid);
3531 check_name = (strcmp(curproxy->id, params[1]) == 0);
3532 break;
3533 }
3534
3535 diff = 0;
3536 bk = curproxy;
3537
3538 /* if backend can't be found, let's continue */
3539 if (!check_id && !check_name)
3540 continue;
3541 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003542 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 +02003543 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3544 }
3545 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003546 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 +02003547 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3548 /* if name doesn't match, we still want to update curproxy if the backend id
3549 * was forced in previous the previous configuration */
3550 if (!bk_f_forced_id)
3551 continue;
3552 }
3553
3554 /* look for the server by its id: param[2] */
3555 /* else look for the server by its name: param[3] */
3556 diff = 0;
3557 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3558
3559 if (!srv) {
3560 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003561 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3562 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003563 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3564 params[3], params[2], params[0], params[1]);
3565 continue;
3566 }
3567 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003568 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 +02003569 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 +02003570 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003571 }
3572 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003573 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 +02003574 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 +02003575 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003576 }
3577
3578 /* now we can proceed with server's state update */
3579 srv_update_state(srv, version, srv_params);
3580 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003581fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003582 fclose(f);
3583 }
3584}
3585
Simon Horman7d09b9a2013-02-12 10:45:51 +09003586/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003587 * update a server's current IP address.
3588 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3589 * ip is in network format.
3590 * updater is a string which contains an information about the requester of the update.
3591 * updater is used if not NULL.
3592 *
3593 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003594 *
3595 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003596 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003597int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003598{
3599 /* generates a log line and a warning on stderr */
3600 if (1) {
3601 /* book enough space for both IPv4 and IPv6 */
3602 char oldip[INET6_ADDRSTRLEN];
3603 char newip[INET6_ADDRSTRLEN];
3604
3605 memset(oldip, '\0', INET6_ADDRSTRLEN);
3606 memset(newip, '\0', INET6_ADDRSTRLEN);
3607
3608 /* copy old IP address in a string */
3609 switch (s->addr.ss_family) {
3610 case AF_INET:
3611 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3612 break;
3613 case AF_INET6:
3614 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3615 break;
3616 };
3617
3618 /* copy new IP address in a string */
3619 switch (ip_sin_family) {
3620 case AF_INET:
3621 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3622 break;
3623 case AF_INET6:
3624 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3625 break;
3626 };
3627
3628 /* save log line into a buffer */
3629 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3630 s->proxy->id, s->id, oldip, newip, updater);
3631
3632 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003633 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003634
3635 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003636 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003637 }
3638
3639 /* save the new IP family */
3640 s->addr.ss_family = ip_sin_family;
3641 /* save the new IP address */
3642 switch (ip_sin_family) {
3643 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003644 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003645 break;
3646 case AF_INET6:
3647 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3648 break;
3649 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003650 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003651
3652 return 0;
3653}
3654
3655/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003656 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3657 *
3658 * Caller can pass its name through <updater> to get it integrated in the response message
3659 * returned by the function.
3660 *
3661 * The function first does the following, in that order:
3662 * - validates the new addr and/or port
3663 * - checks if an update is required (new IP or port is different than current ones)
3664 * - checks the update is allowed:
3665 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3666 * - allow all changes if no CHECKS are configured
3667 * - if CHECK is configured:
3668 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3669 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3670 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003671 *
3672 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003673 */
3674const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3675{
3676 struct sockaddr_storage sa;
3677 int ret, port_change_required;
3678 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003679 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003680 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003681 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003682
3683 msg = get_trash_chunk();
3684 chunk_reset(msg);
3685
3686 if (addr) {
3687 memset(&sa, 0, sizeof(struct sockaddr_storage));
3688 if (str2ip2(addr, &sa, 0) == NULL) {
3689 chunk_printf(msg, "Invalid addr '%s'", addr);
3690 goto out;
3691 }
3692
3693 /* changes are allowed on AF_INET* families only */
3694 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3695 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3696 goto out;
3697 }
3698
3699 /* collecting data currently setup */
3700 memset(current_addr, '\0', sizeof(current_addr));
3701 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3702 /* changes are allowed on AF_INET* families only */
3703 if ((ret != AF_INET) && (ret != AF_INET6)) {
3704 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3705 goto out;
3706 }
3707
3708 /* applying ADDR changes if required and allowed
3709 * ipcmp returns 0 when both ADDR are the same
3710 */
3711 if (ipcmp(&s->addr, &sa) == 0) {
3712 chunk_appendf(msg, "no need to change the addr");
3713 goto port;
3714 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003715 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003716 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003717
3718 /* we also need to update check's ADDR only if it uses the server's one */
3719 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003720 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003721 }
3722
3723 /* we also need to update agent ADDR only if it use the server's one */
3724 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003725 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003726 }
3727
3728 /* update report for caller */
3729 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3730 }
3731
3732 port:
3733 if (port) {
3734 char sign = '\0';
3735 char *endptr;
3736
3737 if (addr)
3738 chunk_appendf(msg, ", ");
3739
3740 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003741 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003742
3743 /* check if PORT change is required */
3744 port_change_required = 0;
3745
3746 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003747 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003748 new_port = strtol(port, &endptr, 10);
3749 if ((errno != 0) || (port == endptr)) {
3750 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3751 goto out;
3752 }
3753
3754 /* check if caller triggers a port mapped or offset */
3755 if (sign == '-' || (sign == '+')) {
3756 /* check if server currently uses port map */
3757 if (!(s->flags & SRV_F_MAPPORTS)) {
3758 /* switch from fixed port to port map mandatorily triggers
3759 * a port change */
3760 port_change_required = 1;
3761 /* check is configured
3762 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3763 * prevent PORT change if check doesn't have it's dedicated port while switching
3764 * to port mapping */
3765 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3766 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.");
3767 goto out;
3768 }
3769 }
3770 /* we're already using port maps */
3771 else {
3772 port_change_required = current_port != new_port;
3773 }
3774 }
3775 /* fixed port */
3776 else {
3777 port_change_required = current_port != new_port;
3778 }
3779
3780 /* applying PORT changes if required and update response message */
3781 if (port_change_required) {
3782 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003783 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003784 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003785
3786 /* prepare message */
3787 chunk_appendf(msg, "port changed from '");
3788 if (s->flags & SRV_F_MAPPORTS)
3789 chunk_appendf(msg, "+");
3790 chunk_appendf(msg, "%d' to '", current_port);
3791
3792 if (sign == '-') {
3793 s->flags |= SRV_F_MAPPORTS;
3794 chunk_appendf(msg, "%c", sign);
3795 /* just use for result output */
3796 new_port = -new_port;
3797 }
3798 else if (sign == '+') {
3799 s->flags |= SRV_F_MAPPORTS;
3800 chunk_appendf(msg, "%c", sign);
3801 }
3802 else {
3803 s->flags &= ~SRV_F_MAPPORTS;
3804 }
3805
3806 chunk_appendf(msg, "%d'", new_port);
3807
3808 /* we also need to update health checks port only if it uses server's realport */
3809 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3810 s->check.port = new_port;
3811 }
3812 }
3813 else {
3814 chunk_appendf(msg, "no need to change the port");
3815 }
3816 }
3817
3818out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003819 if (changed)
3820 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003821 if (updater)
3822 chunk_appendf(msg, " by '%s'", updater);
3823 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003824 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003825}
3826
3827
3828/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003829 * update server status based on result of name resolution
3830 * returns:
3831 * 0 if server status is updated
3832 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003833 *
3834 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003835 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003836int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003837{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003838 struct dns_resolvers *resolvers = s->resolvers;
3839 struct dns_resolution *resolution = s->dns_requester->resolution;
3840 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003841
3842 switch (resolution->status) {
3843 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003844 /* status when HAProxy has just (re)started.
3845 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003846 break;
3847
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003848 case RSLV_STATUS_VALID:
3849 /*
3850 * resume health checks
3851 * server will be turned back on if health check is safe
3852 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003853 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003854 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003855 return 1;
3856 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3857 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003858 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003859 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003860
Emeric Brun52a91d32017-08-31 14:41:55 +02003861 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003862 return 1;
3863 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3864 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3865 s->proxy->id, s->id);
3866
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003867 ha_warning("%s.\n", trash.area);
3868 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003869 return 0;
3870
3871 case RSLV_STATUS_NX:
3872 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003873 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3874 if (!tick_is_expired(exp, now_ms))
3875 break;
3876
3877 if (s->next_admin & SRV_ADMF_RMAINT)
3878 return 1;
3879 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3880 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003881
3882 case RSLV_STATUS_TIMEOUT:
3883 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003884 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3885 if (!tick_is_expired(exp, now_ms))
3886 break;
3887
3888 if (s->next_admin & SRV_ADMF_RMAINT)
3889 return 1;
3890 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3891 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003892
3893 case RSLV_STATUS_REFUSED:
3894 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003895 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3896 if (!tick_is_expired(exp, now_ms))
3897 break;
3898
3899 if (s->next_admin & SRV_ADMF_RMAINT)
3900 return 1;
3901 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3902 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003903
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003904 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003905 /* stop server if resolution failed for a long enough period */
3906 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3907 if (!tick_is_expired(exp, now_ms))
3908 break;
3909
3910 if (s->next_admin & SRV_ADMF_RMAINT)
3911 return 1;
3912 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3913 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003914 }
3915
3916 return 1;
3917}
3918
3919/*
3920 * Server Name Resolution valid response callback
3921 * It expects:
3922 * - <nameserver>: the name server which answered the valid response
3923 * - <response>: buffer containing a valid DNS response
3924 * - <response_len>: size of <response>
3925 * It performs the following actions:
3926 * - ignore response if current ip found and server family not met
3927 * - update with first new ip found if family is met and current IP is not found
3928 * returns:
3929 * 0 on error
3930 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003931 *
3932 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003933 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003934int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003935{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003936 struct server *s = NULL;
3937 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003938 void *serverip, *firstip;
3939 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003940 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003941 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003942 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003943
Christopher Faulet67957bd2017-09-27 11:00:59 +02003944 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003945 if (!s)
3946 return 1;
3947
Christopher Faulet67957bd2017-09-27 11:00:59 +02003948 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003949
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003950 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003951 firstip = NULL; /* pointer to the first valid response found */
3952 /* it will be used as the new IP if a change is required */
3953 firstip_sin_family = AF_UNSPEC;
3954 serverip = NULL; /* current server IP address */
3955
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003956 /* initializing server IP pointer */
3957 server_sin_family = s->addr.ss_family;
3958 switch (server_sin_family) {
3959 case AF_INET:
3960 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3961 break;
3962
3963 case AF_INET6:
3964 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3965 break;
3966
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003967 case AF_UNSPEC:
3968 break;
3969
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003970 default:
3971 goto invalid;
3972 }
3973
Baptiste Assmann729c9012017-05-22 15:13:10 +02003974 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003975 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003976 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003977
3978 switch (ret) {
3979 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003980 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003981
3982 case DNS_UPD_SRVIP_NOT_FOUND:
3983 goto save_ip;
3984
3985 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003986 goto invalid;
3987
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003988 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003989 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003990 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003991
Baptiste Assmannfad03182015-10-28 02:03:32 +01003992 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003993 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003994 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003995 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003996
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003997 default:
3998 goto invalid;
3999
4000 }
4001
4002 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004003 if (nameserver) {
4004 nameserver->counters.update++;
4005 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004006 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004007 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004008 else
4009 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004010 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004011
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004012 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004013 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004014 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004015
4016 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004017 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02004018 nameserver->counters.invalid++;
4019 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004020 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004021 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004022 return 0;
4023}
4024
4025/*
4026 * Server Name Resolution error management callback
4027 * returns:
4028 * 0 on error
4029 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004030 *
4031 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004032 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004033int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004034{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004035 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004036
Christopher Faulet67957bd2017-09-27 11:00:59 +02004037 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004038 if (!s)
4039 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004040 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004041 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004042 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004043 return 1;
4044}
4045
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004046/*
4047 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004048 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004049 * It returns a pointer to the first server found or NULL if <ip> is not yet
4050 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004051 *
4052 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004053 */
4054struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4055{
4056 struct server *tmpsrv;
4057 struct proxy *be;
4058
4059 if (!srv)
4060 return NULL;
4061
4062 be = srv->proxy;
4063 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004064 /* we found the current server is the same, ignore it */
4065 if (srv == tmpsrv)
4066 continue;
4067
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004068 /* We want to compare the IP in the record with the IP of the servers in the
4069 * same backend, only if:
4070 * * DNS resolution is enabled on the server
4071 * * the hostname used for the resolution by our server is the same than the
4072 * one used for the server found in the backend
4073 * * the server found in the backend is not our current server
4074 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004075 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004076 if ((tmpsrv->hostname_dn == NULL) ||
4077 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4078 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004079 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004080 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004081 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004082 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004083
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004084 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004085 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004086 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004087 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004088 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004089
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004090 /* At this point, we have 2 different servers using the same DNS hostname
4091 * for their respective resolution.
4092 */
4093 if (*ip_family == tmpsrv->addr.ss_family &&
4094 ((tmpsrv->addr.ss_family == AF_INET &&
4095 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4096 (tmpsrv->addr.ss_family == AF_INET6 &&
4097 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004098 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004099 return tmpsrv;
4100 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004101 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004102 }
4103
Emeric Brune9fd6b52017-11-02 17:20:39 +01004104
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004105 return NULL;
4106}
4107
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004108/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4109 * resolver. This is suited for initial address configuration. Returns 0 on
4110 * success otherwise a non-zero error code. In case of error, *err_code, if
4111 * not NULL, is filled up.
4112 */
4113int srv_set_addr_via_libc(struct server *srv, int *err_code)
4114{
4115 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004116 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004117 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004118 return 1;
4119 }
4120 return 0;
4121}
4122
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004123/* Set the server's FDQN (->hostname) from <hostname>.
4124 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004125 *
4126 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004127 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004128int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004129{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004130 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004131 char *hostname_dn;
4132 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004133
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004134 /* Note that the server lock is already held. */
4135 if (!srv->resolvers)
4136 return -1;
4137
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004138 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004139 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004140 /* run time DNS resolution was not active for this server
4141 * and we can't enable it at run time for now.
4142 */
4143 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004144 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004145
4146 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004147 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004148 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004149 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4150 hostname_dn, trash.size);
4151 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004152 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004153
Christopher Faulet67957bd2017-09-27 11:00:59 +02004154 resolution = srv->dns_requester->resolution;
4155 if (resolution &&
4156 resolution->hostname_dn &&
4157 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004158 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004159
Christopher Faulet67957bd2017-09-27 11:00:59 +02004160 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004161
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004162 free(srv->hostname);
4163 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004164 srv->hostname = strdup(hostname);
4165 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004166 srv->hostname_dn_len = hostname_dn_len;
4167 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004168 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004169
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004170 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004171 goto err;
4172
4173 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004174 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004175 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004176 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004177
4178 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004179 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004180 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004181 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004182}
4183
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004184/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4185 * from the state file. This is suited for initial address configuration.
4186 * Returns 0 on success otherwise a non-zero error code. In case of error,
4187 * *err_code, if not NULL, is filled up.
4188 */
4189static int srv_apply_lastaddr(struct server *srv, int *err_code)
4190{
4191 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4192 if (err_code)
4193 *err_code |= ERR_WARN;
4194 return 1;
4195 }
4196 return 0;
4197}
4198
Willy Tarreau25e51522016-11-04 15:10:17 +01004199/* returns 0 if no error, otherwise a combination of ERR_* flags */
4200static int srv_iterate_initaddr(struct server *srv)
4201{
4202 int return_code = 0;
4203 int err_code;
4204 unsigned int methods;
4205
4206 methods = srv->init_addr_methods;
4207 if (!methods) { // default to "last,libc"
4208 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4209 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4210 }
4211
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004212 /* "-dr" : always append "none" so that server addresses resolution
4213 * failures are silently ignored, this is convenient to validate some
4214 * configs out of their environment.
4215 */
4216 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4217 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4218
Willy Tarreau25e51522016-11-04 15:10:17 +01004219 while (methods) {
4220 err_code = 0;
4221 switch (srv_get_next_initaddr(&methods)) {
4222 case SRV_IADDR_LAST:
4223 if (!srv->lastaddr)
4224 continue;
4225 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004226 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004227 return_code |= err_code;
4228 break;
4229
4230 case SRV_IADDR_LIBC:
4231 if (!srv->hostname)
4232 continue;
4233 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004234 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004235 return_code |= err_code;
4236 break;
4237
Willy Tarreau37ebe122016-11-04 15:17:58 +01004238 case SRV_IADDR_NONE:
4239 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004240 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004241 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4242 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004243 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004244 return return_code;
4245
Willy Tarreau4310d362016-11-02 15:05:56 +01004246 case SRV_IADDR_IP:
4247 ipcpy(&srv->init_addr, &srv->addr);
4248 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004249 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4250 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004251 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004252 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004253
Willy Tarreau25e51522016-11-04 15:10:17 +01004254 default: /* unhandled method */
4255 break;
4256 }
4257 }
4258
4259 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004260 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004261 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4262 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004263 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004264 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004265 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4266 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004267
4268 return_code |= ERR_ALERT | ERR_FATAL;
4269 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004270out:
4271 srv_set_dyncookie(srv);
4272 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004273}
4274
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004275/*
4276 * This function parses all backends and all servers within each backend
4277 * and performs servers' addr resolution based on information provided by:
4278 * - configuration file
4279 * - server-state file (states provided by an 'old' haproxy process)
4280 *
4281 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4282 */
4283int srv_init_addr(void)
4284{
4285 struct proxy *curproxy;
4286 int return_code = 0;
4287
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004288 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004289 while (curproxy) {
4290 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004291
4292 /* servers are in backend only */
4293 if (!(curproxy->cap & PR_CAP_BE))
4294 goto srv_init_addr_next;
4295
Willy Tarreau25e51522016-11-04 15:10:17 +01004296 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004297 if (srv->hostname)
4298 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004299
4300 srv_init_addr_next:
4301 curproxy = curproxy->next;
4302 }
4303
4304 return return_code;
4305}
4306
Willy Tarreau46b7f532018-08-21 11:54:26 +02004307/*
4308 * Must be called with the server lock held.
4309 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004310const 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 +02004311{
4312
Willy Tarreau83061a82018-07-13 11:56:34 +02004313 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004314
4315 msg = get_trash_chunk();
4316 chunk_reset(msg);
4317
Olivier Houchard8da5f982017-08-04 18:35:36 +02004318 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004319 chunk_appendf(msg, "no need to change the FDQN");
4320 goto out;
4321 }
4322
4323 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4324 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4325 goto out;
4326 }
4327
4328 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4329 server->proxy->id, server->id, server->hostname, fqdn);
4330
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004331 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004332 chunk_reset(msg);
4333 chunk_appendf(msg, "could not update %s/%s FQDN",
4334 server->proxy->id, server->id);
4335 goto out;
4336 }
4337
4338 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004339 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004340
4341 out:
4342 if (updater)
4343 chunk_appendf(msg, " by '%s'", updater);
4344 chunk_appendf(msg, "\n");
4345
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004346 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004347}
4348
4349
Willy Tarreau21b069d2016-11-23 17:15:08 +01004350/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4351 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004352 * 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 +01004353 * used for CLI commands requiring a server name.
4354 * Important: the <arg> is modified to remove the '/'.
4355 */
4356struct server *cli_find_server(struct appctx *appctx, char *arg)
4357{
4358 struct proxy *px;
4359 struct server *sv;
4360 char *line;
4361
4362 /* split "backend/server" and make <line> point to server */
4363 for (line = arg; *line; line++)
4364 if (*line == '/') {
4365 *line++ = '\0';
4366 break;
4367 }
4368
4369 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004370 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004371 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004372 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004373 return NULL;
4374 }
4375
4376 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004377 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004378 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004379 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004380 return NULL;
4381 }
4382
4383 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004384 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004385 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004386 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004387 return NULL;
4388 }
4389
4390 return sv;
4391}
4392
William Lallemand222baf22016-11-19 02:00:33 +01004393
Willy Tarreau46b7f532018-08-21 11:54:26 +02004394/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004395static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004396{
4397 struct server *sv;
4398 const char *warning;
4399
4400 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4401 return 1;
4402
4403 sv = cli_find_server(appctx, args[2]);
4404 if (!sv)
4405 return 1;
4406
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004407 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004408
William Lallemand222baf22016-11-19 02:00:33 +01004409 if (strcmp(args[3], "weight") == 0) {
4410 warning = server_parse_weight_change_request(sv, args[4]);
4411 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004412 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004413 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004414 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004415 }
4416 }
4417 else if (strcmp(args[3], "state") == 0) {
4418 if (strcmp(args[4], "ready") == 0)
4419 srv_adm_set_ready(sv);
4420 else if (strcmp(args[4], "drain") == 0)
4421 srv_adm_set_drain(sv);
4422 else if (strcmp(args[4], "maint") == 0)
4423 srv_adm_set_maint(sv);
4424 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004425 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004426 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004427 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004428 }
4429 }
4430 else if (strcmp(args[3], "health") == 0) {
4431 if (sv->track) {
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 = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004434 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004435 }
4436 else if (strcmp(args[4], "up") == 0) {
4437 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004438 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004439 }
4440 else if (strcmp(args[4], "stopping") == 0) {
4441 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004442 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004443 }
4444 else if (strcmp(args[4], "down") == 0) {
4445 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004446 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004447 }
4448 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004449 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004450 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004451 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004452 }
4453 }
4454 else if (strcmp(args[3], "agent") == 0) {
4455 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004456 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004457 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004458 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004459 }
4460 else if (strcmp(args[4], "up") == 0) {
4461 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004462 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004463 }
4464 else if (strcmp(args[4], "down") == 0) {
4465 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004466 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004467 }
4468 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004469 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004470 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004471 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004472 }
4473 }
Misiek2da082d2017-01-09 09:40:42 +01004474 else if (strcmp(args[3], "agent-addr") == 0) {
4475 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004476 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004477 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4478 appctx->st0 = CLI_ST_PRINT;
4479 } else {
4480 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004481 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004482 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4483 appctx->st0 = CLI_ST_PRINT;
4484 }
4485 }
4486 }
4487 else if (strcmp(args[3], "agent-send") == 0) {
4488 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004489 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004490 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4491 appctx->st0 = CLI_ST_PRINT;
4492 } else {
4493 char *nss = strdup(args[4]);
4494 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004495 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004496 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4497 appctx->st0 = CLI_ST_PRINT;
4498 } else {
4499 free(sv->agent.send_string);
4500 sv->agent.send_string = nss;
4501 sv->agent.send_string_len = strlen(args[4]);
4502 }
4503 }
4504 }
William Lallemand222baf22016-11-19 02:00:33 +01004505 else if (strcmp(args[3], "check-port") == 0) {
4506 int i = 0;
4507 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004508 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004509 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004510 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004511 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004512 }
4513 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004514 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004515 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004516 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004517 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004518 }
4519 /* prevent the update of port to 0 if MAPPORTS are in use */
4520 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004521 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004522 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004523 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004524 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004525 }
4526 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004527 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004528 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004529 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004530 }
4531 else if (strcmp(args[3], "addr") == 0) {
4532 char *addr = NULL;
4533 char *port = NULL;
4534 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004535 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004536 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004537 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004538 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004539 }
4540 else {
4541 addr = args[4];
4542 }
4543 if (strcmp(args[5], "port") == 0) {
4544 port = args[6];
4545 }
4546 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4547 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004548 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004549 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004550 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004551 }
4552 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4553 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004554 else if (strcmp(args[3], "fqdn") == 0) {
4555 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004556 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004557 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4558 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004559 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004560 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004561 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004562 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004563 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004564 appctx->ctx.cli.msg = warning;
4565 appctx->st0 = CLI_ST_PRINT;
4566 }
4567 }
William Lallemand222baf22016-11-19 02:00:33 +01004568 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004569 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004570 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 +01004571 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004572 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004573 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004574 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004575 return 1;
4576}
4577
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004578static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004579{
4580 struct stream_interface *si = appctx->owner;
4581 struct proxy *px;
4582 struct server *sv;
4583 char *line;
4584
4585
4586 /* split "backend/server" and make <line> point to server */
4587 for (line = args[2]; *line; line++)
4588 if (*line == '/') {
4589 *line++ = '\0';
4590 break;
4591 }
4592
4593 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004594 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004595 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004596 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004597 return 1;
4598 }
4599
4600 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004601 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004602 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004603 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004604 return 1;
4605 }
4606
4607 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004608 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4609 sv->iweight);
4610 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004611 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004612 return 0;
4613 }
William Lallemand6b160942016-11-22 12:34:35 +01004614 return 1;
4615}
4616
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004617/* Parse a "set weight" command.
4618 *
4619 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004620 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004621static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004622{
4623 struct server *sv;
4624 const char *warning;
4625
4626 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4627 return 1;
4628
4629 sv = cli_find_server(appctx, args[2]);
4630 if (!sv)
4631 return 1;
4632
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004633 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4634
William Lallemand6b160942016-11-22 12:34:35 +01004635 warning = server_parse_weight_change_request(sv, args[3]);
4636 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004637 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004638 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004639 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004640 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004641
4642 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4643
William Lallemand6b160942016-11-22 12:34:35 +01004644 return 1;
4645}
4646
Willy Tarreau46b7f532018-08-21 11:54:26 +02004647/* parse a "set maxconn server" command. It always returns 1.
4648 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004649 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004650 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004651static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004652{
4653 struct server *sv;
4654 const char *warning;
4655
4656 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4657 return 1;
4658
4659 sv = cli_find_server(appctx, args[3]);
4660 if (!sv)
4661 return 1;
4662
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004663 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4664
Willy Tarreaub8026272016-11-23 11:26:56 +01004665 warning = server_parse_maxconn_change_request(sv, args[4]);
4666 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004667 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004668 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004669 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004670 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004671
4672 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4673
Willy Tarreaub8026272016-11-23 11:26:56 +01004674 return 1;
4675}
William Lallemand6b160942016-11-22 12:34:35 +01004676
Willy Tarreau46b7f532018-08-21 11:54:26 +02004677/* parse a "disable agent" command. It always returns 1.
4678 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004679 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004680 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004681static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004682{
4683 struct server *sv;
4684
4685 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4686 return 1;
4687
4688 sv = cli_find_server(appctx, args[2]);
4689 if (!sv)
4690 return 1;
4691
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004692 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004693 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004694 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004695 return 1;
4696}
4697
Willy Tarreau46b7f532018-08-21 11:54:26 +02004698/* parse a "disable health" command. It always returns 1.
4699 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004700 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004701 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004702static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004703{
4704 struct server *sv;
4705
4706 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4707 return 1;
4708
4709 sv = cli_find_server(appctx, args[2]);
4710 if (!sv)
4711 return 1;
4712
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004713 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004714 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004715 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004716 return 1;
4717}
4718
Willy Tarreau46b7f532018-08-21 11:54:26 +02004719/* parse a "disable server" command. It always returns 1.
4720 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004721 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004722 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004723static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004724{
4725 struct server *sv;
4726
4727 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4728 return 1;
4729
4730 sv = cli_find_server(appctx, args[2]);
4731 if (!sv)
4732 return 1;
4733
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004734 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004735 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004736 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004737 return 1;
4738}
4739
Willy Tarreau46b7f532018-08-21 11:54:26 +02004740/* parse a "enable agent" command. It always returns 1.
4741 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004742 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004743 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004744static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004745{
4746 struct server *sv;
4747
4748 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4749 return 1;
4750
4751 sv = cli_find_server(appctx, args[2]);
4752 if (!sv)
4753 return 1;
4754
4755 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004756 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004757 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004758 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004759 return 1;
4760 }
4761
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004762 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004763 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004764 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004765 return 1;
4766}
4767
Willy Tarreau46b7f532018-08-21 11:54:26 +02004768/* parse a "enable health" command. It always returns 1.
4769 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004770 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004771 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004772static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004773{
4774 struct server *sv;
4775
4776 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4777 return 1;
4778
4779 sv = cli_find_server(appctx, args[2]);
4780 if (!sv)
4781 return 1;
4782
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004783 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004784 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004785 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004786 return 1;
4787}
4788
Willy Tarreau46b7f532018-08-21 11:54:26 +02004789/* parse a "enable server" command. It always returns 1.
4790 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004791 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004792 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004793static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004794{
4795 struct server *sv;
4796
4797 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4798 return 1;
4799
4800 sv = cli_find_server(appctx, args[2]);
4801 if (!sv)
4802 return 1;
4803
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004804 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004805 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004806 if (!(sv->flags & SRV_F_COOKIESET)
4807 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4808 sv->cookie)
4809 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004810 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004811 return 1;
4812}
4813
William Lallemand222baf22016-11-19 02:00:33 +01004814/* register cli keywords */
4815static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004816 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004817 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004818 { { "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 +01004819 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004820 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004821 { { "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 +01004822 { { "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 +01004823 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004824 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4825 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4826
William Lallemand222baf22016-11-19 02:00:33 +01004827 {{},}
4828}};
4829
Willy Tarreau0108d902018-11-25 19:14:37 +01004830INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004831
Emeric Brun64cc49c2017-10-03 14:46:45 +02004832/*
4833 * This function applies server's status changes, it is
4834 * is designed to be called asynchronously.
4835 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004836 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004837 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004838static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004839{
4840 struct check *check = &s->check;
4841 int xferred;
4842 struct proxy *px = s->proxy;
4843 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4844 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4845 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004846 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004847
Emeric Brun64cc49c2017-10-03 14:46:45 +02004848 /* If currently main is not set we try to apply pending state changes */
4849 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4850 int next_admin;
4851
4852 /* Backup next admin */
4853 next_admin = s->next_admin;
4854
4855 /* restore current admin state */
4856 s->next_admin = s->cur_admin;
4857
4858 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4859 s->last_change = now.tv_sec;
4860 if (s->proxy->lbprm.set_server_status_down)
4861 s->proxy->lbprm.set_server_status_down(s);
4862
4863 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4864 srv_shutdown_streams(s, SF_ERR_DOWN);
4865
4866 /* we might have streams queued on this server and waiting for
4867 * a connection. Those which are redispatchable will be queued
4868 * to another server or to the proxy itself.
4869 */
4870 xferred = pendconn_redistribute(s);
4871
4872 tmptrash = alloc_trash_chunk();
4873 if (tmptrash) {
4874 chunk_printf(tmptrash,
4875 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4876 s->proxy->id, s->id);
4877
Emeric Brun5a133512017-10-19 14:42:30 +02004878 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004879 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004880
4881 /* we don't send an alert if the server was previously paused */
4882 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004883 send_log(s->proxy, log_level, "%s.\n",
4884 tmptrash->area);
4885 send_email_alert(s, log_level, "%s",
4886 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004887 free_trash_chunk(tmptrash);
4888 tmptrash = NULL;
4889 }
4890 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4891 set_backend_down(s->proxy);
4892
4893 s->counters.down_trans++;
4894 }
4895 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4896 s->last_change = now.tv_sec;
4897 if (s->proxy->lbprm.set_server_status_down)
4898 s->proxy->lbprm.set_server_status_down(s);
4899
4900 /* we might have streams queued on this server and waiting for
4901 * a connection. Those which are redispatchable will be queued
4902 * to another server or to the proxy itself.
4903 */
4904 xferred = pendconn_redistribute(s);
4905
4906 tmptrash = alloc_trash_chunk();
4907 if (tmptrash) {
4908 chunk_printf(tmptrash,
4909 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4910 s->proxy->id, s->id);
4911
Emeric Brun5a133512017-10-19 14:42:30 +02004912 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004913
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004914 ha_warning("%s.\n", tmptrash->area);
4915 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4916 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004917 free_trash_chunk(tmptrash);
4918 tmptrash = NULL;
4919 }
4920
4921 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4922 set_backend_down(s->proxy);
4923 }
4924 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4925 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4926 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4927 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4928 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4929 s->proxy->last_change = now.tv_sec;
4930 }
4931
4932 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4933 s->down_time += now.tv_sec - s->last_change;
4934
4935 s->last_change = now.tv_sec;
4936 if (s->next_state == SRV_ST_STARTING)
4937 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4938
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004939 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004940 /* now propagate the status change to any LB algorithms */
4941 if (px->lbprm.update_server_eweight)
4942 px->lbprm.update_server_eweight(s);
4943 else if (srv_willbe_usable(s)) {
4944 if (px->lbprm.set_server_status_up)
4945 px->lbprm.set_server_status_up(s);
4946 }
4947 else {
4948 if (px->lbprm.set_server_status_down)
4949 px->lbprm.set_server_status_down(s);
4950 }
4951
4952 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4953 * and it's not a backup server and its effective weight is > 0,
4954 * then it can accept new connections, so we shut down all streams
4955 * on all backup servers.
4956 */
4957 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4958 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4959 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4960
4961 /* check if we can handle some connections queued at the proxy. We
4962 * will take as many as we can handle.
4963 */
4964 xferred = pendconn_grab_from_px(s);
4965
4966 tmptrash = alloc_trash_chunk();
4967 if (tmptrash) {
4968 chunk_printf(tmptrash,
4969 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4970 s->proxy->id, s->id);
4971
Emeric Brun5a133512017-10-19 14:42:30 +02004972 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004973 ha_warning("%s.\n", tmptrash->area);
4974 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4975 tmptrash->area);
4976 send_email_alert(s, LOG_NOTICE, "%s",
4977 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004978 free_trash_chunk(tmptrash);
4979 tmptrash = NULL;
4980 }
4981
4982 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4983 set_backend_down(s->proxy);
4984 }
4985 else if (s->cur_eweight != s->next_eweight) {
4986 /* now propagate the status change to any LB algorithms */
4987 if (px->lbprm.update_server_eweight)
4988 px->lbprm.update_server_eweight(s);
4989 else if (srv_willbe_usable(s)) {
4990 if (px->lbprm.set_server_status_up)
4991 px->lbprm.set_server_status_up(s);
4992 }
4993 else {
4994 if (px->lbprm.set_server_status_down)
4995 px->lbprm.set_server_status_down(s);
4996 }
4997
4998 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4999 set_backend_down(s->proxy);
5000 }
5001
5002 s->next_admin = next_admin;
5003 }
5004
Emeric Brun5a133512017-10-19 14:42:30 +02005005 /* reset operational state change */
5006 *s->op_st_chg.reason = 0;
5007 s->op_st_chg.status = s->op_st_chg.code = -1;
5008 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005009
5010 /* Now we try to apply pending admin changes */
5011
5012 /* Maintenance must also disable health checks */
5013 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5014 if (s->check.state & CHK_ST_ENABLED) {
5015 s->check.state |= CHK_ST_PAUSED;
5016 check->health = 0;
5017 }
5018
5019 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005020 tmptrash = alloc_trash_chunk();
5021 if (tmptrash) {
5022 chunk_printf(tmptrash,
5023 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5024 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5025 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005026
Olivier Houchard796a2b32017-10-24 17:42:47 +02005027 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005028
Olivier Houchard796a2b32017-10-24 17:42:47 +02005029 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005030 ha_warning("%s.\n", tmptrash->area);
5031 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5032 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005033 }
5034 free_trash_chunk(tmptrash);
5035 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005036 }
Emeric Brun8f298292017-12-06 16:47:17 +01005037 /* commit new admin status */
5038
5039 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005040 }
5041 else { /* server was still running */
5042 check->health = 0; /* failure */
5043 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005044
5045 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005046 if (s->proxy->lbprm.set_server_status_down)
5047 s->proxy->lbprm.set_server_status_down(s);
5048
Emeric Brun64cc49c2017-10-03 14:46:45 +02005049 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5050 srv_shutdown_streams(s, SF_ERR_DOWN);
5051
5052 /* we might have streams queued on this server and waiting for
5053 * a connection. Those which are redispatchable will be queued
5054 * to another server or to the proxy itself.
5055 */
5056 xferred = pendconn_redistribute(s);
5057
5058 tmptrash = alloc_trash_chunk();
5059 if (tmptrash) {
5060 chunk_printf(tmptrash,
5061 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5062 s->flags & SRV_F_BACKUP ? "Backup " : "",
5063 s->proxy->id, s->id,
5064 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5065
5066 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5067
5068 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005069 ha_warning("%s.\n", tmptrash->area);
5070 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5071 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005072 }
5073 free_trash_chunk(tmptrash);
5074 tmptrash = NULL;
5075 }
5076 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5077 set_backend_down(s->proxy);
5078
5079 s->counters.down_trans++;
5080 }
5081 }
5082 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5083 /* OK here we're leaving maintenance, we have many things to check,
5084 * because the server might possibly be coming back up depending on
5085 * its state. In practice, leaving maintenance means that we should
5086 * immediately turn to UP (more or less the slowstart) under the
5087 * following conditions :
5088 * - server is neither checked nor tracked
5089 * - server tracks another server which is not checked
5090 * - server tracks another server which is already up
5091 * Which sums up as something simpler :
5092 * "either the tracking server is up or the server's checks are disabled
5093 * or up". Otherwise we only re-enable health checks. There's a special
5094 * case associated to the stopping state which can be inherited. Note
5095 * that the server might still be in drain mode, which is naturally dealt
5096 * with by the lower level functions.
5097 */
5098
5099 if (s->check.state & CHK_ST_ENABLED) {
5100 s->check.state &= ~CHK_ST_PAUSED;
5101 check->health = check->rise; /* start OK but check immediately */
5102 }
5103
5104 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5105 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5106 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5107 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5108 s->next_state = SRV_ST_STOPPING;
5109 }
5110 else {
5111 s->next_state = SRV_ST_STARTING;
5112 if (s->slowstart > 0)
5113 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5114 else
5115 s->next_state = SRV_ST_RUNNING;
5116 }
5117
5118 }
5119
5120 tmptrash = alloc_trash_chunk();
5121 if (tmptrash) {
5122 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5123 chunk_printf(tmptrash,
5124 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5125 s->flags & SRV_F_BACKUP ? "Backup " : "",
5126 s->proxy->id, s->id,
5127 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5128 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5129 }
5130 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5131 chunk_printf(tmptrash,
5132 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5133 s->flags & SRV_F_BACKUP ? "Backup " : "",
5134 s->proxy->id, s->id, s->hostname,
5135 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5136 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5137 }
5138 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5139 chunk_printf(tmptrash,
5140 "%sServer %s/%s is %s/%s (leaving maintenance)",
5141 s->flags & SRV_F_BACKUP ? "Backup " : "",
5142 s->proxy->id, s->id,
5143 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5144 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5145 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005146 ha_warning("%s.\n", tmptrash->area);
5147 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5148 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005149 free_trash_chunk(tmptrash);
5150 tmptrash = NULL;
5151 }
5152
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005153 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005154 /* now propagate the status change to any LB algorithms */
5155 if (px->lbprm.update_server_eweight)
5156 px->lbprm.update_server_eweight(s);
5157 else if (srv_willbe_usable(s)) {
5158 if (px->lbprm.set_server_status_up)
5159 px->lbprm.set_server_status_up(s);
5160 }
5161 else {
5162 if (px->lbprm.set_server_status_down)
5163 px->lbprm.set_server_status_down(s);
5164 }
5165
5166 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5167 set_backend_down(s->proxy);
5168
Willy Tarreau6a78e612018-08-07 10:14:53 +02005169 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5170 * and it's not a backup server and its effective weight is > 0,
5171 * then it can accept new connections, so we shut down all streams
5172 * on all backup servers.
5173 */
5174 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5175 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5176 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5177
5178 /* check if we can handle some connections queued at the proxy. We
5179 * will take as many as we can handle.
5180 */
5181 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005182 }
5183 else if (s->next_admin & SRV_ADMF_MAINT) {
5184 /* remaining in maintenance mode, let's inform precisely about the
5185 * situation.
5186 */
5187 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5188 tmptrash = alloc_trash_chunk();
5189 if (tmptrash) {
5190 chunk_printf(tmptrash,
5191 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5192 s->flags & SRV_F_BACKUP ? "Backup " : "",
5193 s->proxy->id, s->id);
5194
5195 if (s->track) /* normally it's mandatory here */
5196 chunk_appendf(tmptrash, " via %s/%s",
5197 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005198 ha_warning("%s.\n", tmptrash->area);
5199 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5200 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005201 free_trash_chunk(tmptrash);
5202 tmptrash = NULL;
5203 }
5204 }
5205 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5206 tmptrash = alloc_trash_chunk();
5207 if (tmptrash) {
5208 chunk_printf(tmptrash,
5209 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5210 s->flags & SRV_F_BACKUP ? "Backup " : "",
5211 s->proxy->id, s->id, s->hostname);
5212
5213 if (s->track) /* normally it's mandatory here */
5214 chunk_appendf(tmptrash, " via %s/%s",
5215 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005216 ha_warning("%s.\n", tmptrash->area);
5217 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5218 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005219 free_trash_chunk(tmptrash);
5220 tmptrash = NULL;
5221 }
5222 }
5223 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5224 tmptrash = alloc_trash_chunk();
5225 if (tmptrash) {
5226 chunk_printf(tmptrash,
5227 "%sServer %s/%s remains in forced maintenance",
5228 s->flags & SRV_F_BACKUP ? "Backup " : "",
5229 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005230 ha_warning("%s.\n", tmptrash->area);
5231 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5232 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005233 free_trash_chunk(tmptrash);
5234 tmptrash = NULL;
5235 }
5236 }
5237 /* don't report anything when leaving drain mode and remaining in maintenance */
5238
5239 s->cur_admin = s->next_admin;
5240 }
5241
5242 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5243 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5244 /* drain state is applied only if not yet in maint */
5245
5246 s->last_change = now.tv_sec;
5247 if (px->lbprm.set_server_status_down)
5248 px->lbprm.set_server_status_down(s);
5249
5250 /* we might have streams queued on this server and waiting for
5251 * a connection. Those which are redispatchable will be queued
5252 * to another server or to the proxy itself.
5253 */
5254 xferred = pendconn_redistribute(s);
5255
5256 tmptrash = alloc_trash_chunk();
5257 if (tmptrash) {
5258 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5259 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5260 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5261
5262 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5263
5264 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005265 ha_warning("%s.\n", tmptrash->area);
5266 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5267 tmptrash->area);
5268 send_email_alert(s, LOG_NOTICE, "%s",
5269 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005270 }
5271 free_trash_chunk(tmptrash);
5272 tmptrash = NULL;
5273 }
5274
5275 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5276 set_backend_down(s->proxy);
5277 }
5278 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5279 /* OK completely leaving drain mode */
5280 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5281 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5282 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5283 s->proxy->last_change = now.tv_sec;
5284 }
5285
5286 if (s->last_change < now.tv_sec) // ignore negative times
5287 s->down_time += now.tv_sec - s->last_change;
5288 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005289 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005290
5291 tmptrash = alloc_trash_chunk();
5292 if (tmptrash) {
5293 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5294 chunk_printf(tmptrash,
5295 "%sServer %s/%s is %s (leaving forced drain)",
5296 s->flags & SRV_F_BACKUP ? "Backup " : "",
5297 s->proxy->id, s->id,
5298 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5299 }
5300 else {
5301 chunk_printf(tmptrash,
5302 "%sServer %s/%s is %s (leaving drain)",
5303 s->flags & SRV_F_BACKUP ? "Backup " : "",
5304 s->proxy->id, s->id,
5305 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5306 if (s->track) /* normally it's mandatory here */
5307 chunk_appendf(tmptrash, " via %s/%s",
5308 s->track->proxy->id, s->track->id);
5309 }
5310
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005311 ha_warning("%s.\n", tmptrash->area);
5312 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5313 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005314 free_trash_chunk(tmptrash);
5315 tmptrash = NULL;
5316 }
5317
5318 /* now propagate the status change to any LB algorithms */
5319 if (px->lbprm.update_server_eweight)
5320 px->lbprm.update_server_eweight(s);
5321 else if (srv_willbe_usable(s)) {
5322 if (px->lbprm.set_server_status_up)
5323 px->lbprm.set_server_status_up(s);
5324 }
5325 else {
5326 if (px->lbprm.set_server_status_down)
5327 px->lbprm.set_server_status_down(s);
5328 }
5329 }
5330 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5331 /* remaining in drain mode after removing one of its flags */
5332
5333 tmptrash = alloc_trash_chunk();
5334 if (tmptrash) {
5335 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5336 chunk_printf(tmptrash,
5337 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5338 s->flags & SRV_F_BACKUP ? "Backup " : "",
5339 s->proxy->id, s->id);
5340
5341 if (s->track) /* normally it's mandatory here */
5342 chunk_appendf(tmptrash, " via %s/%s",
5343 s->track->proxy->id, s->track->id);
5344 }
5345 else {
5346 chunk_printf(tmptrash,
5347 "%sServer %s/%s remains in forced drain mode",
5348 s->flags & SRV_F_BACKUP ? "Backup " : "",
5349 s->proxy->id, s->id);
5350 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005351 ha_warning("%s.\n", tmptrash->area);
5352 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5353 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005354 free_trash_chunk(tmptrash);
5355 tmptrash = NULL;
5356 }
5357
5358 /* commit new admin status */
5359
5360 s->cur_admin = s->next_admin;
5361 }
5362 }
5363
5364 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005365 *s->adm_st_chg_cause = 0;
5366}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005367
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005368static struct task *cleanup_idle_connections(struct task *task, void *context, unsigned short state)
5369{
5370 struct server *srv = context;
5371 struct connection *conn, *conn_back;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005372 unsigned int to_destroy = srv->curr_idle_conns / 2 + (srv->curr_idle_conns & 1);
5373 unsigned int i = 0;
5374
5375
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005376
5377 list_for_each_entry_safe(conn, conn_back, &srv->idle_orphan_conns[tid], list) {
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005378 if (i == to_destroy)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005379 break;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005380 conn->mux->destroy(conn);
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005381 i++;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005382 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005383 if (!LIST_ISEMPTY(&srv->idle_orphan_conns[tid]))
5384 task_schedule(task, tick_add(now_ms, srv->pool_purge_delay));
5385 else
5386 task->expire = TICK_ETERNITY;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005387 return task;
5388}
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005389/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005390 * Local variables:
5391 * c-indent-level: 8
5392 * c-basic-offset: 8
5393 * End:
5394 */