blob: 86137f3e5c5c6e25cd368c7cb7442d972613f0e6 [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;
1716
1717 srv = calloc(1, sizeof *srv);
1718 if (!srv)
1719 return NULL;
1720
1721 srv->obj_type = OBJ_TYPE_SERVER;
1722 srv->proxy = proxy;
1723 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001724 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001725
Emeric Brun52a91d32017-08-31 14:41:55 +02001726 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001727 srv->last_change = now.tv_sec;
1728
1729 srv->check.status = HCHK_STATUS_INI;
1730 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001731 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001732 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1733
1734 srv->agent.status = HCHK_STATUS_INI;
1735 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001736 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001737 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1738
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001739 srv->pool_purge_delay = 1000;
Olivier Houchard006e3102018-12-10 18:30:32 +01001740 srv->max_idle_conns = -1;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001741 srv->max_reuse = -1;
Olivier Houchard006e3102018-12-10 18:30:32 +01001742
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001743 return srv;
1744}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001745
1746/*
1747 * Validate <srv> server health-check settings.
1748 * Returns 0 if everything is OK, -1 if not.
1749 */
1750static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1751{
1752 struct tcpcheck_rule *r = NULL;
1753 struct list *l;
1754
1755 /*
1756 * We need at least a service port, a check port or the first tcp-check rule must
1757 * be a 'connect' one when checking an IPv4/IPv6 server.
1758 */
1759 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1760 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1761 return 0;
1762
1763 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1764 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001765 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1766 "Check has been disabled.\n",
1767 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001768 return -1;
1769 }
1770
1771 /* search the first action (connect / send / expect) in the list */
1772 l = &srv->proxy->tcpcheck_rules;
1773 list_for_each_entry(r, l, list) {
1774 if (r->action != TCPCHK_ACT_COMMENT)
1775 break;
1776 }
1777
1778 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001779 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1780 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1781 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001782 return -1;
1783 }
1784
1785 /* scan the tcp-check ruleset to ensure a port has been configured */
1786 l = &srv->proxy->tcpcheck_rules;
1787 list_for_each_entry(r, l, list) {
1788 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001789 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1790 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1791 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001792 return -1;
1793 }
1794 }
1795
1796 return 0;
1797}
1798
1799/*
1800 * Initialize <srv> health-check structure.
1801 * Returns the error string in case of memory allocation failure, NULL if not.
1802 */
1803static const char *do_health_check_init(struct server *srv, int check_type, int state)
1804{
1805 const char *ret;
1806
1807 if (!srv->do_check)
1808 return NULL;
1809
1810 ret = init_check(&srv->check, check_type);
1811 if (ret)
1812 return ret;
1813
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001814 srv->check.state |= state;
1815 global.maxsock++;
1816
1817 return NULL;
1818}
1819
1820static int server_health_check_init(const char *file, int linenum,
1821 struct server *srv, struct proxy *curproxy)
1822{
1823 const char *ret;
1824
1825 if (!srv->do_check)
1826 return 0;
1827
1828 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001829 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1830 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001831 return ERR_ALERT | ERR_FATAL;
1832 }
1833
1834 if (server_healthcheck_validate(file, linenum, srv) < 0)
1835 return ERR_ALERT | ERR_ABORT;
1836
1837 /* note: check type will be set during the config review phase */
1838 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1839 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001840 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001841 return ERR_ALERT | ERR_ABORT;
1842 }
1843
1844 return 0;
1845}
1846
1847/*
1848 * Initialize <srv> agent check structure.
1849 * Returns the error string in case of memory allocation failure, NULL if not.
1850 */
1851static const char *do_server_agent_check_init(struct server *srv, int state)
1852{
1853 const char *ret;
1854
1855 if (!srv->do_agent)
1856 return NULL;
1857
1858 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1859 if (ret)
1860 return ret;
1861
1862 if (!srv->agent.inter)
1863 srv->agent.inter = srv->check.inter;
1864
1865 srv->agent.state |= state;
1866 global.maxsock++;
1867
1868 return NULL;
1869}
1870
1871static int server_agent_check_init(const char *file, int linenum,
1872 struct server *srv, struct proxy *curproxy)
1873{
1874 const char *ret;
1875
1876 if (!srv->do_agent)
1877 return 0;
1878
1879 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001880 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 +02001881 file, linenum, srv->id);
1882 return ERR_ALERT | ERR_FATAL;
1883 }
1884
1885 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1886 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001887 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001888 return ERR_ALERT | ERR_ABORT;
1889 }
1890
1891 return 0;
1892}
1893
1894#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1895static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1896 struct server *srv, struct proxy *proxy)
1897{
1898 int ret;
1899 char *err = NULL;
1900
1901 if (!srv->sni_expr)
1902 return 0;
1903
1904 ret = server_parse_sni_expr(srv, proxy, &err);
1905 if (!ret)
1906 return 0;
1907
1908 display_parser_err(file, linenum, args, cur_arg, &err);
1909 free(err);
1910
1911 return ret;
1912}
1913#endif
1914
1915/*
1916 * Server initializations finalization.
1917 * Initialize health check, agent check and SNI expression if enabled.
1918 * Must not be called for a default server instance.
1919 */
1920static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1921 struct server *srv, struct proxy *px)
1922{
1923 int ret;
1924
1925 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1926 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1927 return ret;
1928 }
1929
1930#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1931 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1932 return ret;
1933#endif
1934
1935 if (srv->flags & SRV_F_BACKUP)
1936 px->srv_bck++;
1937 else
1938 px->srv_act++;
1939 srv_lb_commit_status(srv);
1940
Daniel Corbett43bb8422019-01-09 08:13:29 -05001941 if (srv->max_idle_conns != 0) {
Willy Tarreau15c120d2019-01-23 10:41:19 +01001942 int i;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001943
Willy Tarreau15c120d2019-01-23 10:41:19 +01001944 srv->idle_orphan_conns = calloc(global.nbthread, sizeof(*srv->idle_orphan_conns));
1945 if (!srv->idle_orphan_conns)
1946 goto err;
1947 srv->idle_task = calloc(global.nbthread, sizeof(*srv->idle_task));
1948 if (!srv->idle_task)
1949 goto err;
1950 for (i = 0; i < global.nbthread; i++) {
1951 LIST_INIT(&srv->idle_orphan_conns[i]);
1952 srv->idle_task[i] = task_new(1 << i);
1953 if (!srv->idle_task[i])
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001954 goto err;
Willy Tarreau15c120d2019-01-23 10:41:19 +01001955 srv->idle_task[i]->process = cleanup_idle_connections;
1956 srv->idle_task[i]->context = srv;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001957 }
Willy Tarreau15c120d2019-01-23 10:41:19 +01001958 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001959
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001960 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001961err:
1962 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001963}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001964
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001965/*
1966 * Parse as much as possible such a range string argument: low[-high]
1967 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1968 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1969 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1970 * Returns 0 if succeeded, -1 if not.
1971 */
1972static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1973{
1974 char *nb_high_arg;
1975
1976 *nb_high = 0;
1977 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001978 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001979
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001980 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001981 *nb_high_arg++ = '\0';
1982 *nb_high = atoi(nb_high_arg);
1983 }
1984 else {
1985 *nb_high += *nb_low;
1986 *nb_low = 1;
1987 }
1988
1989 if (*nb_low < 0 || *nb_high < *nb_low)
1990 return -1;
1991
1992 return 0;
1993}
1994
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001995static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1996{
1997 chunk_printf(&trash, "%s%d", prefix, nb);
1998 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001999 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002000}
2001
2002/*
2003 * Initialize as much as possible servers from <srv> server template.
2004 * Note that a server template is a special server with
2005 * a few different parameters than a server which has
2006 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002007 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002008 * 'srv' template included.
2009 */
2010static int server_template_init(struct server *srv, struct proxy *px)
2011{
2012 int i;
2013 struct server *newsrv;
2014
2015 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2016 int check_init_state;
2017 int agent_init_state;
2018
2019 newsrv = new_server(px);
2020 if (!newsrv)
2021 goto err;
2022
2023 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002024 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002025#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2026 if (newsrv->sni_expr) {
2027 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2028 if (!newsrv->ssl_ctx.sni)
2029 goto err;
2030 }
2031#endif
2032 /* Set this new server ID. */
2033 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2034
2035 /* Initial checks states. */
2036 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2037 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2038
2039 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2040 do_server_agent_check_init(newsrv, agent_init_state))
2041 goto err;
2042
2043 /* Linked backwards first. This will be restablished after parsing. */
2044 newsrv->next = px->srv;
2045 px->srv = newsrv;
Olivier Houchard006e3102018-12-10 18:30:32 +01002046 if (newsrv->max_idle_conns != 0) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002047 int i;
2048
2049 newsrv->idle_orphan_conns = calloc(global.nbthread, sizeof(*newsrv->idle_orphan_conns));
2050 if (!newsrv->idle_orphan_conns)
2051 goto err;
2052 newsrv->idle_task = calloc(global.nbthread, sizeof(*newsrv->idle_task));
2053 if (!newsrv->idle_task)
2054 goto err;
2055 for (i = 0; i < global.nbthread; i++) {
2056 LIST_INIT(&newsrv->idle_orphan_conns[i]);
2057 newsrv->idle_task[i] = task_new(1 << i);
2058 if (!newsrv->idle_task[i])
2059 goto err;
2060 newsrv->idle_task[i]->process = cleanup_idle_connections;
2061 newsrv->idle_task[i]->context = newsrv;
2062 }
2063 }
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002064 }
2065 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2066
2067 return i - srv->tmpl_info.nb_low;
2068
2069 err:
2070 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2071 if (newsrv) {
2072#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2073 release_sample_expr(newsrv->ssl_ctx.sni);
2074#endif
2075 free_check(&newsrv->agent);
2076 free_check(&newsrv->check);
2077 }
2078 free(newsrv);
2079 return i - srv->tmpl_info.nb_low;
2080}
2081
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002082int 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 +02002083{
2084 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002085 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002086 char *errmsg = NULL;
2087 int err_code = 0;
2088 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002089 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002090
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002091 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002092 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002093 !strcmp(args[0], "default-server") ||
2094 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002095 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002096 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002097 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002098 int srv_tmpl = !defsrv && !srv;
2099 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002100
2101 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002102 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002103 err_code |= ERR_ALERT | ERR_FATAL;
2104 goto out;
2105 }
2106 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002107 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002108
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002109 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002110 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002111 if (!*args[2]) {
2112 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002113 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002114 file, linenum, args[0]);
2115 err_code |= ERR_ALERT | ERR_FATAL;
2116 goto out;
2117 }
2118
2119 err = invalid_char(args[1]);
2120 }
2121 else if (srv_tmpl) {
2122 if (!*args[3]) {
2123 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002124 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 +02002125 file, linenum, args[0]);
2126 err_code |= ERR_ALERT | ERR_FATAL;
2127 goto out;
2128 }
2129
2130 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002131 }
2132
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002133 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002134 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 +02002135 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002136 err_code |= ERR_ALERT | ERR_FATAL;
2137 goto out;
2138 }
2139
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002140 cur_arg = 2;
2141 if (srv_tmpl) {
2142 /* Parse server-template <nb | range> arg. */
2143 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002144 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002145 file, linenum, args[0], args[cur_arg]);
2146 err_code |= ERR_ALERT | ERR_FATAL;
2147 goto out;
2148 }
2149 cur_arg++;
2150 }
2151
Willy Tarreau272adea2014-03-31 10:39:59 +02002152 if (!defsrv) {
2153 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002154 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002155 struct protocol *proto;
2156
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002157 newsrv = new_server(curproxy);
2158 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002159 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002160 err_code |= ERR_ALERT | ERR_ABORT;
2161 goto out;
2162 }
2163
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002164 if (srv_tmpl) {
2165 newsrv->tmpl_info.nb_low = tmpl_range_low;
2166 newsrv->tmpl_info.nb_high = tmpl_range_high;
2167 }
2168
Willy Tarreau272adea2014-03-31 10:39:59 +02002169 /* the servers are linked backwards first */
2170 newsrv->next = curproxy->srv;
2171 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002172 newsrv->conf.file = strdup(file);
2173 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002174 /* Note: for a server template, its id is its prefix.
2175 * This is a temporary id which will be used for server allocations to come
2176 * after parsing.
2177 */
2178 if (srv)
2179 newsrv->id = strdup(args[1]);
2180 else
2181 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002182
2183 /* several ways to check the port component :
2184 * - IP => port=+0, relative (IPv4 only)
2185 * - IP: => port=+0, relative
2186 * - IP:N => port=N, absolute
2187 * - IP:+N => port=+N, relative
2188 * - IP:-N => port=-N, relative
2189 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002190 if (!parse_addr)
2191 goto skip_addr;
2192
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002193 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002194 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002195 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002196 err_code |= ERR_ALERT | ERR_FATAL;
2197 goto out;
2198 }
2199
2200 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002201 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002202 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002203 file, linenum, args[0], args[1]);
2204 err_code |= ERR_ALERT | ERR_FATAL;
2205 goto out;
2206 }
2207
2208 if (!port1 || !port2) {
2209 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002210 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002211 }
2212 else if (port1 != port2) {
2213 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002214 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002215 file, linenum, args[0], args[1], args[2]);
2216 err_code |= ERR_ALERT | ERR_FATAL;
2217 goto out;
2218 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002219
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002220 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002221 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002222 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002223 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002224 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2225 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2226 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002227 err_code |= ERR_ALERT | ERR_FATAL;
2228 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002229 }
2230 }
2231 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002232 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002233 file, linenum, newsrv->id);
2234 err_code |= ERR_ALERT | ERR_FATAL;
2235 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002236 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002237 }
2238
Willy Tarreau272adea2014-03-31 10:39:59 +02002239 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002240 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002241
Olivier Houchard8da5f982017-08-04 18:35:36 +02002242 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002243 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002244 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002245 err_code |= ERR_ALERT | ERR_FATAL;
2246 goto out;
2247 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002248
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002249 cur_arg++;
2250 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002251 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002252 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002253 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002254 } else {
2255 newsrv = &curproxy->defsrv;
2256 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002257 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002258 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002259 }
2260
2261 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002262 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002263 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2264 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002265 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002266 file, linenum, *err, newsrv->id);
2267 err_code |= ERR_ALERT | ERR_FATAL;
2268 goto out;
2269 }
2270 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002271 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002272 file, linenum, val, args[cur_arg], newsrv->id);
2273 err_code |= ERR_ALERT | ERR_FATAL;
2274 goto out;
2275 }
2276 newsrv->agent.inter = val;
2277 cur_arg += 2;
2278 }
Misiekea849332017-01-09 09:39:51 +01002279 else if (!strcmp(args[cur_arg], "agent-addr")) {
2280 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002281 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002282 goto out;
2283 }
2284
2285 cur_arg += 2;
2286 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002287 else if (!strcmp(args[cur_arg], "agent-port")) {
2288 global.maxsock++;
2289 newsrv->agent.port = atol(args[cur_arg + 1]);
2290 cur_arg += 2;
2291 }
James Brown55f9ff12015-10-21 18:19:05 -07002292 else if (!strcmp(args[cur_arg], "agent-send")) {
2293 global.maxsock++;
2294 free(newsrv->agent.send_string);
2295 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2296 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2297 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2298 cur_arg += 2;
2299 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002300 else if (!strcmp(args[cur_arg], "init-addr")) {
2301 char *p, *end;
2302 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002303 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002304
2305 newsrv->init_addr_methods = 0;
2306 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2307
2308 for (p = args[cur_arg + 1]; *p; p = end) {
2309 /* cut on next comma */
2310 for (end = p; *end && *end != ','; end++);
2311 if (*end)
2312 *(end++) = 0;
2313
Willy Tarreau4310d362016-11-02 15:05:56 +01002314 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002315 if (!strcmp(p, "libc")) {
2316 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2317 }
2318 else if (!strcmp(p, "last")) {
2319 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2320 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002321 else if (!strcmp(p, "none")) {
2322 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2323 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002324 else if (str2ip2(p, &sa, 0)) {
2325 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002326 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002327 file, linenum, args[cur_arg], p);
2328 err_code |= ERR_ALERT | ERR_FATAL;
2329 goto out;
2330 }
2331 newsrv->init_addr = sa;
2332 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2333 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002334 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002335 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 +02002336 file, linenum, args[cur_arg], p);
2337 err_code |= ERR_ALERT | ERR_FATAL;
2338 goto out;
2339 }
2340 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002341 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002342 file, linenum, args[cur_arg], p);
2343 err_code |= ERR_ALERT | ERR_FATAL;
2344 goto out;
2345 }
2346 }
2347 cur_arg += 2;
2348 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002349 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002350 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002351 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2352 cur_arg += 2;
2353 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002354 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2355 char *p, *end;
2356
2357 for (p = args[cur_arg + 1]; *p; p = end) {
2358 /* cut on next comma */
2359 for (end = p; *end && *end != ','; end++);
2360 if (*end)
2361 *(end++) = 0;
2362
2363 if (!strcmp(p, "allow-dup-ip")) {
2364 newsrv->dns_opts.accept_duplicate_ip = 1;
2365 }
2366 else if (!strcmp(p, "prevent-dup-ip")) {
2367 newsrv->dns_opts.accept_duplicate_ip = 0;
2368 }
2369 else {
2370 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2371 file, linenum, args[cur_arg], p);
2372 err_code |= ERR_ALERT | ERR_FATAL;
2373 goto out;
2374 }
2375 }
2376
2377 cur_arg += 2;
2378 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002379 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2380 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002381 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002382 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002383 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002384 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002385 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002386 file, linenum, args[cur_arg]);
2387 err_code |= ERR_ALERT | ERR_FATAL;
2388 goto out;
2389 }
2390 cur_arg += 2;
2391 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002392 else if (!strcmp(args[cur_arg], "resolve-net")) {
2393 char *p, *e;
2394 unsigned char mask;
2395 struct dns_options *opt;
2396
2397 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002398 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002399 file, linenum, args[cur_arg]);
2400 err_code |= ERR_ALERT | ERR_FATAL;
2401 goto out;
2402 }
2403
2404 opt = &newsrv->dns_opts;
2405
2406 /* Split arguments by comma, and convert it from ipv4 or ipv6
2407 * string network in in_addr or in6_addr.
2408 */
2409 p = args[cur_arg + 1];
2410 e = p;
2411 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002412 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002413 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002414 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002415 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2416 err_code |= ERR_ALERT | ERR_FATAL;
2417 goto out;
2418 }
2419 /* look for end or comma. */
2420 while (*e != ',' && *e != '\0')
2421 e++;
2422 if (*e == ',') {
2423 *e = '\0';
2424 e++;
2425 }
2426 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2427 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2428 /* Try to convert input string from ipv4 or ipv6 network. */
2429 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2430 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2431 &mask)) {
2432 /* Try to convert input string from ipv6 network. */
2433 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2434 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2435 } else {
2436 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002437 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002438 file, linenum, args[cur_arg], p);
2439 err_code |= ERR_ALERT | ERR_FATAL;
2440 goto out;
2441 }
2442 opt->pref_net_nb++;
2443 p = e;
2444 }
2445
2446 cur_arg += 2;
2447 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002448 else if (!strcmp(args[cur_arg], "rise")) {
2449 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002450 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002451 file, linenum, args[cur_arg]);
2452 err_code |= ERR_ALERT | ERR_FATAL;
2453 goto out;
2454 }
2455
2456 newsrv->check.rise = atol(args[cur_arg + 1]);
2457 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002458 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002459 file, linenum, args[cur_arg]);
2460 err_code |= ERR_ALERT | ERR_FATAL;
2461 goto out;
2462 }
2463
2464 if (newsrv->check.health)
2465 newsrv->check.health = newsrv->check.rise;
2466 cur_arg += 2;
2467 }
2468 else if (!strcmp(args[cur_arg], "fall")) {
2469 newsrv->check.fall = atol(args[cur_arg + 1]);
2470
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 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002479 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002480 file, linenum, args[cur_arg]);
2481 err_code |= ERR_ALERT | ERR_FATAL;
2482 goto out;
2483 }
2484
2485 cur_arg += 2;
2486 }
2487 else if (!strcmp(args[cur_arg], "inter")) {
2488 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2489 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002490 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002491 file, linenum, *err, newsrv->id);
2492 err_code |= ERR_ALERT | ERR_FATAL;
2493 goto out;
2494 }
2495 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002496 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002497 file, linenum, val, args[cur_arg], newsrv->id);
2498 err_code |= ERR_ALERT | ERR_FATAL;
2499 goto out;
2500 }
2501 newsrv->check.inter = val;
2502 cur_arg += 2;
2503 }
2504 else if (!strcmp(args[cur_arg], "fastinter")) {
2505 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2506 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002507 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002508 file, linenum, *err, newsrv->id);
2509 err_code |= ERR_ALERT | ERR_FATAL;
2510 goto out;
2511 }
2512 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002513 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002514 file, linenum, val, args[cur_arg], newsrv->id);
2515 err_code |= ERR_ALERT | ERR_FATAL;
2516 goto out;
2517 }
2518 newsrv->check.fastinter = val;
2519 cur_arg += 2;
2520 }
2521 else if (!strcmp(args[cur_arg], "downinter")) {
2522 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2523 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002524 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002525 file, linenum, *err, newsrv->id);
2526 err_code |= ERR_ALERT | ERR_FATAL;
2527 goto out;
2528 }
2529 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002530 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002531 file, linenum, val, args[cur_arg], newsrv->id);
2532 err_code |= ERR_ALERT | ERR_FATAL;
2533 goto out;
2534 }
2535 newsrv->check.downinter = val;
2536 cur_arg += 2;
2537 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002538 else if (!strcmp(args[cur_arg], "port")) {
2539 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002540 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002541 cur_arg += 2;
2542 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002543 else if (!strcmp(args[cur_arg], "weight")) {
2544 int w;
2545 w = atol(args[cur_arg + 1]);
2546 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002547 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002548 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2549 err_code |= ERR_ALERT | ERR_FATAL;
2550 goto out;
2551 }
2552 newsrv->uweight = newsrv->iweight = w;
2553 cur_arg += 2;
2554 }
2555 else if (!strcmp(args[cur_arg], "minconn")) {
2556 newsrv->minconn = atol(args[cur_arg + 1]);
2557 cur_arg += 2;
2558 }
2559 else if (!strcmp(args[cur_arg], "maxconn")) {
2560 newsrv->maxconn = atol(args[cur_arg + 1]);
2561 cur_arg += 2;
2562 }
2563 else if (!strcmp(args[cur_arg], "maxqueue")) {
2564 newsrv->maxqueue = atol(args[cur_arg + 1]);
2565 cur_arg += 2;
2566 }
2567 else if (!strcmp(args[cur_arg], "slowstart")) {
2568 /* slowstart is stored in seconds */
2569 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2570 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002571 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002572 file, linenum, *err, newsrv->id);
2573 err_code |= ERR_ALERT | ERR_FATAL;
2574 goto out;
2575 }
2576 newsrv->slowstart = (val + 999) / 1000;
2577 cur_arg += 2;
2578 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002579 else if (!strcmp(args[cur_arg], "on-error")) {
2580 if (!strcmp(args[cur_arg + 1], "fastinter"))
2581 newsrv->onerror = HANA_ONERR_FASTINTER;
2582 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2583 newsrv->onerror = HANA_ONERR_FAILCHK;
2584 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2585 newsrv->onerror = HANA_ONERR_SUDDTH;
2586 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2587 newsrv->onerror = HANA_ONERR_MARKDWN;
2588 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002589 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002590 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2591 file, linenum, args[cur_arg], args[cur_arg + 1]);
2592 err_code |= ERR_ALERT | ERR_FATAL;
2593 goto out;
2594 }
2595
2596 cur_arg += 2;
2597 }
2598 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2599 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2600 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2601 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002602 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002603 file, linenum, args[cur_arg], args[cur_arg + 1]);
2604 err_code |= ERR_ALERT | ERR_FATAL;
2605 goto out;
2606 }
2607
2608 cur_arg += 2;
2609 }
2610 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2611 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2612 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2613 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002614 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002615 file, linenum, args[cur_arg], args[cur_arg + 1]);
2616 err_code |= ERR_ALERT | ERR_FATAL;
2617 goto out;
2618 }
2619
2620 cur_arg += 2;
2621 }
2622 else if (!strcmp(args[cur_arg], "error-limit")) {
2623 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002624 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002625 file, linenum, args[cur_arg]);
2626 err_code |= ERR_ALERT | ERR_FATAL;
2627 goto out;
2628 }
2629
2630 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2631
2632 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002633 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002634 file, linenum, args[cur_arg]);
2635 err_code |= ERR_ALERT | ERR_FATAL;
2636 goto out;
2637 }
2638 cur_arg += 2;
2639 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002640 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002641 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002642 file, linenum, "usesrc", "source");
2643 err_code |= ERR_ALERT | ERR_FATAL;
2644 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002645 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002646 else {
2647 static int srv_dumped;
2648 struct srv_kw *kw;
2649 char *err;
2650
2651 kw = srv_find_kw(args[cur_arg]);
2652 if (kw) {
2653 char *err = NULL;
2654 int code;
2655
2656 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002657 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 +02002658 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002659 if (kw->skip != -1)
2660 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002661 err_code |= ERR_ALERT | ERR_FATAL;
2662 goto out;
2663 }
2664
2665 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002666 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002667 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002668 if (kw->skip != -1)
2669 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002670 err_code |= ERR_ALERT;
2671 continue;
2672 }
2673
2674 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2675 err_code |= code;
2676
2677 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002678 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002679 if (code & ERR_FATAL) {
2680 free(err);
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 goto out;
2684 }
2685 }
2686 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002687 if (kw->skip != -1)
2688 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002689 continue;
2690 }
2691
2692 err = NULL;
2693 if (!srv_dumped) {
2694 srv_dump_kws(&err);
2695 indent_msg(&err, 4);
2696 srv_dumped = 1;
2697 }
2698
Christopher Faulet767a84b2017-11-24 16:50:31 +01002699 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002700 file, linenum, args[0], args[1], args[cur_arg],
2701 err ? " Registered keywords :" : "", err ? err : "");
2702 free(err);
2703
2704 err_code |= ERR_ALERT | ERR_FATAL;
2705 goto out;
2706 }
2707 }
2708
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002709 if (!defsrv)
2710 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2711 if (err_code & ERR_FATAL)
2712 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002713 if (srv_tmpl)
2714 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002715 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002716 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002717 return 0;
2718
2719 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002720 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002721 free(errmsg);
2722 return err_code;
2723}
2724
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002725/* Returns a pointer to the first server matching either id <id>.
2726 * NULL is returned if no match is found.
2727 * the lookup is performed in the backend <bk>
2728 */
2729struct server *server_find_by_id(struct proxy *bk, int id)
2730{
2731 struct eb32_node *eb32;
2732 struct server *curserver;
2733
2734 if (!bk || (id ==0))
2735 return NULL;
2736
2737 /* <bk> has no backend capabilities, so it can't have a server */
2738 if (!(bk->cap & PR_CAP_BE))
2739 return NULL;
2740
2741 curserver = NULL;
2742
2743 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2744 if (eb32)
2745 curserver = container_of(eb32, struct server, conf.id);
2746
2747 return curserver;
2748}
2749
2750/* Returns a pointer to the first server matching either name <name>, or id
2751 * if <name> starts with a '#'. NULL is returned if no match is found.
2752 * the lookup is performed in the backend <bk>
2753 */
2754struct server *server_find_by_name(struct proxy *bk, const char *name)
2755{
2756 struct server *curserver;
2757
2758 if (!bk || !name)
2759 return NULL;
2760
2761 /* <bk> has no backend capabilities, so it can't have a server */
2762 if (!(bk->cap & PR_CAP_BE))
2763 return NULL;
2764
2765 curserver = NULL;
2766 if (*name == '#') {
2767 curserver = server_find_by_id(bk, atoi(name + 1));
2768 if (curserver)
2769 return curserver;
2770 }
2771 else {
2772 curserver = bk->srv;
2773
2774 while (curserver && (strcmp(curserver->id, name) != 0))
2775 curserver = curserver->next;
2776
2777 if (curserver)
2778 return curserver;
2779 }
2780
2781 return NULL;
2782}
2783
2784struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2785{
2786 struct server *byname;
2787 struct server *byid;
2788
2789 if (!name && !id)
2790 return NULL;
2791
2792 if (diff)
2793 *diff = 0;
2794
2795 byname = byid = NULL;
2796
2797 if (name) {
2798 byname = server_find_by_name(bk, name);
2799 if (byname && (!id || byname->puid == id))
2800 return byname;
2801 }
2802
2803 /* remaining possibilities :
2804 * - name not set
2805 * - name set but not found
2806 * - name found but ID doesn't match
2807 */
2808 if (id) {
2809 byid = server_find_by_id(bk, id);
2810 if (byid) {
2811 if (byname) {
2812 /* use id only if forced by configuration */
2813 if (byid->flags & SRV_F_FORCED_ID) {
2814 if (diff)
2815 *diff |= 2;
2816 return byid;
2817 }
2818 else {
2819 if (diff)
2820 *diff |= 1;
2821 return byname;
2822 }
2823 }
2824
2825 /* remaining possibilities:
2826 * - name not set
2827 * - name set but not found
2828 */
2829 if (name && diff)
2830 *diff |= 2;
2831 return byid;
2832 }
2833
2834 /* id bot found */
2835 if (byname) {
2836 if (diff)
2837 *diff |= 1;
2838 return byname;
2839 }
2840 }
2841
2842 return NULL;
2843}
2844
Willy Tarreau46b7f532018-08-21 11:54:26 +02002845/* Update a server state using the parameters available in the params list.
2846 *
2847 * Grabs the server lock during operation.
2848 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002849static void srv_update_state(struct server *srv, int version, char **params)
2850{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002851 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002852 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002853
2854 /* fields since version 1
2855 * and common to all other upcoming versions
2856 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002857 enum srv_state srv_op_state;
2858 enum srv_admin srv_admin_state;
2859 unsigned srv_uweight, srv_iweight;
2860 unsigned long srv_last_time_change;
2861 short srv_check_status;
2862 enum chk_result srv_check_result;
2863 int srv_check_health;
2864 int srv_check_state, srv_agent_state;
2865 int bk_f_forced_id;
2866 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002867 int fqdn_set_by_cli;
2868 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002869 const char *port_str;
2870 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002871 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002872
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002873 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002874 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002875 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002876 switch (version) {
2877 case 1:
2878 /*
2879 * now we can proceed with server's state update:
2880 * srv_addr: params[0]
2881 * srv_op_state: params[1]
2882 * srv_admin_state: params[2]
2883 * srv_uweight: params[3]
2884 * srv_iweight: params[4]
2885 * srv_last_time_change: params[5]
2886 * srv_check_status: params[6]
2887 * srv_check_result: params[7]
2888 * srv_check_health: params[8]
2889 * srv_check_state: params[9]
2890 * srv_agent_state: params[10]
2891 * bk_f_forced_id: params[11]
2892 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002893 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002894 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002895 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002896 */
2897
2898 /* validating srv_op_state */
2899 p = NULL;
2900 errno = 0;
2901 srv_op_state = strtol(params[1], &p, 10);
2902 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2903 (srv_op_state != SRV_ST_STOPPED &&
2904 srv_op_state != SRV_ST_STARTING &&
2905 srv_op_state != SRV_ST_RUNNING &&
2906 srv_op_state != SRV_ST_STOPPING)) {
2907 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2908 }
2909
2910 /* validating srv_admin_state */
2911 p = NULL;
2912 errno = 0;
2913 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002914 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002915
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002916 /* inherited statuses will be recomputed later.
2917 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2918 */
2919 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002920
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002921 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2922 (srv_admin_state != 0 &&
2923 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002924 srv_admin_state != SRV_ADMF_CMAINT &&
2925 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002926 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002927 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002928 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2929 }
2930
2931 /* validating srv_uweight */
2932 p = NULL;
2933 errno = 0;
2934 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002935 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002936 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2937
2938 /* validating srv_iweight */
2939 p = NULL;
2940 errno = 0;
2941 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002942 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002943 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2944
2945 /* validating srv_last_time_change */
2946 p = NULL;
2947 errno = 0;
2948 srv_last_time_change = strtol(params[5], &p, 10);
2949 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2950 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2951
2952 /* validating srv_check_status */
2953 p = NULL;
2954 errno = 0;
2955 srv_check_status = strtol(params[6], &p, 10);
2956 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2957 (srv_check_status >= HCHK_STATUS_SIZE))
2958 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2959
2960 /* validating srv_check_result */
2961 p = NULL;
2962 errno = 0;
2963 srv_check_result = strtol(params[7], &p, 10);
2964 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2965 (srv_check_result != CHK_RES_UNKNOWN &&
2966 srv_check_result != CHK_RES_NEUTRAL &&
2967 srv_check_result != CHK_RES_FAILED &&
2968 srv_check_result != CHK_RES_PASSED &&
2969 srv_check_result != CHK_RES_CONDPASS)) {
2970 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2971 }
2972
2973 /* validating srv_check_health */
2974 p = NULL;
2975 errno = 0;
2976 srv_check_health = strtol(params[8], &p, 10);
2977 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2978 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2979
2980 /* validating srv_check_state */
2981 p = NULL;
2982 errno = 0;
2983 srv_check_state = strtol(params[9], &p, 10);
2984 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2985 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2986 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2987
2988 /* validating srv_agent_state */
2989 p = NULL;
2990 errno = 0;
2991 srv_agent_state = strtol(params[10], &p, 10);
2992 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2993 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2994 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2995
2996 /* validating bk_f_forced_id */
2997 p = NULL;
2998 errno = 0;
2999 bk_f_forced_id = strtol(params[11], &p, 10);
3000 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3001 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3002
3003 /* validating srv_f_forced_id */
3004 p = NULL;
3005 errno = 0;
3006 srv_f_forced_id = strtol(params[12], &p, 10);
3007 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3008 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3009
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003010 /* validating srv_fqdn */
3011 fqdn = params[13];
3012 if (fqdn && *fqdn == '-')
3013 fqdn = NULL;
3014 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3015 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3016 fqdn = NULL;
3017 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003018
Frédéric Lécaille31694712017-08-01 08:47:19 +02003019 port_str = params[14];
3020 if (port_str) {
3021 port = strl2uic(port_str, strlen(port_str));
3022 if (port > USHRT_MAX) {
3023 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3024 port_str = NULL;
3025 }
3026 }
3027
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003028 /* SRV record
3029 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3030 */
3031 srvrecord = params[15];
3032 if (srvrecord && *srvrecord != '_')
3033 srvrecord = NULL;
3034
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003035 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003036 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003037 goto out;
3038
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003039 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003040 /* recover operational state and apply it to this server
3041 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003042 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003043 switch (srv_op_state) {
3044 case SRV_ST_STOPPED:
3045 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003046 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003047 break;
3048 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003049 /* If rise == 1 there is no STARTING state, let's switch to
3050 * RUNNING
3051 */
3052 if (srv->check.rise == 1) {
3053 srv->check.health = srv->check.rise + srv->check.fall - 1;
3054 srv_set_running(srv, "", NULL);
3055 break;
3056 }
3057 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3058 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003059 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003060 break;
3061 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003062 /* If fall == 1 there is no STOPPING state, let's switch to
3063 * STOPPED
3064 */
3065 if (srv->check.fall == 1) {
3066 srv->check.health = 0;
3067 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3068 break;
3069 }
3070 if (srv->check.health < srv->check.rise ||
3071 srv->check.health > srv->check.rise + srv->check.fall - 2)
3072 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003073 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003074 break;
3075 case SRV_ST_RUNNING:
3076 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003077 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003078 break;
3079 }
3080
3081 /* When applying server state, the following rules apply:
3082 * - in case of a configuration change, we apply the setting from the new
3083 * configuration, regardless of old running state
3084 * - if no configuration change, we apply old running state only if old running
3085 * state is different from new configuration state
3086 */
3087 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003088 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3089 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003090 srv_adm_set_maint(srv);
3091 else
3092 srv_adm_set_ready(srv);
3093 }
3094 /* configuration is the same, let's compate old running state and new conf state */
3095 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003096 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003097 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003098 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003099 srv_adm_set_ready(srv);
3100 }
3101 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003102 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003103 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003104 * (srv->iweight is the weight set up in configuration).
3105 * There are two possible reasons for FDRAIN to have been present :
3106 * - previous config weight was zero
3107 * - "set server b/s drain" was sent to the CLI
3108 *
3109 * In the first case, we simply want to drop this drain state
3110 * if the new weight is not zero anymore, meaning the administrator
3111 * has intentionally turned the weight back to a positive value to
3112 * enable the server again after an operation. In the second case,
3113 * the drain state was forced on the CLI regardless of the config's
3114 * weight so we don't want a change to the config weight to lose this
3115 * status. What this means is :
3116 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3117 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003118 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003119 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003120 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003121 }
3122
3123 srv->last_change = date.tv_sec - srv_last_time_change;
3124 srv->check.status = srv_check_status;
3125 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003126
3127 /* Only case we want to apply is removing ENABLED flag which could have been
3128 * done by the "disable health" command over the stats socket
3129 */
3130 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3131 (srv_check_state & CHK_ST_CONFIGURED) &&
3132 !(srv_check_state & CHK_ST_ENABLED))
3133 srv->check.state &= ~CHK_ST_ENABLED;
3134
3135 /* Only case we want to apply is removing ENABLED flag which could have been
3136 * done by the "disable agent" command over the stats socket
3137 */
3138 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3139 (srv_agent_state & CHK_ST_CONFIGURED) &&
3140 !(srv_agent_state & CHK_ST_ENABLED))
3141 srv->agent.state &= ~CHK_ST_ENABLED;
3142
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003143 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3144 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003145 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003146 * It means that a configuration file change has precedence over a unix socket change
3147 * for server's weight
3148 *
3149 * by default, HAProxy applies the following weight when parsing the configuration
3150 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003151 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003152 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003153 srv->uweight = srv_uweight;
3154 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003155 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003156
Willy Tarreaue5a60682016-11-09 14:54:53 +01003157 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003158 if (strcmp(params[0], "-"))
3159 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003160
3161 if (fqdn && srv->hostname) {
3162 if (!strcmp(srv->hostname, fqdn)) {
3163 /* Here we reset the 'set from stats socket FQDN' flag
3164 * to support such transitions:
3165 * Let's say initial FQDN value is foo1 (in configuration file).
3166 * - FQDN changed from stats socket, from foo1 to foo2 value,
3167 * - FQDN changed again from file configuration (with the same previous value
3168 set from stats socket, from foo1 to foo2 value),
3169 * - reload for any other reason than a FQDN modification,
3170 * the configuration file FQDN matches the fqdn server state file value.
3171 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003172 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003173 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003174 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003175 }
3176 else {
3177 /* If the FDQN has been changed from stats socket,
3178 * apply fqdn state file value (which is the value set
3179 * from stats socket).
3180 */
3181 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003182 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003183 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003184 }
3185 }
3186 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003187 /* If all the conditions below are validated, this means
3188 * we're evaluating a server managed by SRV resolution
3189 */
3190 else if (fqdn && !srv->hostname && srvrecord) {
3191 int res;
3192
3193 /* we can't apply previous state if SRV record has changed */
3194 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3195 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);
3196 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3197 goto out;
3198 }
3199
3200 /* create or find a SRV resolution for this srv record */
3201 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3202 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3203 if (srv->srvrq == NULL) {
3204 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3205 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3206 goto out;
3207 }
3208
3209 /* prepare DNS resolution for this server */
3210 res = srv_prepare_for_resolution(srv, fqdn);
3211 if (res == -1) {
3212 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3213 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3214 goto out;
3215 }
3216
3217 /* configure check.port accordingly */
3218 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3219 !(srv->flags & SRV_F_CHECKPORT))
3220 srv->check.port = port;
3221
3222 /* Unset SRV_F_MAPPORTS for SRV records.
3223 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3224 * because no ports are provided in the configuration file.
3225 * This is because HAProxy will use the port found into the SRV record.
3226 */
3227 srv->flags &= ~SRV_F_MAPPORTS;
3228 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003229
Frédéric Lécaille31694712017-08-01 08:47:19 +02003230 if (port_str)
3231 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003232 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003233
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003234 break;
3235 default:
3236 chunk_appendf(msg, ", version '%d' not supported", version);
3237 }
3238
3239 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003240 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003241 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003242 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003243 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003244 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003245}
3246
3247/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3248 * For each proxy, it does the following:
3249 * - opens its server state file (either one or local one)
3250 * - read whole file, line by line
3251 * - analyse each line to check if it matches our current backend:
3252 * - backend name matches
3253 * - backend id matches if id is forced and name doesn't match
3254 * - if the server pointed by the line is found, then state is applied
3255 *
3256 * If the running backend uuid or id differs from the state file, then HAProxy reports
3257 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003258 *
3259 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003260 */
3261void apply_server_state(void)
3262{
3263 char *cur, *end;
3264 char mybuf[SRV_STATE_LINE_MAXLEN];
3265 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003266 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3267 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003268 int arg, srv_arg, version, diff;
3269 FILE *f;
3270 char *filepath;
3271 char globalfilepath[MAXPATHLEN + 1];
3272 char localfilepath[MAXPATHLEN + 1];
3273 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003274 struct proxy *curproxy, *bk;
3275 struct server *srv;
3276
3277 globalfilepathlen = 0;
3278 /* create the globalfilepath variable */
3279 if (global.server_state_file) {
3280 /* absolute path or no base directory provided */
3281 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3282 len = strlen(global.server_state_file);
3283 if (len > MAXPATHLEN) {
3284 globalfilepathlen = 0;
3285 goto globalfileerror;
3286 }
3287 memcpy(globalfilepath, global.server_state_file, len);
3288 globalfilepath[len] = '\0';
3289 globalfilepathlen = len;
3290 }
3291 else if (global.server_state_base) {
3292 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003293 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003294 globalfilepathlen = 0;
3295 goto globalfileerror;
3296 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003297 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003298 globalfilepath[len] = 0;
3299 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003300
3301 /* append a slash if needed */
3302 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3303 if (globalfilepathlen + 1 > MAXPATHLEN) {
3304 globalfilepathlen = 0;
3305 goto globalfileerror;
3306 }
3307 globalfilepath[globalfilepathlen++] = '/';
3308 }
3309
3310 len = strlen(global.server_state_file);
3311 if (globalfilepathlen + len > MAXPATHLEN) {
3312 globalfilepathlen = 0;
3313 goto globalfileerror;
3314 }
3315 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3316 globalfilepathlen += len;
3317 globalfilepath[globalfilepathlen++] = 0;
3318 }
3319 }
3320 globalfileerror:
3321 if (globalfilepathlen == 0)
3322 globalfilepath[0] = '\0';
3323
3324 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003325 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003326 /* servers are only in backends */
3327 if (!(curproxy->cap & PR_CAP_BE))
3328 continue;
3329 fileopenerr = 0;
3330 filepath = NULL;
3331
3332 /* search server state file path and name */
3333 switch (curproxy->load_server_state_from_file) {
3334 /* read servers state from global file */
3335 case PR_SRV_STATE_FILE_GLOBAL:
3336 /* there was an error while generating global server state file path */
3337 if (globalfilepathlen == 0)
3338 continue;
3339 filepath = globalfilepath;
3340 fileopenerr = 1;
3341 break;
3342 /* this backend has its own file */
3343 case PR_SRV_STATE_FILE_LOCAL:
3344 localfilepathlen = 0;
3345 localfilepath[0] = '\0';
3346 len = 0;
3347 /* create the localfilepath variable */
3348 /* absolute path or no base directory provided */
3349 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3350 len = strlen(curproxy->server_state_file_name);
3351 if (len > MAXPATHLEN) {
3352 localfilepathlen = 0;
3353 goto localfileerror;
3354 }
3355 memcpy(localfilepath, curproxy->server_state_file_name, len);
3356 localfilepath[len] = '\0';
3357 localfilepathlen = len;
3358 }
3359 else if (global.server_state_base) {
3360 len = strlen(global.server_state_base);
3361 localfilepathlen += len;
3362
3363 if (localfilepathlen > MAXPATHLEN) {
3364 localfilepathlen = 0;
3365 goto localfileerror;
3366 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003367 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003368 localfilepath[localfilepathlen] = 0;
3369
3370 /* append a slash if needed */
3371 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3372 if (localfilepathlen + 1 > MAXPATHLEN) {
3373 localfilepathlen = 0;
3374 goto localfileerror;
3375 }
3376 localfilepath[localfilepathlen++] = '/';
3377 }
3378
3379 len = strlen(curproxy->server_state_file_name);
3380 if (localfilepathlen + len > MAXPATHLEN) {
3381 localfilepathlen = 0;
3382 goto localfileerror;
3383 }
3384 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3385 localfilepathlen += len;
3386 localfilepath[localfilepathlen++] = 0;
3387 }
3388 filepath = localfilepath;
3389 localfileerror:
3390 if (localfilepathlen == 0)
3391 localfilepath[0] = '\0';
3392
3393 break;
3394 case PR_SRV_STATE_FILE_NONE:
3395 default:
3396 continue;
3397 }
3398
3399 /* preload global state file */
3400 errno = 0;
3401 f = fopen(filepath, "r");
3402 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003403 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003404 if (!f)
3405 continue;
3406
3407 mybuf[0] = '\0';
3408 mybuflen = 0;
3409 version = 0;
3410
3411 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003412 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003413 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003414 goto fileclose;
3415 }
3416
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003417 cur = mybuf;
3418 version = atoi(cur);
3419 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3420 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003421 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003422
3423 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3424 int bk_f_forced_id = 0;
3425 int check_id = 0;
3426 int check_name = 0;
3427
3428 mybuflen = strlen(mybuf);
3429 cur = mybuf;
3430 end = cur + mybuflen;
3431
3432 bk = NULL;
3433 srv = NULL;
3434
3435 /* we need at least one character */
3436 if (mybuflen == 0)
3437 continue;
3438
3439 /* ignore blank characters at the beginning of the line */
3440 while (isspace(*cur))
3441 ++cur;
3442
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003443 /* Ignore empty or commented lines */
3444 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003445 continue;
3446
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003447 /* truncated lines */
3448 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003449 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003450 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003451 }
3452
3453 /* Removes trailing '\n' */
3454 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003455
3456 /* we're now ready to move the line into *srv_params[] */
3457 params[0] = cur;
3458 arg = 1;
3459 srv_arg = 0;
3460 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3461 if (isspace(*cur)) {
3462 *cur = '\0';
3463 ++cur;
3464 while (isspace(*cur))
3465 ++cur;
3466 switch (version) {
3467 case 1:
3468 /*
3469 * srv_addr: params[4] => srv_params[0]
3470 * srv_op_state: params[5] => srv_params[1]
3471 * srv_admin_state: params[6] => srv_params[2]
3472 * srv_uweight: params[7] => srv_params[3]
3473 * srv_iweight: params[8] => srv_params[4]
3474 * srv_last_time_change: params[9] => srv_params[5]
3475 * srv_check_status: params[10] => srv_params[6]
3476 * srv_check_result: params[11] => srv_params[7]
3477 * srv_check_health: params[12] => srv_params[8]
3478 * srv_check_state: params[13] => srv_params[9]
3479 * srv_agent_state: params[14] => srv_params[10]
3480 * bk_f_forced_id: params[15] => srv_params[11]
3481 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003482 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003483 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003484 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003485 */
3486 if (arg >= 4) {
3487 srv_params[srv_arg] = cur;
3488 ++srv_arg;
3489 }
3490 break;
3491 }
3492
3493 params[arg] = cur;
3494 ++arg;
3495 }
3496 else {
3497 ++cur;
3498 }
3499 }
3500
3501 /* if line is incomplete line, then ignore it.
3502 * otherwise, update useful flags */
3503 switch (version) {
3504 case 1:
3505 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3506 continue;
3507 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3508 check_id = (atoi(params[0]) == curproxy->uuid);
3509 check_name = (strcmp(curproxy->id, params[1]) == 0);
3510 break;
3511 }
3512
3513 diff = 0;
3514 bk = curproxy;
3515
3516 /* if backend can't be found, let's continue */
3517 if (!check_id && !check_name)
3518 continue;
3519 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003520 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 +02003521 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3522 }
3523 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003524 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 +02003525 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3526 /* if name doesn't match, we still want to update curproxy if the backend id
3527 * was forced in previous the previous configuration */
3528 if (!bk_f_forced_id)
3529 continue;
3530 }
3531
3532 /* look for the server by its id: param[2] */
3533 /* else look for the server by its name: param[3] */
3534 diff = 0;
3535 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3536
3537 if (!srv) {
3538 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003539 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3540 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003541 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3542 params[3], params[2], params[0], params[1]);
3543 continue;
3544 }
3545 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003546 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 +02003547 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 +02003548 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003549 }
3550 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003551 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 +02003552 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 +02003553 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003554 }
3555
3556 /* now we can proceed with server's state update */
3557 srv_update_state(srv, version, srv_params);
3558 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003559fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003560 fclose(f);
3561 }
3562}
3563
Simon Horman7d09b9a2013-02-12 10:45:51 +09003564/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003565 * update a server's current IP address.
3566 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3567 * ip is in network format.
3568 * updater is a string which contains an information about the requester of the update.
3569 * updater is used if not NULL.
3570 *
3571 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003572 *
3573 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003574 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003575int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003576{
3577 /* generates a log line and a warning on stderr */
3578 if (1) {
3579 /* book enough space for both IPv4 and IPv6 */
3580 char oldip[INET6_ADDRSTRLEN];
3581 char newip[INET6_ADDRSTRLEN];
3582
3583 memset(oldip, '\0', INET6_ADDRSTRLEN);
3584 memset(newip, '\0', INET6_ADDRSTRLEN);
3585
3586 /* copy old IP address in a string */
3587 switch (s->addr.ss_family) {
3588 case AF_INET:
3589 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3590 break;
3591 case AF_INET6:
3592 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3593 break;
3594 };
3595
3596 /* copy new IP address in a string */
3597 switch (ip_sin_family) {
3598 case AF_INET:
3599 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3600 break;
3601 case AF_INET6:
3602 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3603 break;
3604 };
3605
3606 /* save log line into a buffer */
3607 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3608 s->proxy->id, s->id, oldip, newip, updater);
3609
3610 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003611 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003612
3613 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003614 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003615 }
3616
3617 /* save the new IP family */
3618 s->addr.ss_family = ip_sin_family;
3619 /* save the new IP address */
3620 switch (ip_sin_family) {
3621 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003622 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003623 break;
3624 case AF_INET6:
3625 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3626 break;
3627 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003628 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003629
3630 return 0;
3631}
3632
3633/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003634 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3635 *
3636 * Caller can pass its name through <updater> to get it integrated in the response message
3637 * returned by the function.
3638 *
3639 * The function first does the following, in that order:
3640 * - validates the new addr and/or port
3641 * - checks if an update is required (new IP or port is different than current ones)
3642 * - checks the update is allowed:
3643 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3644 * - allow all changes if no CHECKS are configured
3645 * - if CHECK is configured:
3646 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3647 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3648 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003649 *
3650 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003651 */
3652const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3653{
3654 struct sockaddr_storage sa;
3655 int ret, port_change_required;
3656 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003657 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003658 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003659 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003660
3661 msg = get_trash_chunk();
3662 chunk_reset(msg);
3663
3664 if (addr) {
3665 memset(&sa, 0, sizeof(struct sockaddr_storage));
3666 if (str2ip2(addr, &sa, 0) == NULL) {
3667 chunk_printf(msg, "Invalid addr '%s'", addr);
3668 goto out;
3669 }
3670
3671 /* changes are allowed on AF_INET* families only */
3672 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3673 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3674 goto out;
3675 }
3676
3677 /* collecting data currently setup */
3678 memset(current_addr, '\0', sizeof(current_addr));
3679 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3680 /* changes are allowed on AF_INET* families only */
3681 if ((ret != AF_INET) && (ret != AF_INET6)) {
3682 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3683 goto out;
3684 }
3685
3686 /* applying ADDR changes if required and allowed
3687 * ipcmp returns 0 when both ADDR are the same
3688 */
3689 if (ipcmp(&s->addr, &sa) == 0) {
3690 chunk_appendf(msg, "no need to change the addr");
3691 goto port;
3692 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003693 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003694 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003695
3696 /* we also need to update check's ADDR only if it uses the server's one */
3697 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003698 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003699 }
3700
3701 /* we also need to update agent ADDR only if it use the server's one */
3702 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003703 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003704 }
3705
3706 /* update report for caller */
3707 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3708 }
3709
3710 port:
3711 if (port) {
3712 char sign = '\0';
3713 char *endptr;
3714
3715 if (addr)
3716 chunk_appendf(msg, ", ");
3717
3718 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003719 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003720
3721 /* check if PORT change is required */
3722 port_change_required = 0;
3723
3724 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003725 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003726 new_port = strtol(port, &endptr, 10);
3727 if ((errno != 0) || (port == endptr)) {
3728 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3729 goto out;
3730 }
3731
3732 /* check if caller triggers a port mapped or offset */
3733 if (sign == '-' || (sign == '+')) {
3734 /* check if server currently uses port map */
3735 if (!(s->flags & SRV_F_MAPPORTS)) {
3736 /* switch from fixed port to port map mandatorily triggers
3737 * a port change */
3738 port_change_required = 1;
3739 /* check is configured
3740 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3741 * prevent PORT change if check doesn't have it's dedicated port while switching
3742 * to port mapping */
3743 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3744 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.");
3745 goto out;
3746 }
3747 }
3748 /* we're already using port maps */
3749 else {
3750 port_change_required = current_port != new_port;
3751 }
3752 }
3753 /* fixed port */
3754 else {
3755 port_change_required = current_port != new_port;
3756 }
3757
3758 /* applying PORT changes if required and update response message */
3759 if (port_change_required) {
3760 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003761 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003762 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003763
3764 /* prepare message */
3765 chunk_appendf(msg, "port changed from '");
3766 if (s->flags & SRV_F_MAPPORTS)
3767 chunk_appendf(msg, "+");
3768 chunk_appendf(msg, "%d' to '", current_port);
3769
3770 if (sign == '-') {
3771 s->flags |= SRV_F_MAPPORTS;
3772 chunk_appendf(msg, "%c", sign);
3773 /* just use for result output */
3774 new_port = -new_port;
3775 }
3776 else if (sign == '+') {
3777 s->flags |= SRV_F_MAPPORTS;
3778 chunk_appendf(msg, "%c", sign);
3779 }
3780 else {
3781 s->flags &= ~SRV_F_MAPPORTS;
3782 }
3783
3784 chunk_appendf(msg, "%d'", new_port);
3785
3786 /* we also need to update health checks port only if it uses server's realport */
3787 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3788 s->check.port = new_port;
3789 }
3790 }
3791 else {
3792 chunk_appendf(msg, "no need to change the port");
3793 }
3794 }
3795
3796out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003797 if (changed)
3798 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003799 if (updater)
3800 chunk_appendf(msg, " by '%s'", updater);
3801 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003802 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003803}
3804
3805
3806/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003807 * update server status based on result of name resolution
3808 * returns:
3809 * 0 if server status is updated
3810 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003811 *
3812 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003813 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003814int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003815{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003816 struct dns_resolvers *resolvers = s->resolvers;
3817 struct dns_resolution *resolution = s->dns_requester->resolution;
3818 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003819
3820 switch (resolution->status) {
3821 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003822 /* status when HAProxy has just (re)started.
3823 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003824 break;
3825
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003826 case RSLV_STATUS_VALID:
3827 /*
3828 * resume health checks
3829 * server will be turned back on if health check is safe
3830 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003831 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003832 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003833 return 1;
3834 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3835 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003836 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003837 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003838
Emeric Brun52a91d32017-08-31 14:41:55 +02003839 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003840 return 1;
3841 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3842 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3843 s->proxy->id, s->id);
3844
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003845 ha_warning("%s.\n", trash.area);
3846 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003847 return 0;
3848
3849 case RSLV_STATUS_NX:
3850 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003851 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3852 if (!tick_is_expired(exp, now_ms))
3853 break;
3854
3855 if (s->next_admin & SRV_ADMF_RMAINT)
3856 return 1;
3857 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3858 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003859
3860 case RSLV_STATUS_TIMEOUT:
3861 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003862 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3863 if (!tick_is_expired(exp, now_ms))
3864 break;
3865
3866 if (s->next_admin & SRV_ADMF_RMAINT)
3867 return 1;
3868 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3869 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003870
3871 case RSLV_STATUS_REFUSED:
3872 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003873 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
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 refused status");
3880 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003881
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003882 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003883 /* stop server if resolution failed for a long enough period */
3884 exp = tick_add(resolution->last_valid, resolvers->hold.other);
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, "unspecified DNS error");
3891 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003892 }
3893
3894 return 1;
3895}
3896
3897/*
3898 * Server Name Resolution valid response callback
3899 * It expects:
3900 * - <nameserver>: the name server which answered the valid response
3901 * - <response>: buffer containing a valid DNS response
3902 * - <response_len>: size of <response>
3903 * It performs the following actions:
3904 * - ignore response if current ip found and server family not met
3905 * - update with first new ip found if family is met and current IP is not found
3906 * returns:
3907 * 0 on error
3908 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003909 *
3910 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003911 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003912int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003913{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003914 struct server *s = NULL;
3915 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003916 void *serverip, *firstip;
3917 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003918 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003919 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003920 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003921
Christopher Faulet67957bd2017-09-27 11:00:59 +02003922 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003923 if (!s)
3924 return 1;
3925
Christopher Faulet67957bd2017-09-27 11:00:59 +02003926 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003927
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003928 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003929 firstip = NULL; /* pointer to the first valid response found */
3930 /* it will be used as the new IP if a change is required */
3931 firstip_sin_family = AF_UNSPEC;
3932 serverip = NULL; /* current server IP address */
3933
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003934 /* initializing server IP pointer */
3935 server_sin_family = s->addr.ss_family;
3936 switch (server_sin_family) {
3937 case AF_INET:
3938 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3939 break;
3940
3941 case AF_INET6:
3942 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3943 break;
3944
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003945 case AF_UNSPEC:
3946 break;
3947
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003948 default:
3949 goto invalid;
3950 }
3951
Baptiste Assmann729c9012017-05-22 15:13:10 +02003952 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003953 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003954 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003955
3956 switch (ret) {
3957 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003958 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003959
3960 case DNS_UPD_SRVIP_NOT_FOUND:
3961 goto save_ip;
3962
3963 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003964 goto invalid;
3965
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003966 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003967 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003968 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003969
Baptiste Assmannfad03182015-10-28 02:03:32 +01003970 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003971 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003972 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003973 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003974
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003975 default:
3976 goto invalid;
3977
3978 }
3979
3980 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003981 if (nameserver) {
3982 nameserver->counters.update++;
3983 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003984 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003985 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003986 else
3987 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003988 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003989
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003990 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003991 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003992 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003993
3994 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003995 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003996 nameserver->counters.invalid++;
3997 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003998 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003999 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004000 return 0;
4001}
4002
4003/*
4004 * Server Name Resolution error management callback
4005 * returns:
4006 * 0 on error
4007 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004008 *
4009 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004010 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004011int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004012{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004013 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004014
Christopher Faulet67957bd2017-09-27 11:00:59 +02004015 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004016 if (!s)
4017 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004018 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004019 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004020 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004021 return 1;
4022}
4023
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004024/*
4025 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004026 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004027 * It returns a pointer to the first server found or NULL if <ip> is not yet
4028 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004029 *
4030 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004031 */
4032struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4033{
4034 struct server *tmpsrv;
4035 struct proxy *be;
4036
4037 if (!srv)
4038 return NULL;
4039
4040 be = srv->proxy;
4041 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004042 /* we found the current server is the same, ignore it */
4043 if (srv == tmpsrv)
4044 continue;
4045
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004046 /* We want to compare the IP in the record with the IP of the servers in the
4047 * same backend, only if:
4048 * * DNS resolution is enabled on the server
4049 * * the hostname used for the resolution by our server is the same than the
4050 * one used for the server found in the backend
4051 * * the server found in the backend is not our current server
4052 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004053 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004054 if ((tmpsrv->hostname_dn == NULL) ||
4055 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4056 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004057 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004058 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004059 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004060 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004061
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004062 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004063 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004064 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004065 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004066 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004067
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004068 /* At this point, we have 2 different servers using the same DNS hostname
4069 * for their respective resolution.
4070 */
4071 if (*ip_family == tmpsrv->addr.ss_family &&
4072 ((tmpsrv->addr.ss_family == AF_INET &&
4073 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4074 (tmpsrv->addr.ss_family == AF_INET6 &&
4075 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004076 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004077 return tmpsrv;
4078 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004079 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004080 }
4081
Emeric Brune9fd6b52017-11-02 17:20:39 +01004082
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004083 return NULL;
4084}
4085
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004086/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4087 * resolver. This is suited for initial address configuration. Returns 0 on
4088 * success otherwise a non-zero error code. In case of error, *err_code, if
4089 * not NULL, is filled up.
4090 */
4091int srv_set_addr_via_libc(struct server *srv, int *err_code)
4092{
4093 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004094 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004095 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004096 return 1;
4097 }
4098 return 0;
4099}
4100
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004101/* Set the server's FDQN (->hostname) from <hostname>.
4102 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004103 *
4104 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004105 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004106int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004107{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004108 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004109 char *hostname_dn;
4110 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004111
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004112 /* Note that the server lock is already held. */
4113 if (!srv->resolvers)
4114 return -1;
4115
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004116 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004117 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004118 /* run time DNS resolution was not active for this server
4119 * and we can't enable it at run time for now.
4120 */
4121 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004122 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004123
4124 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004125 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004126 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004127 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4128 hostname_dn, trash.size);
4129 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004130 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004131
Christopher Faulet67957bd2017-09-27 11:00:59 +02004132 resolution = srv->dns_requester->resolution;
4133 if (resolution &&
4134 resolution->hostname_dn &&
4135 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004136 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004137
Christopher Faulet67957bd2017-09-27 11:00:59 +02004138 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004139
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004140 free(srv->hostname);
4141 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004142 srv->hostname = strdup(hostname);
4143 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004144 srv->hostname_dn_len = hostname_dn_len;
4145 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004146 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004147
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004148 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004149 goto err;
4150
4151 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004152 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004153 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004154 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004155
4156 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004157 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004158 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004159 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004160}
4161
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004162/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4163 * from the state file. This is suited for initial address configuration.
4164 * Returns 0 on success otherwise a non-zero error code. In case of error,
4165 * *err_code, if not NULL, is filled up.
4166 */
4167static int srv_apply_lastaddr(struct server *srv, int *err_code)
4168{
4169 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4170 if (err_code)
4171 *err_code |= ERR_WARN;
4172 return 1;
4173 }
4174 return 0;
4175}
4176
Willy Tarreau25e51522016-11-04 15:10:17 +01004177/* returns 0 if no error, otherwise a combination of ERR_* flags */
4178static int srv_iterate_initaddr(struct server *srv)
4179{
4180 int return_code = 0;
4181 int err_code;
4182 unsigned int methods;
4183
4184 methods = srv->init_addr_methods;
4185 if (!methods) { // default to "last,libc"
4186 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4187 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4188 }
4189
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004190 /* "-dr" : always append "none" so that server addresses resolution
4191 * failures are silently ignored, this is convenient to validate some
4192 * configs out of their environment.
4193 */
4194 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4195 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4196
Willy Tarreau25e51522016-11-04 15:10:17 +01004197 while (methods) {
4198 err_code = 0;
4199 switch (srv_get_next_initaddr(&methods)) {
4200 case SRV_IADDR_LAST:
4201 if (!srv->lastaddr)
4202 continue;
4203 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004204 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004205 return_code |= err_code;
4206 break;
4207
4208 case SRV_IADDR_LIBC:
4209 if (!srv->hostname)
4210 continue;
4211 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004212 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004213 return_code |= err_code;
4214 break;
4215
Willy Tarreau37ebe122016-11-04 15:17:58 +01004216 case SRV_IADDR_NONE:
4217 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004218 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004219 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4220 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004221 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004222 return return_code;
4223
Willy Tarreau4310d362016-11-02 15:05:56 +01004224 case SRV_IADDR_IP:
4225 ipcpy(&srv->init_addr, &srv->addr);
4226 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004227 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4228 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004229 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004230 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004231
Willy Tarreau25e51522016-11-04 15:10:17 +01004232 default: /* unhandled method */
4233 break;
4234 }
4235 }
4236
4237 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004238 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004239 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4240 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004241 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004242 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004243 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4244 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004245
4246 return_code |= ERR_ALERT | ERR_FATAL;
4247 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004248out:
4249 srv_set_dyncookie(srv);
4250 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004251}
4252
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004253/*
4254 * This function parses all backends and all servers within each backend
4255 * and performs servers' addr resolution based on information provided by:
4256 * - configuration file
4257 * - server-state file (states provided by an 'old' haproxy process)
4258 *
4259 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4260 */
4261int srv_init_addr(void)
4262{
4263 struct proxy *curproxy;
4264 int return_code = 0;
4265
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004266 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004267 while (curproxy) {
4268 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004269
4270 /* servers are in backend only */
4271 if (!(curproxy->cap & PR_CAP_BE))
4272 goto srv_init_addr_next;
4273
Willy Tarreau25e51522016-11-04 15:10:17 +01004274 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004275 if (srv->hostname)
4276 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004277
4278 srv_init_addr_next:
4279 curproxy = curproxy->next;
4280 }
4281
4282 return return_code;
4283}
4284
Willy Tarreau46b7f532018-08-21 11:54:26 +02004285/*
4286 * Must be called with the server lock held.
4287 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004288const 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 +02004289{
4290
Willy Tarreau83061a82018-07-13 11:56:34 +02004291 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004292
4293 msg = get_trash_chunk();
4294 chunk_reset(msg);
4295
Olivier Houchard8da5f982017-08-04 18:35:36 +02004296 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004297 chunk_appendf(msg, "no need to change the FDQN");
4298 goto out;
4299 }
4300
4301 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4302 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4303 goto out;
4304 }
4305
4306 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4307 server->proxy->id, server->id, server->hostname, fqdn);
4308
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004309 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004310 chunk_reset(msg);
4311 chunk_appendf(msg, "could not update %s/%s FQDN",
4312 server->proxy->id, server->id);
4313 goto out;
4314 }
4315
4316 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004317 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004318
4319 out:
4320 if (updater)
4321 chunk_appendf(msg, " by '%s'", updater);
4322 chunk_appendf(msg, "\n");
4323
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004324 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004325}
4326
4327
Willy Tarreau21b069d2016-11-23 17:15:08 +01004328/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4329 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004330 * 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 +01004331 * used for CLI commands requiring a server name.
4332 * Important: the <arg> is modified to remove the '/'.
4333 */
4334struct server *cli_find_server(struct appctx *appctx, char *arg)
4335{
4336 struct proxy *px;
4337 struct server *sv;
4338 char *line;
4339
4340 /* split "backend/server" and make <line> point to server */
4341 for (line = arg; *line; line++)
4342 if (*line == '/') {
4343 *line++ = '\0';
4344 break;
4345 }
4346
4347 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004348 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004349 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004350 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004351 return NULL;
4352 }
4353
4354 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004355 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004356 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004357 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004358 return NULL;
4359 }
4360
4361 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004362 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004363 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004364 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004365 return NULL;
4366 }
4367
4368 return sv;
4369}
4370
William Lallemand222baf22016-11-19 02:00:33 +01004371
Willy Tarreau46b7f532018-08-21 11:54:26 +02004372/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004373static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004374{
4375 struct server *sv;
4376 const char *warning;
4377
4378 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4379 return 1;
4380
4381 sv = cli_find_server(appctx, args[2]);
4382 if (!sv)
4383 return 1;
4384
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004385 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004386
William Lallemand222baf22016-11-19 02:00:33 +01004387 if (strcmp(args[3], "weight") == 0) {
4388 warning = server_parse_weight_change_request(sv, args[4]);
4389 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004390 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004391 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004392 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004393 }
4394 }
4395 else if (strcmp(args[3], "state") == 0) {
4396 if (strcmp(args[4], "ready") == 0)
4397 srv_adm_set_ready(sv);
4398 else if (strcmp(args[4], "drain") == 0)
4399 srv_adm_set_drain(sv);
4400 else if (strcmp(args[4], "maint") == 0)
4401 srv_adm_set_maint(sv);
4402 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004403 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004404 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004405 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004406 }
4407 }
4408 else if (strcmp(args[3], "health") == 0) {
4409 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004410 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004411 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004412 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004413 }
4414 else if (strcmp(args[4], "up") == 0) {
4415 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004416 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004417 }
4418 else if (strcmp(args[4], "stopping") == 0) {
4419 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004420 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004421 }
4422 else if (strcmp(args[4], "down") == 0) {
4423 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004424 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004425 }
4426 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004427 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004428 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004429 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004430 }
4431 }
4432 else if (strcmp(args[3], "agent") == 0) {
4433 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004434 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004435 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004436 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004437 }
4438 else if (strcmp(args[4], "up") == 0) {
4439 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004440 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004441 }
4442 else if (strcmp(args[4], "down") == 0) {
4443 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004444 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004445 }
4446 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004447 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004448 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004449 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004450 }
4451 }
Misiek2da082d2017-01-09 09:40:42 +01004452 else if (strcmp(args[3], "agent-addr") == 0) {
4453 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004454 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004455 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4456 appctx->st0 = CLI_ST_PRINT;
4457 } else {
4458 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004459 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004460 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4461 appctx->st0 = CLI_ST_PRINT;
4462 }
4463 }
4464 }
4465 else if (strcmp(args[3], "agent-send") == 0) {
4466 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004467 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004468 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4469 appctx->st0 = CLI_ST_PRINT;
4470 } else {
4471 char *nss = strdup(args[4]);
4472 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004473 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004474 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4475 appctx->st0 = CLI_ST_PRINT;
4476 } else {
4477 free(sv->agent.send_string);
4478 sv->agent.send_string = nss;
4479 sv->agent.send_string_len = strlen(args[4]);
4480 }
4481 }
4482 }
William Lallemand222baf22016-11-19 02:00:33 +01004483 else if (strcmp(args[3], "check-port") == 0) {
4484 int i = 0;
4485 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004486 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004487 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004488 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004489 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004490 }
4491 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004492 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004493 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004494 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004495 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004496 }
4497 /* prevent the update of port to 0 if MAPPORTS are in use */
4498 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004499 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004500 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004501 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004502 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004503 }
4504 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004505 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004506 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004507 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004508 }
4509 else if (strcmp(args[3], "addr") == 0) {
4510 char *addr = NULL;
4511 char *port = NULL;
4512 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004513 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004514 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004515 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004516 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004517 }
4518 else {
4519 addr = args[4];
4520 }
4521 if (strcmp(args[5], "port") == 0) {
4522 port = args[6];
4523 }
4524 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4525 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004526 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004527 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004528 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004529 }
4530 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4531 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004532 else if (strcmp(args[3], "fqdn") == 0) {
4533 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004534 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004535 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4536 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004537 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004538 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004539 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004540 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004541 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004542 appctx->ctx.cli.msg = warning;
4543 appctx->st0 = CLI_ST_PRINT;
4544 }
4545 }
William Lallemand222baf22016-11-19 02:00:33 +01004546 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004547 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004548 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 +01004549 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004550 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004551 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004552 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004553 return 1;
4554}
4555
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004556static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004557{
4558 struct stream_interface *si = appctx->owner;
4559 struct proxy *px;
4560 struct server *sv;
4561 char *line;
4562
4563
4564 /* split "backend/server" and make <line> point to server */
4565 for (line = args[2]; *line; line++)
4566 if (*line == '/') {
4567 *line++ = '\0';
4568 break;
4569 }
4570
4571 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004572 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004573 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004574 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004575 return 1;
4576 }
4577
4578 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004579 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004580 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004581 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004582 return 1;
4583 }
4584
4585 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004586 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4587 sv->iweight);
4588 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004589 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004590 return 0;
4591 }
William Lallemand6b160942016-11-22 12:34:35 +01004592 return 1;
4593}
4594
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004595/* Parse a "set weight" command.
4596 *
4597 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004598 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004599static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004600{
4601 struct server *sv;
4602 const char *warning;
4603
4604 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4605 return 1;
4606
4607 sv = cli_find_server(appctx, args[2]);
4608 if (!sv)
4609 return 1;
4610
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004611 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4612
William Lallemand6b160942016-11-22 12:34:35 +01004613 warning = server_parse_weight_change_request(sv, args[3]);
4614 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004615 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004616 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004617 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004618 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004619
4620 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4621
William Lallemand6b160942016-11-22 12:34:35 +01004622 return 1;
4623}
4624
Willy Tarreau46b7f532018-08-21 11:54:26 +02004625/* parse a "set maxconn server" command. It always returns 1.
4626 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004627 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004628 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004629static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004630{
4631 struct server *sv;
4632 const char *warning;
4633
4634 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4635 return 1;
4636
4637 sv = cli_find_server(appctx, args[3]);
4638 if (!sv)
4639 return 1;
4640
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004641 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4642
Willy Tarreaub8026272016-11-23 11:26:56 +01004643 warning = server_parse_maxconn_change_request(sv, args[4]);
4644 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004645 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004646 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004647 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004648 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004649
4650 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4651
Willy Tarreaub8026272016-11-23 11:26:56 +01004652 return 1;
4653}
William Lallemand6b160942016-11-22 12:34:35 +01004654
Willy Tarreau46b7f532018-08-21 11:54:26 +02004655/* parse a "disable agent" command. It always returns 1.
4656 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004657 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004658 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004659static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004660{
4661 struct server *sv;
4662
4663 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4664 return 1;
4665
4666 sv = cli_find_server(appctx, args[2]);
4667 if (!sv)
4668 return 1;
4669
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004670 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004671 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004672 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004673 return 1;
4674}
4675
Willy Tarreau46b7f532018-08-21 11:54:26 +02004676/* parse a "disable health" command. It always returns 1.
4677 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004678 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004679 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004680static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004681{
4682 struct server *sv;
4683
4684 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4685 return 1;
4686
4687 sv = cli_find_server(appctx, args[2]);
4688 if (!sv)
4689 return 1;
4690
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004691 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004692 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004693 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004694 return 1;
4695}
4696
Willy Tarreau46b7f532018-08-21 11:54:26 +02004697/* parse a "disable server" command. It always returns 1.
4698 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004699 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004700 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004701static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004702{
4703 struct server *sv;
4704
4705 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4706 return 1;
4707
4708 sv = cli_find_server(appctx, args[2]);
4709 if (!sv)
4710 return 1;
4711
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004712 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004713 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004714 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004715 return 1;
4716}
4717
Willy Tarreau46b7f532018-08-21 11:54:26 +02004718/* parse a "enable agent" command. It always returns 1.
4719 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004720 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004721 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004722static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004723{
4724 struct server *sv;
4725
4726 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4727 return 1;
4728
4729 sv = cli_find_server(appctx, args[2]);
4730 if (!sv)
4731 return 1;
4732
4733 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004734 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004735 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004736 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004737 return 1;
4738 }
4739
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004740 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004741 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004742 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004743 return 1;
4744}
4745
Willy Tarreau46b7f532018-08-21 11:54:26 +02004746/* parse a "enable health" command. It always returns 1.
4747 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004748 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004749 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004750static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004751{
4752 struct server *sv;
4753
4754 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4755 return 1;
4756
4757 sv = cli_find_server(appctx, args[2]);
4758 if (!sv)
4759 return 1;
4760
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004761 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004762 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004763 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004764 return 1;
4765}
4766
Willy Tarreau46b7f532018-08-21 11:54:26 +02004767/* parse a "enable server" command. It always returns 1.
4768 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004769 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004770 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004771static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004772{
4773 struct server *sv;
4774
4775 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4776 return 1;
4777
4778 sv = cli_find_server(appctx, args[2]);
4779 if (!sv)
4780 return 1;
4781
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004782 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004783 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004784 if (!(sv->flags & SRV_F_COOKIESET)
4785 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4786 sv->cookie)
4787 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004788 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004789 return 1;
4790}
4791
William Lallemand222baf22016-11-19 02:00:33 +01004792/* register cli keywords */
4793static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004794 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004795 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004796 { { "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 +01004797 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004798 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004799 { { "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 +01004800 { { "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 +01004801 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004802 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4803 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4804
William Lallemand222baf22016-11-19 02:00:33 +01004805 {{},}
4806}};
4807
Willy Tarreau0108d902018-11-25 19:14:37 +01004808INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004809
Emeric Brun64cc49c2017-10-03 14:46:45 +02004810/*
4811 * This function applies server's status changes, it is
4812 * is designed to be called asynchronously.
4813 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004814 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004815 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004816static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004817{
4818 struct check *check = &s->check;
4819 int xferred;
4820 struct proxy *px = s->proxy;
4821 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4822 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4823 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004824 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004825
Emeric Brun64cc49c2017-10-03 14:46:45 +02004826 /* If currently main is not set we try to apply pending state changes */
4827 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4828 int next_admin;
4829
4830 /* Backup next admin */
4831 next_admin = s->next_admin;
4832
4833 /* restore current admin state */
4834 s->next_admin = s->cur_admin;
4835
4836 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4837 s->last_change = now.tv_sec;
4838 if (s->proxy->lbprm.set_server_status_down)
4839 s->proxy->lbprm.set_server_status_down(s);
4840
4841 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4842 srv_shutdown_streams(s, SF_ERR_DOWN);
4843
4844 /* we might have streams queued on this server and waiting for
4845 * a connection. Those which are redispatchable will be queued
4846 * to another server or to the proxy itself.
4847 */
4848 xferred = pendconn_redistribute(s);
4849
4850 tmptrash = alloc_trash_chunk();
4851 if (tmptrash) {
4852 chunk_printf(tmptrash,
4853 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4854 s->proxy->id, s->id);
4855
Emeric Brun5a133512017-10-19 14:42:30 +02004856 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004857 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004858
4859 /* we don't send an alert if the server was previously paused */
4860 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004861 send_log(s->proxy, log_level, "%s.\n",
4862 tmptrash->area);
4863 send_email_alert(s, log_level, "%s",
4864 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004865 free_trash_chunk(tmptrash);
4866 tmptrash = NULL;
4867 }
4868 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4869 set_backend_down(s->proxy);
4870
4871 s->counters.down_trans++;
4872 }
4873 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4874 s->last_change = now.tv_sec;
4875 if (s->proxy->lbprm.set_server_status_down)
4876 s->proxy->lbprm.set_server_status_down(s);
4877
4878 /* we might have streams queued on this server and waiting for
4879 * a connection. Those which are redispatchable will be queued
4880 * to another server or to the proxy itself.
4881 */
4882 xferred = pendconn_redistribute(s);
4883
4884 tmptrash = alloc_trash_chunk();
4885 if (tmptrash) {
4886 chunk_printf(tmptrash,
4887 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4888 s->proxy->id, s->id);
4889
Emeric Brun5a133512017-10-19 14:42:30 +02004890 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004891
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004892 ha_warning("%s.\n", tmptrash->area);
4893 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4894 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004895 free_trash_chunk(tmptrash);
4896 tmptrash = NULL;
4897 }
4898
4899 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4900 set_backend_down(s->proxy);
4901 }
4902 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4903 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4904 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4905 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4906 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4907 s->proxy->last_change = now.tv_sec;
4908 }
4909
4910 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4911 s->down_time += now.tv_sec - s->last_change;
4912
4913 s->last_change = now.tv_sec;
4914 if (s->next_state == SRV_ST_STARTING)
4915 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4916
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004917 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004918 /* now propagate the status change to any LB algorithms */
4919 if (px->lbprm.update_server_eweight)
4920 px->lbprm.update_server_eweight(s);
4921 else if (srv_willbe_usable(s)) {
4922 if (px->lbprm.set_server_status_up)
4923 px->lbprm.set_server_status_up(s);
4924 }
4925 else {
4926 if (px->lbprm.set_server_status_down)
4927 px->lbprm.set_server_status_down(s);
4928 }
4929
4930 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4931 * and it's not a backup server and its effective weight is > 0,
4932 * then it can accept new connections, so we shut down all streams
4933 * on all backup servers.
4934 */
4935 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4936 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4937 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4938
4939 /* check if we can handle some connections queued at the proxy. We
4940 * will take as many as we can handle.
4941 */
4942 xferred = pendconn_grab_from_px(s);
4943
4944 tmptrash = alloc_trash_chunk();
4945 if (tmptrash) {
4946 chunk_printf(tmptrash,
4947 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4948 s->proxy->id, s->id);
4949
Emeric Brun5a133512017-10-19 14:42:30 +02004950 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004951 ha_warning("%s.\n", tmptrash->area);
4952 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4953 tmptrash->area);
4954 send_email_alert(s, LOG_NOTICE, "%s",
4955 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004956 free_trash_chunk(tmptrash);
4957 tmptrash = NULL;
4958 }
4959
4960 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4961 set_backend_down(s->proxy);
4962 }
4963 else if (s->cur_eweight != s->next_eweight) {
4964 /* now propagate the status change to any LB algorithms */
4965 if (px->lbprm.update_server_eweight)
4966 px->lbprm.update_server_eweight(s);
4967 else if (srv_willbe_usable(s)) {
4968 if (px->lbprm.set_server_status_up)
4969 px->lbprm.set_server_status_up(s);
4970 }
4971 else {
4972 if (px->lbprm.set_server_status_down)
4973 px->lbprm.set_server_status_down(s);
4974 }
4975
4976 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4977 set_backend_down(s->proxy);
4978 }
4979
4980 s->next_admin = next_admin;
4981 }
4982
Emeric Brun5a133512017-10-19 14:42:30 +02004983 /* reset operational state change */
4984 *s->op_st_chg.reason = 0;
4985 s->op_st_chg.status = s->op_st_chg.code = -1;
4986 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004987
4988 /* Now we try to apply pending admin changes */
4989
4990 /* Maintenance must also disable health checks */
4991 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4992 if (s->check.state & CHK_ST_ENABLED) {
4993 s->check.state |= CHK_ST_PAUSED;
4994 check->health = 0;
4995 }
4996
4997 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004998 tmptrash = alloc_trash_chunk();
4999 if (tmptrash) {
5000 chunk_printf(tmptrash,
5001 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5002 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5003 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005004
Olivier Houchard796a2b32017-10-24 17:42:47 +02005005 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005006
Olivier Houchard796a2b32017-10-24 17:42:47 +02005007 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005008 ha_warning("%s.\n", tmptrash->area);
5009 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5010 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005011 }
5012 free_trash_chunk(tmptrash);
5013 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005014 }
Emeric Brun8f298292017-12-06 16:47:17 +01005015 /* commit new admin status */
5016
5017 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005018 }
5019 else { /* server was still running */
5020 check->health = 0; /* failure */
5021 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005022
5023 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005024 if (s->proxy->lbprm.set_server_status_down)
5025 s->proxy->lbprm.set_server_status_down(s);
5026
Emeric Brun64cc49c2017-10-03 14:46:45 +02005027 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5028 srv_shutdown_streams(s, SF_ERR_DOWN);
5029
5030 /* we might have streams queued on this server and waiting for
5031 * a connection. Those which are redispatchable will be queued
5032 * to another server or to the proxy itself.
5033 */
5034 xferred = pendconn_redistribute(s);
5035
5036 tmptrash = alloc_trash_chunk();
5037 if (tmptrash) {
5038 chunk_printf(tmptrash,
5039 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5040 s->flags & SRV_F_BACKUP ? "Backup " : "",
5041 s->proxy->id, s->id,
5042 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5043
5044 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5045
5046 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005047 ha_warning("%s.\n", tmptrash->area);
5048 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5049 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005050 }
5051 free_trash_chunk(tmptrash);
5052 tmptrash = NULL;
5053 }
5054 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5055 set_backend_down(s->proxy);
5056
5057 s->counters.down_trans++;
5058 }
5059 }
5060 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5061 /* OK here we're leaving maintenance, we have many things to check,
5062 * because the server might possibly be coming back up depending on
5063 * its state. In practice, leaving maintenance means that we should
5064 * immediately turn to UP (more or less the slowstart) under the
5065 * following conditions :
5066 * - server is neither checked nor tracked
5067 * - server tracks another server which is not checked
5068 * - server tracks another server which is already up
5069 * Which sums up as something simpler :
5070 * "either the tracking server is up or the server's checks are disabled
5071 * or up". Otherwise we only re-enable health checks. There's a special
5072 * case associated to the stopping state which can be inherited. Note
5073 * that the server might still be in drain mode, which is naturally dealt
5074 * with by the lower level functions.
5075 */
5076
5077 if (s->check.state & CHK_ST_ENABLED) {
5078 s->check.state &= ~CHK_ST_PAUSED;
5079 check->health = check->rise; /* start OK but check immediately */
5080 }
5081
5082 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5083 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5084 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5085 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5086 s->next_state = SRV_ST_STOPPING;
5087 }
5088 else {
5089 s->next_state = SRV_ST_STARTING;
5090 if (s->slowstart > 0)
5091 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5092 else
5093 s->next_state = SRV_ST_RUNNING;
5094 }
5095
5096 }
5097
5098 tmptrash = alloc_trash_chunk();
5099 if (tmptrash) {
5100 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5101 chunk_printf(tmptrash,
5102 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5103 s->flags & SRV_F_BACKUP ? "Backup " : "",
5104 s->proxy->id, s->id,
5105 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5106 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5107 }
5108 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5109 chunk_printf(tmptrash,
5110 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5111 s->flags & SRV_F_BACKUP ? "Backup " : "",
5112 s->proxy->id, s->id, s->hostname,
5113 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5114 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5115 }
5116 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5117 chunk_printf(tmptrash,
5118 "%sServer %s/%s is %s/%s (leaving maintenance)",
5119 s->flags & SRV_F_BACKUP ? "Backup " : "",
5120 s->proxy->id, s->id,
5121 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5122 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5123 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005124 ha_warning("%s.\n", tmptrash->area);
5125 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5126 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005127 free_trash_chunk(tmptrash);
5128 tmptrash = NULL;
5129 }
5130
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005131 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005132 /* now propagate the status change to any LB algorithms */
5133 if (px->lbprm.update_server_eweight)
5134 px->lbprm.update_server_eweight(s);
5135 else if (srv_willbe_usable(s)) {
5136 if (px->lbprm.set_server_status_up)
5137 px->lbprm.set_server_status_up(s);
5138 }
5139 else {
5140 if (px->lbprm.set_server_status_down)
5141 px->lbprm.set_server_status_down(s);
5142 }
5143
5144 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5145 set_backend_down(s->proxy);
5146
Willy Tarreau6a78e612018-08-07 10:14:53 +02005147 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5148 * and it's not a backup server and its effective weight is > 0,
5149 * then it can accept new connections, so we shut down all streams
5150 * on all backup servers.
5151 */
5152 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5153 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5154 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5155
5156 /* check if we can handle some connections queued at the proxy. We
5157 * will take as many as we can handle.
5158 */
5159 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005160 }
5161 else if (s->next_admin & SRV_ADMF_MAINT) {
5162 /* remaining in maintenance mode, let's inform precisely about the
5163 * situation.
5164 */
5165 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5166 tmptrash = alloc_trash_chunk();
5167 if (tmptrash) {
5168 chunk_printf(tmptrash,
5169 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5170 s->flags & SRV_F_BACKUP ? "Backup " : "",
5171 s->proxy->id, s->id);
5172
5173 if (s->track) /* normally it's mandatory here */
5174 chunk_appendf(tmptrash, " via %s/%s",
5175 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005176 ha_warning("%s.\n", tmptrash->area);
5177 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5178 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005179 free_trash_chunk(tmptrash);
5180 tmptrash = NULL;
5181 }
5182 }
5183 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5184 tmptrash = alloc_trash_chunk();
5185 if (tmptrash) {
5186 chunk_printf(tmptrash,
5187 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5188 s->flags & SRV_F_BACKUP ? "Backup " : "",
5189 s->proxy->id, s->id, s->hostname);
5190
5191 if (s->track) /* normally it's mandatory here */
5192 chunk_appendf(tmptrash, " via %s/%s",
5193 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005194 ha_warning("%s.\n", tmptrash->area);
5195 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5196 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005197 free_trash_chunk(tmptrash);
5198 tmptrash = NULL;
5199 }
5200 }
5201 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5202 tmptrash = alloc_trash_chunk();
5203 if (tmptrash) {
5204 chunk_printf(tmptrash,
5205 "%sServer %s/%s remains in forced maintenance",
5206 s->flags & SRV_F_BACKUP ? "Backup " : "",
5207 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005208 ha_warning("%s.\n", tmptrash->area);
5209 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5210 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005211 free_trash_chunk(tmptrash);
5212 tmptrash = NULL;
5213 }
5214 }
5215 /* don't report anything when leaving drain mode and remaining in maintenance */
5216
5217 s->cur_admin = s->next_admin;
5218 }
5219
5220 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5221 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5222 /* drain state is applied only if not yet in maint */
5223
5224 s->last_change = now.tv_sec;
5225 if (px->lbprm.set_server_status_down)
5226 px->lbprm.set_server_status_down(s);
5227
5228 /* we might have streams queued on this server and waiting for
5229 * a connection. Those which are redispatchable will be queued
5230 * to another server or to the proxy itself.
5231 */
5232 xferred = pendconn_redistribute(s);
5233
5234 tmptrash = alloc_trash_chunk();
5235 if (tmptrash) {
5236 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5237 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5238 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5239
5240 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5241
5242 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005243 ha_warning("%s.\n", tmptrash->area);
5244 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5245 tmptrash->area);
5246 send_email_alert(s, LOG_NOTICE, "%s",
5247 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005248 }
5249 free_trash_chunk(tmptrash);
5250 tmptrash = NULL;
5251 }
5252
5253 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5254 set_backend_down(s->proxy);
5255 }
5256 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5257 /* OK completely leaving drain mode */
5258 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5259 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5260 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5261 s->proxy->last_change = now.tv_sec;
5262 }
5263
5264 if (s->last_change < now.tv_sec) // ignore negative times
5265 s->down_time += now.tv_sec - s->last_change;
5266 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005267 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005268
5269 tmptrash = alloc_trash_chunk();
5270 if (tmptrash) {
5271 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5272 chunk_printf(tmptrash,
5273 "%sServer %s/%s is %s (leaving forced drain)",
5274 s->flags & SRV_F_BACKUP ? "Backup " : "",
5275 s->proxy->id, s->id,
5276 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5277 }
5278 else {
5279 chunk_printf(tmptrash,
5280 "%sServer %s/%s is %s (leaving drain)",
5281 s->flags & SRV_F_BACKUP ? "Backup " : "",
5282 s->proxy->id, s->id,
5283 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5284 if (s->track) /* normally it's mandatory here */
5285 chunk_appendf(tmptrash, " via %s/%s",
5286 s->track->proxy->id, s->track->id);
5287 }
5288
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005289 ha_warning("%s.\n", tmptrash->area);
5290 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5291 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005292 free_trash_chunk(tmptrash);
5293 tmptrash = NULL;
5294 }
5295
5296 /* now propagate the status change to any LB algorithms */
5297 if (px->lbprm.update_server_eweight)
5298 px->lbprm.update_server_eweight(s);
5299 else if (srv_willbe_usable(s)) {
5300 if (px->lbprm.set_server_status_up)
5301 px->lbprm.set_server_status_up(s);
5302 }
5303 else {
5304 if (px->lbprm.set_server_status_down)
5305 px->lbprm.set_server_status_down(s);
5306 }
5307 }
5308 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5309 /* remaining in drain mode after removing one of its flags */
5310
5311 tmptrash = alloc_trash_chunk();
5312 if (tmptrash) {
5313 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5314 chunk_printf(tmptrash,
5315 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5316 s->flags & SRV_F_BACKUP ? "Backup " : "",
5317 s->proxy->id, s->id);
5318
5319 if (s->track) /* normally it's mandatory here */
5320 chunk_appendf(tmptrash, " via %s/%s",
5321 s->track->proxy->id, s->track->id);
5322 }
5323 else {
5324 chunk_printf(tmptrash,
5325 "%sServer %s/%s remains in forced drain mode",
5326 s->flags & SRV_F_BACKUP ? "Backup " : "",
5327 s->proxy->id, s->id);
5328 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005329 ha_warning("%s.\n", tmptrash->area);
5330 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5331 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005332 free_trash_chunk(tmptrash);
5333 tmptrash = NULL;
5334 }
5335
5336 /* commit new admin status */
5337
5338 s->cur_admin = s->next_admin;
5339 }
5340 }
5341
5342 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005343 *s->adm_st_chg_cause = 0;
5344}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005345
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005346static struct task *cleanup_idle_connections(struct task *task, void *context, unsigned short state)
5347{
5348 struct server *srv = context;
5349 struct connection *conn, *conn_back;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005350 unsigned int to_destroy = srv->curr_idle_conns / 2 + (srv->curr_idle_conns & 1);
5351 unsigned int i = 0;
5352
5353
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005354
5355 list_for_each_entry_safe(conn, conn_back, &srv->idle_orphan_conns[tid], list) {
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005356 if (i == to_destroy)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005357 break;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005358 conn->mux->destroy(conn);
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005359 i++;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005360 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005361 if (!LIST_ISEMPTY(&srv->idle_orphan_conns[tid]))
5362 task_schedule(task, tick_add(now_ms, srv->pool_purge_delay));
5363 else
5364 task->expire = TICK_ETERNITY;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005365 return task;
5366}
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005367/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005368 * Local variables:
5369 * c-indent-level: 8
5370 * c-basic-offset: 8
5371 * End:
5372 */