blob: 25a918931094f77bdc9c620d41c463043fa972d0 [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);
Willy Tarreaubaaee002006-06-26 02:48:02 +020053
Willy Tarreau21faa912012-10-10 08:27:36 +020054/* List head of all known server keywords */
55static struct srv_kw_list srv_keywords = {
56 .list = LIST_HEAD_INIT(srv_keywords.list)
57};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020058
Olivier Houchard9ea5d362019-02-14 18:29:09 +010059__decl_hathreads(HA_SPINLOCK_T idle_conn_srv_lock);
60struct eb_root idle_conn_srv = EB_ROOT;
61struct task *idle_conn_task = NULL;
62struct task *idle_conn_cleanup[MAX_THREADS] = { NULL };
63struct list toremove_connections[MAX_THREADS];
64
Simon Hormana3608442013-11-01 16:46:15 +090065int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020066{
Emeric Brun52a91d32017-08-31 14:41:55 +020067 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020068 return s->down_time;
69
70 return now.tv_sec - s->last_change + s->down_time;
71}
Willy Tarreaubaaee002006-06-26 02:48:02 +020072
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050073int srv_lastsession(const struct server *s)
74{
75 if (s->counters.last_sess)
76 return now.tv_sec - s->counters.last_sess;
77
78 return -1;
79}
80
Simon Horman4a741432013-02-23 15:35:38 +090081int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020082{
Simon Horman4a741432013-02-23 15:35:38 +090083 const struct server *s = check->server;
84
Willy Tarreauff5ae352013-12-11 20:36:34 +010085 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090086 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010087
Emeric Brun52a91d32017-08-31 14:41:55 +020088 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090089 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010090
Simon Horman4a741432013-02-23 15:35:38 +090091 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010092}
93
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010094/*
95 * Check that we did not get a hash collision.
96 * Unlikely, but it can happen.
97 */
98static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +010099{
100 struct proxy *p = s->proxy;
101 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100102
103 for (tmpserv = p->srv; tmpserv != NULL;
104 tmpserv = tmpserv->next) {
105 if (tmpserv == s)
106 continue;
107 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
108 continue;
109 if (tmpserv->cookie &&
110 strcmp(tmpserv->cookie, s->cookie) == 0) {
111 ha_warning("We generated two equal cookies for two different servers.\n"
112 "Please change the secret key for '%s'.\n",
113 s->proxy->id);
114 }
115 }
116
117}
118
Willy Tarreau46b7f532018-08-21 11:54:26 +0200119/*
120 * Must be called with the server lock held.
121 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100122void srv_set_dyncookie(struct server *s)
123{
124 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100125 char *tmpbuf;
126 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100127 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100128 size_t buffer_len;
129 int addr_len;
130 int port;
131
132 if ((s->flags & SRV_F_COOKIESET) ||
133 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
134 s->proxy->dyncookie_key == NULL)
135 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100136 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100137
138 if (s->addr.ss_family != AF_INET &&
139 s->addr.ss_family != AF_INET6)
140 return;
141 /*
142 * Buffer to calculate the cookie value.
143 * The buffer contains the secret key + the server IP address
144 * + the TCP port.
145 */
146 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
147 /*
148 * The TCP port should use only 2 bytes, but is stored in
149 * an unsigned int in struct server, so let's use 4, to be
150 * on the safe side.
151 */
152 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200153 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100154 memcpy(tmpbuf, p->dyncookie_key, key_len);
155 memcpy(&(tmpbuf[key_len]),
156 s->addr.ss_family == AF_INET ?
157 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
158 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
159 addr_len);
160 /*
161 * Make sure it's the same across all the load balancers,
162 * no matter their endianness.
163 */
164 port = htonl(s->svc_port);
165 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
166 hash_value = XXH64(tmpbuf, buffer_len, 0);
167 memprintf(&s->cookie, "%016llx", hash_value);
168 if (!s->cookie)
169 return;
170 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100171
172 /* Don't bother checking if the dyncookie is duplicated if
173 * the server is marked as "disabled", maybe it doesn't have
174 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100175 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100176 if (!(s->next_admin & SRV_ADMF_FMAINT))
177 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100178}
179
Willy Tarreau21faa912012-10-10 08:27:36 +0200180/*
181 * Registers the server keyword list <kwl> as a list of valid keywords for next
182 * parsing sessions.
183 */
184void srv_register_keywords(struct srv_kw_list *kwl)
185{
186 LIST_ADDQ(&srv_keywords.list, &kwl->list);
187}
188
189/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
190 * keyword is found with a NULL ->parse() function, then an attempt is made to
191 * find one with a valid ->parse() function. This way it is possible to declare
192 * platform-dependant, known keywords as NULL, then only declare them as valid
193 * if some options are met. Note that if the requested keyword contains an
194 * opening parenthesis, everything from this point is ignored.
195 */
196struct srv_kw *srv_find_kw(const char *kw)
197{
198 int index;
199 const char *kwend;
200 struct srv_kw_list *kwl;
201 struct srv_kw *ret = NULL;
202
203 kwend = strchr(kw, '(');
204 if (!kwend)
205 kwend = kw + strlen(kw);
206
207 list_for_each_entry(kwl, &srv_keywords.list, list) {
208 for (index = 0; kwl->kw[index].kw != NULL; index++) {
209 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
210 kwl->kw[index].kw[kwend-kw] == 0) {
211 if (kwl->kw[index].parse)
212 return &kwl->kw[index]; /* found it !*/
213 else
214 ret = &kwl->kw[index]; /* may be OK */
215 }
216 }
217 }
218 return ret;
219}
220
221/* Dumps all registered "server" keywords to the <out> string pointer. The
222 * unsupported keywords are only dumped if their supported form was not
223 * found.
224 */
225void srv_dump_kws(char **out)
226{
227 struct srv_kw_list *kwl;
228 int index;
229
230 *out = NULL;
231 list_for_each_entry(kwl, &srv_keywords.list, list) {
232 for (index = 0; kwl->kw[index].kw != NULL; index++) {
233 if (kwl->kw[index].parse ||
234 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
235 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
236 kwl->scope,
237 kwl->kw[index].kw,
238 kwl->kw[index].skip ? " <arg>" : "",
239 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
240 kwl->kw[index].parse ? "" : " (not supported)");
241 }
242 }
243 }
244}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100245
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100246/* Parse the "addr" server keyword */
247static int srv_parse_addr(char **args, int *cur_arg,
248 struct proxy *curproxy, struct server *newsrv, char **err)
249{
250 char *errmsg, *arg;
251 struct sockaddr_storage *sk;
252 int port1, port2;
253 struct protocol *proto;
254
255 errmsg = NULL;
256 arg = args[*cur_arg + 1];
257
258 if (!*arg) {
259 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
260 goto err;
261 }
262
263 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
264 if (!sk) {
265 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
266 goto err;
267 }
268
269 proto = protocol_by_family(sk->ss_family);
270 if (!proto || !proto->connect) {
271 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
272 args[*cur_arg], arg);
273 goto err;
274 }
275
276 if (port1 != port2) {
277 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
278 args[*cur_arg], arg);
279 goto err;
280 }
281
282 newsrv->check.addr = newsrv->agent.addr = *sk;
283 newsrv->flags |= SRV_F_CHECKADDR;
284 newsrv->flags |= SRV_F_AGENTADDR;
285
286 return 0;
287
288 err:
289 free(errmsg);
290 return ERR_ALERT | ERR_FATAL;
291}
292
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100293/* Parse the "agent-check" server keyword */
294static int srv_parse_agent_check(char **args, int *cur_arg,
295 struct proxy *curproxy, struct server *newsrv, char **err)
296{
297 newsrv->do_agent = 1;
298 return 0;
299}
300
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100301/* Parse the "backup" server keyword */
302static int srv_parse_backup(char **args, int *cur_arg,
303 struct proxy *curproxy, struct server *newsrv, char **err)
304{
305 newsrv->flags |= SRV_F_BACKUP;
306 return 0;
307}
308
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100309/* Parse the "check" server keyword */
310static int srv_parse_check(char **args, int *cur_arg,
311 struct proxy *curproxy, struct server *newsrv, char **err)
312{
313 newsrv->do_check = 1;
314 return 0;
315}
316
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100317/* Parse the "check-send-proxy" server keyword */
318static int srv_parse_check_send_proxy(char **args, int *cur_arg,
319 struct proxy *curproxy, struct server *newsrv, char **err)
320{
321 newsrv->check.send_proxy = 1;
322 return 0;
323}
324
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100325/* Parse the "cookie" server keyword */
326static int srv_parse_cookie(char **args, int *cur_arg,
327 struct proxy *curproxy, struct server *newsrv, char **err)
328{
329 char *arg;
330
331 arg = args[*cur_arg + 1];
332 if (!*arg) {
333 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
334 return ERR_ALERT | ERR_FATAL;
335 }
336
337 free(newsrv->cookie);
338 newsrv->cookie = strdup(arg);
339 newsrv->cklen = strlen(arg);
340 newsrv->flags |= SRV_F_COOKIESET;
341 return 0;
342}
343
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100344/* Parse the "disabled" server keyword */
345static int srv_parse_disabled(char **args, int *cur_arg,
346 struct proxy *curproxy, struct server *newsrv, char **err)
347{
Emeric Brun52a91d32017-08-31 14:41:55 +0200348 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
349 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100350 newsrv->check.state |= CHK_ST_PAUSED;
351 newsrv->check.health = 0;
352 return 0;
353}
354
355/* Parse the "enabled" server keyword */
356static int srv_parse_enabled(char **args, int *cur_arg,
357 struct proxy *curproxy, struct server *newsrv, char **err)
358{
Emeric Brun52a91d32017-08-31 14:41:55 +0200359 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
360 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100361 newsrv->check.state &= ~CHK_ST_PAUSED;
362 newsrv->check.health = newsrv->check.rise;
363 return 0;
364}
365
Willy Tarreau9c538e02019-01-23 10:21:49 +0100366static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
367{
368 char *arg;
369
370 arg = args[*cur_arg + 1];
371 if (!*arg) {
372 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
373 return ERR_ALERT | ERR_FATAL;
374 }
375 newsrv->max_reuse = atoi(arg);
376
377 return 0;
378}
379
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100380static 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 +0100381{
382 const char *res;
383 char *arg;
384 unsigned int time;
385
386 arg = args[*cur_arg + 1];
387 if (!*arg) {
388 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
389 return ERR_ALERT | ERR_FATAL;
390 }
391 res = parse_time_err(arg, &time, TIME_UNIT_MS);
392 if (res) {
393 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
394 *res, args[*cur_arg]);
395 return ERR_ALERT | ERR_FATAL;
396 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100397 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100398
399 return 0;
400}
401
Olivier Houchard006e3102018-12-10 18:30:32 +0100402static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
403{
404 char *arg;
405
406 arg = args[*cur_arg + 1];
407 if (!*arg) {
408 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
409 return ERR_ALERT | ERR_FATAL;
410 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100411
Olivier Houchard006e3102018-12-10 18:30:32 +0100412 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100413 if ((int)newsrv->max_idle_conns < -1) {
414 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
415 return ERR_ALERT | ERR_FATAL;
416 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100417
418 return 0;
419}
420
Willy Tarreaudff55432012-10-10 17:51:05 +0200421/* parse the "id" server keyword */
422static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
423{
424 struct eb32_node *node;
425
426 if (!*args[*cur_arg + 1]) {
427 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
428 return ERR_ALERT | ERR_FATAL;
429 }
430
431 newsrv->puid = atol(args[*cur_arg + 1]);
432 newsrv->conf.id.key = newsrv->puid;
433
434 if (newsrv->puid <= 0) {
435 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
436 return ERR_ALERT | ERR_FATAL;
437 }
438
439 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
440 if (node) {
441 struct server *target = container_of(node, struct server, conf.id);
442 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
443 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
444 target->id);
445 return ERR_ALERT | ERR_FATAL;
446 }
447
448 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200449 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200450 return 0;
451}
452
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100453/* Parse the "namespace" server keyword */
454static int srv_parse_namespace(char **args, int *cur_arg,
455 struct proxy *curproxy, struct server *newsrv, char **err)
456{
457#ifdef CONFIG_HAP_NS
458 char *arg;
459
460 arg = args[*cur_arg + 1];
461 if (!*arg) {
462 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
463 return ERR_ALERT | ERR_FATAL;
464 }
465
466 if (!strcmp(arg, "*")) {
467 /* Use the namespace associated with the connection (if present). */
468 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
469 return 0;
470 }
471
472 /*
473 * As this parser may be called several times for the same 'default-server'
474 * object, or for a new 'server' instance deriving from a 'default-server'
475 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
476 */
477 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
478
479 newsrv->netns = netns_store_lookup(arg, strlen(arg));
480 if (!newsrv->netns)
481 newsrv->netns = netns_store_insert(arg);
482
483 if (!newsrv->netns) {
484 memprintf(err, "Cannot open namespace '%s'", arg);
485 return ERR_ALERT | ERR_FATAL;
486 }
487
488 return 0;
489#else
490 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
491 return ERR_ALERT | ERR_FATAL;
492#endif
493}
494
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100495/* Parse the "no-agent-check" server keyword */
496static int srv_parse_no_agent_check(char **args, int *cur_arg,
497 struct proxy *curproxy, struct server *newsrv, char **err)
498{
499 free_check(&newsrv->agent);
500 newsrv->agent.inter = 0;
501 newsrv->agent.port = 0;
502 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
503 newsrv->do_agent = 0;
504 return 0;
505}
506
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100507/* Parse the "no-backup" server keyword */
508static int srv_parse_no_backup(char **args, int *cur_arg,
509 struct proxy *curproxy, struct server *newsrv, char **err)
510{
511 newsrv->flags &= ~SRV_F_BACKUP;
512 return 0;
513}
514
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100515/* Parse the "no-check" server keyword */
516static int srv_parse_no_check(char **args, int *cur_arg,
517 struct proxy *curproxy, struct server *newsrv, char **err)
518{
519 free_check(&newsrv->check);
520 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
521 newsrv->do_check = 0;
522 return 0;
523}
524
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100525/* Parse the "no-check-send-proxy" server keyword */
526static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
527 struct proxy *curproxy, struct server *newsrv, char **err)
528{
529 newsrv->check.send_proxy = 0;
530 return 0;
531}
532
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100533/* Disable server PROXY protocol flags. */
534static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
535{
536 srv->pp_opts &= ~flags;
537 return 0;
538}
539
540/* Parse the "no-send-proxy" server keyword */
541static int srv_parse_no_send_proxy(char **args, int *cur_arg,
542 struct proxy *curproxy, struct server *newsrv, char **err)
543{
544 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
545}
546
547/* Parse the "no-send-proxy-v2" server keyword */
548static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
549 struct proxy *curproxy, struct server *newsrv, char **err)
550{
551 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
552}
553
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100554/* Parse the "non-stick" server keyword */
555static int srv_parse_non_stick(char **args, int *cur_arg,
556 struct proxy *curproxy, struct server *newsrv, char **err)
557{
558 newsrv->flags |= SRV_F_NON_STICK;
559 return 0;
560}
561
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100562/* Enable server PROXY protocol flags. */
563static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
564{
565 srv->pp_opts |= flags;
566 return 0;
567}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200568/* parse the "proto" server keyword */
569static int srv_parse_proto(char **args, int *cur_arg,
570 struct proxy *px, struct server *newsrv, char **err)
571{
572 struct ist proto;
573
574 if (!*args[*cur_arg + 1]) {
575 memprintf(err, "'%s' : missing value", args[*cur_arg]);
576 return ERR_ALERT | ERR_FATAL;
577 }
578 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
579 newsrv->mux_proto = get_mux_proto(proto);
580 if (!newsrv->mux_proto) {
581 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
582 return ERR_ALERT | ERR_FATAL;
583 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200584 return 0;
585}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100586
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100587/* parse the "proxy-v2-options" */
588static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
589 struct proxy *px, struct server *newsrv, char **err)
590{
591 char *p, *n;
592 for (p = args[*cur_arg+1]; p; p = n) {
593 n = strchr(p, ',');
594 if (n)
595 *n++ = '\0';
596 if (!strcmp(p, "ssl")) {
597 newsrv->pp_opts |= SRV_PP_V2_SSL;
598 } else if (!strcmp(p, "cert-cn")) {
599 newsrv->pp_opts |= SRV_PP_V2_SSL;
600 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100601 } else if (!strcmp(p, "cert-key")) {
602 newsrv->pp_opts |= SRV_PP_V2_SSL;
603 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
604 } else if (!strcmp(p, "cert-sig")) {
605 newsrv->pp_opts |= SRV_PP_V2_SSL;
606 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
607 } else if (!strcmp(p, "ssl-cipher")) {
608 newsrv->pp_opts |= SRV_PP_V2_SSL;
609 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100610 } else if (!strcmp(p, "authority")) {
611 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100612 } else if (!strcmp(p, "crc32c")) {
613 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100614 } else
615 goto fail;
616 }
617 return 0;
618 fail:
619 if (err)
620 memprintf(err, "'%s' : proxy v2 option not implemented", p);
621 return ERR_ALERT | ERR_FATAL;
622}
623
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100624/* Parse the "observe" server keyword */
625static int srv_parse_observe(char **args, int *cur_arg,
626 struct proxy *curproxy, struct server *newsrv, char **err)
627{
628 char *arg;
629
630 arg = args[*cur_arg + 1];
631 if (!*arg) {
632 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
633 return ERR_ALERT | ERR_FATAL;
634 }
635
636 if (!strcmp(arg, "none")) {
637 newsrv->observe = HANA_OBS_NONE;
638 }
639 else if (!strcmp(arg, "layer4")) {
640 newsrv->observe = HANA_OBS_LAYER4;
641 }
642 else if (!strcmp(arg, "layer7")) {
643 if (curproxy->mode != PR_MODE_HTTP) {
644 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
645 return ERR_ALERT;
646 }
647 newsrv->observe = HANA_OBS_LAYER7;
648 }
649 else {
650 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
651 "but got '%s'\n", args[*cur_arg], arg);
652 return ERR_ALERT | ERR_FATAL;
653 }
654
655 return 0;
656}
657
Frédéric Lécaille16186232017-03-14 16:42:49 +0100658/* Parse the "redir" server keyword */
659static int srv_parse_redir(char **args, int *cur_arg,
660 struct proxy *curproxy, struct server *newsrv, char **err)
661{
662 char *arg;
663
664 arg = args[*cur_arg + 1];
665 if (!*arg) {
666 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
667 return ERR_ALERT | ERR_FATAL;
668 }
669
670 free(newsrv->rdr_pfx);
671 newsrv->rdr_pfx = strdup(arg);
672 newsrv->rdr_len = strlen(arg);
673
674 return 0;
675}
676
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100677/* Parse the "send-proxy" server keyword */
678static int srv_parse_send_proxy(char **args, int *cur_arg,
679 struct proxy *curproxy, struct server *newsrv, char **err)
680{
681 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
682}
683
684/* Parse the "send-proxy-v2" server keyword */
685static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
686 struct proxy *curproxy, struct server *newsrv, char **err)
687{
688 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
689}
690
Frédéric Lécailledba97072017-03-17 15:33:50 +0100691
692/* Parse the "source" server keyword */
693static int srv_parse_source(char **args, int *cur_arg,
694 struct proxy *curproxy, struct server *newsrv, char **err)
695{
696 char *errmsg;
697 int port_low, port_high;
698 struct sockaddr_storage *sk;
699 struct protocol *proto;
700
701 errmsg = NULL;
702
703 if (!*args[*cur_arg + 1]) {
704 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
705 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
706 goto err;
707 }
708
709 /* 'sk' is statically allocated (no need to be freed). */
710 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
711 if (!sk) {
712 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
713 goto err;
714 }
715
716 proto = protocol_by_family(sk->ss_family);
717 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100718 ha_alert("'%s %s' : connect() not supported for this address family.\n",
719 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100720 goto err;
721 }
722
723 newsrv->conn_src.opts |= CO_SRC_BIND;
724 newsrv->conn_src.source_addr = *sk;
725
726 if (port_low != port_high) {
727 int i;
728
729 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100730 ha_alert("'%s' does not support port offsets (found '%s').\n",
731 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100732 goto err;
733 }
734
735 if (port_low <= 0 || port_low > 65535 ||
736 port_high <= 0 || port_high > 65535 ||
737 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100738 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 +0100739 goto err;
740 }
741 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
742 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
743 newsrv->conn_src.sport_range->ports[i] = port_low + i;
744 }
745
746 *cur_arg += 2;
747 while (*(args[*cur_arg])) {
748 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
749#if defined(CONFIG_HAP_TRANSPARENT)
750 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100751 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
752 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100753 goto err;
754 }
755 if (!strcmp(args[*cur_arg + 1], "client")) {
756 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
757 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
758 }
759 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
760 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
761 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
762 }
763 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
764 char *name, *end;
765
766 name = args[*cur_arg + 1] + 7;
767 while (isspace(*name))
768 name++;
769
770 end = name;
771 while (*end && !isspace(*end) && *end != ',' && *end != ')')
772 end++;
773
774 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
775 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
776 free(newsrv->conn_src.bind_hdr_name);
777 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
778 newsrv->conn_src.bind_hdr_len = end - name;
779 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
780 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
781 newsrv->conn_src.bind_hdr_occ = -1;
782
783 /* now look for an occurrence number */
784 while (isspace(*end))
785 end++;
786 if (*end == ',') {
787 end++;
788 name = end;
789 if (*end == '-')
790 end++;
791 while (isdigit((int)*end))
792 end++;
793 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
794 }
795
796 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100797 ha_alert("usesrc hdr_ip(name,num) does not support negative"
798 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100799 goto err;
800 }
801 }
802 else {
803 struct sockaddr_storage *sk;
804 int port1, port2;
805
806 /* 'sk' is statically allocated (no need to be freed). */
807 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
808 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100809 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100810 goto err;
811 }
812
813 proto = protocol_by_family(sk->ss_family);
814 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100815 ha_alert("'%s %s' : connect() not supported for this address family.\n",
816 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100817 goto err;
818 }
819
820 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100821 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
822 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100823 goto err;
824 }
825 newsrv->conn_src.tproxy_addr = *sk;
826 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
827 }
828 global.last_checks |= LSTCHK_NETADM;
829 *cur_arg += 2;
830 continue;
831#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100832 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 +0100833 goto err;
834#endif /* defined(CONFIG_HAP_TRANSPARENT) */
835 } /* "usesrc" */
836
837 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
838#ifdef SO_BINDTODEVICE
839 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100840 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100841 goto err;
842 }
843 free(newsrv->conn_src.iface_name);
844 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
845 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
846 global.last_checks |= LSTCHK_NETADM;
847#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100848 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100849 goto err;
850#endif
851 *cur_arg += 2;
852 continue;
853 }
854 /* this keyword in not an option of "source" */
855 break;
856 } /* while */
857
858 return 0;
859
860 err:
861 free(errmsg);
862 return ERR_ALERT | ERR_FATAL;
863}
864
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100865/* Parse the "stick" server keyword */
866static int srv_parse_stick(char **args, int *cur_arg,
867 struct proxy *curproxy, struct server *newsrv, char **err)
868{
869 newsrv->flags &= ~SRV_F_NON_STICK;
870 return 0;
871}
872
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100873/* Parse the "track" server keyword */
874static int srv_parse_track(char **args, int *cur_arg,
875 struct proxy *curproxy, struct server *newsrv, char **err)
876{
877 char *arg;
878
879 arg = args[*cur_arg + 1];
880 if (!*arg) {
881 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
882 return ERR_ALERT | ERR_FATAL;
883 }
884
885 free(newsrv->trackit);
886 newsrv->trackit = strdup(arg);
887
888 return 0;
889}
890
Frédéric Lécailledba97072017-03-17 15:33:50 +0100891
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200892/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200893 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200894 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200895 *
896 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200897 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200898void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200899{
Willy Tarreau87b09662015-04-03 00:22:06 +0200900 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200901
Willy Tarreau87b09662015-04-03 00:22:06 +0200902 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
903 if (stream->srv_conn == srv)
904 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200905}
906
907/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200908 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200909 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200910 *
911 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200912 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200913void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200914{
915 struct server *srv;
916
917 for (srv = px->srv; srv != NULL; srv = srv->next)
918 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200919 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200920}
921
Willy Tarreaubda92272014-05-20 21:55:30 +0200922/* Appends some information to a message string related to a server going UP or
923 * DOWN. If both <forced> and <reason> are null and the server tracks another
924 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200925 * If <check> is non-null, an entire string describing the check result will be
926 * appended after a comma and a space (eg: to report some information from the
927 * check that changed the state). In the other case, the string will be built
928 * using the check results stored into the struct server if present.
929 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200930 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200931 *
932 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200933 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200934void srv_append_status(struct buffer *msg, struct server *s,
935 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200936{
Emeric Brun5a133512017-10-19 14:42:30 +0200937 short status = s->op_st_chg.status;
938 short code = s->op_st_chg.code;
939 long duration = s->op_st_chg.duration;
940 char *desc = s->op_st_chg.reason;
941
942 if (check) {
943 status = check->status;
944 code = check->code;
945 duration = check->duration;
946 desc = check->desc;
947 }
948
949 if (status != -1) {
950 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
951
952 if (status >= HCHK_STATUS_L57DATA)
953 chunk_appendf(msg, ", code: %d", code);
954
955 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200956 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200957
958 chunk_appendf(msg, ", info: \"");
959
960 chunk_initlen(&src, desc, 0, strlen(desc));
961 chunk_asciiencode(msg, &src, '"');
962
963 chunk_appendf(msg, "\"");
964 }
965
966 if (duration >= 0)
967 chunk_appendf(msg, ", check duration: %ldms", duration);
968 }
969 else if (desc && *desc) {
970 chunk_appendf(msg, ", %s", desc);
971 }
972 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200973 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200974 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200975
976 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200977 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200978 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
979 " %d sessions active, %d requeued, %d remaining in queue",
980 s->proxy->srv_act, s->proxy->srv_bck,
981 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
982 s->cur_sess, xferred, s->nbpend);
983 else
984 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
985 " %d sessions requeued, %d total in queue",
986 s->proxy->srv_act, s->proxy->srv_bck,
987 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
988 xferred, s->nbpend);
989 }
990}
991
Emeric Brun5a133512017-10-19 14:42:30 +0200992/* Marks server <s> down, regardless of its checks' statuses. The server is
993 * registered in a list to postpone the counting of the remaining servers on
994 * the proxy and transfers queued streams whenever possible to other servers at
995 * a sync point. Maintenance servers are ignored. It stores the <reason> if
996 * non-null as the reason for going down or the available data from the check
997 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200998 *
999 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001000 */
Emeric Brun5a133512017-10-19 14:42:30 +02001001void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001002{
1003 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001004
Emeric Brun64cc49c2017-10-03 14:46:45 +02001005 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001006 return;
1007
Emeric Brun52a91d32017-08-31 14:41:55 +02001008 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001009 *s->op_st_chg.reason = 0;
1010 s->op_st_chg.status = -1;
1011 if (reason) {
1012 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1013 }
1014 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001015 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001016 s->op_st_chg.code = check->code;
1017 s->op_st_chg.status = check->status;
1018 s->op_st_chg.duration = check->duration;
1019 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001020
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001021 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001022 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001023
Emeric Brun9f0b4582017-10-23 14:39:51 +02001024 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001025 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001026 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001027 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001028 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001029}
1030
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001031/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001032 * in maintenance. The server is registered in a list to postpone the counting
1033 * of the remaining servers on the proxy and tries to grab requests from the
1034 * proxy at a sync point. Maintenance servers are ignored. It stores the
1035 * <reason> if non-null as the reason for going down or the available data
1036 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001037 *
1038 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001039 */
Emeric Brun5a133512017-10-19 14:42:30 +02001040void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001041{
1042 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001043
Emeric Brun64cc49c2017-10-03 14:46:45 +02001044 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001045 return;
1046
Emeric Brun52a91d32017-08-31 14:41:55 +02001047 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001048 return;
1049
Emeric Brun52a91d32017-08-31 14:41:55 +02001050 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001051 *s->op_st_chg.reason = 0;
1052 s->op_st_chg.status = -1;
1053 if (reason) {
1054 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1055 }
1056 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001057 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001058 s->op_st_chg.code = check->code;
1059 s->op_st_chg.status = check->status;
1060 s->op_st_chg.duration = check->duration;
1061 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001062
Emeric Brun64cc49c2017-10-03 14:46:45 +02001063 if (s->slowstart <= 0)
1064 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001065
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001066 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001067 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001068
Emeric Brun9f0b4582017-10-23 14:39:51 +02001069 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001070 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001071 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001072 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001073 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001074}
1075
Willy Tarreau8eb77842014-05-21 13:54:57 +02001076/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001077 * isn't in maintenance. The server is registered in a list to postpone the
1078 * counting of the remaining servers on the proxy and tries to grab requests
1079 * from the proxy. Maintenance servers are ignored. It stores the
1080 * <reason> if non-null as the reason for going down or the available data
1081 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001082 * up. Note that it makes use of the trash to build the log strings, so <reason>
1083 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001084 *
1085 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001086 */
Emeric Brun5a133512017-10-19 14:42:30 +02001087void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001088{
1089 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001090
Emeric Brun64cc49c2017-10-03 14:46:45 +02001091 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001092 return;
1093
Emeric Brun52a91d32017-08-31 14:41:55 +02001094 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001095 return;
1096
Emeric Brun52a91d32017-08-31 14:41:55 +02001097 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001098 *s->op_st_chg.reason = 0;
1099 s->op_st_chg.status = -1;
1100 if (reason) {
1101 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1102 }
1103 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001104 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001105 s->op_st_chg.code = check->code;
1106 s->op_st_chg.status = check->status;
1107 s->op_st_chg.duration = check->duration;
1108 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001109
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001110 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001111 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001112
Emeric Brun9f0b4582017-10-23 14:39:51 +02001113 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001114 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001115 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001116 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001117 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001118}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001119
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001120/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1121 * enforce either maint mode or drain mode. It is not allowed to set more than
1122 * one flag at once. The equivalent "inherited" flag is propagated to all
1123 * tracking servers. Maintenance mode disables health checks (but not agent
1124 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001125 * is done. If <cause> is non-null, it will be displayed at the end of the log
1126 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001127 *
1128 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001129 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001130void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001131{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001132 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001133
1134 if (!mode)
1135 return;
1136
1137 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001138 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001139 return;
1140
Emeric Brun52a91d32017-08-31 14:41:55 +02001141 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001142 if (cause)
1143 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1144
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001145 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001146 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001147
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001148 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001149 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1150 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001151 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001152
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001153 /* compute the inherited flag to propagate */
1154 if (mode & SRV_ADMF_MAINT)
1155 mode = SRV_ADMF_IMAINT;
1156 else if (mode & SRV_ADMF_DRAIN)
1157 mode = SRV_ADMF_IDRAIN;
1158
Emeric Brun9f0b4582017-10-23 14:39:51 +02001159 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001160 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001161 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001162 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001163 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001164}
1165
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001166/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1167 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1168 * than one flag at once. The equivalent "inherited" flag is propagated to all
1169 * tracking servers. Leaving maintenance mode re-enables health checks. When
1170 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001171 *
1172 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001173 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001174void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001175{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001176 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001177
1178 if (!mode)
1179 return;
1180
1181 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001182 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001183 return;
1184
Emeric Brun52a91d32017-08-31 14:41:55 +02001185 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001186
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001187 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001188 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001189
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001190 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001191 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1192 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001193 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001194
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001195 if (mode & SRV_ADMF_MAINT)
1196 mode = SRV_ADMF_IMAINT;
1197 else if (mode & SRV_ADMF_DRAIN)
1198 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001199
Emeric Brun9f0b4582017-10-23 14:39:51 +02001200 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001201 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001202 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001203 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001204 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001205}
1206
Willy Tarreau757478e2016-11-03 19:22:19 +01001207/* principle: propagate maint and drain to tracking servers. This is useful
1208 * upon startup so that inherited states are correct.
1209 */
1210static void srv_propagate_admin_state(struct server *srv)
1211{
1212 struct server *srv2;
1213
1214 if (!srv->trackers)
1215 return;
1216
1217 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001218 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001219 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001220 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001221
Emeric Brun52a91d32017-08-31 14:41:55 +02001222 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001223 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001224 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001225 }
1226}
1227
1228/* Compute and propagate the admin states for all servers in proxy <px>.
1229 * Only servers *not* tracking another one are considered, because other
1230 * ones will be handled when the server they track is visited.
1231 */
1232void srv_compute_all_admin_states(struct proxy *px)
1233{
1234 struct server *srv;
1235
1236 for (srv = px->srv; srv; srv = srv->next) {
1237 if (srv->track)
1238 continue;
1239 srv_propagate_admin_state(srv);
1240 }
1241}
1242
Willy Tarreaudff55432012-10-10 17:51:05 +02001243/* Note: must not be declared <const> as its list will be overwritten.
1244 * Please take care of keeping this list alphabetically sorted, doing so helps
1245 * all code contributors.
1246 * Optional keywords are also declared with a NULL ->parse() function so that
1247 * the config parser can report an appropriate error when a known keyword was
1248 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001249 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001250 */
1251static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001252 { "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 +01001253 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001254 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001255 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001256 { "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 +01001257 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001258 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1259 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001260 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001261 { "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 +01001262 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001263 { "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 +01001264 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001265 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001266 { "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 +01001267 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1268 { "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 +01001269 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001270 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001271 { "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 +01001272 { "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 +02001273 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001274 { "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 +01001275 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001276 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1277 { "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 +02001278 { "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 +01001279 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001280 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001281 { NULL, NULL, 0 },
1282}};
1283
Willy Tarreau0108d902018-11-25 19:14:37 +01001284INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001285
Willy Tarreau004e0452013-11-21 11:22:01 +01001286/* Recomputes the server's eweight based on its state, uweight, the current time,
1287 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001288 * state is automatically disabled if the time is elapsed. If <must_update> is
1289 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001290 *
1291 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001292 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001293void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001294{
1295 struct proxy *px = sv->proxy;
1296 unsigned w;
1297
1298 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1299 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001300 if (sv->next_state == SRV_ST_STARTING)
1301 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001302 }
1303
1304 /* We must take care of not pushing the server to full throttle during slow starts.
1305 * It must also start immediately, at least at the minimal step when leaving maintenance.
1306 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001307 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001308 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1309 else
1310 w = px->lbprm.wdiv;
1311
Emeric Brun52a91d32017-08-31 14:41:55 +02001312 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001313
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001314 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001315 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001316 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001317}
1318
Willy Tarreaubaaee002006-06-26 02:48:02 +02001319/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001320 * Parses weight_str and configures sv accordingly.
1321 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001322 *
1323 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001324 */
1325const char *server_parse_weight_change_request(struct server *sv,
1326 const char *weight_str)
1327{
1328 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001329 long int w;
1330 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001331
1332 px = sv->proxy;
1333
1334 /* if the weight is terminated with '%', it is set relative to
1335 * the initial weight, otherwise it is absolute.
1336 */
1337 if (!*weight_str)
1338 return "Require <weight> or <weight%>.\n";
1339
Simon Hormanb796afa2013-02-12 10:45:53 +09001340 w = strtol(weight_str, &end, 10);
1341 if (end == weight_str)
1342 return "Empty weight string empty or preceded by garbage";
1343 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001344 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001345 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001346 /* Avoid integer overflow */
1347 if (w > 25600)
1348 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001349 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001350 if (w > 256)
1351 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001352 }
1353 else if (w < 0 || w > 256)
1354 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001355 else if (end[0] != '\0')
1356 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001357
1358 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1359 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1360
1361 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001362 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001363
1364 return NULL;
1365}
1366
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001367/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001368 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1369 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001370 * Returns:
1371 * - error string on error
1372 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001373 *
1374 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001375 */
1376const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001377 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001378{
1379 unsigned char ip[INET6_ADDRSTRLEN];
1380
1381 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001382 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001383 return NULL;
1384 }
1385 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001386 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001387 return NULL;
1388 }
1389
1390 return "Could not understand IP address format.\n";
1391}
1392
Willy Tarreau46b7f532018-08-21 11:54:26 +02001393/*
1394 * Must be called with the server lock held.
1395 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001396const char *server_parse_maxconn_change_request(struct server *sv,
1397 const char *maxconn_str)
1398{
1399 long int v;
1400 char *end;
1401
1402 if (!*maxconn_str)
1403 return "Require <maxconn>.\n";
1404
1405 v = strtol(maxconn_str, &end, 10);
1406 if (end == maxconn_str)
1407 return "maxconn string empty or preceded by garbage";
1408 else if (end[0] != '\0')
1409 return "Trailing garbage in maxconn string";
1410
1411 if (sv->maxconn == sv->minconn) { // static maxconn
1412 sv->maxconn = sv->minconn = v;
1413 } else { // dynamic maxconn
1414 sv->maxconn = v;
1415 }
1416
1417 if (may_dequeue_tasks(sv, sv->proxy))
1418 process_srv_queue(sv);
1419
1420 return NULL;
1421}
1422
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001423#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001424static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1425 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001426{
1427 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001428 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001429 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001430 NULL,
1431 };
1432
1433 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001434 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001435
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001436 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1437}
1438
1439static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1440{
1441 struct sample_expr *expr;
1442
1443 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 +01001444 if (!expr) {
1445 memprintf(err, "error detected while parsing sni expression : %s", *err);
1446 return ERR_ALERT | ERR_FATAL;
1447 }
1448
1449 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1450 memprintf(err, "error detected while parsing sni expression : "
1451 " fetch method '%s' extracts information from '%s', "
1452 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001453 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001454 return ERR_ALERT | ERR_FATAL;
1455 }
1456
1457 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1458 release_sample_expr(newsrv->ssl_ctx.sni);
1459 newsrv->ssl_ctx.sni = expr;
1460
1461 return 0;
1462}
1463#endif
1464
1465static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1466{
1467 if (err && *err) {
1468 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001469 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 +01001470 }
1471 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001472 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1473 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001474}
1475
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001476static void srv_conn_src_sport_range_cpy(struct server *srv,
1477 struct server *src)
1478{
1479 int range_sz;
1480
1481 range_sz = src->conn_src.sport_range->size;
1482 if (range_sz > 0) {
1483 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1484 if (srv->conn_src.sport_range != NULL) {
1485 int i;
1486
1487 for (i = 0; i < range_sz; i++) {
1488 srv->conn_src.sport_range->ports[i] =
1489 src->conn_src.sport_range->ports[i];
1490 }
1491 }
1492 }
1493}
1494
1495/*
1496 * Copy <src> server connection source settings to <srv> server everything needed.
1497 */
1498static void srv_conn_src_cpy(struct server *srv, struct server *src)
1499{
1500 srv->conn_src.opts = src->conn_src.opts;
1501 srv->conn_src.source_addr = src->conn_src.source_addr;
1502
1503 /* Source port range copy. */
1504 if (src->conn_src.sport_range != NULL)
1505 srv_conn_src_sport_range_cpy(srv, src);
1506
1507#ifdef CONFIG_HAP_TRANSPARENT
1508 if (src->conn_src.bind_hdr_name != NULL) {
1509 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1510 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1511 }
1512 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1513 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1514#endif
1515 if (src->conn_src.iface_name != NULL)
1516 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1517}
1518
1519/*
1520 * Copy <src> server SSL settings to <srv> server allocating
1521 * everything needed.
1522 */
1523#if defined(USE_OPENSSL)
1524static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1525{
1526 if (src->ssl_ctx.ca_file != NULL)
1527 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1528 if (src->ssl_ctx.crl_file != NULL)
1529 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1530 if (src->ssl_ctx.client_crt != NULL)
1531 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1532
1533 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1534
1535 if (src->ssl_ctx.verify_host != NULL)
1536 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1537 if (src->ssl_ctx.ciphers != NULL)
1538 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001539#if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
1540 if (src->ssl_ctx.ciphersuites != NULL)
1541 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1542#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001543 if (src->sni_expr != NULL)
1544 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001545
1546#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1547 if (src->ssl_ctx.alpn_str) {
1548 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1549 if (srv->ssl_ctx.alpn_str) {
1550 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1551 src->ssl_ctx.alpn_len);
1552 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1553 }
1554 }
1555#endif
1556#ifdef OPENSSL_NPN_NEGOTIATED
1557 if (src->ssl_ctx.npn_str) {
1558 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1559 if (srv->ssl_ctx.npn_str) {
1560 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1561 src->ssl_ctx.npn_len);
1562 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1563 }
1564 }
1565#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001566}
1567#endif
1568
1569/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001570 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001571 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001572 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001573 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001574static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001575{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001576 char *hostname_dn;
1577 int hostname_len, hostname_dn_len;
1578
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001579 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001580 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001581
Christopher Faulet67957bd2017-09-27 11:00:59 +02001582 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001583 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001584 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1585 hostname_dn, trash.size);
1586 if (hostname_dn_len == -1)
1587 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001588
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001589
Christopher Faulet67957bd2017-09-27 11:00:59 +02001590 free(srv->hostname);
1591 free(srv->hostname_dn);
1592 srv->hostname = strdup(hostname);
1593 srv->hostname_dn = strdup(hostname_dn);
1594 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001595 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001596 goto err;
1597
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001598 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001599
1600 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001601 free(srv->hostname); srv->hostname = NULL;
1602 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001603 return -1;
1604}
1605
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001606/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001607 * Copy <src> server settings to <srv> server allocating
1608 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001609 * This function is not supposed to be called at any time, but only
1610 * during server settings parsing or during server allocations from
1611 * a server template, and just after having calloc()'ed a new server.
1612 * So, <src> may only be a default server (when parsing server settings)
1613 * or a server template (during server allocations from a server template).
1614 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1615 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001616 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001617static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001618{
1619 /* Connection source settings copy */
1620 srv_conn_src_cpy(srv, src);
1621
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001622 if (srv_tmpl) {
1623 srv->addr = src->addr;
1624 srv->svc_port = src->svc_port;
1625 }
1626
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001627 srv->pp_opts = src->pp_opts;
1628 if (src->rdr_pfx != NULL) {
1629 srv->rdr_pfx = strdup(src->rdr_pfx);
1630 srv->rdr_len = src->rdr_len;
1631 }
1632 if (src->cookie != NULL) {
1633 srv->cookie = strdup(src->cookie);
1634 srv->cklen = src->cklen;
1635 }
1636 srv->use_ssl = src->use_ssl;
1637 srv->check.addr = srv->agent.addr = src->check.addr;
1638 srv->check.use_ssl = src->check.use_ssl;
1639 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001640 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001641 srv->check.alpn_str = src->check.alpn_str;
1642 srv->check.alpn_len = srv->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001643 /* Note: 'flags' field has potentially been already initialized. */
1644 srv->flags |= src->flags;
1645 srv->do_check = src->do_check;
1646 srv->do_agent = src->do_agent;
1647 if (srv->check.port)
1648 srv->flags |= SRV_F_CHECKPORT;
1649 srv->check.inter = src->check.inter;
1650 srv->check.fastinter = src->check.fastinter;
1651 srv->check.downinter = src->check.downinter;
1652 srv->agent.use_ssl = src->agent.use_ssl;
1653 srv->agent.port = src->agent.port;
1654 if (src->agent.send_string != NULL)
1655 srv->agent.send_string = strdup(src->agent.send_string);
1656 srv->agent.send_string_len = src->agent.send_string_len;
1657 srv->agent.inter = src->agent.inter;
1658 srv->agent.fastinter = src->agent.fastinter;
1659 srv->agent.downinter = src->agent.downinter;
1660 srv->maxqueue = src->maxqueue;
1661 srv->minconn = src->minconn;
1662 srv->maxconn = src->maxconn;
1663 srv->slowstart = src->slowstart;
1664 srv->observe = src->observe;
1665 srv->onerror = src->onerror;
1666 srv->onmarkeddown = src->onmarkeddown;
1667 srv->onmarkedup = src->onmarkedup;
1668 if (src->trackit != NULL)
1669 srv->trackit = strdup(src->trackit);
1670 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1671 srv->uweight = srv->iweight = src->iweight;
1672
1673 srv->check.send_proxy = src->check.send_proxy;
1674 /* health: up, but will fall down at first failure */
1675 srv->check.rise = srv->check.health = src->check.rise;
1676 srv->check.fall = src->check.fall;
1677
1678 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001679 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1680 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1681 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001682 srv->check.state |= CHK_ST_PAUSED;
1683 srv->check.health = 0;
1684 }
1685
1686 /* health: up but will fall down at first failure */
1687 srv->agent.rise = srv->agent.health = src->agent.rise;
1688 srv->agent.fall = src->agent.fall;
1689
1690 if (src->resolvers_id != NULL)
1691 srv->resolvers_id = strdup(src->resolvers_id);
1692 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001693 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001694 if (srv->dns_opts.family_prio == AF_UNSPEC)
1695 srv->dns_opts.family_prio = AF_INET6;
1696 memcpy(srv->dns_opts.pref_net,
1697 src->dns_opts.pref_net,
1698 sizeof srv->dns_opts.pref_net);
1699 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1700
1701 srv->init_addr_methods = src->init_addr_methods;
1702 srv->init_addr = src->init_addr;
1703#if defined(USE_OPENSSL)
1704 srv_ssl_settings_cpy(srv, src);
1705#endif
1706#ifdef TCP_USER_TIMEOUT
1707 srv->tcp_ut = src->tcp_ut;
1708#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001709 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001710 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001711 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001712 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001713
Olivier Houchard8da5f982017-08-04 18:35:36 +02001714 if (srv_tmpl)
1715 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001716}
1717
William Lallemand313bfd12018-10-26 14:47:32 +02001718struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001719{
1720 struct server *srv;
1721
1722 srv = calloc(1, sizeof *srv);
1723 if (!srv)
1724 return NULL;
1725
1726 srv->obj_type = OBJ_TYPE_SERVER;
1727 srv->proxy = proxy;
1728 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001729 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001730
Emeric Brun52a91d32017-08-31 14:41:55 +02001731 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001732 srv->last_change = now.tv_sec;
1733
1734 srv->check.status = HCHK_STATUS_INI;
1735 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001736 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001737 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1738
1739 srv->agent.status = HCHK_STATUS_INI;
1740 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001741 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001742 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1743
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001744 srv->pool_purge_delay = 1000;
Olivier Houchard006e3102018-12-10 18:30:32 +01001745 srv->max_idle_conns = -1;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001746 srv->max_reuse = -1;
Olivier Houchard006e3102018-12-10 18:30:32 +01001747
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001748 return srv;
1749}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001750
1751/*
1752 * Validate <srv> server health-check settings.
1753 * Returns 0 if everything is OK, -1 if not.
1754 */
1755static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1756{
1757 struct tcpcheck_rule *r = NULL;
1758 struct list *l;
1759
1760 /*
1761 * We need at least a service port, a check port or the first tcp-check rule must
1762 * be a 'connect' one when checking an IPv4/IPv6 server.
1763 */
1764 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1765 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1766 return 0;
1767
1768 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1769 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001770 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1771 "Check has been disabled.\n",
1772 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001773 return -1;
1774 }
1775
1776 /* search the first action (connect / send / expect) in the list */
1777 l = &srv->proxy->tcpcheck_rules;
1778 list_for_each_entry(r, l, list) {
1779 if (r->action != TCPCHK_ACT_COMMENT)
1780 break;
1781 }
1782
1783 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001784 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1785 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1786 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001787 return -1;
1788 }
1789
1790 /* scan the tcp-check ruleset to ensure a port has been configured */
1791 l = &srv->proxy->tcpcheck_rules;
1792 list_for_each_entry(r, l, list) {
1793 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001794 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1795 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1796 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001797 return -1;
1798 }
1799 }
1800
1801 return 0;
1802}
1803
1804/*
1805 * Initialize <srv> health-check structure.
1806 * Returns the error string in case of memory allocation failure, NULL if not.
1807 */
1808static const char *do_health_check_init(struct server *srv, int check_type, int state)
1809{
1810 const char *ret;
1811
1812 if (!srv->do_check)
1813 return NULL;
1814
1815 ret = init_check(&srv->check, check_type);
1816 if (ret)
1817 return ret;
1818
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001819 srv->check.state |= state;
1820 global.maxsock++;
1821
1822 return NULL;
1823}
1824
1825static int server_health_check_init(const char *file, int linenum,
1826 struct server *srv, struct proxy *curproxy)
1827{
1828 const char *ret;
1829
1830 if (!srv->do_check)
1831 return 0;
1832
1833 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001834 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1835 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001836 return ERR_ALERT | ERR_FATAL;
1837 }
1838
1839 if (server_healthcheck_validate(file, linenum, srv) < 0)
1840 return ERR_ALERT | ERR_ABORT;
1841
1842 /* note: check type will be set during the config review phase */
1843 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1844 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001845 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001846 return ERR_ALERT | ERR_ABORT;
1847 }
1848
1849 return 0;
1850}
1851
1852/*
1853 * Initialize <srv> agent check structure.
1854 * Returns the error string in case of memory allocation failure, NULL if not.
1855 */
1856static const char *do_server_agent_check_init(struct server *srv, int state)
1857{
1858 const char *ret;
1859
1860 if (!srv->do_agent)
1861 return NULL;
1862
1863 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1864 if (ret)
1865 return ret;
1866
1867 if (!srv->agent.inter)
1868 srv->agent.inter = srv->check.inter;
1869
1870 srv->agent.state |= state;
1871 global.maxsock++;
1872
1873 return NULL;
1874}
1875
1876static int server_agent_check_init(const char *file, int linenum,
1877 struct server *srv, struct proxy *curproxy)
1878{
1879 const char *ret;
1880
1881 if (!srv->do_agent)
1882 return 0;
1883
1884 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001885 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001886 file, linenum, srv->id);
1887 return ERR_ALERT | ERR_FATAL;
1888 }
1889
1890 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1891 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001892 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001893 return ERR_ALERT | ERR_ABORT;
1894 }
1895
1896 return 0;
1897}
1898
1899#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1900static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1901 struct server *srv, struct proxy *proxy)
1902{
1903 int ret;
1904 char *err = NULL;
1905
1906 if (!srv->sni_expr)
1907 return 0;
1908
1909 ret = server_parse_sni_expr(srv, proxy, &err);
1910 if (!ret)
1911 return 0;
1912
1913 display_parser_err(file, linenum, args, cur_arg, &err);
1914 free(err);
1915
1916 return ret;
1917}
1918#endif
1919
1920/*
1921 * Server initializations finalization.
1922 * Initialize health check, agent check and SNI expression if enabled.
1923 * Must not be called for a default server instance.
1924 */
1925static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1926 struct server *srv, struct proxy *px)
1927{
1928 int ret;
1929
1930 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1931 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1932 return ret;
1933 }
1934
1935#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1936 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1937 return ret;
1938#endif
1939
1940 if (srv->flags & SRV_F_BACKUP)
1941 px->srv_bck++;
1942 else
1943 px->srv_act++;
1944 srv_lb_commit_status(srv);
1945
1946 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001947err:
1948 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001949}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001950
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001951/*
1952 * Parse as much as possible such a range string argument: low[-high]
1953 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1954 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1955 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1956 * Returns 0 if succeeded, -1 if not.
1957 */
1958static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1959{
1960 char *nb_high_arg;
1961
1962 *nb_high = 0;
1963 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001964 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001965
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001966 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001967 *nb_high_arg++ = '\0';
1968 *nb_high = atoi(nb_high_arg);
1969 }
1970 else {
1971 *nb_high += *nb_low;
1972 *nb_low = 1;
1973 }
1974
1975 if (*nb_low < 0 || *nb_high < *nb_low)
1976 return -1;
1977
1978 return 0;
1979}
1980
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001981static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1982{
1983 chunk_printf(&trash, "%s%d", prefix, nb);
1984 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001985 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001986}
1987
1988/*
1989 * Initialize as much as possible servers from <srv> server template.
1990 * Note that a server template is a special server with
1991 * a few different parameters than a server which has
1992 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001993 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001994 * 'srv' template included.
1995 */
1996static int server_template_init(struct server *srv, struct proxy *px)
1997{
1998 int i;
1999 struct server *newsrv;
2000
2001 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2002 int check_init_state;
2003 int agent_init_state;
2004
2005 newsrv = new_server(px);
2006 if (!newsrv)
2007 goto err;
2008
2009 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002010 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002011#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2012 if (newsrv->sni_expr) {
2013 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2014 if (!newsrv->ssl_ctx.sni)
2015 goto err;
2016 }
2017#endif
2018 /* Set this new server ID. */
2019 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2020
2021 /* Initial checks states. */
2022 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2023 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2024
2025 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2026 do_server_agent_check_init(newsrv, agent_init_state))
2027 goto err;
2028
2029 /* Linked backwards first. This will be restablished after parsing. */
2030 newsrv->next = px->srv;
2031 px->srv = newsrv;
2032 }
2033 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2034
2035 return i - srv->tmpl_info.nb_low;
2036
2037 err:
2038 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2039 if (newsrv) {
2040#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2041 release_sample_expr(newsrv->ssl_ctx.sni);
2042#endif
2043 free_check(&newsrv->agent);
2044 free_check(&newsrv->check);
2045 }
2046 free(newsrv);
2047 return i - srv->tmpl_info.nb_low;
2048}
2049
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002050int 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 +02002051{
2052 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002053 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002054 char *errmsg = NULL;
2055 int err_code = 0;
2056 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002057 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002058
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002059 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002060 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002061 !strcmp(args[0], "default-server") ||
2062 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002063 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002064 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002065 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002066 int srv_tmpl = !defsrv && !srv;
2067 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002068
2069 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002070 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002071 err_code |= ERR_ALERT | ERR_FATAL;
2072 goto out;
2073 }
2074 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002075 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002076
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002077 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002078 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002079 if (!*args[2]) {
2080 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002081 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002082 file, linenum, args[0]);
2083 err_code |= ERR_ALERT | ERR_FATAL;
2084 goto out;
2085 }
2086
2087 err = invalid_char(args[1]);
2088 }
2089 else if (srv_tmpl) {
2090 if (!*args[3]) {
2091 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002092 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 +02002093 file, linenum, args[0]);
2094 err_code |= ERR_ALERT | ERR_FATAL;
2095 goto out;
2096 }
2097
2098 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002099 }
2100
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002101 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002102 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 +02002103 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002104 err_code |= ERR_ALERT | ERR_FATAL;
2105 goto out;
2106 }
2107
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002108 cur_arg = 2;
2109 if (srv_tmpl) {
2110 /* Parse server-template <nb | range> arg. */
2111 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002112 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002113 file, linenum, args[0], args[cur_arg]);
2114 err_code |= ERR_ALERT | ERR_FATAL;
2115 goto out;
2116 }
2117 cur_arg++;
2118 }
2119
Willy Tarreau272adea2014-03-31 10:39:59 +02002120 if (!defsrv) {
2121 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002122 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002123 struct protocol *proto;
2124
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002125 newsrv = new_server(curproxy);
2126 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002127 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002128 err_code |= ERR_ALERT | ERR_ABORT;
2129 goto out;
2130 }
2131
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002132 if (srv_tmpl) {
2133 newsrv->tmpl_info.nb_low = tmpl_range_low;
2134 newsrv->tmpl_info.nb_high = tmpl_range_high;
2135 }
2136
Willy Tarreau272adea2014-03-31 10:39:59 +02002137 /* the servers are linked backwards first */
2138 newsrv->next = curproxy->srv;
2139 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002140 newsrv->conf.file = strdup(file);
2141 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002142 /* Note: for a server template, its id is its prefix.
2143 * This is a temporary id which will be used for server allocations to come
2144 * after parsing.
2145 */
2146 if (srv)
2147 newsrv->id = strdup(args[1]);
2148 else
2149 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002150
2151 /* several ways to check the port component :
2152 * - IP => port=+0, relative (IPv4 only)
2153 * - IP: => port=+0, relative
2154 * - IP:N => port=N, absolute
2155 * - IP:+N => port=+N, relative
2156 * - IP:-N => port=-N, relative
2157 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002158 if (!parse_addr)
2159 goto skip_addr;
2160
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002161 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002162 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002163 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002164 err_code |= ERR_ALERT | ERR_FATAL;
2165 goto out;
2166 }
2167
2168 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002169 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002170 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002171 file, linenum, args[0], args[1]);
2172 err_code |= ERR_ALERT | ERR_FATAL;
2173 goto out;
2174 }
2175
2176 if (!port1 || !port2) {
2177 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002178 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002179 }
2180 else if (port1 != port2) {
2181 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002182 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002183 file, linenum, args[0], args[1], args[2]);
2184 err_code |= ERR_ALERT | ERR_FATAL;
2185 goto out;
2186 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002187
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002188 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002189 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002190 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002191 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002192 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2193 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2194 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002195 err_code |= ERR_ALERT | ERR_FATAL;
2196 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002197 }
2198 }
2199 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002200 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002201 file, linenum, newsrv->id);
2202 err_code |= ERR_ALERT | ERR_FATAL;
2203 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002204 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002205 }
2206
Willy Tarreau272adea2014-03-31 10:39:59 +02002207 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002208 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002209
Olivier Houchard8da5f982017-08-04 18:35:36 +02002210 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002211 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002212 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002213 err_code |= ERR_ALERT | ERR_FATAL;
2214 goto out;
2215 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002216
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002217 cur_arg++;
2218 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002219 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002220 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002221 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002222 } else {
2223 newsrv = &curproxy->defsrv;
2224 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002225 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002226 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002227 }
2228
2229 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002230 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002231 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2232 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002233 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002234 file, linenum, *err, newsrv->id);
2235 err_code |= ERR_ALERT | ERR_FATAL;
2236 goto out;
2237 }
2238 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002239 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002240 file, linenum, val, args[cur_arg], newsrv->id);
2241 err_code |= ERR_ALERT | ERR_FATAL;
2242 goto out;
2243 }
2244 newsrv->agent.inter = val;
2245 cur_arg += 2;
2246 }
Misiekea849332017-01-09 09:39:51 +01002247 else if (!strcmp(args[cur_arg], "agent-addr")) {
2248 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002249 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002250 goto out;
2251 }
2252
2253 cur_arg += 2;
2254 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002255 else if (!strcmp(args[cur_arg], "agent-port")) {
2256 global.maxsock++;
2257 newsrv->agent.port = atol(args[cur_arg + 1]);
2258 cur_arg += 2;
2259 }
James Brown55f9ff12015-10-21 18:19:05 -07002260 else if (!strcmp(args[cur_arg], "agent-send")) {
2261 global.maxsock++;
2262 free(newsrv->agent.send_string);
2263 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2264 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2265 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2266 cur_arg += 2;
2267 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002268 else if (!strcmp(args[cur_arg], "init-addr")) {
2269 char *p, *end;
2270 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002271 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002272
2273 newsrv->init_addr_methods = 0;
2274 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2275
2276 for (p = args[cur_arg + 1]; *p; p = end) {
2277 /* cut on next comma */
2278 for (end = p; *end && *end != ','; end++);
2279 if (*end)
2280 *(end++) = 0;
2281
Willy Tarreau4310d362016-11-02 15:05:56 +01002282 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002283 if (!strcmp(p, "libc")) {
2284 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2285 }
2286 else if (!strcmp(p, "last")) {
2287 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2288 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002289 else if (!strcmp(p, "none")) {
2290 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2291 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002292 else if (str2ip2(p, &sa, 0)) {
2293 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002294 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002295 file, linenum, args[cur_arg], p);
2296 err_code |= ERR_ALERT | ERR_FATAL;
2297 goto out;
2298 }
2299 newsrv->init_addr = sa;
2300 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2301 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002302 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002303 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 +02002304 file, linenum, args[cur_arg], p);
2305 err_code |= ERR_ALERT | ERR_FATAL;
2306 goto out;
2307 }
2308 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002309 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002310 file, linenum, args[cur_arg], p);
2311 err_code |= ERR_ALERT | ERR_FATAL;
2312 goto out;
2313 }
2314 }
2315 cur_arg += 2;
2316 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002317 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002318 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002319 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2320 cur_arg += 2;
2321 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002322 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2323 char *p, *end;
2324
2325 for (p = args[cur_arg + 1]; *p; p = end) {
2326 /* cut on next comma */
2327 for (end = p; *end && *end != ','; end++);
2328 if (*end)
2329 *(end++) = 0;
2330
2331 if (!strcmp(p, "allow-dup-ip")) {
2332 newsrv->dns_opts.accept_duplicate_ip = 1;
2333 }
2334 else if (!strcmp(p, "prevent-dup-ip")) {
2335 newsrv->dns_opts.accept_duplicate_ip = 0;
2336 }
2337 else {
2338 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2339 file, linenum, args[cur_arg], p);
2340 err_code |= ERR_ALERT | ERR_FATAL;
2341 goto out;
2342 }
2343 }
2344
2345 cur_arg += 2;
2346 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002347 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2348 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002349 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002350 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002351 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002352 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002353 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002354 file, linenum, args[cur_arg]);
2355 err_code |= ERR_ALERT | ERR_FATAL;
2356 goto out;
2357 }
2358 cur_arg += 2;
2359 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002360 else if (!strcmp(args[cur_arg], "resolve-net")) {
2361 char *p, *e;
2362 unsigned char mask;
2363 struct dns_options *opt;
2364
2365 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002366 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002367 file, linenum, args[cur_arg]);
2368 err_code |= ERR_ALERT | ERR_FATAL;
2369 goto out;
2370 }
2371
2372 opt = &newsrv->dns_opts;
2373
2374 /* Split arguments by comma, and convert it from ipv4 or ipv6
2375 * string network in in_addr or in6_addr.
2376 */
2377 p = args[cur_arg + 1];
2378 e = p;
2379 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002380 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002381 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002382 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002383 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2384 err_code |= ERR_ALERT | ERR_FATAL;
2385 goto out;
2386 }
2387 /* look for end or comma. */
2388 while (*e != ',' && *e != '\0')
2389 e++;
2390 if (*e == ',') {
2391 *e = '\0';
2392 e++;
2393 }
2394 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2395 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2396 /* Try to convert input string from ipv4 or ipv6 network. */
2397 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2398 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2399 &mask)) {
2400 /* Try to convert input string from ipv6 network. */
2401 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2402 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2403 } else {
2404 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002405 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002406 file, linenum, args[cur_arg], p);
2407 err_code |= ERR_ALERT | ERR_FATAL;
2408 goto out;
2409 }
2410 opt->pref_net_nb++;
2411 p = e;
2412 }
2413
2414 cur_arg += 2;
2415 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002416 else if (!strcmp(args[cur_arg], "rise")) {
2417 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002418 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002419 file, linenum, args[cur_arg]);
2420 err_code |= ERR_ALERT | ERR_FATAL;
2421 goto out;
2422 }
2423
2424 newsrv->check.rise = atol(args[cur_arg + 1]);
2425 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002426 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002427 file, linenum, args[cur_arg]);
2428 err_code |= ERR_ALERT | ERR_FATAL;
2429 goto out;
2430 }
2431
2432 if (newsrv->check.health)
2433 newsrv->check.health = newsrv->check.rise;
2434 cur_arg += 2;
2435 }
2436 else if (!strcmp(args[cur_arg], "fall")) {
2437 newsrv->check.fall = atol(args[cur_arg + 1]);
2438
2439 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002440 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002441 file, linenum, args[cur_arg]);
2442 err_code |= ERR_ALERT | ERR_FATAL;
2443 goto out;
2444 }
2445
2446 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002447 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002448 file, linenum, args[cur_arg]);
2449 err_code |= ERR_ALERT | ERR_FATAL;
2450 goto out;
2451 }
2452
2453 cur_arg += 2;
2454 }
2455 else if (!strcmp(args[cur_arg], "inter")) {
2456 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2457 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002458 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002459 file, linenum, *err, newsrv->id);
2460 err_code |= ERR_ALERT | ERR_FATAL;
2461 goto out;
2462 }
2463 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002464 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002465 file, linenum, val, args[cur_arg], newsrv->id);
2466 err_code |= ERR_ALERT | ERR_FATAL;
2467 goto out;
2468 }
2469 newsrv->check.inter = val;
2470 cur_arg += 2;
2471 }
2472 else if (!strcmp(args[cur_arg], "fastinter")) {
2473 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2474 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002475 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002476 file, linenum, *err, newsrv->id);
2477 err_code |= ERR_ALERT | ERR_FATAL;
2478 goto out;
2479 }
2480 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002481 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002482 file, linenum, val, args[cur_arg], newsrv->id);
2483 err_code |= ERR_ALERT | ERR_FATAL;
2484 goto out;
2485 }
2486 newsrv->check.fastinter = val;
2487 cur_arg += 2;
2488 }
2489 else if (!strcmp(args[cur_arg], "downinter")) {
2490 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2491 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002492 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002493 file, linenum, *err, newsrv->id);
2494 err_code |= ERR_ALERT | ERR_FATAL;
2495 goto out;
2496 }
2497 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002498 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002499 file, linenum, val, args[cur_arg], newsrv->id);
2500 err_code |= ERR_ALERT | ERR_FATAL;
2501 goto out;
2502 }
2503 newsrv->check.downinter = val;
2504 cur_arg += 2;
2505 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002506 else if (!strcmp(args[cur_arg], "port")) {
2507 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002508 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002509 cur_arg += 2;
2510 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002511 else if (!strcmp(args[cur_arg], "weight")) {
2512 int w;
2513 w = atol(args[cur_arg + 1]);
2514 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002515 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002516 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2517 err_code |= ERR_ALERT | ERR_FATAL;
2518 goto out;
2519 }
2520 newsrv->uweight = newsrv->iweight = w;
2521 cur_arg += 2;
2522 }
2523 else if (!strcmp(args[cur_arg], "minconn")) {
2524 newsrv->minconn = atol(args[cur_arg + 1]);
2525 cur_arg += 2;
2526 }
2527 else if (!strcmp(args[cur_arg], "maxconn")) {
2528 newsrv->maxconn = atol(args[cur_arg + 1]);
2529 cur_arg += 2;
2530 }
2531 else if (!strcmp(args[cur_arg], "maxqueue")) {
2532 newsrv->maxqueue = atol(args[cur_arg + 1]);
2533 cur_arg += 2;
2534 }
2535 else if (!strcmp(args[cur_arg], "slowstart")) {
2536 /* slowstart is stored in seconds */
2537 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2538 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002539 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002540 file, linenum, *err, newsrv->id);
2541 err_code |= ERR_ALERT | ERR_FATAL;
2542 goto out;
2543 }
2544 newsrv->slowstart = (val + 999) / 1000;
2545 cur_arg += 2;
2546 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002547 else if (!strcmp(args[cur_arg], "on-error")) {
2548 if (!strcmp(args[cur_arg + 1], "fastinter"))
2549 newsrv->onerror = HANA_ONERR_FASTINTER;
2550 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2551 newsrv->onerror = HANA_ONERR_FAILCHK;
2552 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2553 newsrv->onerror = HANA_ONERR_SUDDTH;
2554 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2555 newsrv->onerror = HANA_ONERR_MARKDWN;
2556 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002557 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002558 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2559 file, linenum, args[cur_arg], args[cur_arg + 1]);
2560 err_code |= ERR_ALERT | ERR_FATAL;
2561 goto out;
2562 }
2563
2564 cur_arg += 2;
2565 }
2566 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2567 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2568 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2569 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002570 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002571 file, linenum, args[cur_arg], args[cur_arg + 1]);
2572 err_code |= ERR_ALERT | ERR_FATAL;
2573 goto out;
2574 }
2575
2576 cur_arg += 2;
2577 }
2578 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2579 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2580 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2581 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002582 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002583 file, linenum, args[cur_arg], args[cur_arg + 1]);
2584 err_code |= ERR_ALERT | ERR_FATAL;
2585 goto out;
2586 }
2587
2588 cur_arg += 2;
2589 }
2590 else if (!strcmp(args[cur_arg], "error-limit")) {
2591 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002592 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002593 file, linenum, args[cur_arg]);
2594 err_code |= ERR_ALERT | ERR_FATAL;
2595 goto out;
2596 }
2597
2598 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2599
2600 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002601 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002602 file, linenum, args[cur_arg]);
2603 err_code |= ERR_ALERT | ERR_FATAL;
2604 goto out;
2605 }
2606 cur_arg += 2;
2607 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002608 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002609 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002610 file, linenum, "usesrc", "source");
2611 err_code |= ERR_ALERT | ERR_FATAL;
2612 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002613 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002614 else {
2615 static int srv_dumped;
2616 struct srv_kw *kw;
2617 char *err;
2618
2619 kw = srv_find_kw(args[cur_arg]);
2620 if (kw) {
2621 char *err = NULL;
2622 int code;
2623
2624 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002625 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 +02002626 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002627 if (kw->skip != -1)
2628 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002629 err_code |= ERR_ALERT | ERR_FATAL;
2630 goto out;
2631 }
2632
2633 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002634 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002635 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002636 if (kw->skip != -1)
2637 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002638 err_code |= ERR_ALERT;
2639 continue;
2640 }
2641
2642 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2643 err_code |= code;
2644
2645 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002646 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002647 if (code & ERR_FATAL) {
2648 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002649 if (kw->skip != -1)
2650 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002651 goto out;
2652 }
2653 }
2654 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002655 if (kw->skip != -1)
2656 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002657 continue;
2658 }
2659
2660 err = NULL;
2661 if (!srv_dumped) {
2662 srv_dump_kws(&err);
2663 indent_msg(&err, 4);
2664 srv_dumped = 1;
2665 }
2666
Christopher Faulet767a84b2017-11-24 16:50:31 +01002667 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002668 file, linenum, args[0], args[1], args[cur_arg],
2669 err ? " Registered keywords :" : "", err ? err : "");
2670 free(err);
2671
2672 err_code |= ERR_ALERT | ERR_FATAL;
2673 goto out;
2674 }
2675 }
2676
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002677 if (!defsrv)
2678 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2679 if (err_code & ERR_FATAL)
2680 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002681 if (srv_tmpl)
2682 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002683 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002684 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002685 return 0;
2686
2687 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002688 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002689 free(errmsg);
2690 return err_code;
2691}
2692
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002693/* Returns a pointer to the first server matching either id <id>.
2694 * NULL is returned if no match is found.
2695 * the lookup is performed in the backend <bk>
2696 */
2697struct server *server_find_by_id(struct proxy *bk, int id)
2698{
2699 struct eb32_node *eb32;
2700 struct server *curserver;
2701
2702 if (!bk || (id ==0))
2703 return NULL;
2704
2705 /* <bk> has no backend capabilities, so it can't have a server */
2706 if (!(bk->cap & PR_CAP_BE))
2707 return NULL;
2708
2709 curserver = NULL;
2710
2711 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2712 if (eb32)
2713 curserver = container_of(eb32, struct server, conf.id);
2714
2715 return curserver;
2716}
2717
2718/* Returns a pointer to the first server matching either name <name>, or id
2719 * if <name> starts with a '#'. NULL is returned if no match is found.
2720 * the lookup is performed in the backend <bk>
2721 */
2722struct server *server_find_by_name(struct proxy *bk, const char *name)
2723{
2724 struct server *curserver;
2725
2726 if (!bk || !name)
2727 return NULL;
2728
2729 /* <bk> has no backend capabilities, so it can't have a server */
2730 if (!(bk->cap & PR_CAP_BE))
2731 return NULL;
2732
2733 curserver = NULL;
2734 if (*name == '#') {
2735 curserver = server_find_by_id(bk, atoi(name + 1));
2736 if (curserver)
2737 return curserver;
2738 }
2739 else {
2740 curserver = bk->srv;
2741
2742 while (curserver && (strcmp(curserver->id, name) != 0))
2743 curserver = curserver->next;
2744
2745 if (curserver)
2746 return curserver;
2747 }
2748
2749 return NULL;
2750}
2751
2752struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2753{
2754 struct server *byname;
2755 struct server *byid;
2756
2757 if (!name && !id)
2758 return NULL;
2759
2760 if (diff)
2761 *diff = 0;
2762
2763 byname = byid = NULL;
2764
2765 if (name) {
2766 byname = server_find_by_name(bk, name);
2767 if (byname && (!id || byname->puid == id))
2768 return byname;
2769 }
2770
2771 /* remaining possibilities :
2772 * - name not set
2773 * - name set but not found
2774 * - name found but ID doesn't match
2775 */
2776 if (id) {
2777 byid = server_find_by_id(bk, id);
2778 if (byid) {
2779 if (byname) {
2780 /* use id only if forced by configuration */
2781 if (byid->flags & SRV_F_FORCED_ID) {
2782 if (diff)
2783 *diff |= 2;
2784 return byid;
2785 }
2786 else {
2787 if (diff)
2788 *diff |= 1;
2789 return byname;
2790 }
2791 }
2792
2793 /* remaining possibilities:
2794 * - name not set
2795 * - name set but not found
2796 */
2797 if (name && diff)
2798 *diff |= 2;
2799 return byid;
2800 }
2801
2802 /* id bot found */
2803 if (byname) {
2804 if (diff)
2805 *diff |= 1;
2806 return byname;
2807 }
2808 }
2809
2810 return NULL;
2811}
2812
Willy Tarreau46b7f532018-08-21 11:54:26 +02002813/* Update a server state using the parameters available in the params list.
2814 *
2815 * Grabs the server lock during operation.
2816 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002817static void srv_update_state(struct server *srv, int version, char **params)
2818{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002819 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002820 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002821
2822 /* fields since version 1
2823 * and common to all other upcoming versions
2824 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002825 enum srv_state srv_op_state;
2826 enum srv_admin srv_admin_state;
2827 unsigned srv_uweight, srv_iweight;
2828 unsigned long srv_last_time_change;
2829 short srv_check_status;
2830 enum chk_result srv_check_result;
2831 int srv_check_health;
2832 int srv_check_state, srv_agent_state;
2833 int bk_f_forced_id;
2834 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002835 int fqdn_set_by_cli;
2836 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002837 const char *port_str;
2838 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002839 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002840
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002841 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002842 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002843 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002844 switch (version) {
2845 case 1:
2846 /*
2847 * now we can proceed with server's state update:
2848 * srv_addr: params[0]
2849 * srv_op_state: params[1]
2850 * srv_admin_state: params[2]
2851 * srv_uweight: params[3]
2852 * srv_iweight: params[4]
2853 * srv_last_time_change: params[5]
2854 * srv_check_status: params[6]
2855 * srv_check_result: params[7]
2856 * srv_check_health: params[8]
2857 * srv_check_state: params[9]
2858 * srv_agent_state: params[10]
2859 * bk_f_forced_id: params[11]
2860 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002861 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002862 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002863 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002864 */
2865
2866 /* validating srv_op_state */
2867 p = NULL;
2868 errno = 0;
2869 srv_op_state = strtol(params[1], &p, 10);
2870 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2871 (srv_op_state != SRV_ST_STOPPED &&
2872 srv_op_state != SRV_ST_STARTING &&
2873 srv_op_state != SRV_ST_RUNNING &&
2874 srv_op_state != SRV_ST_STOPPING)) {
2875 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2876 }
2877
2878 /* validating srv_admin_state */
2879 p = NULL;
2880 errno = 0;
2881 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002882 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002883
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002884 /* inherited statuses will be recomputed later.
2885 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2886 */
2887 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002888
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002889 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2890 (srv_admin_state != 0 &&
2891 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002892 srv_admin_state != SRV_ADMF_CMAINT &&
2893 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002894 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002895 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002896 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2897 }
2898
2899 /* validating srv_uweight */
2900 p = NULL;
2901 errno = 0;
2902 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002903 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002904 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2905
2906 /* validating srv_iweight */
2907 p = NULL;
2908 errno = 0;
2909 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002910 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002911 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2912
2913 /* validating srv_last_time_change */
2914 p = NULL;
2915 errno = 0;
2916 srv_last_time_change = strtol(params[5], &p, 10);
2917 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2918 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2919
2920 /* validating srv_check_status */
2921 p = NULL;
2922 errno = 0;
2923 srv_check_status = strtol(params[6], &p, 10);
2924 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2925 (srv_check_status >= HCHK_STATUS_SIZE))
2926 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2927
2928 /* validating srv_check_result */
2929 p = NULL;
2930 errno = 0;
2931 srv_check_result = strtol(params[7], &p, 10);
2932 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2933 (srv_check_result != CHK_RES_UNKNOWN &&
2934 srv_check_result != CHK_RES_NEUTRAL &&
2935 srv_check_result != CHK_RES_FAILED &&
2936 srv_check_result != CHK_RES_PASSED &&
2937 srv_check_result != CHK_RES_CONDPASS)) {
2938 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2939 }
2940
2941 /* validating srv_check_health */
2942 p = NULL;
2943 errno = 0;
2944 srv_check_health = strtol(params[8], &p, 10);
2945 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2946 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2947
2948 /* validating srv_check_state */
2949 p = NULL;
2950 errno = 0;
2951 srv_check_state = strtol(params[9], &p, 10);
2952 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2953 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2954 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2955
2956 /* validating srv_agent_state */
2957 p = NULL;
2958 errno = 0;
2959 srv_agent_state = strtol(params[10], &p, 10);
2960 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2961 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2962 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2963
2964 /* validating bk_f_forced_id */
2965 p = NULL;
2966 errno = 0;
2967 bk_f_forced_id = strtol(params[11], &p, 10);
2968 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2969 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2970
2971 /* validating srv_f_forced_id */
2972 p = NULL;
2973 errno = 0;
2974 srv_f_forced_id = strtol(params[12], &p, 10);
2975 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2976 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2977
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002978 /* validating srv_fqdn */
2979 fqdn = params[13];
2980 if (fqdn && *fqdn == '-')
2981 fqdn = NULL;
2982 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2983 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2984 fqdn = NULL;
2985 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002986
Frédéric Lécaille31694712017-08-01 08:47:19 +02002987 port_str = params[14];
2988 if (port_str) {
2989 port = strl2uic(port_str, strlen(port_str));
2990 if (port > USHRT_MAX) {
2991 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2992 port_str = NULL;
2993 }
2994 }
2995
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002996 /* SRV record
2997 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2998 */
2999 srvrecord = params[15];
3000 if (srvrecord && *srvrecord != '_')
3001 srvrecord = NULL;
3002
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003003 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003004 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003005 goto out;
3006
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003007 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003008 /* recover operational state and apply it to this server
3009 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003010 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003011 switch (srv_op_state) {
3012 case SRV_ST_STOPPED:
3013 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003014 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003015 break;
3016 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003017 /* If rise == 1 there is no STARTING state, let's switch to
3018 * RUNNING
3019 */
3020 if (srv->check.rise == 1) {
3021 srv->check.health = srv->check.rise + srv->check.fall - 1;
3022 srv_set_running(srv, "", NULL);
3023 break;
3024 }
3025 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3026 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003027 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003028 break;
3029 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003030 /* If fall == 1 there is no STOPPING state, let's switch to
3031 * STOPPED
3032 */
3033 if (srv->check.fall == 1) {
3034 srv->check.health = 0;
3035 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3036 break;
3037 }
3038 if (srv->check.health < srv->check.rise ||
3039 srv->check.health > srv->check.rise + srv->check.fall - 2)
3040 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003041 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003042 break;
3043 case SRV_ST_RUNNING:
3044 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003045 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003046 break;
3047 }
3048
3049 /* When applying server state, the following rules apply:
3050 * - in case of a configuration change, we apply the setting from the new
3051 * configuration, regardless of old running state
3052 * - if no configuration change, we apply old running state only if old running
3053 * state is different from new configuration state
3054 */
3055 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003056 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3057 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003058 srv_adm_set_maint(srv);
3059 else
3060 srv_adm_set_ready(srv);
3061 }
3062 /* configuration is the same, let's compate old running state and new conf state */
3063 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003064 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003065 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003066 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003067 srv_adm_set_ready(srv);
3068 }
3069 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003070 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003071 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003072 * (srv->iweight is the weight set up in configuration).
3073 * There are two possible reasons for FDRAIN to have been present :
3074 * - previous config weight was zero
3075 * - "set server b/s drain" was sent to the CLI
3076 *
3077 * In the first case, we simply want to drop this drain state
3078 * if the new weight is not zero anymore, meaning the administrator
3079 * has intentionally turned the weight back to a positive value to
3080 * enable the server again after an operation. In the second case,
3081 * the drain state was forced on the CLI regardless of the config's
3082 * weight so we don't want a change to the config weight to lose this
3083 * status. What this means is :
3084 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3085 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003086 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003087 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003088 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003089 }
3090
3091 srv->last_change = date.tv_sec - srv_last_time_change;
3092 srv->check.status = srv_check_status;
3093 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003094
3095 /* Only case we want to apply is removing ENABLED flag which could have been
3096 * done by the "disable health" command over the stats socket
3097 */
3098 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3099 (srv_check_state & CHK_ST_CONFIGURED) &&
3100 !(srv_check_state & CHK_ST_ENABLED))
3101 srv->check.state &= ~CHK_ST_ENABLED;
3102
3103 /* Only case we want to apply is removing ENABLED flag which could have been
3104 * done by the "disable agent" command over the stats socket
3105 */
3106 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3107 (srv_agent_state & CHK_ST_CONFIGURED) &&
3108 !(srv_agent_state & CHK_ST_ENABLED))
3109 srv->agent.state &= ~CHK_ST_ENABLED;
3110
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003111 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3112 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003113 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003114 * It means that a configuration file change has precedence over a unix socket change
3115 * for server's weight
3116 *
3117 * by default, HAProxy applies the following weight when parsing the configuration
3118 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003119 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003120 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003121 srv->uweight = srv_uweight;
3122 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003123 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003124
Willy Tarreaue5a60682016-11-09 14:54:53 +01003125 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003126 if (strcmp(params[0], "-"))
3127 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003128
3129 if (fqdn && srv->hostname) {
3130 if (!strcmp(srv->hostname, fqdn)) {
3131 /* Here we reset the 'set from stats socket FQDN' flag
3132 * to support such transitions:
3133 * Let's say initial FQDN value is foo1 (in configuration file).
3134 * - FQDN changed from stats socket, from foo1 to foo2 value,
3135 * - FQDN changed again from file configuration (with the same previous value
3136 set from stats socket, from foo1 to foo2 value),
3137 * - reload for any other reason than a FQDN modification,
3138 * the configuration file FQDN matches the fqdn server state file value.
3139 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003140 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003141 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003142 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003143 }
3144 else {
3145 /* If the FDQN has been changed from stats socket,
3146 * apply fqdn state file value (which is the value set
3147 * from stats socket).
3148 */
3149 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003150 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003151 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003152 }
3153 }
3154 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003155 /* If all the conditions below are validated, this means
3156 * we're evaluating a server managed by SRV resolution
3157 */
3158 else if (fqdn && !srv->hostname && srvrecord) {
3159 int res;
3160
3161 /* we can't apply previous state if SRV record has changed */
3162 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3163 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);
3164 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3165 goto out;
3166 }
3167
3168 /* create or find a SRV resolution for this srv record */
3169 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3170 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3171 if (srv->srvrq == NULL) {
3172 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3173 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3174 goto out;
3175 }
3176
3177 /* prepare DNS resolution for this server */
3178 res = srv_prepare_for_resolution(srv, fqdn);
3179 if (res == -1) {
3180 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3181 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3182 goto out;
3183 }
3184
3185 /* configure check.port accordingly */
3186 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3187 !(srv->flags & SRV_F_CHECKPORT))
3188 srv->check.port = port;
3189
3190 /* Unset SRV_F_MAPPORTS for SRV records.
3191 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3192 * because no ports are provided in the configuration file.
3193 * This is because HAProxy will use the port found into the SRV record.
3194 */
3195 srv->flags &= ~SRV_F_MAPPORTS;
3196 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003197
Frédéric Lécaille31694712017-08-01 08:47:19 +02003198 if (port_str)
3199 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003200 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003201
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003202 break;
3203 default:
3204 chunk_appendf(msg, ", version '%d' not supported", version);
3205 }
3206
3207 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003208 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003209 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003210 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003211 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003212 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003213}
3214
3215/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3216 * For each proxy, it does the following:
3217 * - opens its server state file (either one or local one)
3218 * - read whole file, line by line
3219 * - analyse each line to check if it matches our current backend:
3220 * - backend name matches
3221 * - backend id matches if id is forced and name doesn't match
3222 * - if the server pointed by the line is found, then state is applied
3223 *
3224 * If the running backend uuid or id differs from the state file, then HAProxy reports
3225 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003226 *
3227 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003228 */
3229void apply_server_state(void)
3230{
3231 char *cur, *end;
3232 char mybuf[SRV_STATE_LINE_MAXLEN];
3233 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003234 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3235 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003236 int arg, srv_arg, version, diff;
3237 FILE *f;
3238 char *filepath;
3239 char globalfilepath[MAXPATHLEN + 1];
3240 char localfilepath[MAXPATHLEN + 1];
3241 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003242 struct proxy *curproxy, *bk;
3243 struct server *srv;
3244
3245 globalfilepathlen = 0;
3246 /* create the globalfilepath variable */
3247 if (global.server_state_file) {
3248 /* absolute path or no base directory provided */
3249 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3250 len = strlen(global.server_state_file);
3251 if (len > MAXPATHLEN) {
3252 globalfilepathlen = 0;
3253 goto globalfileerror;
3254 }
3255 memcpy(globalfilepath, global.server_state_file, len);
3256 globalfilepath[len] = '\0';
3257 globalfilepathlen = len;
3258 }
3259 else if (global.server_state_base) {
3260 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003261 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003262 globalfilepathlen = 0;
3263 goto globalfileerror;
3264 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003265 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003266 globalfilepath[len] = 0;
3267 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003268
3269 /* append a slash if needed */
3270 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3271 if (globalfilepathlen + 1 > MAXPATHLEN) {
3272 globalfilepathlen = 0;
3273 goto globalfileerror;
3274 }
3275 globalfilepath[globalfilepathlen++] = '/';
3276 }
3277
3278 len = strlen(global.server_state_file);
3279 if (globalfilepathlen + len > MAXPATHLEN) {
3280 globalfilepathlen = 0;
3281 goto globalfileerror;
3282 }
3283 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3284 globalfilepathlen += len;
3285 globalfilepath[globalfilepathlen++] = 0;
3286 }
3287 }
3288 globalfileerror:
3289 if (globalfilepathlen == 0)
3290 globalfilepath[0] = '\0';
3291
3292 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003293 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003294 /* servers are only in backends */
3295 if (!(curproxy->cap & PR_CAP_BE))
3296 continue;
3297 fileopenerr = 0;
3298 filepath = NULL;
3299
3300 /* search server state file path and name */
3301 switch (curproxy->load_server_state_from_file) {
3302 /* read servers state from global file */
3303 case PR_SRV_STATE_FILE_GLOBAL:
3304 /* there was an error while generating global server state file path */
3305 if (globalfilepathlen == 0)
3306 continue;
3307 filepath = globalfilepath;
3308 fileopenerr = 1;
3309 break;
3310 /* this backend has its own file */
3311 case PR_SRV_STATE_FILE_LOCAL:
3312 localfilepathlen = 0;
3313 localfilepath[0] = '\0';
3314 len = 0;
3315 /* create the localfilepath variable */
3316 /* absolute path or no base directory provided */
3317 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3318 len = strlen(curproxy->server_state_file_name);
3319 if (len > MAXPATHLEN) {
3320 localfilepathlen = 0;
3321 goto localfileerror;
3322 }
3323 memcpy(localfilepath, curproxy->server_state_file_name, len);
3324 localfilepath[len] = '\0';
3325 localfilepathlen = len;
3326 }
3327 else if (global.server_state_base) {
3328 len = strlen(global.server_state_base);
3329 localfilepathlen += len;
3330
3331 if (localfilepathlen > MAXPATHLEN) {
3332 localfilepathlen = 0;
3333 goto localfileerror;
3334 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003335 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003336 localfilepath[localfilepathlen] = 0;
3337
3338 /* append a slash if needed */
3339 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3340 if (localfilepathlen + 1 > MAXPATHLEN) {
3341 localfilepathlen = 0;
3342 goto localfileerror;
3343 }
3344 localfilepath[localfilepathlen++] = '/';
3345 }
3346
3347 len = strlen(curproxy->server_state_file_name);
3348 if (localfilepathlen + len > MAXPATHLEN) {
3349 localfilepathlen = 0;
3350 goto localfileerror;
3351 }
3352 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3353 localfilepathlen += len;
3354 localfilepath[localfilepathlen++] = 0;
3355 }
3356 filepath = localfilepath;
3357 localfileerror:
3358 if (localfilepathlen == 0)
3359 localfilepath[0] = '\0';
3360
3361 break;
3362 case PR_SRV_STATE_FILE_NONE:
3363 default:
3364 continue;
3365 }
3366
3367 /* preload global state file */
3368 errno = 0;
3369 f = fopen(filepath, "r");
3370 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003371 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003372 if (!f)
3373 continue;
3374
3375 mybuf[0] = '\0';
3376 mybuflen = 0;
3377 version = 0;
3378
3379 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003380 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003381 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003382 goto fileclose;
3383 }
3384
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003385 cur = mybuf;
3386 version = atoi(cur);
3387 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3388 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003389 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003390
3391 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3392 int bk_f_forced_id = 0;
3393 int check_id = 0;
3394 int check_name = 0;
3395
3396 mybuflen = strlen(mybuf);
3397 cur = mybuf;
3398 end = cur + mybuflen;
3399
3400 bk = NULL;
3401 srv = NULL;
3402
3403 /* we need at least one character */
3404 if (mybuflen == 0)
3405 continue;
3406
3407 /* ignore blank characters at the beginning of the line */
3408 while (isspace(*cur))
3409 ++cur;
3410
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003411 /* Ignore empty or commented lines */
3412 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003413 continue;
3414
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003415 /* truncated lines */
3416 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003417 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003418 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003419 }
3420
3421 /* Removes trailing '\n' */
3422 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003423
3424 /* we're now ready to move the line into *srv_params[] */
3425 params[0] = cur;
3426 arg = 1;
3427 srv_arg = 0;
3428 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3429 if (isspace(*cur)) {
3430 *cur = '\0';
3431 ++cur;
3432 while (isspace(*cur))
3433 ++cur;
3434 switch (version) {
3435 case 1:
3436 /*
3437 * srv_addr: params[4] => srv_params[0]
3438 * srv_op_state: params[5] => srv_params[1]
3439 * srv_admin_state: params[6] => srv_params[2]
3440 * srv_uweight: params[7] => srv_params[3]
3441 * srv_iweight: params[8] => srv_params[4]
3442 * srv_last_time_change: params[9] => srv_params[5]
3443 * srv_check_status: params[10] => srv_params[6]
3444 * srv_check_result: params[11] => srv_params[7]
3445 * srv_check_health: params[12] => srv_params[8]
3446 * srv_check_state: params[13] => srv_params[9]
3447 * srv_agent_state: params[14] => srv_params[10]
3448 * bk_f_forced_id: params[15] => srv_params[11]
3449 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003450 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003451 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003452 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003453 */
3454 if (arg >= 4) {
3455 srv_params[srv_arg] = cur;
3456 ++srv_arg;
3457 }
3458 break;
3459 }
3460
3461 params[arg] = cur;
3462 ++arg;
3463 }
3464 else {
3465 ++cur;
3466 }
3467 }
3468
3469 /* if line is incomplete line, then ignore it.
3470 * otherwise, update useful flags */
3471 switch (version) {
3472 case 1:
3473 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3474 continue;
3475 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3476 check_id = (atoi(params[0]) == curproxy->uuid);
3477 check_name = (strcmp(curproxy->id, params[1]) == 0);
3478 break;
3479 }
3480
3481 diff = 0;
3482 bk = curproxy;
3483
3484 /* if backend can't be found, let's continue */
3485 if (!check_id && !check_name)
3486 continue;
3487 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003488 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 +02003489 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3490 }
3491 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003492 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 +02003493 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3494 /* if name doesn't match, we still want to update curproxy if the backend id
3495 * was forced in previous the previous configuration */
3496 if (!bk_f_forced_id)
3497 continue;
3498 }
3499
3500 /* look for the server by its id: param[2] */
3501 /* else look for the server by its name: param[3] */
3502 diff = 0;
3503 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3504
3505 if (!srv) {
3506 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003507 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3508 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003509 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3510 params[3], params[2], params[0], params[1]);
3511 continue;
3512 }
3513 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003514 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 +02003515 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 +02003516 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003517 }
3518 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003519 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 +02003520 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 +02003521 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003522 }
3523
3524 /* now we can proceed with server's state update */
3525 srv_update_state(srv, version, srv_params);
3526 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003527fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003528 fclose(f);
3529 }
3530}
3531
Simon Horman7d09b9a2013-02-12 10:45:51 +09003532/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003533 * update a server's current IP address.
3534 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3535 * ip is in network format.
3536 * updater is a string which contains an information about the requester of the update.
3537 * updater is used if not NULL.
3538 *
3539 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003540 *
3541 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003542 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003543int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003544{
3545 /* generates a log line and a warning on stderr */
3546 if (1) {
3547 /* book enough space for both IPv4 and IPv6 */
3548 char oldip[INET6_ADDRSTRLEN];
3549 char newip[INET6_ADDRSTRLEN];
3550
3551 memset(oldip, '\0', INET6_ADDRSTRLEN);
3552 memset(newip, '\0', INET6_ADDRSTRLEN);
3553
3554 /* copy old IP address in a string */
3555 switch (s->addr.ss_family) {
3556 case AF_INET:
3557 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3558 break;
3559 case AF_INET6:
3560 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3561 break;
3562 };
3563
3564 /* copy new IP address in a string */
3565 switch (ip_sin_family) {
3566 case AF_INET:
3567 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3568 break;
3569 case AF_INET6:
3570 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3571 break;
3572 };
3573
3574 /* save log line into a buffer */
3575 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3576 s->proxy->id, s->id, oldip, newip, updater);
3577
3578 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003579 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003580
3581 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003582 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003583 }
3584
3585 /* save the new IP family */
3586 s->addr.ss_family = ip_sin_family;
3587 /* save the new IP address */
3588 switch (ip_sin_family) {
3589 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003590 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003591 break;
3592 case AF_INET6:
3593 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3594 break;
3595 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003596 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003597
3598 return 0;
3599}
3600
3601/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003602 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3603 *
3604 * Caller can pass its name through <updater> to get it integrated in the response message
3605 * returned by the function.
3606 *
3607 * The function first does the following, in that order:
3608 * - validates the new addr and/or port
3609 * - checks if an update is required (new IP or port is different than current ones)
3610 * - checks the update is allowed:
3611 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3612 * - allow all changes if no CHECKS are configured
3613 * - if CHECK is configured:
3614 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3615 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3616 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003617 *
3618 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003619 */
3620const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3621{
3622 struct sockaddr_storage sa;
3623 int ret, port_change_required;
3624 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003625 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003626 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003627 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003628
3629 msg = get_trash_chunk();
3630 chunk_reset(msg);
3631
3632 if (addr) {
3633 memset(&sa, 0, sizeof(struct sockaddr_storage));
3634 if (str2ip2(addr, &sa, 0) == NULL) {
3635 chunk_printf(msg, "Invalid addr '%s'", addr);
3636 goto out;
3637 }
3638
3639 /* changes are allowed on AF_INET* families only */
3640 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3641 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3642 goto out;
3643 }
3644
3645 /* collecting data currently setup */
3646 memset(current_addr, '\0', sizeof(current_addr));
3647 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3648 /* changes are allowed on AF_INET* families only */
3649 if ((ret != AF_INET) && (ret != AF_INET6)) {
3650 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3651 goto out;
3652 }
3653
3654 /* applying ADDR changes if required and allowed
3655 * ipcmp returns 0 when both ADDR are the same
3656 */
3657 if (ipcmp(&s->addr, &sa) == 0) {
3658 chunk_appendf(msg, "no need to change the addr");
3659 goto port;
3660 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003661 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003662 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003663
3664 /* we also need to update check's ADDR only if it uses the server's one */
3665 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003666 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003667 }
3668
3669 /* we also need to update agent ADDR only if it use the server's one */
3670 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003671 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003672 }
3673
3674 /* update report for caller */
3675 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3676 }
3677
3678 port:
3679 if (port) {
3680 char sign = '\0';
3681 char *endptr;
3682
3683 if (addr)
3684 chunk_appendf(msg, ", ");
3685
3686 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003687 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003688
3689 /* check if PORT change is required */
3690 port_change_required = 0;
3691
3692 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003693 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003694 new_port = strtol(port, &endptr, 10);
3695 if ((errno != 0) || (port == endptr)) {
3696 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3697 goto out;
3698 }
3699
3700 /* check if caller triggers a port mapped or offset */
3701 if (sign == '-' || (sign == '+')) {
3702 /* check if server currently uses port map */
3703 if (!(s->flags & SRV_F_MAPPORTS)) {
3704 /* switch from fixed port to port map mandatorily triggers
3705 * a port change */
3706 port_change_required = 1;
3707 /* check is configured
3708 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3709 * prevent PORT change if check doesn't have it's dedicated port while switching
3710 * to port mapping */
3711 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3712 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.");
3713 goto out;
3714 }
3715 }
3716 /* we're already using port maps */
3717 else {
3718 port_change_required = current_port != new_port;
3719 }
3720 }
3721 /* fixed port */
3722 else {
3723 port_change_required = current_port != new_port;
3724 }
3725
3726 /* applying PORT changes if required and update response message */
3727 if (port_change_required) {
3728 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003729 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003730 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003731
3732 /* prepare message */
3733 chunk_appendf(msg, "port changed from '");
3734 if (s->flags & SRV_F_MAPPORTS)
3735 chunk_appendf(msg, "+");
3736 chunk_appendf(msg, "%d' to '", current_port);
3737
3738 if (sign == '-') {
3739 s->flags |= SRV_F_MAPPORTS;
3740 chunk_appendf(msg, "%c", sign);
3741 /* just use for result output */
3742 new_port = -new_port;
3743 }
3744 else if (sign == '+') {
3745 s->flags |= SRV_F_MAPPORTS;
3746 chunk_appendf(msg, "%c", sign);
3747 }
3748 else {
3749 s->flags &= ~SRV_F_MAPPORTS;
3750 }
3751
3752 chunk_appendf(msg, "%d'", new_port);
3753
3754 /* we also need to update health checks port only if it uses server's realport */
3755 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3756 s->check.port = new_port;
3757 }
3758 }
3759 else {
3760 chunk_appendf(msg, "no need to change the port");
3761 }
3762 }
3763
3764out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003765 if (changed)
3766 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003767 if (updater)
3768 chunk_appendf(msg, " by '%s'", updater);
3769 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003770 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003771}
3772
3773
3774/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003775 * update server status based on result of name resolution
3776 * returns:
3777 * 0 if server status is updated
3778 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003779 *
3780 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003781 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003782int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003783{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003784 struct dns_resolvers *resolvers = s->resolvers;
3785 struct dns_resolution *resolution = s->dns_requester->resolution;
3786 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003787
3788 switch (resolution->status) {
3789 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003790 /* status when HAProxy has just (re)started.
3791 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003792 break;
3793
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003794 case RSLV_STATUS_VALID:
3795 /*
3796 * resume health checks
3797 * server will be turned back on if health check is safe
3798 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003799 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003800 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003801 return 1;
3802 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3803 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003804 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003805 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003806
Emeric Brun52a91d32017-08-31 14:41:55 +02003807 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003808 return 1;
3809 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3810 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3811 s->proxy->id, s->id);
3812
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003813 ha_warning("%s.\n", trash.area);
3814 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003815 return 0;
3816
3817 case RSLV_STATUS_NX:
3818 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003819 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3820 if (!tick_is_expired(exp, now_ms))
3821 break;
3822
3823 if (s->next_admin & SRV_ADMF_RMAINT)
3824 return 1;
3825 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3826 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003827
3828 case RSLV_STATUS_TIMEOUT:
3829 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003830 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3831 if (!tick_is_expired(exp, now_ms))
3832 break;
3833
3834 if (s->next_admin & SRV_ADMF_RMAINT)
3835 return 1;
3836 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3837 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003838
3839 case RSLV_STATUS_REFUSED:
3840 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003841 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3842 if (!tick_is_expired(exp, now_ms))
3843 break;
3844
3845 if (s->next_admin & SRV_ADMF_RMAINT)
3846 return 1;
3847 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3848 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003849
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003850 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003851 /* stop server if resolution failed for a long enough period */
3852 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3853 if (!tick_is_expired(exp, now_ms))
3854 break;
3855
3856 if (s->next_admin & SRV_ADMF_RMAINT)
3857 return 1;
3858 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3859 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003860 }
3861
3862 return 1;
3863}
3864
3865/*
3866 * Server Name Resolution valid response callback
3867 * It expects:
3868 * - <nameserver>: the name server which answered the valid response
3869 * - <response>: buffer containing a valid DNS response
3870 * - <response_len>: size of <response>
3871 * It performs the following actions:
3872 * - ignore response if current ip found and server family not met
3873 * - update with first new ip found if family is met and current IP is not found
3874 * returns:
3875 * 0 on error
3876 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003877 *
3878 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003879 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003880int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003881{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003882 struct server *s = NULL;
3883 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003884 void *serverip, *firstip;
3885 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003886 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003887 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003888 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003889
Christopher Faulet67957bd2017-09-27 11:00:59 +02003890 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003891 if (!s)
3892 return 1;
3893
Christopher Faulet67957bd2017-09-27 11:00:59 +02003894 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003895
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003896 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003897 firstip = NULL; /* pointer to the first valid response found */
3898 /* it will be used as the new IP if a change is required */
3899 firstip_sin_family = AF_UNSPEC;
3900 serverip = NULL; /* current server IP address */
3901
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003902 /* initializing server IP pointer */
3903 server_sin_family = s->addr.ss_family;
3904 switch (server_sin_family) {
3905 case AF_INET:
3906 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3907 break;
3908
3909 case AF_INET6:
3910 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3911 break;
3912
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003913 case AF_UNSPEC:
3914 break;
3915
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003916 default:
3917 goto invalid;
3918 }
3919
Baptiste Assmann729c9012017-05-22 15:13:10 +02003920 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003921 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003922 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003923
3924 switch (ret) {
3925 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003926 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003927
3928 case DNS_UPD_SRVIP_NOT_FOUND:
3929 goto save_ip;
3930
3931 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003932 goto invalid;
3933
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003934 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003935 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003936 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003937
Baptiste Assmannfad03182015-10-28 02:03:32 +01003938 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003939 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003940 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003941 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003942
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003943 default:
3944 goto invalid;
3945
3946 }
3947
3948 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003949 if (nameserver) {
3950 nameserver->counters.update++;
3951 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003952 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003953 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003954 else
3955 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003956 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003957
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003958 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003959 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003960 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003961
3962 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003963 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003964 nameserver->counters.invalid++;
3965 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003966 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003967 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003968 return 0;
3969}
3970
3971/*
3972 * Server Name Resolution error management callback
3973 * returns:
3974 * 0 on error
3975 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003976 *
3977 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003978 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003979int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003980{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003981 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003982
Christopher Faulet67957bd2017-09-27 11:00:59 +02003983 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003984 if (!s)
3985 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003986 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003987 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003988 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003989 return 1;
3990}
3991
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003992/*
3993 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003994 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003995 * It returns a pointer to the first server found or NULL if <ip> is not yet
3996 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003997 *
3998 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003999 */
4000struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4001{
4002 struct server *tmpsrv;
4003 struct proxy *be;
4004
4005 if (!srv)
4006 return NULL;
4007
4008 be = srv->proxy;
4009 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004010 /* we found the current server is the same, ignore it */
4011 if (srv == tmpsrv)
4012 continue;
4013
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004014 /* We want to compare the IP in the record with the IP of the servers in the
4015 * same backend, only if:
4016 * * DNS resolution is enabled on the server
4017 * * the hostname used for the resolution by our server is the same than the
4018 * one used for the server found in the backend
4019 * * the server found in the backend is not our current server
4020 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004021 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004022 if ((tmpsrv->hostname_dn == NULL) ||
4023 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4024 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004025 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004026 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004027 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004028 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004029
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004030 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004031 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004032 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004033 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004034 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004035
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004036 /* At this point, we have 2 different servers using the same DNS hostname
4037 * for their respective resolution.
4038 */
4039 if (*ip_family == tmpsrv->addr.ss_family &&
4040 ((tmpsrv->addr.ss_family == AF_INET &&
4041 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4042 (tmpsrv->addr.ss_family == AF_INET6 &&
4043 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004044 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004045 return tmpsrv;
4046 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004047 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004048 }
4049
Emeric Brune9fd6b52017-11-02 17:20:39 +01004050
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004051 return NULL;
4052}
4053
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004054/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4055 * resolver. This is suited for initial address configuration. Returns 0 on
4056 * success otherwise a non-zero error code. In case of error, *err_code, if
4057 * not NULL, is filled up.
4058 */
4059int srv_set_addr_via_libc(struct server *srv, int *err_code)
4060{
4061 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004062 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004063 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004064 return 1;
4065 }
4066 return 0;
4067}
4068
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004069/* Set the server's FDQN (->hostname) from <hostname>.
4070 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004071 *
4072 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004073 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004074int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004075{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004076 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004077 char *hostname_dn;
4078 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004079
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004080 /* Note that the server lock is already held. */
4081 if (!srv->resolvers)
4082 return -1;
4083
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004084 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004085 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004086 /* run time DNS resolution was not active for this server
4087 * and we can't enable it at run time for now.
4088 */
4089 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004090 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004091
4092 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004093 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004094 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004095 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4096 hostname_dn, trash.size);
4097 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004098 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004099
Christopher Faulet67957bd2017-09-27 11:00:59 +02004100 resolution = srv->dns_requester->resolution;
4101 if (resolution &&
4102 resolution->hostname_dn &&
4103 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004104 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004105
Christopher Faulet67957bd2017-09-27 11:00:59 +02004106 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004107
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004108 free(srv->hostname);
4109 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004110 srv->hostname = strdup(hostname);
4111 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004112 srv->hostname_dn_len = hostname_dn_len;
4113 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004114 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004115
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004116 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004117 goto err;
4118
4119 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004120 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004121 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004122 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004123
4124 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004125 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004126 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004127 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004128}
4129
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004130/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4131 * from the state file. This is suited for initial address configuration.
4132 * Returns 0 on success otherwise a non-zero error code. In case of error,
4133 * *err_code, if not NULL, is filled up.
4134 */
4135static int srv_apply_lastaddr(struct server *srv, int *err_code)
4136{
4137 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4138 if (err_code)
4139 *err_code |= ERR_WARN;
4140 return 1;
4141 }
4142 return 0;
4143}
4144
Willy Tarreau25e51522016-11-04 15:10:17 +01004145/* returns 0 if no error, otherwise a combination of ERR_* flags */
4146static int srv_iterate_initaddr(struct server *srv)
4147{
4148 int return_code = 0;
4149 int err_code;
4150 unsigned int methods;
4151
4152 methods = srv->init_addr_methods;
4153 if (!methods) { // default to "last,libc"
4154 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4155 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4156 }
4157
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004158 /* "-dr" : always append "none" so that server addresses resolution
4159 * failures are silently ignored, this is convenient to validate some
4160 * configs out of their environment.
4161 */
4162 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4163 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4164
Willy Tarreau25e51522016-11-04 15:10:17 +01004165 while (methods) {
4166 err_code = 0;
4167 switch (srv_get_next_initaddr(&methods)) {
4168 case SRV_IADDR_LAST:
4169 if (!srv->lastaddr)
4170 continue;
4171 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004172 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004173 return_code |= err_code;
4174 break;
4175
4176 case SRV_IADDR_LIBC:
4177 if (!srv->hostname)
4178 continue;
4179 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004180 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004181 return_code |= err_code;
4182 break;
4183
Willy Tarreau37ebe122016-11-04 15:17:58 +01004184 case SRV_IADDR_NONE:
4185 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004186 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004187 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4188 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004189 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004190 return return_code;
4191
Willy Tarreau4310d362016-11-02 15:05:56 +01004192 case SRV_IADDR_IP:
4193 ipcpy(&srv->init_addr, &srv->addr);
4194 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004195 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4196 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004197 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004198 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004199
Willy Tarreau25e51522016-11-04 15:10:17 +01004200 default: /* unhandled method */
4201 break;
4202 }
4203 }
4204
4205 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004206 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004207 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4208 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004209 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004210 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004211 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4212 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004213
4214 return_code |= ERR_ALERT | ERR_FATAL;
4215 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004216out:
4217 srv_set_dyncookie(srv);
4218 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004219}
4220
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004221/*
4222 * This function parses all backends and all servers within each backend
4223 * and performs servers' addr resolution based on information provided by:
4224 * - configuration file
4225 * - server-state file (states provided by an 'old' haproxy process)
4226 *
4227 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4228 */
4229int srv_init_addr(void)
4230{
4231 struct proxy *curproxy;
4232 int return_code = 0;
4233
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004234 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004235 while (curproxy) {
4236 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004237
4238 /* servers are in backend only */
4239 if (!(curproxy->cap & PR_CAP_BE))
4240 goto srv_init_addr_next;
4241
Willy Tarreau25e51522016-11-04 15:10:17 +01004242 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004243 if (srv->hostname)
4244 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004245
4246 srv_init_addr_next:
4247 curproxy = curproxy->next;
4248 }
4249
4250 return return_code;
4251}
4252
Willy Tarreau46b7f532018-08-21 11:54:26 +02004253/*
4254 * Must be called with the server lock held.
4255 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004256const 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 +02004257{
4258
Willy Tarreau83061a82018-07-13 11:56:34 +02004259 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004260
4261 msg = get_trash_chunk();
4262 chunk_reset(msg);
4263
Olivier Houchard8da5f982017-08-04 18:35:36 +02004264 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004265 chunk_appendf(msg, "no need to change the FDQN");
4266 goto out;
4267 }
4268
4269 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4270 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4271 goto out;
4272 }
4273
4274 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4275 server->proxy->id, server->id, server->hostname, fqdn);
4276
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004277 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004278 chunk_reset(msg);
4279 chunk_appendf(msg, "could not update %s/%s FQDN",
4280 server->proxy->id, server->id);
4281 goto out;
4282 }
4283
4284 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004285 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004286
4287 out:
4288 if (updater)
4289 chunk_appendf(msg, " by '%s'", updater);
4290 chunk_appendf(msg, "\n");
4291
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004292 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004293}
4294
4295
Willy Tarreau21b069d2016-11-23 17:15:08 +01004296/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4297 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004298 * 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 +01004299 * used for CLI commands requiring a server name.
4300 * Important: the <arg> is modified to remove the '/'.
4301 */
4302struct server *cli_find_server(struct appctx *appctx, char *arg)
4303{
4304 struct proxy *px;
4305 struct server *sv;
4306 char *line;
4307
4308 /* split "backend/server" and make <line> point to server */
4309 for (line = arg; *line; line++)
4310 if (*line == '/') {
4311 *line++ = '\0';
4312 break;
4313 }
4314
4315 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004316 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004317 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004318 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004319 return NULL;
4320 }
4321
4322 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004323 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004324 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004325 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004326 return NULL;
4327 }
4328
4329 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004330 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004331 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004332 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004333 return NULL;
4334 }
4335
4336 return sv;
4337}
4338
William Lallemand222baf22016-11-19 02:00:33 +01004339
Willy Tarreau46b7f532018-08-21 11:54:26 +02004340/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004341static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004342{
4343 struct server *sv;
4344 const char *warning;
4345
4346 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4347 return 1;
4348
4349 sv = cli_find_server(appctx, args[2]);
4350 if (!sv)
4351 return 1;
4352
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004353 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004354
William Lallemand222baf22016-11-19 02:00:33 +01004355 if (strcmp(args[3], "weight") == 0) {
4356 warning = server_parse_weight_change_request(sv, args[4]);
4357 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004358 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004359 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004360 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004361 }
4362 }
4363 else if (strcmp(args[3], "state") == 0) {
4364 if (strcmp(args[4], "ready") == 0)
4365 srv_adm_set_ready(sv);
4366 else if (strcmp(args[4], "drain") == 0)
4367 srv_adm_set_drain(sv);
4368 else if (strcmp(args[4], "maint") == 0)
4369 srv_adm_set_maint(sv);
4370 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004371 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004372 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004373 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004374 }
4375 }
4376 else if (strcmp(args[3], "health") == 0) {
4377 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004378 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004379 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004380 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004381 }
4382 else if (strcmp(args[4], "up") == 0) {
4383 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004384 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004385 }
4386 else if (strcmp(args[4], "stopping") == 0) {
4387 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004388 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004389 }
4390 else if (strcmp(args[4], "down") == 0) {
4391 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004392 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004393 }
4394 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004395 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004396 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004397 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004398 }
4399 }
4400 else if (strcmp(args[3], "agent") == 0) {
4401 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004402 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004403 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004404 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004405 }
4406 else if (strcmp(args[4], "up") == 0) {
4407 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004408 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004409 }
4410 else if (strcmp(args[4], "down") == 0) {
4411 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004412 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004413 }
4414 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004415 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004416 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004417 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004418 }
4419 }
Misiek2da082d2017-01-09 09:40:42 +01004420 else if (strcmp(args[3], "agent-addr") == 0) {
4421 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004422 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004423 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4424 appctx->st0 = CLI_ST_PRINT;
4425 } else {
4426 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004427 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004428 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4429 appctx->st0 = CLI_ST_PRINT;
4430 }
4431 }
4432 }
4433 else if (strcmp(args[3], "agent-send") == 0) {
4434 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004435 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004436 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4437 appctx->st0 = CLI_ST_PRINT;
4438 } else {
4439 char *nss = strdup(args[4]);
4440 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004441 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004442 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4443 appctx->st0 = CLI_ST_PRINT;
4444 } else {
4445 free(sv->agent.send_string);
4446 sv->agent.send_string = nss;
4447 sv->agent.send_string_len = strlen(args[4]);
4448 }
4449 }
4450 }
William Lallemand222baf22016-11-19 02:00:33 +01004451 else if (strcmp(args[3], "check-port") == 0) {
4452 int i = 0;
4453 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004454 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004455 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004456 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004457 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004458 }
4459 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004460 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004461 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004462 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004463 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004464 }
4465 /* prevent the update of port to 0 if MAPPORTS are in use */
4466 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004467 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004468 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004469 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004470 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004471 }
4472 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004473 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004474 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004475 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004476 }
4477 else if (strcmp(args[3], "addr") == 0) {
4478 char *addr = NULL;
4479 char *port = NULL;
4480 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004481 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004482 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004483 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004484 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004485 }
4486 else {
4487 addr = args[4];
4488 }
4489 if (strcmp(args[5], "port") == 0) {
4490 port = args[6];
4491 }
4492 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4493 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004494 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004495 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004496 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004497 }
4498 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4499 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004500 else if (strcmp(args[3], "fqdn") == 0) {
4501 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004502 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004503 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4504 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004505 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004506 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004507 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004508 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004509 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004510 appctx->ctx.cli.msg = warning;
4511 appctx->st0 = CLI_ST_PRINT;
4512 }
4513 }
William Lallemand222baf22016-11-19 02:00:33 +01004514 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004515 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004516 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 +01004517 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004518 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004519 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004520 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004521 return 1;
4522}
4523
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004524static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004525{
4526 struct stream_interface *si = appctx->owner;
4527 struct proxy *px;
4528 struct server *sv;
4529 char *line;
4530
4531
4532 /* split "backend/server" and make <line> point to server */
4533 for (line = args[2]; *line; line++)
4534 if (*line == '/') {
4535 *line++ = '\0';
4536 break;
4537 }
4538
4539 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004540 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004541 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004542 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004543 return 1;
4544 }
4545
4546 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004547 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004548 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004549 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004550 return 1;
4551 }
4552
4553 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004554 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4555 sv->iweight);
4556 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004557 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004558 return 0;
4559 }
William Lallemand6b160942016-11-22 12:34:35 +01004560 return 1;
4561}
4562
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004563/* Parse a "set weight" command.
4564 *
4565 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004566 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004567static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004568{
4569 struct server *sv;
4570 const char *warning;
4571
4572 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4573 return 1;
4574
4575 sv = cli_find_server(appctx, args[2]);
4576 if (!sv)
4577 return 1;
4578
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004579 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4580
William Lallemand6b160942016-11-22 12:34:35 +01004581 warning = server_parse_weight_change_request(sv, args[3]);
4582 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004583 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004584 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004585 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004586 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004587
4588 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4589
William Lallemand6b160942016-11-22 12:34:35 +01004590 return 1;
4591}
4592
Willy Tarreau46b7f532018-08-21 11:54:26 +02004593/* parse a "set maxconn server" command. It always returns 1.
4594 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004595 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004596 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004597static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004598{
4599 struct server *sv;
4600 const char *warning;
4601
4602 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4603 return 1;
4604
4605 sv = cli_find_server(appctx, args[3]);
4606 if (!sv)
4607 return 1;
4608
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004609 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4610
Willy Tarreaub8026272016-11-23 11:26:56 +01004611 warning = server_parse_maxconn_change_request(sv, args[4]);
4612 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004613 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004614 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004615 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004616 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004617
4618 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4619
Willy Tarreaub8026272016-11-23 11:26:56 +01004620 return 1;
4621}
William Lallemand6b160942016-11-22 12:34:35 +01004622
Willy Tarreau46b7f532018-08-21 11:54:26 +02004623/* parse a "disable agent" command. It always returns 1.
4624 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004625 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004626 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004627static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004628{
4629 struct server *sv;
4630
4631 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4632 return 1;
4633
4634 sv = cli_find_server(appctx, args[2]);
4635 if (!sv)
4636 return 1;
4637
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004638 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004639 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004640 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004641 return 1;
4642}
4643
Willy Tarreau46b7f532018-08-21 11:54:26 +02004644/* parse a "disable health" command. It always returns 1.
4645 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004646 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004647 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004648static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004649{
4650 struct server *sv;
4651
4652 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4653 return 1;
4654
4655 sv = cli_find_server(appctx, args[2]);
4656 if (!sv)
4657 return 1;
4658
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004659 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004660 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004661 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004662 return 1;
4663}
4664
Willy Tarreau46b7f532018-08-21 11:54:26 +02004665/* parse a "disable server" command. It always returns 1.
4666 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004667 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004668 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004669static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004670{
4671 struct server *sv;
4672
4673 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4674 return 1;
4675
4676 sv = cli_find_server(appctx, args[2]);
4677 if (!sv)
4678 return 1;
4679
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004680 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004681 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004682 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004683 return 1;
4684}
4685
Willy Tarreau46b7f532018-08-21 11:54:26 +02004686/* parse a "enable agent" command. It always returns 1.
4687 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004688 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004689 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004690static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004691{
4692 struct server *sv;
4693
4694 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4695 return 1;
4696
4697 sv = cli_find_server(appctx, args[2]);
4698 if (!sv)
4699 return 1;
4700
4701 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004702 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004703 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004704 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004705 return 1;
4706 }
4707
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004708 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004709 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004710 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004711 return 1;
4712}
4713
Willy Tarreau46b7f532018-08-21 11:54:26 +02004714/* parse a "enable health" command. It always returns 1.
4715 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004716 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004717 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004718static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004719{
4720 struct server *sv;
4721
4722 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4723 return 1;
4724
4725 sv = cli_find_server(appctx, args[2]);
4726 if (!sv)
4727 return 1;
4728
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004729 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004730 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004731 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004732 return 1;
4733}
4734
Willy Tarreau46b7f532018-08-21 11:54:26 +02004735/* parse a "enable server" command. It always returns 1.
4736 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004737 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004738 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004739static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004740{
4741 struct server *sv;
4742
4743 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4744 return 1;
4745
4746 sv = cli_find_server(appctx, args[2]);
4747 if (!sv)
4748 return 1;
4749
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004750 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004751 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004752 if (!(sv->flags & SRV_F_COOKIESET)
4753 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4754 sv->cookie)
4755 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004756 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004757 return 1;
4758}
4759
William Lallemand222baf22016-11-19 02:00:33 +01004760/* register cli keywords */
4761static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004762 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004763 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004764 { { "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 +01004765 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004766 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004767 { { "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 +01004768 { { "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 +01004769 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004770 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4771 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4772
William Lallemand222baf22016-11-19 02:00:33 +01004773 {{},}
4774}};
4775
Willy Tarreau0108d902018-11-25 19:14:37 +01004776INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004777
Emeric Brun64cc49c2017-10-03 14:46:45 +02004778/*
4779 * This function applies server's status changes, it is
4780 * is designed to be called asynchronously.
4781 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004782 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004783 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004784static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004785{
4786 struct check *check = &s->check;
4787 int xferred;
4788 struct proxy *px = s->proxy;
4789 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4790 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4791 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004792 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004793
Emeric Brun64cc49c2017-10-03 14:46:45 +02004794 /* If currently main is not set we try to apply pending state changes */
4795 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4796 int next_admin;
4797
4798 /* Backup next admin */
4799 next_admin = s->next_admin;
4800
4801 /* restore current admin state */
4802 s->next_admin = s->cur_admin;
4803
4804 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4805 s->last_change = now.tv_sec;
4806 if (s->proxy->lbprm.set_server_status_down)
4807 s->proxy->lbprm.set_server_status_down(s);
4808
4809 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4810 srv_shutdown_streams(s, SF_ERR_DOWN);
4811
4812 /* we might have streams queued on this server and waiting for
4813 * a connection. Those which are redispatchable will be queued
4814 * to another server or to the proxy itself.
4815 */
4816 xferred = pendconn_redistribute(s);
4817
4818 tmptrash = alloc_trash_chunk();
4819 if (tmptrash) {
4820 chunk_printf(tmptrash,
4821 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4822 s->proxy->id, s->id);
4823
Emeric Brun5a133512017-10-19 14:42:30 +02004824 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004825 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004826
4827 /* we don't send an alert if the server was previously paused */
4828 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004829 send_log(s->proxy, log_level, "%s.\n",
4830 tmptrash->area);
4831 send_email_alert(s, log_level, "%s",
4832 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004833 free_trash_chunk(tmptrash);
4834 tmptrash = NULL;
4835 }
4836 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4837 set_backend_down(s->proxy);
4838
4839 s->counters.down_trans++;
4840 }
4841 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4842 s->last_change = now.tv_sec;
4843 if (s->proxy->lbprm.set_server_status_down)
4844 s->proxy->lbprm.set_server_status_down(s);
4845
4846 /* we might have streams queued on this server and waiting for
4847 * a connection. Those which are redispatchable will be queued
4848 * to another server or to the proxy itself.
4849 */
4850 xferred = pendconn_redistribute(s);
4851
4852 tmptrash = alloc_trash_chunk();
4853 if (tmptrash) {
4854 chunk_printf(tmptrash,
4855 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4856 s->proxy->id, s->id);
4857
Emeric Brun5a133512017-10-19 14:42:30 +02004858 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004859
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004860 ha_warning("%s.\n", tmptrash->area);
4861 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4862 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004863 free_trash_chunk(tmptrash);
4864 tmptrash = NULL;
4865 }
4866
4867 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4868 set_backend_down(s->proxy);
4869 }
4870 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4871 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4872 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4873 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4874 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4875 s->proxy->last_change = now.tv_sec;
4876 }
4877
4878 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4879 s->down_time += now.tv_sec - s->last_change;
4880
4881 s->last_change = now.tv_sec;
4882 if (s->next_state == SRV_ST_STARTING)
4883 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4884
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004885 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004886 /* now propagate the status change to any LB algorithms */
4887 if (px->lbprm.update_server_eweight)
4888 px->lbprm.update_server_eweight(s);
4889 else if (srv_willbe_usable(s)) {
4890 if (px->lbprm.set_server_status_up)
4891 px->lbprm.set_server_status_up(s);
4892 }
4893 else {
4894 if (px->lbprm.set_server_status_down)
4895 px->lbprm.set_server_status_down(s);
4896 }
4897
4898 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4899 * and it's not a backup server and its effective weight is > 0,
4900 * then it can accept new connections, so we shut down all streams
4901 * on all backup servers.
4902 */
4903 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4904 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4905 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4906
4907 /* check if we can handle some connections queued at the proxy. We
4908 * will take as many as we can handle.
4909 */
4910 xferred = pendconn_grab_from_px(s);
4911
4912 tmptrash = alloc_trash_chunk();
4913 if (tmptrash) {
4914 chunk_printf(tmptrash,
4915 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4916 s->proxy->id, s->id);
4917
Emeric Brun5a133512017-10-19 14:42:30 +02004918 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004919 ha_warning("%s.\n", tmptrash->area);
4920 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4921 tmptrash->area);
4922 send_email_alert(s, LOG_NOTICE, "%s",
4923 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004924 free_trash_chunk(tmptrash);
4925 tmptrash = NULL;
4926 }
4927
4928 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4929 set_backend_down(s->proxy);
4930 }
4931 else if (s->cur_eweight != s->next_eweight) {
4932 /* now propagate the status change to any LB algorithms */
4933 if (px->lbprm.update_server_eweight)
4934 px->lbprm.update_server_eweight(s);
4935 else if (srv_willbe_usable(s)) {
4936 if (px->lbprm.set_server_status_up)
4937 px->lbprm.set_server_status_up(s);
4938 }
4939 else {
4940 if (px->lbprm.set_server_status_down)
4941 px->lbprm.set_server_status_down(s);
4942 }
4943
4944 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4945 set_backend_down(s->proxy);
4946 }
4947
4948 s->next_admin = next_admin;
4949 }
4950
Emeric Brun5a133512017-10-19 14:42:30 +02004951 /* reset operational state change */
4952 *s->op_st_chg.reason = 0;
4953 s->op_st_chg.status = s->op_st_chg.code = -1;
4954 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004955
4956 /* Now we try to apply pending admin changes */
4957
4958 /* Maintenance must also disable health checks */
4959 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4960 if (s->check.state & CHK_ST_ENABLED) {
4961 s->check.state |= CHK_ST_PAUSED;
4962 check->health = 0;
4963 }
4964
4965 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004966 tmptrash = alloc_trash_chunk();
4967 if (tmptrash) {
4968 chunk_printf(tmptrash,
4969 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4970 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4971 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004972
Olivier Houchard796a2b32017-10-24 17:42:47 +02004973 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004974
Olivier Houchard796a2b32017-10-24 17:42:47 +02004975 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004976 ha_warning("%s.\n", tmptrash->area);
4977 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4978 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004979 }
4980 free_trash_chunk(tmptrash);
4981 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004982 }
Emeric Brun8f298292017-12-06 16:47:17 +01004983 /* commit new admin status */
4984
4985 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004986 }
4987 else { /* server was still running */
4988 check->health = 0; /* failure */
4989 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004990
4991 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004992 if (s->proxy->lbprm.set_server_status_down)
4993 s->proxy->lbprm.set_server_status_down(s);
4994
Emeric Brun64cc49c2017-10-03 14:46:45 +02004995 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4996 srv_shutdown_streams(s, SF_ERR_DOWN);
4997
4998 /* we might have streams queued on this server and waiting for
4999 * a connection. Those which are redispatchable will be queued
5000 * to another server or to the proxy itself.
5001 */
5002 xferred = pendconn_redistribute(s);
5003
5004 tmptrash = alloc_trash_chunk();
5005 if (tmptrash) {
5006 chunk_printf(tmptrash,
5007 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5008 s->flags & SRV_F_BACKUP ? "Backup " : "",
5009 s->proxy->id, s->id,
5010 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5011
5012 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5013
5014 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005015 ha_warning("%s.\n", tmptrash->area);
5016 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5017 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005018 }
5019 free_trash_chunk(tmptrash);
5020 tmptrash = NULL;
5021 }
5022 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5023 set_backend_down(s->proxy);
5024
5025 s->counters.down_trans++;
5026 }
5027 }
5028 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5029 /* OK here we're leaving maintenance, we have many things to check,
5030 * because the server might possibly be coming back up depending on
5031 * its state. In practice, leaving maintenance means that we should
5032 * immediately turn to UP (more or less the slowstart) under the
5033 * following conditions :
5034 * - server is neither checked nor tracked
5035 * - server tracks another server which is not checked
5036 * - server tracks another server which is already up
5037 * Which sums up as something simpler :
5038 * "either the tracking server is up or the server's checks are disabled
5039 * or up". Otherwise we only re-enable health checks. There's a special
5040 * case associated to the stopping state which can be inherited. Note
5041 * that the server might still be in drain mode, which is naturally dealt
5042 * with by the lower level functions.
5043 */
5044
5045 if (s->check.state & CHK_ST_ENABLED) {
5046 s->check.state &= ~CHK_ST_PAUSED;
5047 check->health = check->rise; /* start OK but check immediately */
5048 }
5049
5050 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5051 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5052 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5053 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5054 s->next_state = SRV_ST_STOPPING;
5055 }
5056 else {
5057 s->next_state = SRV_ST_STARTING;
5058 if (s->slowstart > 0)
5059 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5060 else
5061 s->next_state = SRV_ST_RUNNING;
5062 }
5063
5064 }
5065
5066 tmptrash = alloc_trash_chunk();
5067 if (tmptrash) {
5068 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5069 chunk_printf(tmptrash,
5070 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5071 s->flags & SRV_F_BACKUP ? "Backup " : "",
5072 s->proxy->id, s->id,
5073 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5074 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5075 }
5076 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5077 chunk_printf(tmptrash,
5078 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5079 s->flags & SRV_F_BACKUP ? "Backup " : "",
5080 s->proxy->id, s->id, s->hostname,
5081 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5082 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5083 }
5084 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5085 chunk_printf(tmptrash,
5086 "%sServer %s/%s is %s/%s (leaving maintenance)",
5087 s->flags & SRV_F_BACKUP ? "Backup " : "",
5088 s->proxy->id, s->id,
5089 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5090 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5091 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005092 ha_warning("%s.\n", tmptrash->area);
5093 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5094 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005095 free_trash_chunk(tmptrash);
5096 tmptrash = NULL;
5097 }
5098
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005099 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005100 /* now propagate the status change to any LB algorithms */
5101 if (px->lbprm.update_server_eweight)
5102 px->lbprm.update_server_eweight(s);
5103 else if (srv_willbe_usable(s)) {
5104 if (px->lbprm.set_server_status_up)
5105 px->lbprm.set_server_status_up(s);
5106 }
5107 else {
5108 if (px->lbprm.set_server_status_down)
5109 px->lbprm.set_server_status_down(s);
5110 }
5111
5112 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5113 set_backend_down(s->proxy);
5114
Willy Tarreau6a78e612018-08-07 10:14:53 +02005115 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5116 * and it's not a backup server and its effective weight is > 0,
5117 * then it can accept new connections, so we shut down all streams
5118 * on all backup servers.
5119 */
5120 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5121 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5122 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5123
5124 /* check if we can handle some connections queued at the proxy. We
5125 * will take as many as we can handle.
5126 */
5127 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005128 }
5129 else if (s->next_admin & SRV_ADMF_MAINT) {
5130 /* remaining in maintenance mode, let's inform precisely about the
5131 * situation.
5132 */
5133 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5134 tmptrash = alloc_trash_chunk();
5135 if (tmptrash) {
5136 chunk_printf(tmptrash,
5137 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5138 s->flags & SRV_F_BACKUP ? "Backup " : "",
5139 s->proxy->id, s->id);
5140
5141 if (s->track) /* normally it's mandatory here */
5142 chunk_appendf(tmptrash, " via %s/%s",
5143 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005144 ha_warning("%s.\n", tmptrash->area);
5145 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5146 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005147 free_trash_chunk(tmptrash);
5148 tmptrash = NULL;
5149 }
5150 }
5151 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5152 tmptrash = alloc_trash_chunk();
5153 if (tmptrash) {
5154 chunk_printf(tmptrash,
5155 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5156 s->flags & SRV_F_BACKUP ? "Backup " : "",
5157 s->proxy->id, s->id, s->hostname);
5158
5159 if (s->track) /* normally it's mandatory here */
5160 chunk_appendf(tmptrash, " via %s/%s",
5161 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005162 ha_warning("%s.\n", tmptrash->area);
5163 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5164 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005165 free_trash_chunk(tmptrash);
5166 tmptrash = NULL;
5167 }
5168 }
5169 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5170 tmptrash = alloc_trash_chunk();
5171 if (tmptrash) {
5172 chunk_printf(tmptrash,
5173 "%sServer %s/%s remains in forced maintenance",
5174 s->flags & SRV_F_BACKUP ? "Backup " : "",
5175 s->proxy->id, s->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 /* don't report anything when leaving drain mode and remaining in maintenance */
5184
5185 s->cur_admin = s->next_admin;
5186 }
5187
5188 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5189 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5190 /* drain state is applied only if not yet in maint */
5191
5192 s->last_change = now.tv_sec;
5193 if (px->lbprm.set_server_status_down)
5194 px->lbprm.set_server_status_down(s);
5195
5196 /* we might have streams queued on this server and waiting for
5197 * a connection. Those which are redispatchable will be queued
5198 * to another server or to the proxy itself.
5199 */
5200 xferred = pendconn_redistribute(s);
5201
5202 tmptrash = alloc_trash_chunk();
5203 if (tmptrash) {
5204 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5205 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5206 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5207
5208 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5209
5210 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005211 ha_warning("%s.\n", tmptrash->area);
5212 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5213 tmptrash->area);
5214 send_email_alert(s, LOG_NOTICE, "%s",
5215 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005216 }
5217 free_trash_chunk(tmptrash);
5218 tmptrash = NULL;
5219 }
5220
5221 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5222 set_backend_down(s->proxy);
5223 }
5224 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5225 /* OK completely leaving drain mode */
5226 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5227 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5228 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5229 s->proxy->last_change = now.tv_sec;
5230 }
5231
5232 if (s->last_change < now.tv_sec) // ignore negative times
5233 s->down_time += now.tv_sec - s->last_change;
5234 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005235 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005236
5237 tmptrash = alloc_trash_chunk();
5238 if (tmptrash) {
5239 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5240 chunk_printf(tmptrash,
5241 "%sServer %s/%s is %s (leaving forced drain)",
5242 s->flags & SRV_F_BACKUP ? "Backup " : "",
5243 s->proxy->id, s->id,
5244 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5245 }
5246 else {
5247 chunk_printf(tmptrash,
5248 "%sServer %s/%s is %s (leaving drain)",
5249 s->flags & SRV_F_BACKUP ? "Backup " : "",
5250 s->proxy->id, s->id,
5251 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5252 if (s->track) /* normally it's mandatory here */
5253 chunk_appendf(tmptrash, " via %s/%s",
5254 s->track->proxy->id, s->track->id);
5255 }
5256
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005257 ha_warning("%s.\n", tmptrash->area);
5258 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5259 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005260 free_trash_chunk(tmptrash);
5261 tmptrash = NULL;
5262 }
5263
5264 /* now propagate the status change to any LB algorithms */
5265 if (px->lbprm.update_server_eweight)
5266 px->lbprm.update_server_eweight(s);
5267 else if (srv_willbe_usable(s)) {
5268 if (px->lbprm.set_server_status_up)
5269 px->lbprm.set_server_status_up(s);
5270 }
5271 else {
5272 if (px->lbprm.set_server_status_down)
5273 px->lbprm.set_server_status_down(s);
5274 }
5275 }
5276 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5277 /* remaining in drain mode after removing one of its flags */
5278
5279 tmptrash = alloc_trash_chunk();
5280 if (tmptrash) {
5281 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5282 chunk_printf(tmptrash,
5283 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5284 s->flags & SRV_F_BACKUP ? "Backup " : "",
5285 s->proxy->id, s->id);
5286
5287 if (s->track) /* normally it's mandatory here */
5288 chunk_appendf(tmptrash, " via %s/%s",
5289 s->track->proxy->id, s->track->id);
5290 }
5291 else {
5292 chunk_printf(tmptrash,
5293 "%sServer %s/%s remains in forced drain mode",
5294 s->flags & SRV_F_BACKUP ? "Backup " : "",
5295 s->proxy->id, s->id);
5296 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005297 ha_warning("%s.\n", tmptrash->area);
5298 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5299 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005300 free_trash_chunk(tmptrash);
5301 tmptrash = NULL;
5302 }
5303
5304 /* commit new admin status */
5305
5306 s->cur_admin = s->next_admin;
5307 }
5308 }
5309
5310 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005311 *s->adm_st_chg_cause = 0;
5312}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005313
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005314struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5315{
5316 struct connection *conn;
5317
5318 while ((conn = LIST_POP_LOCKED(&toremove_connections[tid],
5319 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005320 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005321 }
5322
5323 return task;
5324}
5325
Willy Tarreau980855b2019-02-07 14:59:29 +01005326struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005327{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005328 struct server *srv;
5329 struct eb32_node *eb;
5330 int i;
5331 unsigned int next_wakeup;
5332 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005333
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005334 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5335 while (1) {
5336 int srv_is_empty = 1;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005337
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005338 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5339 if (!eb) {
5340 /* we might have reached the end of the tree, typically because
5341 * <now_ms> is in the first half and we're first scanning the last
5342 * half. Let's loop back to the beginning of the tree now.
5343 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005344
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005345 eb = eb32_first(&idle_conn_srv);
5346 if (likely(!eb))
5347 break;
5348 }
5349 if (tick_is_lt(now_ms, eb->key)) {
5350 /* timer not expired yet, revisit it later */
5351 next_wakeup = eb->key;
5352 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005353 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005354 }
5355 srv = eb32_entry(eb, struct server, idle_node);
5356 for (i = 0; i < global.nbthread; i++) {
5357 int max_conn = (srv->curr_idle_thr[i] / 2) +
5358 (srv->curr_idle_thr[i] & 1);
5359 int j;
5360 int did_remove = 0;
5361
5362 for (j = 0; j < max_conn; j++) {
5363 struct connection *conn = LIST_POP_LOCKED(&srv->idle_orphan_conns[i], struct connection *, list);
5364 if (!conn)
5365 break;
5366 did_remove = 1;
5367 LIST_ADDQ_LOCKED(&toremove_connections[i], &conn->list);
5368 }
5369 if (did_remove && max_conn < srv->curr_idle_thr[i])
5370 srv_is_empty = 0;
5371 if (did_remove)
5372 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5373 }
5374 eb32_delete(&srv->idle_node);
5375 if (!srv_is_empty) {
5376 /* There are still more idle connections, add the
5377 * server back in the tree.
5378 */
5379 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5380 now_ms);
5381 eb32_insert(&idle_conn_srv, &srv->idle_node);
5382 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005383 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005384 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5385
5386 if (need_wakeup)
5387 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005388 else
5389 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005390
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005391 return task;
5392}
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005393/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005394 * Local variables:
5395 * c-indent-level: 8
5396 * c-basic-offset: 8
5397 * End:
5398 */