blob: 236d6baea9b22f6c6127b461a44f5ea287344bea [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>
Frédéric Lécaille7da71292019-05-20 09:47:07 +020028#include <types/dict.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020029#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010030#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020031#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010032#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020033
William Lallemand222baf22016-11-19 02:00:33 +010034#include <proto/applet.h>
35#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090036#include <proto/checks.h>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020037#include <proto/connection.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020038#include <proto/port_range.h>
39#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020040#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010041#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020042#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020043#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010044#include <proto/stream_interface.h>
45#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020046#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020047#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010048#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020049
Willy Tarreau3ff577e2018-08-02 11:48:52 +020050static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020051static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010052static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010053static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
Willy Tarreau21faa912012-10-10 08:27:36 +020055/* List head of all known server keywords */
56static struct srv_kw_list srv_keywords = {
57 .list = LIST_HEAD_INIT(srv_keywords.list)
58};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020059
Olivier Houchard9ea5d362019-02-14 18:29:09 +010060__decl_hathreads(HA_SPINLOCK_T idle_conn_srv_lock);
61struct eb_root idle_conn_srv = EB_ROOT;
62struct task *idle_conn_task = NULL;
63struct task *idle_conn_cleanup[MAX_THREADS] = { NULL };
64struct list toremove_connections[MAX_THREADS];
Olivier Houchardba936e52019-07-11 15:49:00 +020065__decl_hathreads(HA_SPINLOCK_T toremove_lock[MAX_THREADS]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010066
Frédéric Lécaille7da71292019-05-20 09:47:07 +020067/* The server names dictionary */
68struct dict server_name_dict = {
69 .name = "server names",
70 .values = EB_ROOT_UNIQUE,
71};
72
Simon Hormana3608442013-11-01 16:46:15 +090073int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020074{
Emeric Brun52a91d32017-08-31 14:41:55 +020075 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020076 return s->down_time;
77
78 return now.tv_sec - s->last_change + s->down_time;
79}
Willy Tarreaubaaee002006-06-26 02:48:02 +020080
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050081int srv_lastsession(const struct server *s)
82{
83 if (s->counters.last_sess)
84 return now.tv_sec - s->counters.last_sess;
85
86 return -1;
87}
88
Simon Horman4a741432013-02-23 15:35:38 +090089int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020090{
Simon Horman4a741432013-02-23 15:35:38 +090091 const struct server *s = check->server;
92
Willy Tarreauff5ae352013-12-11 20:36:34 +010093 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090094 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010095
Emeric Brun52a91d32017-08-31 14:41:55 +020096 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090097 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010098
Simon Horman4a741432013-02-23 15:35:38 +090099 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100100}
101
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100102/*
103 * Check that we did not get a hash collision.
104 * Unlikely, but it can happen.
105 */
106static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100107{
108 struct proxy *p = s->proxy;
109 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100110
111 for (tmpserv = p->srv; tmpserv != NULL;
112 tmpserv = tmpserv->next) {
113 if (tmpserv == s)
114 continue;
115 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
116 continue;
117 if (tmpserv->cookie &&
118 strcmp(tmpserv->cookie, s->cookie) == 0) {
119 ha_warning("We generated two equal cookies for two different servers.\n"
120 "Please change the secret key for '%s'.\n",
121 s->proxy->id);
122 }
123 }
124
125}
126
Willy Tarreau46b7f532018-08-21 11:54:26 +0200127/*
Willy Tarreau0ff395c2019-07-30 11:59:34 +0200128 * Must be called with the server lock held, and will grab the proxy lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200129 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100130void srv_set_dyncookie(struct server *s)
131{
132 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100133 char *tmpbuf;
134 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100135 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100136 size_t buffer_len;
137 int addr_len;
138 int port;
139
Willy Tarreau0ff395c2019-07-30 11:59:34 +0200140 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
141
Olivier Houchard4e694042017-03-14 20:01:29 +0100142 if ((s->flags & SRV_F_COOKIESET) ||
143 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
144 s->proxy->dyncookie_key == NULL)
Willy Tarreau0ff395c2019-07-30 11:59:34 +0200145 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100146 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100147
148 if (s->addr.ss_family != AF_INET &&
149 s->addr.ss_family != AF_INET6)
Willy Tarreau0ff395c2019-07-30 11:59:34 +0200150 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100151 /*
152 * Buffer to calculate the cookie value.
153 * The buffer contains the secret key + the server IP address
154 * + the TCP port.
155 */
156 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
157 /*
158 * The TCP port should use only 2 bytes, but is stored in
159 * an unsigned int in struct server, so let's use 4, to be
160 * on the safe side.
161 */
162 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200163 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100164 memcpy(tmpbuf, p->dyncookie_key, key_len);
165 memcpy(&(tmpbuf[key_len]),
166 s->addr.ss_family == AF_INET ?
167 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
168 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
169 addr_len);
170 /*
171 * Make sure it's the same across all the load balancers,
172 * no matter their endianness.
173 */
174 port = htonl(s->svc_port);
175 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
176 hash_value = XXH64(tmpbuf, buffer_len, 0);
177 memprintf(&s->cookie, "%016llx", hash_value);
178 if (!s->cookie)
Willy Tarreau0ff395c2019-07-30 11:59:34 +0200179 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100180 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100181
182 /* Don't bother checking if the dyncookie is duplicated if
183 * the server is marked as "disabled", maybe it doesn't have
184 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100185 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100186 if (!(s->next_admin & SRV_ADMF_FMAINT))
187 srv_check_for_dup_dyncookie(s);
Willy Tarreau0ff395c2019-07-30 11:59:34 +0200188 out:
189 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100190}
191
Willy Tarreau21faa912012-10-10 08:27:36 +0200192/*
193 * Registers the server keyword list <kwl> as a list of valid keywords for next
194 * parsing sessions.
195 */
196void srv_register_keywords(struct srv_kw_list *kwl)
197{
198 LIST_ADDQ(&srv_keywords.list, &kwl->list);
199}
200
201/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
202 * keyword is found with a NULL ->parse() function, then an attempt is made to
203 * find one with a valid ->parse() function. This way it is possible to declare
204 * platform-dependant, known keywords as NULL, then only declare them as valid
205 * if some options are met. Note that if the requested keyword contains an
206 * opening parenthesis, everything from this point is ignored.
207 */
208struct srv_kw *srv_find_kw(const char *kw)
209{
210 int index;
211 const char *kwend;
212 struct srv_kw_list *kwl;
213 struct srv_kw *ret = NULL;
214
215 kwend = strchr(kw, '(');
216 if (!kwend)
217 kwend = kw + strlen(kw);
218
219 list_for_each_entry(kwl, &srv_keywords.list, list) {
220 for (index = 0; kwl->kw[index].kw != NULL; index++) {
221 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
222 kwl->kw[index].kw[kwend-kw] == 0) {
223 if (kwl->kw[index].parse)
224 return &kwl->kw[index]; /* found it !*/
225 else
226 ret = &kwl->kw[index]; /* may be OK */
227 }
228 }
229 }
230 return ret;
231}
232
233/* Dumps all registered "server" keywords to the <out> string pointer. The
234 * unsupported keywords are only dumped if their supported form was not
235 * found.
236 */
237void srv_dump_kws(char **out)
238{
239 struct srv_kw_list *kwl;
240 int index;
241
242 *out = NULL;
243 list_for_each_entry(kwl, &srv_keywords.list, list) {
244 for (index = 0; kwl->kw[index].kw != NULL; index++) {
245 if (kwl->kw[index].parse ||
246 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
247 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
248 kwl->scope,
249 kwl->kw[index].kw,
250 kwl->kw[index].skip ? " <arg>" : "",
251 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
252 kwl->kw[index].parse ? "" : " (not supported)");
253 }
254 }
255 }
256}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100257
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100258/* Parse the "addr" server keyword */
259static int srv_parse_addr(char **args, int *cur_arg,
260 struct proxy *curproxy, struct server *newsrv, char **err)
261{
262 char *errmsg, *arg;
263 struct sockaddr_storage *sk;
264 int port1, port2;
265 struct protocol *proto;
266
267 errmsg = NULL;
268 arg = args[*cur_arg + 1];
269
270 if (!*arg) {
271 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
272 goto err;
273 }
274
275 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
276 if (!sk) {
277 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
278 goto err;
279 }
280
281 proto = protocol_by_family(sk->ss_family);
282 if (!proto || !proto->connect) {
283 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
284 args[*cur_arg], arg);
285 goto err;
286 }
287
288 if (port1 != port2) {
289 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
290 args[*cur_arg], arg);
291 goto err;
292 }
293
294 newsrv->check.addr = newsrv->agent.addr = *sk;
295 newsrv->flags |= SRV_F_CHECKADDR;
296 newsrv->flags |= SRV_F_AGENTADDR;
297
298 return 0;
299
300 err:
301 free(errmsg);
302 return ERR_ALERT | ERR_FATAL;
303}
304
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100305/* Parse the "agent-check" server keyword */
306static int srv_parse_agent_check(char **args, int *cur_arg,
307 struct proxy *curproxy, struct server *newsrv, char **err)
308{
309 newsrv->do_agent = 1;
310 return 0;
311}
312
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100313/* Parse the "backup" server keyword */
314static int srv_parse_backup(char **args, int *cur_arg,
315 struct proxy *curproxy, struct server *newsrv, char **err)
316{
317 newsrv->flags |= SRV_F_BACKUP;
318 return 0;
319}
320
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100321/* Parse the "check" server keyword */
322static int srv_parse_check(char **args, int *cur_arg,
323 struct proxy *curproxy, struct server *newsrv, char **err)
324{
325 newsrv->do_check = 1;
326 return 0;
327}
328
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100329/* Parse the "check-send-proxy" server keyword */
330static int srv_parse_check_send_proxy(char **args, int *cur_arg,
331 struct proxy *curproxy, struct server *newsrv, char **err)
332{
333 newsrv->check.send_proxy = 1;
334 return 0;
335}
336
Alexander Liu2a54bb72019-05-22 19:44:48 +0800337/* Parse the "check-via-socks4" server keyword */
338static int srv_parse_check_via_socks4(char **args, int *cur_arg,
339 struct proxy *curproxy, struct server *newsrv, char **err)
340{
341 newsrv->check.via_socks4 = 1;
342 return 0;
343}
344
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100345/* Parse the "cookie" server keyword */
346static int srv_parse_cookie(char **args, int *cur_arg,
347 struct proxy *curproxy, struct server *newsrv, char **err)
348{
349 char *arg;
350
351 arg = args[*cur_arg + 1];
352 if (!*arg) {
353 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
354 return ERR_ALERT | ERR_FATAL;
355 }
356
357 free(newsrv->cookie);
358 newsrv->cookie = strdup(arg);
359 newsrv->cklen = strlen(arg);
360 newsrv->flags |= SRV_F_COOKIESET;
361 return 0;
362}
363
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100364/* Parse the "disabled" server keyword */
365static int srv_parse_disabled(char **args, int *cur_arg,
366 struct proxy *curproxy, struct server *newsrv, char **err)
367{
Emeric Brun52a91d32017-08-31 14:41:55 +0200368 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
369 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100370 newsrv->check.state |= CHK_ST_PAUSED;
371 newsrv->check.health = 0;
372 return 0;
373}
374
375/* Parse the "enabled" server keyword */
376static int srv_parse_enabled(char **args, int *cur_arg,
377 struct proxy *curproxy, struct server *newsrv, char **err)
378{
Emeric Brun52a91d32017-08-31 14:41:55 +0200379 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
380 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100381 newsrv->check.state &= ~CHK_ST_PAUSED;
382 newsrv->check.health = newsrv->check.rise;
383 return 0;
384}
385
Willy Tarreau9c538e02019-01-23 10:21:49 +0100386static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
387{
388 char *arg;
389
390 arg = args[*cur_arg + 1];
391 if (!*arg) {
392 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
393 return ERR_ALERT | ERR_FATAL;
394 }
395 newsrv->max_reuse = atoi(arg);
396
397 return 0;
398}
399
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100400static 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 +0100401{
402 const char *res;
403 char *arg;
404 unsigned int time;
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 }
411 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200412 if (res == PARSE_TIME_OVER) {
413 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
414 args[*cur_arg+1], args[*cur_arg]);
415 return ERR_ALERT | ERR_FATAL;
416 }
417 else if (res == PARSE_TIME_UNDER) {
418 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
419 args[*cur_arg+1], args[*cur_arg]);
420 return ERR_ALERT | ERR_FATAL;
421 }
422 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100423 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
424 *res, args[*cur_arg]);
425 return ERR_ALERT | ERR_FATAL;
426 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100427 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100428
429 return 0;
430}
431
Olivier Houchard006e3102018-12-10 18:30:32 +0100432static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
433{
434 char *arg;
435
436 arg = args[*cur_arg + 1];
437 if (!*arg) {
438 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
439 return ERR_ALERT | ERR_FATAL;
440 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100441
Olivier Houchard006e3102018-12-10 18:30:32 +0100442 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100443 if ((int)newsrv->max_idle_conns < -1) {
444 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
445 return ERR_ALERT | ERR_FATAL;
446 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100447
448 return 0;
449}
450
Willy Tarreaudff55432012-10-10 17:51:05 +0200451/* parse the "id" server keyword */
452static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
453{
454 struct eb32_node *node;
455
456 if (!*args[*cur_arg + 1]) {
457 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
458 return ERR_ALERT | ERR_FATAL;
459 }
460
461 newsrv->puid = atol(args[*cur_arg + 1]);
462 newsrv->conf.id.key = newsrv->puid;
463
464 if (newsrv->puid <= 0) {
465 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
466 return ERR_ALERT | ERR_FATAL;
467 }
468
469 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
470 if (node) {
471 struct server *target = container_of(node, struct server, conf.id);
472 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
473 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
474 target->id);
475 return ERR_ALERT | ERR_FATAL;
476 }
477
478 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200479 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200480 return 0;
481}
482
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100483/* Parse the "namespace" server keyword */
484static int srv_parse_namespace(char **args, int *cur_arg,
485 struct proxy *curproxy, struct server *newsrv, char **err)
486{
Willy Tarreaue5733232019-05-22 19:24:06 +0200487#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100488 char *arg;
489
490 arg = args[*cur_arg + 1];
491 if (!*arg) {
492 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
493 return ERR_ALERT | ERR_FATAL;
494 }
495
496 if (!strcmp(arg, "*")) {
497 /* Use the namespace associated with the connection (if present). */
498 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
499 return 0;
500 }
501
502 /*
503 * As this parser may be called several times for the same 'default-server'
504 * object, or for a new 'server' instance deriving from a 'default-server'
505 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
506 */
507 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
508
509 newsrv->netns = netns_store_lookup(arg, strlen(arg));
510 if (!newsrv->netns)
511 newsrv->netns = netns_store_insert(arg);
512
513 if (!newsrv->netns) {
514 memprintf(err, "Cannot open namespace '%s'", arg);
515 return ERR_ALERT | ERR_FATAL;
516 }
517
518 return 0;
519#else
520 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
521 return ERR_ALERT | ERR_FATAL;
522#endif
523}
524
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100525/* Parse the "no-agent-check" server keyword */
526static int srv_parse_no_agent_check(char **args, int *cur_arg,
527 struct proxy *curproxy, struct server *newsrv, char **err)
528{
529 free_check(&newsrv->agent);
530 newsrv->agent.inter = 0;
531 newsrv->agent.port = 0;
532 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
533 newsrv->do_agent = 0;
534 return 0;
535}
536
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100537/* Parse the "no-backup" server keyword */
538static int srv_parse_no_backup(char **args, int *cur_arg,
539 struct proxy *curproxy, struct server *newsrv, char **err)
540{
541 newsrv->flags &= ~SRV_F_BACKUP;
542 return 0;
543}
544
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100545/* Parse the "no-check" server keyword */
546static int srv_parse_no_check(char **args, int *cur_arg,
547 struct proxy *curproxy, struct server *newsrv, char **err)
548{
549 free_check(&newsrv->check);
550 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
551 newsrv->do_check = 0;
552 return 0;
553}
554
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100555/* Parse the "no-check-send-proxy" server keyword */
556static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
557 struct proxy *curproxy, struct server *newsrv, char **err)
558{
559 newsrv->check.send_proxy = 0;
560 return 0;
561}
562
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100563/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200564static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100565{
566 srv->pp_opts &= ~flags;
567 return 0;
568}
569
570/* Parse the "no-send-proxy" server keyword */
571static int srv_parse_no_send_proxy(char **args, int *cur_arg,
572 struct proxy *curproxy, struct server *newsrv, char **err)
573{
574 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
575}
576
577/* Parse the "no-send-proxy-v2" server keyword */
578static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
579 struct proxy *curproxy, struct server *newsrv, char **err)
580{
581 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
582}
583
Frédéric Lécailleaeeb1c92019-07-04 14:19:06 +0200584/* Parse the "no-tfo" server keyword */
585static int srv_parse_no_tfo(char **args, int *cur_arg,
586 struct proxy *curproxy, struct server *newsrv, char **err)
587{
588 newsrv->flags &= ~SRV_F_FASTOPEN;
589 return 0;
590}
591
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100592/* Parse the "non-stick" server keyword */
593static int srv_parse_non_stick(char **args, int *cur_arg,
594 struct proxy *curproxy, struct server *newsrv, char **err)
595{
596 newsrv->flags |= SRV_F_NON_STICK;
597 return 0;
598}
599
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100600/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200601static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100602{
603 srv->pp_opts |= flags;
604 return 0;
605}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200606/* parse the "proto" server keyword */
607static int srv_parse_proto(char **args, int *cur_arg,
608 struct proxy *px, struct server *newsrv, char **err)
609{
610 struct ist proto;
611
612 if (!*args[*cur_arg + 1]) {
613 memprintf(err, "'%s' : missing value", args[*cur_arg]);
614 return ERR_ALERT | ERR_FATAL;
615 }
616 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
617 newsrv->mux_proto = get_mux_proto(proto);
618 if (!newsrv->mux_proto) {
619 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
620 return ERR_ALERT | ERR_FATAL;
621 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200622 return 0;
623}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100624
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100625/* parse the "proxy-v2-options" */
626static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
627 struct proxy *px, struct server *newsrv, char **err)
628{
629 char *p, *n;
630 for (p = args[*cur_arg+1]; p; p = n) {
631 n = strchr(p, ',');
632 if (n)
633 *n++ = '\0';
634 if (!strcmp(p, "ssl")) {
635 newsrv->pp_opts |= SRV_PP_V2_SSL;
636 } else if (!strcmp(p, "cert-cn")) {
637 newsrv->pp_opts |= SRV_PP_V2_SSL;
638 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100639 } else if (!strcmp(p, "cert-key")) {
640 newsrv->pp_opts |= SRV_PP_V2_SSL;
641 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
642 } else if (!strcmp(p, "cert-sig")) {
643 newsrv->pp_opts |= SRV_PP_V2_SSL;
644 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
645 } else if (!strcmp(p, "ssl-cipher")) {
646 newsrv->pp_opts |= SRV_PP_V2_SSL;
647 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100648 } else if (!strcmp(p, "authority")) {
649 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100650 } else if (!strcmp(p, "crc32c")) {
651 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100652 } else
653 goto fail;
654 }
655 return 0;
656 fail:
657 if (err)
658 memprintf(err, "'%s' : proxy v2 option not implemented", p);
659 return ERR_ALERT | ERR_FATAL;
660}
661
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100662/* Parse the "observe" server keyword */
663static int srv_parse_observe(char **args, int *cur_arg,
664 struct proxy *curproxy, struct server *newsrv, char **err)
665{
666 char *arg;
667
668 arg = args[*cur_arg + 1];
669 if (!*arg) {
670 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
671 return ERR_ALERT | ERR_FATAL;
672 }
673
674 if (!strcmp(arg, "none")) {
675 newsrv->observe = HANA_OBS_NONE;
676 }
677 else if (!strcmp(arg, "layer4")) {
678 newsrv->observe = HANA_OBS_LAYER4;
679 }
680 else if (!strcmp(arg, "layer7")) {
681 if (curproxy->mode != PR_MODE_HTTP) {
682 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
683 return ERR_ALERT;
684 }
685 newsrv->observe = HANA_OBS_LAYER7;
686 }
687 else {
688 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
689 "but got '%s'\n", args[*cur_arg], arg);
690 return ERR_ALERT | ERR_FATAL;
691 }
692
693 return 0;
694}
695
Frédéric Lécaille16186232017-03-14 16:42:49 +0100696/* Parse the "redir" server keyword */
697static int srv_parse_redir(char **args, int *cur_arg,
698 struct proxy *curproxy, struct server *newsrv, char **err)
699{
700 char *arg;
701
702 arg = args[*cur_arg + 1];
703 if (!*arg) {
704 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
705 return ERR_ALERT | ERR_FATAL;
706 }
707
708 free(newsrv->rdr_pfx);
709 newsrv->rdr_pfx = strdup(arg);
710 newsrv->rdr_len = strlen(arg);
711
712 return 0;
713}
714
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100715/* Parse the "send-proxy" server keyword */
716static int srv_parse_send_proxy(char **args, int *cur_arg,
717 struct proxy *curproxy, struct server *newsrv, char **err)
718{
719 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
720}
721
722/* Parse the "send-proxy-v2" server keyword */
723static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
724 struct proxy *curproxy, struct server *newsrv, char **err)
725{
726 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
727}
728
Frédéric Lécailledba97072017-03-17 15:33:50 +0100729
730/* Parse the "source" server keyword */
731static int srv_parse_source(char **args, int *cur_arg,
732 struct proxy *curproxy, struct server *newsrv, char **err)
733{
734 char *errmsg;
735 int port_low, port_high;
736 struct sockaddr_storage *sk;
737 struct protocol *proto;
738
739 errmsg = NULL;
740
741 if (!*args[*cur_arg + 1]) {
742 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
743 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
744 goto err;
745 }
746
747 /* 'sk' is statically allocated (no need to be freed). */
748 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
749 if (!sk) {
750 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
751 goto err;
752 }
753
754 proto = protocol_by_family(sk->ss_family);
755 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100756 ha_alert("'%s %s' : connect() not supported for this address family.\n",
757 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100758 goto err;
759 }
760
761 newsrv->conn_src.opts |= CO_SRC_BIND;
762 newsrv->conn_src.source_addr = *sk;
763
764 if (port_low != port_high) {
765 int i;
766
767 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100768 ha_alert("'%s' does not support port offsets (found '%s').\n",
769 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100770 goto err;
771 }
772
773 if (port_low <= 0 || port_low > 65535 ||
774 port_high <= 0 || port_high > 65535 ||
775 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100776 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 +0100777 goto err;
778 }
779 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
780 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
781 newsrv->conn_src.sport_range->ports[i] = port_low + i;
782 }
783
784 *cur_arg += 2;
785 while (*(args[*cur_arg])) {
786 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
787#if defined(CONFIG_HAP_TRANSPARENT)
788 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100789 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
790 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100791 goto err;
792 }
793 if (!strcmp(args[*cur_arg + 1], "client")) {
794 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
795 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
796 }
797 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
798 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
799 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
800 }
801 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
802 char *name, *end;
803
804 name = args[*cur_arg + 1] + 7;
805 while (isspace(*name))
806 name++;
807
808 end = name;
809 while (*end && !isspace(*end) && *end != ',' && *end != ')')
810 end++;
811
812 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
813 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
814 free(newsrv->conn_src.bind_hdr_name);
815 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
816 newsrv->conn_src.bind_hdr_len = end - name;
817 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
818 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
819 newsrv->conn_src.bind_hdr_occ = -1;
820
821 /* now look for an occurrence number */
822 while (isspace(*end))
823 end++;
824 if (*end == ',') {
825 end++;
826 name = end;
827 if (*end == '-')
828 end++;
829 while (isdigit((int)*end))
830 end++;
831 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
832 }
833
834 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100835 ha_alert("usesrc hdr_ip(name,num) does not support negative"
836 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100837 goto err;
838 }
839 }
840 else {
841 struct sockaddr_storage *sk;
842 int port1, port2;
843
844 /* 'sk' is statically allocated (no need to be freed). */
845 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
846 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100847 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100848 goto err;
849 }
850
851 proto = protocol_by_family(sk->ss_family);
852 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100853 ha_alert("'%s %s' : connect() not supported for this address family.\n",
854 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100855 goto err;
856 }
857
858 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100859 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
860 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100861 goto err;
862 }
863 newsrv->conn_src.tproxy_addr = *sk;
864 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
865 }
866 global.last_checks |= LSTCHK_NETADM;
867 *cur_arg += 2;
868 continue;
869#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100870 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 +0100871 goto err;
872#endif /* defined(CONFIG_HAP_TRANSPARENT) */
873 } /* "usesrc" */
874
875 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
876#ifdef SO_BINDTODEVICE
877 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100878 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100879 goto err;
880 }
881 free(newsrv->conn_src.iface_name);
882 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
883 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
884 global.last_checks |= LSTCHK_NETADM;
885#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100886 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100887 goto err;
888#endif
889 *cur_arg += 2;
890 continue;
891 }
892 /* this keyword in not an option of "source" */
893 break;
894 } /* while */
895
896 return 0;
897
898 err:
899 free(errmsg);
900 return ERR_ALERT | ERR_FATAL;
901}
902
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100903/* Parse the "stick" server keyword */
904static int srv_parse_stick(char **args, int *cur_arg,
905 struct proxy *curproxy, struct server *newsrv, char **err)
906{
907 newsrv->flags &= ~SRV_F_NON_STICK;
908 return 0;
909}
910
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100911/* Parse the "track" server keyword */
912static int srv_parse_track(char **args, int *cur_arg,
913 struct proxy *curproxy, struct server *newsrv, char **err)
914{
915 char *arg;
916
917 arg = args[*cur_arg + 1];
918 if (!*arg) {
919 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
920 return ERR_ALERT | ERR_FATAL;
921 }
922
923 free(newsrv->trackit);
924 newsrv->trackit = strdup(arg);
925
926 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800927}
928
929/* Parse the "socks4" server keyword */
930static int srv_parse_socks4(char **args, int *cur_arg,
931 struct proxy *curproxy, struct server *newsrv, char **err)
932{
933 char *errmsg;
934 int port_low, port_high;
935 struct sockaddr_storage *sk;
936 struct protocol *proto;
937
938 errmsg = NULL;
939
940 if (!*args[*cur_arg + 1]) {
941 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
942 goto err;
943 }
944
945 /* 'sk' is statically allocated (no need to be freed). */
946 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
947 if (!sk) {
948 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
949 goto err;
950 }
951
952 proto = protocol_by_family(sk->ss_family);
953 if (!proto || !proto->connect) {
954 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
955 goto err;
956 }
957
958 newsrv->flags |= SRV_F_SOCKS4_PROXY;
959 newsrv->socks4_addr = *sk;
960
961 if (port_low != port_high) {
962 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
963 goto err;
964 }
965
966 if (!port_low) {
967 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
968 goto err;
969 }
970
971 return 0;
972
973 err:
974 free(errmsg);
975 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100976}
977
Frédéric Lécailledba97072017-03-17 15:33:50 +0100978
Willy Tarreau034c88c2017-01-23 23:36:45 +0100979/* parse the "tfo" server keyword */
980static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
981{
982 newsrv->flags |= SRV_F_FASTOPEN;
983 return 0;
984}
985
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200986/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200987 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200988 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200989 *
990 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200991 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200992void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200993{
Willy Tarreau87b09662015-04-03 00:22:06 +0200994 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200995
Willy Tarreau87b09662015-04-03 00:22:06 +0200996 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
997 if (stream->srv_conn == srv)
998 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200999}
1000
1001/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001002 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001003 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001004 *
1005 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001006 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001007void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001008{
1009 struct server *srv;
1010
1011 for (srv = px->srv; srv != NULL; srv = srv->next)
1012 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001013 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001014}
1015
Willy Tarreaubda92272014-05-20 21:55:30 +02001016/* Appends some information to a message string related to a server going UP or
1017 * DOWN. If both <forced> and <reason> are null and the server tracks another
1018 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001019 * If <check> is non-null, an entire string describing the check result will be
1020 * appended after a comma and a space (eg: to report some information from the
1021 * check that changed the state). In the other case, the string will be built
1022 * using the check results stored into the struct server if present.
1023 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001024 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001025 *
1026 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001027 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001028void srv_append_status(struct buffer *msg, struct server *s,
1029 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001030{
Emeric Brun5a133512017-10-19 14:42:30 +02001031 short status = s->op_st_chg.status;
1032 short code = s->op_st_chg.code;
1033 long duration = s->op_st_chg.duration;
1034 char *desc = s->op_st_chg.reason;
1035
1036 if (check) {
1037 status = check->status;
1038 code = check->code;
1039 duration = check->duration;
1040 desc = check->desc;
1041 }
1042
1043 if (status != -1) {
1044 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1045
1046 if (status >= HCHK_STATUS_L57DATA)
1047 chunk_appendf(msg, ", code: %d", code);
1048
1049 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001050 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001051
1052 chunk_appendf(msg, ", info: \"");
1053
1054 chunk_initlen(&src, desc, 0, strlen(desc));
1055 chunk_asciiencode(msg, &src, '"');
1056
1057 chunk_appendf(msg, "\"");
1058 }
1059
1060 if (duration >= 0)
1061 chunk_appendf(msg, ", check duration: %ldms", duration);
1062 }
1063 else if (desc && *desc) {
1064 chunk_appendf(msg, ", %s", desc);
1065 }
1066 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001067 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001068 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001069
1070 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001071 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001072 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1073 " %d sessions active, %d requeued, %d remaining in queue",
1074 s->proxy->srv_act, s->proxy->srv_bck,
1075 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1076 s->cur_sess, xferred, s->nbpend);
1077 else
1078 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1079 " %d sessions requeued, %d total in queue",
1080 s->proxy->srv_act, s->proxy->srv_bck,
1081 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1082 xferred, s->nbpend);
1083 }
1084}
1085
Emeric Brun5a133512017-10-19 14:42:30 +02001086/* Marks server <s> down, regardless of its checks' statuses. The server is
1087 * registered in a list to postpone the counting of the remaining servers on
1088 * the proxy and transfers queued streams whenever possible to other servers at
1089 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1090 * non-null as the reason for going down or the available data from the check
1091 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001092 *
1093 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001094 */
Emeric Brun5a133512017-10-19 14:42:30 +02001095void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001096{
1097 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001098
Emeric Brun64cc49c2017-10-03 14:46:45 +02001099 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001100 return;
1101
Emeric Brun52a91d32017-08-31 14:41:55 +02001102 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001103 *s->op_st_chg.reason = 0;
1104 s->op_st_chg.status = -1;
1105 if (reason) {
1106 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1107 }
1108 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001109 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001110 s->op_st_chg.code = check->code;
1111 s->op_st_chg.status = check->status;
1112 s->op_st_chg.duration = check->duration;
1113 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001114
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001115 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001116 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001117
Emeric Brun9f0b4582017-10-23 14:39:51 +02001118 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001119 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001120 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001121 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001122 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001123}
1124
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001125/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001126 * in maintenance. The server is registered in a list to postpone the counting
1127 * of the remaining servers on the proxy and tries to grab requests from the
1128 * proxy at a sync point. Maintenance servers are ignored. It stores the
1129 * <reason> if non-null as the reason for going down or the available data
1130 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001131 *
1132 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001133 */
Emeric Brun5a133512017-10-19 14:42:30 +02001134void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001135{
1136 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001137
Emeric Brun64cc49c2017-10-03 14:46:45 +02001138 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001139 return;
1140
Emeric Brun52a91d32017-08-31 14:41:55 +02001141 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001142 return;
1143
Emeric Brun52a91d32017-08-31 14:41:55 +02001144 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001145 *s->op_st_chg.reason = 0;
1146 s->op_st_chg.status = -1;
1147 if (reason) {
1148 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1149 }
1150 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001151 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001152 s->op_st_chg.code = check->code;
1153 s->op_st_chg.status = check->status;
1154 s->op_st_chg.duration = check->duration;
1155 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001156
Emeric Brun64cc49c2017-10-03 14:46:45 +02001157 if (s->slowstart <= 0)
1158 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001159
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001160 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001161 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001162
Emeric Brun9f0b4582017-10-23 14:39:51 +02001163 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001164 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001165 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001166 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001167 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001168}
1169
Willy Tarreau8eb77842014-05-21 13:54:57 +02001170/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001171 * isn't in maintenance. The server is registered in a list to postpone the
1172 * counting of the remaining servers on the proxy and tries to grab requests
1173 * from the proxy. Maintenance servers are ignored. It stores the
1174 * <reason> if non-null as the reason for going down or the available data
1175 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001176 * up. Note that it makes use of the trash to build the log strings, so <reason>
1177 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001178 *
1179 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001180 */
Emeric Brun5a133512017-10-19 14:42:30 +02001181void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001182{
1183 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001184
Emeric Brun64cc49c2017-10-03 14:46:45 +02001185 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001186 return;
1187
Emeric Brun52a91d32017-08-31 14:41:55 +02001188 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001189 return;
1190
Emeric Brun52a91d32017-08-31 14:41:55 +02001191 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001192 *s->op_st_chg.reason = 0;
1193 s->op_st_chg.status = -1;
1194 if (reason) {
1195 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1196 }
1197 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001198 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001199 s->op_st_chg.code = check->code;
1200 s->op_st_chg.status = check->status;
1201 s->op_st_chg.duration = check->duration;
1202 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001203
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001204 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001205 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001206
Emeric Brun9f0b4582017-10-23 14:39:51 +02001207 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001208 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001209 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001210 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001211 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001212}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001213
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001214/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1215 * enforce either maint mode or drain mode. It is not allowed to set more than
1216 * one flag at once. The equivalent "inherited" flag is propagated to all
1217 * tracking servers. Maintenance mode disables health checks (but not agent
1218 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001219 * is done. If <cause> is non-null, it will be displayed at the end of the log
1220 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001221 *
1222 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001223 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001224void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001225{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001226 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001227
1228 if (!mode)
1229 return;
1230
1231 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001232 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001233 return;
1234
Emeric Brun52a91d32017-08-31 14:41:55 +02001235 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001236 if (cause)
1237 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1238
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001239 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001240 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001241
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001242 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001243 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1244 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001245 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001246
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001247 /* compute the inherited flag to propagate */
1248 if (mode & SRV_ADMF_MAINT)
1249 mode = SRV_ADMF_IMAINT;
1250 else if (mode & SRV_ADMF_DRAIN)
1251 mode = SRV_ADMF_IDRAIN;
1252
Emeric Brun9f0b4582017-10-23 14:39:51 +02001253 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001254 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001255 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001256 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001257 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001258}
1259
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001260/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1261 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1262 * than one flag at once. The equivalent "inherited" flag is propagated to all
1263 * tracking servers. Leaving maintenance mode re-enables health checks. When
1264 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001265 *
1266 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001267 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001268void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001269{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001270 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001271
1272 if (!mode)
1273 return;
1274
1275 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001276 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001277 return;
1278
Emeric Brun52a91d32017-08-31 14:41:55 +02001279 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001280
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001281 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001282 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001283
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001284 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001285 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1286 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001287 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001288
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001289 if (mode & SRV_ADMF_MAINT)
1290 mode = SRV_ADMF_IMAINT;
1291 else if (mode & SRV_ADMF_DRAIN)
1292 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001293
Emeric Brun9f0b4582017-10-23 14:39:51 +02001294 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001295 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001296 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001297 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001298 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001299}
1300
Willy Tarreau757478e2016-11-03 19:22:19 +01001301/* principle: propagate maint and drain to tracking servers. This is useful
1302 * upon startup so that inherited states are correct.
1303 */
1304static void srv_propagate_admin_state(struct server *srv)
1305{
1306 struct server *srv2;
1307
1308 if (!srv->trackers)
1309 return;
1310
1311 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001312 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001313 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001314 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001315
Emeric Brun52a91d32017-08-31 14:41:55 +02001316 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001317 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001318 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001319 }
1320}
1321
1322/* Compute and propagate the admin states for all servers in proxy <px>.
1323 * Only servers *not* tracking another one are considered, because other
1324 * ones will be handled when the server they track is visited.
1325 */
1326void srv_compute_all_admin_states(struct proxy *px)
1327{
1328 struct server *srv;
1329
1330 for (srv = px->srv; srv; srv = srv->next) {
1331 if (srv->track)
1332 continue;
1333 srv_propagate_admin_state(srv);
1334 }
1335}
1336
Willy Tarreaudff55432012-10-10 17:51:05 +02001337/* Note: must not be declared <const> as its list will be overwritten.
1338 * Please take care of keeping this list alphabetically sorted, doing so helps
1339 * all code contributors.
1340 * Optional keywords are also declared with a NULL ->parse() function so that
1341 * the config parser can report an appropriate error when a known keyword was
1342 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001343 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001344 */
1345static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001346 { "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 +01001347 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001348 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001349 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001350 { "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 +01001351 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001352 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1353 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001354 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001355 { "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 +01001356 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001357 { "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 +01001358 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001359 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001360 { "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 +01001361 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1362 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécailleaeeb1c92019-07-04 14:19:06 +02001363 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001364 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001365 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001366 { "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 +01001367 { "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 +02001368 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001369 { "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 +01001370 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001371 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1372 { "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 +02001373 { "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 +01001374 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard1a9c25f2019-07-04 13:34:10 +02001375 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001376 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001377 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
1378 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* enable socks4 proxy for health checks */
Willy Tarreaudff55432012-10-10 17:51:05 +02001379 { NULL, NULL, 0 },
1380}};
1381
Willy Tarreau0108d902018-11-25 19:14:37 +01001382INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001383
Willy Tarreau004e0452013-11-21 11:22:01 +01001384/* Recomputes the server's eweight based on its state, uweight, the current time,
1385 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001386 * state is automatically disabled if the time is elapsed. If <must_update> is
1387 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001388 *
1389 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001390 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001391void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001392{
1393 struct proxy *px = sv->proxy;
1394 unsigned w;
1395
1396 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1397 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001398 if (sv->next_state == SRV_ST_STARTING)
1399 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001400 }
1401
1402 /* We must take care of not pushing the server to full throttle during slow starts.
1403 * It must also start immediately, at least at the minimal step when leaving maintenance.
1404 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001405 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001406 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1407 else
1408 w = px->lbprm.wdiv;
1409
Emeric Brun52a91d32017-08-31 14:41:55 +02001410 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001411
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001412 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001413 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001414 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001415}
1416
Willy Tarreaubaaee002006-06-26 02:48:02 +02001417/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001418 * Parses weight_str and configures sv accordingly.
1419 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001420 *
1421 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001422 */
1423const char *server_parse_weight_change_request(struct server *sv,
1424 const char *weight_str)
1425{
1426 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001427 long int w;
1428 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001429
1430 px = sv->proxy;
1431
1432 /* if the weight is terminated with '%', it is set relative to
1433 * the initial weight, otherwise it is absolute.
1434 */
1435 if (!*weight_str)
1436 return "Require <weight> or <weight%>.\n";
1437
Simon Hormanb796afa2013-02-12 10:45:53 +09001438 w = strtol(weight_str, &end, 10);
1439 if (end == weight_str)
1440 return "Empty weight string empty or preceded by garbage";
1441 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001442 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001443 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001444 /* Avoid integer overflow */
1445 if (w > 25600)
1446 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001447 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001448 if (w > 256)
1449 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001450 }
1451 else if (w < 0 || w > 256)
1452 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001453 else if (end[0] != '\0')
1454 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001455
1456 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1457 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1458
1459 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001460 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001461
1462 return NULL;
1463}
1464
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001465/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001466 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1467 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001468 * Returns:
1469 * - error string on error
1470 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001471 *
1472 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001473 */
1474const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001475 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001476{
1477 unsigned char ip[INET6_ADDRSTRLEN];
1478
1479 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001480 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001481 return NULL;
1482 }
1483 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001484 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001485 return NULL;
1486 }
1487
1488 return "Could not understand IP address format.\n";
1489}
1490
Willy Tarreau46b7f532018-08-21 11:54:26 +02001491/*
1492 * Must be called with the server lock held.
1493 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001494const char *server_parse_maxconn_change_request(struct server *sv,
1495 const char *maxconn_str)
1496{
1497 long int v;
1498 char *end;
1499
1500 if (!*maxconn_str)
1501 return "Require <maxconn>.\n";
1502
1503 v = strtol(maxconn_str, &end, 10);
1504 if (end == maxconn_str)
1505 return "maxconn string empty or preceded by garbage";
1506 else if (end[0] != '\0')
1507 return "Trailing garbage in maxconn string";
1508
1509 if (sv->maxconn == sv->minconn) { // static maxconn
1510 sv->maxconn = sv->minconn = v;
1511 } else { // dynamic maxconn
1512 sv->maxconn = v;
1513 }
1514
1515 if (may_dequeue_tasks(sv, sv->proxy))
1516 process_srv_queue(sv);
1517
1518 return NULL;
1519}
1520
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001521#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001522static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1523 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001524{
1525 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001526 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001527 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001528 NULL,
1529 };
1530
1531 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001532 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001533
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001534 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1535}
1536
1537static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1538{
1539 struct sample_expr *expr;
1540
1541 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 +01001542 if (!expr) {
1543 memprintf(err, "error detected while parsing sni expression : %s", *err);
1544 return ERR_ALERT | ERR_FATAL;
1545 }
1546
1547 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1548 memprintf(err, "error detected while parsing sni expression : "
1549 " fetch method '%s' extracts information from '%s', "
1550 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001551 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001552 return ERR_ALERT | ERR_FATAL;
1553 }
1554
1555 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1556 release_sample_expr(newsrv->ssl_ctx.sni);
1557 newsrv->ssl_ctx.sni = expr;
1558
1559 return 0;
1560}
1561#endif
1562
1563static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1564{
1565 if (err && *err) {
1566 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001567 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 +01001568 }
1569 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001570 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1571 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001572}
1573
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001574static void srv_conn_src_sport_range_cpy(struct server *srv,
1575 struct server *src)
1576{
1577 int range_sz;
1578
1579 range_sz = src->conn_src.sport_range->size;
1580 if (range_sz > 0) {
1581 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1582 if (srv->conn_src.sport_range != NULL) {
1583 int i;
1584
1585 for (i = 0; i < range_sz; i++) {
1586 srv->conn_src.sport_range->ports[i] =
1587 src->conn_src.sport_range->ports[i];
1588 }
1589 }
1590 }
1591}
1592
1593/*
1594 * Copy <src> server connection source settings to <srv> server everything needed.
1595 */
1596static void srv_conn_src_cpy(struct server *srv, struct server *src)
1597{
1598 srv->conn_src.opts = src->conn_src.opts;
1599 srv->conn_src.source_addr = src->conn_src.source_addr;
1600
1601 /* Source port range copy. */
1602 if (src->conn_src.sport_range != NULL)
1603 srv_conn_src_sport_range_cpy(srv, src);
1604
1605#ifdef CONFIG_HAP_TRANSPARENT
1606 if (src->conn_src.bind_hdr_name != NULL) {
1607 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1608 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1609 }
1610 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1611 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1612#endif
1613 if (src->conn_src.iface_name != NULL)
1614 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1615}
1616
1617/*
1618 * Copy <src> server SSL settings to <srv> server allocating
1619 * everything needed.
1620 */
1621#if defined(USE_OPENSSL)
1622static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1623{
1624 if (src->ssl_ctx.ca_file != NULL)
1625 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1626 if (src->ssl_ctx.crl_file != NULL)
1627 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1628 if (src->ssl_ctx.client_crt != NULL)
1629 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1630
1631 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1632
1633 if (src->ssl_ctx.verify_host != NULL)
1634 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1635 if (src->ssl_ctx.ciphers != NULL)
1636 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Willy Tarreau5db847a2019-05-09 14:13:35 +02001637#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001638 if (src->ssl_ctx.ciphersuites != NULL)
1639 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1640#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001641 if (src->sni_expr != NULL)
1642 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001643
1644#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1645 if (src->ssl_ctx.alpn_str) {
1646 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1647 if (srv->ssl_ctx.alpn_str) {
1648 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1649 src->ssl_ctx.alpn_len);
1650 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1651 }
1652 }
1653#endif
1654#ifdef OPENSSL_NPN_NEGOTIATED
1655 if (src->ssl_ctx.npn_str) {
1656 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1657 if (srv->ssl_ctx.npn_str) {
1658 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1659 src->ssl_ctx.npn_len);
1660 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1661 }
1662 }
1663#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001664}
1665#endif
1666
1667/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001668 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001669 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001670 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001671 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001672static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001673{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001674 char *hostname_dn;
1675 int hostname_len, hostname_dn_len;
1676
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001677 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001678 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001679
Christopher Faulet67957bd2017-09-27 11:00:59 +02001680 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001681 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001682 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1683 hostname_dn, trash.size);
1684 if (hostname_dn_len == -1)
1685 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001686
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001687
Christopher Faulet67957bd2017-09-27 11:00:59 +02001688 free(srv->hostname);
1689 free(srv->hostname_dn);
1690 srv->hostname = strdup(hostname);
1691 srv->hostname_dn = strdup(hostname_dn);
1692 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001693 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001694 goto err;
1695
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001696 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001697
1698 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001699 free(srv->hostname); srv->hostname = NULL;
1700 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001701 return -1;
1702}
1703
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001704/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001705 * Copy <src> server settings to <srv> server allocating
1706 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001707 * This function is not supposed to be called at any time, but only
1708 * during server settings parsing or during server allocations from
1709 * a server template, and just after having calloc()'ed a new server.
1710 * So, <src> may only be a default server (when parsing server settings)
1711 * or a server template (during server allocations from a server template).
1712 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1713 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001714 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001715static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001716{
1717 /* Connection source settings copy */
1718 srv_conn_src_cpy(srv, src);
1719
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001720 if (srv_tmpl) {
1721 srv->addr = src->addr;
1722 srv->svc_port = src->svc_port;
1723 }
1724
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001725 srv->pp_opts = src->pp_opts;
1726 if (src->rdr_pfx != NULL) {
1727 srv->rdr_pfx = strdup(src->rdr_pfx);
1728 srv->rdr_len = src->rdr_len;
1729 }
1730 if (src->cookie != NULL) {
1731 srv->cookie = strdup(src->cookie);
1732 srv->cklen = src->cklen;
1733 }
1734 srv->use_ssl = src->use_ssl;
1735 srv->check.addr = srv->agent.addr = src->check.addr;
1736 srv->check.use_ssl = src->check.use_ssl;
1737 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001738 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001739 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001740 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001741 /* Note: 'flags' field has potentially been already initialized. */
1742 srv->flags |= src->flags;
1743 srv->do_check = src->do_check;
1744 srv->do_agent = src->do_agent;
1745 if (srv->check.port)
1746 srv->flags |= SRV_F_CHECKPORT;
1747 srv->check.inter = src->check.inter;
1748 srv->check.fastinter = src->check.fastinter;
1749 srv->check.downinter = src->check.downinter;
1750 srv->agent.use_ssl = src->agent.use_ssl;
1751 srv->agent.port = src->agent.port;
1752 if (src->agent.send_string != NULL)
1753 srv->agent.send_string = strdup(src->agent.send_string);
1754 srv->agent.send_string_len = src->agent.send_string_len;
1755 srv->agent.inter = src->agent.inter;
1756 srv->agent.fastinter = src->agent.fastinter;
1757 srv->agent.downinter = src->agent.downinter;
1758 srv->maxqueue = src->maxqueue;
1759 srv->minconn = src->minconn;
1760 srv->maxconn = src->maxconn;
1761 srv->slowstart = src->slowstart;
1762 srv->observe = src->observe;
1763 srv->onerror = src->onerror;
1764 srv->onmarkeddown = src->onmarkeddown;
1765 srv->onmarkedup = src->onmarkedup;
1766 if (src->trackit != NULL)
1767 srv->trackit = strdup(src->trackit);
1768 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1769 srv->uweight = srv->iweight = src->iweight;
1770
1771 srv->check.send_proxy = src->check.send_proxy;
1772 /* health: up, but will fall down at first failure */
1773 srv->check.rise = srv->check.health = src->check.rise;
1774 srv->check.fall = src->check.fall;
1775
1776 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001777 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1778 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1779 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001780 srv->check.state |= CHK_ST_PAUSED;
1781 srv->check.health = 0;
1782 }
1783
1784 /* health: up but will fall down at first failure */
1785 srv->agent.rise = srv->agent.health = src->agent.rise;
1786 srv->agent.fall = src->agent.fall;
1787
1788 if (src->resolvers_id != NULL)
1789 srv->resolvers_id = strdup(src->resolvers_id);
1790 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001791 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001792 if (srv->dns_opts.family_prio == AF_UNSPEC)
1793 srv->dns_opts.family_prio = AF_INET6;
1794 memcpy(srv->dns_opts.pref_net,
1795 src->dns_opts.pref_net,
1796 sizeof srv->dns_opts.pref_net);
1797 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1798
1799 srv->init_addr_methods = src->init_addr_methods;
1800 srv->init_addr = src->init_addr;
1801#if defined(USE_OPENSSL)
1802 srv_ssl_settings_cpy(srv, src);
1803#endif
1804#ifdef TCP_USER_TIMEOUT
1805 srv->tcp_ut = src->tcp_ut;
1806#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001807 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001808 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001809 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001810 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001811
Olivier Houchard8da5f982017-08-04 18:35:36 +02001812 if (srv_tmpl)
1813 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001814
1815 srv->check.via_socks4 = src->check.via_socks4;
1816 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001817}
1818
William Lallemand313bfd12018-10-26 14:47:32 +02001819struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001820{
1821 struct server *srv;
1822
1823 srv = calloc(1, sizeof *srv);
1824 if (!srv)
1825 return NULL;
1826
1827 srv->obj_type = OBJ_TYPE_SERVER;
1828 srv->proxy = proxy;
1829 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001830 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001831
Emeric Brun52a91d32017-08-31 14:41:55 +02001832 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001833 srv->last_change = now.tv_sec;
1834
1835 srv->check.status = HCHK_STATUS_INI;
1836 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001837 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001838 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1839
1840 srv->agent.status = HCHK_STATUS_INI;
1841 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001842 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001843 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1844
Willy Tarreau975b1552019-06-06 16:25:55 +02001845 /* please don't put default server settings here, they are set in
1846 * init_default_instance().
1847 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001848 return srv;
1849}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001850
1851/*
1852 * Validate <srv> server health-check settings.
1853 * Returns 0 if everything is OK, -1 if not.
1854 */
1855static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1856{
1857 struct tcpcheck_rule *r = NULL;
1858 struct list *l;
1859
1860 /*
1861 * We need at least a service port, a check port or the first tcp-check rule must
1862 * be a 'connect' one when checking an IPv4/IPv6 server.
1863 */
1864 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1865 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1866 return 0;
1867
1868 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1869 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001870 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1871 "Check has been disabled.\n",
1872 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001873 return -1;
1874 }
1875
1876 /* search the first action (connect / send / expect) in the list */
1877 l = &srv->proxy->tcpcheck_rules;
1878 list_for_each_entry(r, l, list) {
1879 if (r->action != TCPCHK_ACT_COMMENT)
1880 break;
1881 }
1882
1883 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001884 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1885 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1886 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001887 return -1;
1888 }
1889
1890 /* scan the tcp-check ruleset to ensure a port has been configured */
1891 l = &srv->proxy->tcpcheck_rules;
1892 list_for_each_entry(r, l, list) {
1893 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001894 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1895 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1896 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001897 return -1;
1898 }
1899 }
1900
1901 return 0;
1902}
1903
1904/*
1905 * Initialize <srv> health-check structure.
1906 * Returns the error string in case of memory allocation failure, NULL if not.
1907 */
1908static const char *do_health_check_init(struct server *srv, int check_type, int state)
1909{
1910 const char *ret;
1911
1912 if (!srv->do_check)
1913 return NULL;
1914
1915 ret = init_check(&srv->check, check_type);
1916 if (ret)
1917 return ret;
1918
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001919 srv->check.state |= state;
1920 global.maxsock++;
1921
1922 return NULL;
1923}
1924
1925static int server_health_check_init(const char *file, int linenum,
1926 struct server *srv, struct proxy *curproxy)
1927{
1928 const char *ret;
1929
1930 if (!srv->do_check)
1931 return 0;
1932
1933 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001934 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1935 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001936 return ERR_ALERT | ERR_FATAL;
1937 }
1938
1939 if (server_healthcheck_validate(file, linenum, srv) < 0)
1940 return ERR_ALERT | ERR_ABORT;
1941
1942 /* note: check type will be set during the config review phase */
1943 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1944 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001945 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001946 return ERR_ALERT | ERR_ABORT;
1947 }
1948
1949 return 0;
1950}
1951
1952/*
1953 * Initialize <srv> agent check structure.
1954 * Returns the error string in case of memory allocation failure, NULL if not.
1955 */
1956static const char *do_server_agent_check_init(struct server *srv, int state)
1957{
1958 const char *ret;
1959
1960 if (!srv->do_agent)
1961 return NULL;
1962
1963 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1964 if (ret)
1965 return ret;
1966
1967 if (!srv->agent.inter)
1968 srv->agent.inter = srv->check.inter;
1969
1970 srv->agent.state |= state;
1971 global.maxsock++;
1972
1973 return NULL;
1974}
1975
1976static int server_agent_check_init(const char *file, int linenum,
1977 struct server *srv, struct proxy *curproxy)
1978{
1979 const char *ret;
1980
1981 if (!srv->do_agent)
1982 return 0;
1983
1984 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001985 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 +02001986 file, linenum, srv->id);
1987 return ERR_ALERT | ERR_FATAL;
1988 }
1989
1990 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1991 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001992 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001993 return ERR_ALERT | ERR_ABORT;
1994 }
1995
1996 return 0;
1997}
1998
1999#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2000static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
2001 struct server *srv, struct proxy *proxy)
2002{
2003 int ret;
2004 char *err = NULL;
2005
2006 if (!srv->sni_expr)
2007 return 0;
2008
2009 ret = server_parse_sni_expr(srv, proxy, &err);
2010 if (!ret)
2011 return 0;
2012
2013 display_parser_err(file, linenum, args, cur_arg, &err);
2014 free(err);
2015
2016 return ret;
2017}
2018#endif
2019
2020/*
2021 * Server initializations finalization.
2022 * Initialize health check, agent check and SNI expression if enabled.
2023 * Must not be called for a default server instance.
2024 */
2025static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2026 struct server *srv, struct proxy *px)
2027{
2028 int ret;
2029
2030 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2031 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2032 return ret;
2033 }
2034
2035#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2036 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2037 return ret;
2038#endif
2039
2040 if (srv->flags & SRV_F_BACKUP)
2041 px->srv_bck++;
2042 else
2043 px->srv_act++;
2044 srv_lb_commit_status(srv);
2045
2046 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002047err:
2048 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002049}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002050
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002051/*
2052 * Parse as much as possible such a range string argument: low[-high]
2053 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2054 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2055 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2056 * Returns 0 if succeeded, -1 if not.
2057 */
2058static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2059{
2060 char *nb_high_arg;
2061
2062 *nb_high = 0;
2063 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002064 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002065
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002066 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002067 *nb_high_arg++ = '\0';
2068 *nb_high = atoi(nb_high_arg);
2069 }
2070 else {
2071 *nb_high += *nb_low;
2072 *nb_low = 1;
2073 }
2074
2075 if (*nb_low < 0 || *nb_high < *nb_low)
2076 return -1;
2077
2078 return 0;
2079}
2080
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002081static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2082{
2083 chunk_printf(&trash, "%s%d", prefix, nb);
2084 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002085 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002086}
2087
2088/*
2089 * Initialize as much as possible servers from <srv> server template.
2090 * Note that a server template is a special server with
2091 * a few different parameters than a server which has
2092 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002093 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002094 * 'srv' template included.
2095 */
2096static int server_template_init(struct server *srv, struct proxy *px)
2097{
2098 int i;
2099 struct server *newsrv;
2100
2101 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2102 int check_init_state;
2103 int agent_init_state;
2104
2105 newsrv = new_server(px);
2106 if (!newsrv)
2107 goto err;
2108
2109 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002110 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002111#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2112 if (newsrv->sni_expr) {
2113 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2114 if (!newsrv->ssl_ctx.sni)
2115 goto err;
2116 }
2117#endif
2118 /* Set this new server ID. */
2119 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2120
2121 /* Initial checks states. */
2122 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2123 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2124
2125 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2126 do_server_agent_check_init(newsrv, agent_init_state))
2127 goto err;
2128
2129 /* Linked backwards first. This will be restablished after parsing. */
2130 newsrv->next = px->srv;
2131 px->srv = newsrv;
2132 }
2133 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2134
2135 return i - srv->tmpl_info.nb_low;
2136
2137 err:
2138 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2139 if (newsrv) {
2140#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2141 release_sample_expr(newsrv->ssl_ctx.sni);
2142#endif
2143 free_check(&newsrv->agent);
2144 free_check(&newsrv->check);
2145 }
2146 free(newsrv);
2147 return i - srv->tmpl_info.nb_low;
2148}
2149
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002150int 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 +02002151{
2152 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002153 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002154 char *errmsg = NULL;
2155 int err_code = 0;
2156 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002157 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002158
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002159 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002160 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002161 !strcmp(args[0], "default-server") ||
2162 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002163 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002164 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002165 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002166 int srv_tmpl = !defsrv && !srv;
2167 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002168
2169 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002170 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002171 err_code |= ERR_ALERT | ERR_FATAL;
2172 goto out;
2173 }
2174 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002175 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002176
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002177 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002178 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002179 if (!*args[2]) {
2180 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002181 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002182 file, linenum, args[0]);
2183 err_code |= ERR_ALERT | ERR_FATAL;
2184 goto out;
2185 }
2186
2187 err = invalid_char(args[1]);
2188 }
2189 else if (srv_tmpl) {
2190 if (!*args[3]) {
2191 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002192 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 +02002193 file, linenum, args[0]);
2194 err_code |= ERR_ALERT | ERR_FATAL;
2195 goto out;
2196 }
2197
2198 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002199 }
2200
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002201 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002202 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 +02002203 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002204 err_code |= ERR_ALERT | ERR_FATAL;
2205 goto out;
2206 }
2207
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002208 cur_arg = 2;
2209 if (srv_tmpl) {
2210 /* Parse server-template <nb | range> arg. */
2211 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002212 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002213 file, linenum, args[0], args[cur_arg]);
2214 err_code |= ERR_ALERT | ERR_FATAL;
2215 goto out;
2216 }
2217 cur_arg++;
2218 }
2219
Willy Tarreau272adea2014-03-31 10:39:59 +02002220 if (!defsrv) {
2221 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002222 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002223 struct protocol *proto;
2224
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002225 newsrv = new_server(curproxy);
2226 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002227 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002228 err_code |= ERR_ALERT | ERR_ABORT;
2229 goto out;
2230 }
2231
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002232 if (srv_tmpl) {
2233 newsrv->tmpl_info.nb_low = tmpl_range_low;
2234 newsrv->tmpl_info.nb_high = tmpl_range_high;
2235 }
2236
Willy Tarreau272adea2014-03-31 10:39:59 +02002237 /* the servers are linked backwards first */
2238 newsrv->next = curproxy->srv;
2239 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002240 newsrv->conf.file = strdup(file);
2241 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002242 /* Note: for a server template, its id is its prefix.
2243 * This is a temporary id which will be used for server allocations to come
2244 * after parsing.
2245 */
2246 if (srv)
2247 newsrv->id = strdup(args[1]);
2248 else
2249 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002250
2251 /* several ways to check the port component :
2252 * - IP => port=+0, relative (IPv4 only)
2253 * - IP: => port=+0, relative
2254 * - IP:N => port=N, absolute
2255 * - IP:+N => port=+N, relative
2256 * - IP:-N => port=-N, relative
2257 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002258 if (!parse_addr)
2259 goto skip_addr;
2260
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002261 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002262 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002263 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002264 err_code |= ERR_ALERT | ERR_FATAL;
2265 goto out;
2266 }
2267
2268 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002269 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002270 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002271 file, linenum, args[0], args[1]);
2272 err_code |= ERR_ALERT | ERR_FATAL;
2273 goto out;
2274 }
2275
2276 if (!port1 || !port2) {
2277 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002278 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002279 }
2280 else if (port1 != port2) {
2281 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002282 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002283 file, linenum, args[0], args[1], args[2]);
2284 err_code |= ERR_ALERT | ERR_FATAL;
2285 goto out;
2286 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002287
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002288 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002289 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002290 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002291 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002292 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2293 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2294 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002295 err_code |= ERR_ALERT | ERR_FATAL;
2296 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002297 }
2298 }
2299 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002300 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002301 file, linenum, newsrv->id);
2302 err_code |= ERR_ALERT | ERR_FATAL;
2303 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002304 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002305 }
2306
Willy Tarreau272adea2014-03-31 10:39:59 +02002307 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002308 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002309
Olivier Houchard8da5f982017-08-04 18:35:36 +02002310 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002311 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002312 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002313 err_code |= ERR_ALERT | ERR_FATAL;
2314 goto out;
2315 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002316
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002317 cur_arg++;
2318 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002319 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002320 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002321 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002322 } else {
2323 newsrv = &curproxy->defsrv;
2324 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002325 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002326 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002327 }
2328
2329 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002330 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002331 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002332
2333 if (err == PARSE_TIME_OVER) {
2334 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2335 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2336 err_code |= ERR_ALERT | ERR_FATAL;
2337 goto out;
2338 }
2339 else if (err == PARSE_TIME_UNDER) {
2340 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2341 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2342 err_code |= ERR_ALERT | ERR_FATAL;
2343 goto out;
2344 }
2345 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002346 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002347 file, linenum, *err, newsrv->id);
2348 err_code |= ERR_ALERT | ERR_FATAL;
2349 goto out;
2350 }
2351 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002352 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002353 file, linenum, val, args[cur_arg], newsrv->id);
2354 err_code |= ERR_ALERT | ERR_FATAL;
2355 goto out;
2356 }
2357 newsrv->agent.inter = val;
2358 cur_arg += 2;
2359 }
Misiekea849332017-01-09 09:39:51 +01002360 else if (!strcmp(args[cur_arg], "agent-addr")) {
2361 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002362 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002363 goto out;
2364 }
2365
2366 cur_arg += 2;
2367 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002368 else if (!strcmp(args[cur_arg], "agent-port")) {
2369 global.maxsock++;
2370 newsrv->agent.port = atol(args[cur_arg + 1]);
2371 cur_arg += 2;
2372 }
James Brown55f9ff12015-10-21 18:19:05 -07002373 else if (!strcmp(args[cur_arg], "agent-send")) {
2374 global.maxsock++;
2375 free(newsrv->agent.send_string);
2376 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2377 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2378 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2379 cur_arg += 2;
2380 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002381 else if (!strcmp(args[cur_arg], "init-addr")) {
2382 char *p, *end;
2383 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002384 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002385
2386 newsrv->init_addr_methods = 0;
2387 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2388
2389 for (p = args[cur_arg + 1]; *p; p = end) {
2390 /* cut on next comma */
2391 for (end = p; *end && *end != ','; end++);
2392 if (*end)
2393 *(end++) = 0;
2394
Willy Tarreau4310d362016-11-02 15:05:56 +01002395 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002396 if (!strcmp(p, "libc")) {
2397 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2398 }
2399 else if (!strcmp(p, "last")) {
2400 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2401 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002402 else if (!strcmp(p, "none")) {
2403 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2404 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002405 else if (str2ip2(p, &sa, 0)) {
2406 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002407 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002408 file, linenum, args[cur_arg], p);
2409 err_code |= ERR_ALERT | ERR_FATAL;
2410 goto out;
2411 }
2412 newsrv->init_addr = sa;
2413 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2414 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002415 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002416 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 +02002417 file, linenum, args[cur_arg], p);
2418 err_code |= ERR_ALERT | ERR_FATAL;
2419 goto out;
2420 }
2421 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002422 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002423 file, linenum, args[cur_arg], p);
2424 err_code |= ERR_ALERT | ERR_FATAL;
2425 goto out;
2426 }
2427 }
2428 cur_arg += 2;
2429 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002430 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002431 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002432 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2433 cur_arg += 2;
2434 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002435 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2436 char *p, *end;
2437
2438 for (p = args[cur_arg + 1]; *p; p = end) {
2439 /* cut on next comma */
2440 for (end = p; *end && *end != ','; end++);
2441 if (*end)
2442 *(end++) = 0;
2443
2444 if (!strcmp(p, "allow-dup-ip")) {
2445 newsrv->dns_opts.accept_duplicate_ip = 1;
2446 }
2447 else if (!strcmp(p, "prevent-dup-ip")) {
2448 newsrv->dns_opts.accept_duplicate_ip = 0;
2449 }
2450 else {
2451 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2452 file, linenum, args[cur_arg], p);
2453 err_code |= ERR_ALERT | ERR_FATAL;
2454 goto out;
2455 }
2456 }
2457
2458 cur_arg += 2;
2459 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002460 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2461 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002462 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002463 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002464 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002465 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002466 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002467 file, linenum, args[cur_arg]);
2468 err_code |= ERR_ALERT | ERR_FATAL;
2469 goto out;
2470 }
2471 cur_arg += 2;
2472 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002473 else if (!strcmp(args[cur_arg], "resolve-net")) {
2474 char *p, *e;
2475 unsigned char mask;
2476 struct dns_options *opt;
2477
2478 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002479 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002480 file, linenum, args[cur_arg]);
2481 err_code |= ERR_ALERT | ERR_FATAL;
2482 goto out;
2483 }
2484
2485 opt = &newsrv->dns_opts;
2486
2487 /* Split arguments by comma, and convert it from ipv4 or ipv6
2488 * string network in in_addr or in6_addr.
2489 */
2490 p = args[cur_arg + 1];
2491 e = p;
2492 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002493 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002494 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002495 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002496 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2497 err_code |= ERR_ALERT | ERR_FATAL;
2498 goto out;
2499 }
2500 /* look for end or comma. */
2501 while (*e != ',' && *e != '\0')
2502 e++;
2503 if (*e == ',') {
2504 *e = '\0';
2505 e++;
2506 }
2507 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2508 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2509 /* Try to convert input string from ipv4 or ipv6 network. */
2510 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2511 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2512 &mask)) {
2513 /* Try to convert input string from ipv6 network. */
2514 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2515 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2516 } else {
2517 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002518 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002519 file, linenum, args[cur_arg], p);
2520 err_code |= ERR_ALERT | ERR_FATAL;
2521 goto out;
2522 }
2523 opt->pref_net_nb++;
2524 p = e;
2525 }
2526
2527 cur_arg += 2;
2528 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002529 else if (!strcmp(args[cur_arg], "rise")) {
2530 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002531 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002532 file, linenum, args[cur_arg]);
2533 err_code |= ERR_ALERT | ERR_FATAL;
2534 goto out;
2535 }
2536
2537 newsrv->check.rise = atol(args[cur_arg + 1]);
2538 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002539 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002540 file, linenum, args[cur_arg]);
2541 err_code |= ERR_ALERT | ERR_FATAL;
2542 goto out;
2543 }
2544
2545 if (newsrv->check.health)
2546 newsrv->check.health = newsrv->check.rise;
2547 cur_arg += 2;
2548 }
2549 else if (!strcmp(args[cur_arg], "fall")) {
2550 newsrv->check.fall = atol(args[cur_arg + 1]);
2551
2552 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002553 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002554 file, linenum, args[cur_arg]);
2555 err_code |= ERR_ALERT | ERR_FATAL;
2556 goto out;
2557 }
2558
2559 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002560 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002561 file, linenum, args[cur_arg]);
2562 err_code |= ERR_ALERT | ERR_FATAL;
2563 goto out;
2564 }
2565
2566 cur_arg += 2;
2567 }
2568 else if (!strcmp(args[cur_arg], "inter")) {
2569 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002570
2571 if (err == PARSE_TIME_OVER) {
2572 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2573 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2574 err_code |= ERR_ALERT | ERR_FATAL;
2575 goto out;
2576 }
2577 else if (err == PARSE_TIME_UNDER) {
2578 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2579 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2580 err_code |= ERR_ALERT | ERR_FATAL;
2581 goto out;
2582 }
2583 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002584 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002585 file, linenum, *err, newsrv->id);
2586 err_code |= ERR_ALERT | ERR_FATAL;
2587 goto out;
2588 }
2589 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002590 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002591 file, linenum, val, args[cur_arg], newsrv->id);
2592 err_code |= ERR_ALERT | ERR_FATAL;
2593 goto out;
2594 }
2595 newsrv->check.inter = val;
2596 cur_arg += 2;
2597 }
2598 else if (!strcmp(args[cur_arg], "fastinter")) {
2599 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002600
2601 if (err == PARSE_TIME_OVER) {
2602 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2603 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2604 err_code |= ERR_ALERT | ERR_FATAL;
2605 goto out;
2606 }
2607 else if (err == PARSE_TIME_UNDER) {
2608 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2609 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2610 err_code |= ERR_ALERT | ERR_FATAL;
2611 goto out;
2612 }
2613 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002614 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002615 file, linenum, *err, newsrv->id);
2616 err_code |= ERR_ALERT | ERR_FATAL;
2617 goto out;
2618 }
2619 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002620 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002621 file, linenum, val, args[cur_arg], newsrv->id);
2622 err_code |= ERR_ALERT | ERR_FATAL;
2623 goto out;
2624 }
2625 newsrv->check.fastinter = val;
2626 cur_arg += 2;
2627 }
2628 else if (!strcmp(args[cur_arg], "downinter")) {
2629 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002630
2631 if (err == PARSE_TIME_OVER) {
2632 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2633 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2634 err_code |= ERR_ALERT | ERR_FATAL;
2635 goto out;
2636 }
2637 else if (err == PARSE_TIME_UNDER) {
2638 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2639 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2640 err_code |= ERR_ALERT | ERR_FATAL;
2641 goto out;
2642 }
2643 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002644 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002645 file, linenum, *err, newsrv->id);
2646 err_code |= ERR_ALERT | ERR_FATAL;
2647 goto out;
2648 }
2649 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002650 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002651 file, linenum, val, args[cur_arg], newsrv->id);
2652 err_code |= ERR_ALERT | ERR_FATAL;
2653 goto out;
2654 }
2655 newsrv->check.downinter = val;
2656 cur_arg += 2;
2657 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002658 else if (!strcmp(args[cur_arg], "port")) {
2659 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002660 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002661 cur_arg += 2;
2662 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002663 else if (!strcmp(args[cur_arg], "weight")) {
2664 int w;
2665 w = atol(args[cur_arg + 1]);
2666 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002667 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002668 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2669 err_code |= ERR_ALERT | ERR_FATAL;
2670 goto out;
2671 }
2672 newsrv->uweight = newsrv->iweight = w;
2673 cur_arg += 2;
2674 }
2675 else if (!strcmp(args[cur_arg], "minconn")) {
2676 newsrv->minconn = atol(args[cur_arg + 1]);
2677 cur_arg += 2;
2678 }
2679 else if (!strcmp(args[cur_arg], "maxconn")) {
2680 newsrv->maxconn = atol(args[cur_arg + 1]);
2681 cur_arg += 2;
2682 }
2683 else if (!strcmp(args[cur_arg], "maxqueue")) {
2684 newsrv->maxqueue = atol(args[cur_arg + 1]);
2685 cur_arg += 2;
2686 }
2687 else if (!strcmp(args[cur_arg], "slowstart")) {
2688 /* slowstart is stored in seconds */
2689 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002690
2691 if (err == PARSE_TIME_OVER) {
2692 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2693 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2694 err_code |= ERR_ALERT | ERR_FATAL;
2695 goto out;
2696 }
2697 else if (err == PARSE_TIME_UNDER) {
2698 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2699 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2700 err_code |= ERR_ALERT | ERR_FATAL;
2701 goto out;
2702 }
2703 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002704 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002705 file, linenum, *err, newsrv->id);
2706 err_code |= ERR_ALERT | ERR_FATAL;
2707 goto out;
2708 }
2709 newsrv->slowstart = (val + 999) / 1000;
2710 cur_arg += 2;
2711 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002712 else if (!strcmp(args[cur_arg], "on-error")) {
2713 if (!strcmp(args[cur_arg + 1], "fastinter"))
2714 newsrv->onerror = HANA_ONERR_FASTINTER;
2715 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2716 newsrv->onerror = HANA_ONERR_FAILCHK;
2717 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2718 newsrv->onerror = HANA_ONERR_SUDDTH;
2719 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2720 newsrv->onerror = HANA_ONERR_MARKDWN;
2721 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002722 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002723 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2724 file, linenum, args[cur_arg], args[cur_arg + 1]);
2725 err_code |= ERR_ALERT | ERR_FATAL;
2726 goto out;
2727 }
2728
2729 cur_arg += 2;
2730 }
2731 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2732 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2733 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2734 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002735 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002736 file, linenum, args[cur_arg], args[cur_arg + 1]);
2737 err_code |= ERR_ALERT | ERR_FATAL;
2738 goto out;
2739 }
2740
2741 cur_arg += 2;
2742 }
2743 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2744 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2745 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2746 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002747 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002748 file, linenum, args[cur_arg], args[cur_arg + 1]);
2749 err_code |= ERR_ALERT | ERR_FATAL;
2750 goto out;
2751 }
2752
2753 cur_arg += 2;
2754 }
2755 else if (!strcmp(args[cur_arg], "error-limit")) {
2756 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002757 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002758 file, linenum, args[cur_arg]);
2759 err_code |= ERR_ALERT | ERR_FATAL;
2760 goto out;
2761 }
2762
2763 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2764
2765 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002766 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002767 file, linenum, args[cur_arg]);
2768 err_code |= ERR_ALERT | ERR_FATAL;
2769 goto out;
2770 }
2771 cur_arg += 2;
2772 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002773 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002774 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002775 file, linenum, "usesrc", "source");
2776 err_code |= ERR_ALERT | ERR_FATAL;
2777 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002778 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002779 else {
2780 static int srv_dumped;
2781 struct srv_kw *kw;
2782 char *err;
2783
2784 kw = srv_find_kw(args[cur_arg]);
2785 if (kw) {
2786 char *err = NULL;
2787 int code;
2788
2789 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002790 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 +02002791 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002792 if (kw->skip != -1)
2793 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002794 err_code |= ERR_ALERT | ERR_FATAL;
2795 goto out;
2796 }
2797
2798 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002799 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002800 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002801 if (kw->skip != -1)
2802 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002803 err_code |= ERR_ALERT;
2804 continue;
2805 }
2806
2807 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2808 err_code |= code;
2809
2810 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002811 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002812 if (code & ERR_FATAL) {
2813 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002814 if (kw->skip != -1)
2815 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002816 goto out;
2817 }
2818 }
2819 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002820 if (kw->skip != -1)
2821 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002822 continue;
2823 }
2824
2825 err = NULL;
2826 if (!srv_dumped) {
2827 srv_dump_kws(&err);
2828 indent_msg(&err, 4);
2829 srv_dumped = 1;
2830 }
2831
Christopher Faulet767a84b2017-11-24 16:50:31 +01002832 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002833 file, linenum, args[0], args[1], args[cur_arg],
2834 err ? " Registered keywords :" : "", err ? err : "");
2835 free(err);
2836
2837 err_code |= ERR_ALERT | ERR_FATAL;
2838 goto out;
2839 }
2840 }
2841
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002842 if (!defsrv)
2843 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2844 if (err_code & ERR_FATAL)
2845 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002846 if (srv_tmpl)
2847 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002848 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002849 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002850 return 0;
2851
2852 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002853 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002854 free(errmsg);
2855 return err_code;
2856}
2857
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002858/* Returns a pointer to the first server matching either id <id>.
2859 * NULL is returned if no match is found.
2860 * the lookup is performed in the backend <bk>
2861 */
2862struct server *server_find_by_id(struct proxy *bk, int id)
2863{
2864 struct eb32_node *eb32;
2865 struct server *curserver;
2866
2867 if (!bk || (id ==0))
2868 return NULL;
2869
2870 /* <bk> has no backend capabilities, so it can't have a server */
2871 if (!(bk->cap & PR_CAP_BE))
2872 return NULL;
2873
2874 curserver = NULL;
2875
2876 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2877 if (eb32)
2878 curserver = container_of(eb32, struct server, conf.id);
2879
2880 return curserver;
2881}
2882
2883/* Returns a pointer to the first server matching either name <name>, or id
2884 * if <name> starts with a '#'. NULL is returned if no match is found.
2885 * the lookup is performed in the backend <bk>
2886 */
2887struct server *server_find_by_name(struct proxy *bk, const char *name)
2888{
2889 struct server *curserver;
2890
2891 if (!bk || !name)
2892 return NULL;
2893
2894 /* <bk> has no backend capabilities, so it can't have a server */
2895 if (!(bk->cap & PR_CAP_BE))
2896 return NULL;
2897
2898 curserver = NULL;
2899 if (*name == '#') {
2900 curserver = server_find_by_id(bk, atoi(name + 1));
2901 if (curserver)
2902 return curserver;
2903 }
2904 else {
2905 curserver = bk->srv;
2906
2907 while (curserver && (strcmp(curserver->id, name) != 0))
2908 curserver = curserver->next;
2909
2910 if (curserver)
2911 return curserver;
2912 }
2913
2914 return NULL;
2915}
2916
2917struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2918{
2919 struct server *byname;
2920 struct server *byid;
2921
2922 if (!name && !id)
2923 return NULL;
2924
2925 if (diff)
2926 *diff = 0;
2927
2928 byname = byid = NULL;
2929
2930 if (name) {
2931 byname = server_find_by_name(bk, name);
2932 if (byname && (!id || byname->puid == id))
2933 return byname;
2934 }
2935
2936 /* remaining possibilities :
2937 * - name not set
2938 * - name set but not found
2939 * - name found but ID doesn't match
2940 */
2941 if (id) {
2942 byid = server_find_by_id(bk, id);
2943 if (byid) {
2944 if (byname) {
2945 /* use id only if forced by configuration */
2946 if (byid->flags & SRV_F_FORCED_ID) {
2947 if (diff)
2948 *diff |= 2;
2949 return byid;
2950 }
2951 else {
2952 if (diff)
2953 *diff |= 1;
2954 return byname;
2955 }
2956 }
2957
2958 /* remaining possibilities:
2959 * - name not set
2960 * - name set but not found
2961 */
2962 if (name && diff)
2963 *diff |= 2;
2964 return byid;
2965 }
2966
2967 /* id bot found */
2968 if (byname) {
2969 if (diff)
2970 *diff |= 1;
2971 return byname;
2972 }
2973 }
2974
2975 return NULL;
2976}
2977
Willy Tarreau46b7f532018-08-21 11:54:26 +02002978/* Update a server state using the parameters available in the params list.
2979 *
2980 * Grabs the server lock during operation.
2981 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002982static void srv_update_state(struct server *srv, int version, char **params)
2983{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002984 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002985 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002986
2987 /* fields since version 1
2988 * and common to all other upcoming versions
2989 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002990 enum srv_state srv_op_state;
2991 enum srv_admin srv_admin_state;
2992 unsigned srv_uweight, srv_iweight;
2993 unsigned long srv_last_time_change;
2994 short srv_check_status;
2995 enum chk_result srv_check_result;
2996 int srv_check_health;
2997 int srv_check_state, srv_agent_state;
2998 int bk_f_forced_id;
2999 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003000 int fqdn_set_by_cli;
3001 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02003002 const char *port_str;
3003 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003004 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003005
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003006 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02003007 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02003008 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003009 switch (version) {
3010 case 1:
3011 /*
3012 * now we can proceed with server's state update:
3013 * srv_addr: params[0]
3014 * srv_op_state: params[1]
3015 * srv_admin_state: params[2]
3016 * srv_uweight: params[3]
3017 * srv_iweight: params[4]
3018 * srv_last_time_change: params[5]
3019 * srv_check_status: params[6]
3020 * srv_check_result: params[7]
3021 * srv_check_health: params[8]
3022 * srv_check_state: params[9]
3023 * srv_agent_state: params[10]
3024 * bk_f_forced_id: params[11]
3025 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003026 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003027 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003028 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003029 */
3030
3031 /* validating srv_op_state */
3032 p = NULL;
3033 errno = 0;
3034 srv_op_state = strtol(params[1], &p, 10);
3035 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
3036 (srv_op_state != SRV_ST_STOPPED &&
3037 srv_op_state != SRV_ST_STARTING &&
3038 srv_op_state != SRV_ST_RUNNING &&
3039 srv_op_state != SRV_ST_STOPPING)) {
3040 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
3041 }
3042
3043 /* validating srv_admin_state */
3044 p = NULL;
3045 errno = 0;
3046 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003047 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01003048
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003049 /* inherited statuses will be recomputed later.
3050 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
3051 */
3052 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01003053
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003054 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
3055 (srv_admin_state != 0 &&
3056 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003057 srv_admin_state != SRV_ADMF_CMAINT &&
3058 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01003059 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01003060 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003061 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
3062 }
3063
3064 /* validating srv_uweight */
3065 p = NULL;
3066 errno = 0;
3067 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003068 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003069 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
3070
3071 /* validating srv_iweight */
3072 p = NULL;
3073 errno = 0;
3074 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003075 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003076 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
3077
3078 /* validating srv_last_time_change */
3079 p = NULL;
3080 errno = 0;
3081 srv_last_time_change = strtol(params[5], &p, 10);
3082 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
3083 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
3084
3085 /* validating srv_check_status */
3086 p = NULL;
3087 errno = 0;
3088 srv_check_status = strtol(params[6], &p, 10);
3089 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3090 (srv_check_status >= HCHK_STATUS_SIZE))
3091 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3092
3093 /* validating srv_check_result */
3094 p = NULL;
3095 errno = 0;
3096 srv_check_result = strtol(params[7], &p, 10);
3097 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3098 (srv_check_result != CHK_RES_UNKNOWN &&
3099 srv_check_result != CHK_RES_NEUTRAL &&
3100 srv_check_result != CHK_RES_FAILED &&
3101 srv_check_result != CHK_RES_PASSED &&
3102 srv_check_result != CHK_RES_CONDPASS)) {
3103 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3104 }
3105
3106 /* validating srv_check_health */
3107 p = NULL;
3108 errno = 0;
3109 srv_check_health = strtol(params[8], &p, 10);
3110 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3111 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3112
3113 /* validating srv_check_state */
3114 p = NULL;
3115 errno = 0;
3116 srv_check_state = strtol(params[9], &p, 10);
3117 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3118 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3119 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3120
3121 /* validating srv_agent_state */
3122 p = NULL;
3123 errno = 0;
3124 srv_agent_state = strtol(params[10], &p, 10);
3125 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3126 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3127 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3128
3129 /* validating bk_f_forced_id */
3130 p = NULL;
3131 errno = 0;
3132 bk_f_forced_id = strtol(params[11], &p, 10);
3133 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3134 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3135
3136 /* validating srv_f_forced_id */
3137 p = NULL;
3138 errno = 0;
3139 srv_f_forced_id = strtol(params[12], &p, 10);
3140 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3141 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3142
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003143 /* validating srv_fqdn */
3144 fqdn = params[13];
3145 if (fqdn && *fqdn == '-')
3146 fqdn = NULL;
3147 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3148 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3149 fqdn = NULL;
3150 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003151
Frédéric Lécaille31694712017-08-01 08:47:19 +02003152 port_str = params[14];
3153 if (port_str) {
3154 port = strl2uic(port_str, strlen(port_str));
3155 if (port > USHRT_MAX) {
3156 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3157 port_str = NULL;
3158 }
3159 }
3160
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003161 /* SRV record
3162 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3163 */
3164 srvrecord = params[15];
3165 if (srvrecord && *srvrecord != '_')
3166 srvrecord = NULL;
3167
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003168 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003169 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003170 goto out;
3171
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003172 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003173 /* recover operational state and apply it to this server
3174 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003175 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003176 switch (srv_op_state) {
3177 case SRV_ST_STOPPED:
3178 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003179 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003180 break;
3181 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003182 /* If rise == 1 there is no STARTING state, let's switch to
3183 * RUNNING
3184 */
3185 if (srv->check.rise == 1) {
3186 srv->check.health = srv->check.rise + srv->check.fall - 1;
3187 srv_set_running(srv, "", NULL);
3188 break;
3189 }
3190 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3191 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003192 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003193 break;
3194 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003195 /* If fall == 1 there is no STOPPING state, let's switch to
3196 * STOPPED
3197 */
3198 if (srv->check.fall == 1) {
3199 srv->check.health = 0;
3200 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3201 break;
3202 }
3203 if (srv->check.health < srv->check.rise ||
3204 srv->check.health > srv->check.rise + srv->check.fall - 2)
3205 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003206 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003207 break;
3208 case SRV_ST_RUNNING:
3209 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003210 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003211 break;
3212 }
3213
3214 /* When applying server state, the following rules apply:
3215 * - in case of a configuration change, we apply the setting from the new
3216 * configuration, regardless of old running state
3217 * - if no configuration change, we apply old running state only if old running
3218 * state is different from new configuration state
3219 */
3220 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003221 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3222 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003223 srv_adm_set_maint(srv);
3224 else
3225 srv_adm_set_ready(srv);
3226 }
3227 /* configuration is the same, let's compate old running state and new conf state */
3228 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003229 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003230 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003231 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003232 srv_adm_set_ready(srv);
3233 }
3234 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003235 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003236 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003237 * (srv->iweight is the weight set up in configuration).
3238 * There are two possible reasons for FDRAIN to have been present :
3239 * - previous config weight was zero
3240 * - "set server b/s drain" was sent to the CLI
3241 *
3242 * In the first case, we simply want to drop this drain state
3243 * if the new weight is not zero anymore, meaning the administrator
3244 * has intentionally turned the weight back to a positive value to
3245 * enable the server again after an operation. In the second case,
3246 * the drain state was forced on the CLI regardless of the config's
3247 * weight so we don't want a change to the config weight to lose this
3248 * status. What this means is :
3249 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3250 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003251 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003252 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003253 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003254 }
3255
3256 srv->last_change = date.tv_sec - srv_last_time_change;
3257 srv->check.status = srv_check_status;
3258 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003259
3260 /* Only case we want to apply is removing ENABLED flag which could have been
3261 * done by the "disable health" command over the stats socket
3262 */
3263 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3264 (srv_check_state & CHK_ST_CONFIGURED) &&
3265 !(srv_check_state & CHK_ST_ENABLED))
3266 srv->check.state &= ~CHK_ST_ENABLED;
3267
3268 /* Only case we want to apply is removing ENABLED flag which could have been
3269 * done by the "disable agent" command over the stats socket
3270 */
3271 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3272 (srv_agent_state & CHK_ST_CONFIGURED) &&
3273 !(srv_agent_state & CHK_ST_ENABLED))
3274 srv->agent.state &= ~CHK_ST_ENABLED;
3275
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003276 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3277 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003278 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003279 * It means that a configuration file change has precedence over a unix socket change
3280 * for server's weight
3281 *
3282 * by default, HAProxy applies the following weight when parsing the configuration
3283 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003284 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003285 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003286 srv->uweight = srv_uweight;
3287 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003288 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003289
Willy Tarreaue5a60682016-11-09 14:54:53 +01003290 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003291 if (strcmp(params[0], "-"))
3292 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003293
3294 if (fqdn && srv->hostname) {
3295 if (!strcmp(srv->hostname, fqdn)) {
3296 /* Here we reset the 'set from stats socket FQDN' flag
3297 * to support such transitions:
3298 * Let's say initial FQDN value is foo1 (in configuration file).
3299 * - FQDN changed from stats socket, from foo1 to foo2 value,
3300 * - FQDN changed again from file configuration (with the same previous value
3301 set from stats socket, from foo1 to foo2 value),
3302 * - reload for any other reason than a FQDN modification,
3303 * the configuration file FQDN matches the fqdn server state file value.
3304 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003305 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003306 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003307 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003308 }
3309 else {
3310 /* If the FDQN has been changed from stats socket,
3311 * apply fqdn state file value (which is the value set
3312 * from stats socket).
3313 */
3314 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003315 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003316 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003317 }
3318 }
3319 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003320 /* If all the conditions below are validated, this means
3321 * we're evaluating a server managed by SRV resolution
3322 */
3323 else if (fqdn && !srv->hostname && srvrecord) {
3324 int res;
3325
3326 /* we can't apply previous state if SRV record has changed */
3327 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3328 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);
3329 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3330 goto out;
3331 }
3332
3333 /* create or find a SRV resolution for this srv record */
3334 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3335 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3336 if (srv->srvrq == NULL) {
3337 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3338 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3339 goto out;
3340 }
3341
3342 /* prepare DNS resolution for this server */
3343 res = srv_prepare_for_resolution(srv, fqdn);
3344 if (res == -1) {
3345 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3346 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3347 goto out;
3348 }
3349
3350 /* configure check.port accordingly */
3351 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3352 !(srv->flags & SRV_F_CHECKPORT))
3353 srv->check.port = port;
3354
3355 /* Unset SRV_F_MAPPORTS for SRV records.
3356 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3357 * because no ports are provided in the configuration file.
3358 * This is because HAProxy will use the port found into the SRV record.
3359 */
3360 srv->flags &= ~SRV_F_MAPPORTS;
3361 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003362
Frédéric Lécaille31694712017-08-01 08:47:19 +02003363 if (port_str)
3364 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003365 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003366
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003367 break;
3368 default:
3369 chunk_appendf(msg, ", version '%d' not supported", version);
3370 }
3371
3372 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003373 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003374 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003375 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003376 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003377 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003378}
3379
3380/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3381 * For each proxy, it does the following:
3382 * - opens its server state file (either one or local one)
3383 * - read whole file, line by line
3384 * - analyse each line to check if it matches our current backend:
3385 * - backend name matches
3386 * - backend id matches if id is forced and name doesn't match
3387 * - if the server pointed by the line is found, then state is applied
3388 *
3389 * If the running backend uuid or id differs from the state file, then HAProxy reports
3390 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003391 *
3392 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003393 */
3394void apply_server_state(void)
3395{
3396 char *cur, *end;
3397 char mybuf[SRV_STATE_LINE_MAXLEN];
3398 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003399 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3400 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmann95c2c012019-06-11 14:51:49 +02003401 int arg, srv_arg, version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003402 FILE *f;
3403 char *filepath;
3404 char globalfilepath[MAXPATHLEN + 1];
3405 char localfilepath[MAXPATHLEN + 1];
3406 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003407 struct proxy *curproxy, *bk;
3408 struct server *srv;
3409
3410 globalfilepathlen = 0;
3411 /* create the globalfilepath variable */
3412 if (global.server_state_file) {
3413 /* absolute path or no base directory provided */
3414 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3415 len = strlen(global.server_state_file);
3416 if (len > MAXPATHLEN) {
3417 globalfilepathlen = 0;
3418 goto globalfileerror;
3419 }
3420 memcpy(globalfilepath, global.server_state_file, len);
3421 globalfilepath[len] = '\0';
3422 globalfilepathlen = len;
3423 }
3424 else if (global.server_state_base) {
3425 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003426 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003427 globalfilepathlen = 0;
3428 goto globalfileerror;
3429 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003430 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003431 globalfilepath[len] = 0;
3432 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003433
3434 /* append a slash if needed */
3435 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3436 if (globalfilepathlen + 1 > MAXPATHLEN) {
3437 globalfilepathlen = 0;
3438 goto globalfileerror;
3439 }
3440 globalfilepath[globalfilepathlen++] = '/';
3441 }
3442
3443 len = strlen(global.server_state_file);
3444 if (globalfilepathlen + len > MAXPATHLEN) {
3445 globalfilepathlen = 0;
3446 goto globalfileerror;
3447 }
3448 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3449 globalfilepathlen += len;
3450 globalfilepath[globalfilepathlen++] = 0;
3451 }
3452 }
3453 globalfileerror:
3454 if (globalfilepathlen == 0)
3455 globalfilepath[0] = '\0';
3456
3457 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003458 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003459 /* servers are only in backends */
3460 if (!(curproxy->cap & PR_CAP_BE))
3461 continue;
3462 fileopenerr = 0;
3463 filepath = NULL;
3464
3465 /* search server state file path and name */
3466 switch (curproxy->load_server_state_from_file) {
3467 /* read servers state from global file */
3468 case PR_SRV_STATE_FILE_GLOBAL:
3469 /* there was an error while generating global server state file path */
3470 if (globalfilepathlen == 0)
3471 continue;
3472 filepath = globalfilepath;
3473 fileopenerr = 1;
3474 break;
3475 /* this backend has its own file */
3476 case PR_SRV_STATE_FILE_LOCAL:
3477 localfilepathlen = 0;
3478 localfilepath[0] = '\0';
3479 len = 0;
3480 /* create the localfilepath variable */
3481 /* absolute path or no base directory provided */
3482 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3483 len = strlen(curproxy->server_state_file_name);
3484 if (len > MAXPATHLEN) {
3485 localfilepathlen = 0;
3486 goto localfileerror;
3487 }
3488 memcpy(localfilepath, curproxy->server_state_file_name, len);
3489 localfilepath[len] = '\0';
3490 localfilepathlen = len;
3491 }
3492 else if (global.server_state_base) {
3493 len = strlen(global.server_state_base);
3494 localfilepathlen += len;
3495
3496 if (localfilepathlen > MAXPATHLEN) {
3497 localfilepathlen = 0;
3498 goto localfileerror;
3499 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003500 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003501 localfilepath[localfilepathlen] = 0;
3502
3503 /* append a slash if needed */
3504 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3505 if (localfilepathlen + 1 > MAXPATHLEN) {
3506 localfilepathlen = 0;
3507 goto localfileerror;
3508 }
3509 localfilepath[localfilepathlen++] = '/';
3510 }
3511
3512 len = strlen(curproxy->server_state_file_name);
3513 if (localfilepathlen + len > MAXPATHLEN) {
3514 localfilepathlen = 0;
3515 goto localfileerror;
3516 }
3517 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3518 localfilepathlen += len;
3519 localfilepath[localfilepathlen++] = 0;
3520 }
3521 filepath = localfilepath;
3522 localfileerror:
3523 if (localfilepathlen == 0)
3524 localfilepath[0] = '\0';
3525
3526 break;
3527 case PR_SRV_STATE_FILE_NONE:
3528 default:
3529 continue;
3530 }
3531
3532 /* preload global state file */
3533 errno = 0;
3534 f = fopen(filepath, "r");
3535 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003536 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003537 if (!f)
3538 continue;
3539
3540 mybuf[0] = '\0';
3541 mybuflen = 0;
3542 version = 0;
3543
3544 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003545 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003546 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003547 goto fileclose;
3548 }
3549
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003550 cur = mybuf;
3551 version = atoi(cur);
3552 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3553 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003554 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003555
3556 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3557 int bk_f_forced_id = 0;
3558 int check_id = 0;
3559 int check_name = 0;
3560
3561 mybuflen = strlen(mybuf);
3562 cur = mybuf;
3563 end = cur + mybuflen;
3564
3565 bk = NULL;
3566 srv = NULL;
3567
3568 /* we need at least one character */
3569 if (mybuflen == 0)
3570 continue;
3571
3572 /* ignore blank characters at the beginning of the line */
3573 while (isspace(*cur))
3574 ++cur;
3575
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003576 /* Ignore empty or commented lines */
3577 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003578 continue;
3579
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003580 /* truncated lines */
3581 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003582 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003583 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003584 }
3585
3586 /* Removes trailing '\n' */
3587 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003588
3589 /* we're now ready to move the line into *srv_params[] */
3590 params[0] = cur;
3591 arg = 1;
3592 srv_arg = 0;
3593 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3594 if (isspace(*cur)) {
3595 *cur = '\0';
3596 ++cur;
3597 while (isspace(*cur))
3598 ++cur;
3599 switch (version) {
3600 case 1:
3601 /*
3602 * srv_addr: params[4] => srv_params[0]
3603 * srv_op_state: params[5] => srv_params[1]
3604 * srv_admin_state: params[6] => srv_params[2]
3605 * srv_uweight: params[7] => srv_params[3]
3606 * srv_iweight: params[8] => srv_params[4]
3607 * srv_last_time_change: params[9] => srv_params[5]
3608 * srv_check_status: params[10] => srv_params[6]
3609 * srv_check_result: params[11] => srv_params[7]
3610 * srv_check_health: params[12] => srv_params[8]
3611 * srv_check_state: params[13] => srv_params[9]
3612 * srv_agent_state: params[14] => srv_params[10]
3613 * bk_f_forced_id: params[15] => srv_params[11]
3614 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003615 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003616 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003617 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003618 */
3619 if (arg >= 4) {
3620 srv_params[srv_arg] = cur;
3621 ++srv_arg;
3622 }
3623 break;
3624 }
3625
3626 params[arg] = cur;
3627 ++arg;
3628 }
3629 else {
3630 ++cur;
3631 }
3632 }
3633
3634 /* if line is incomplete line, then ignore it.
3635 * otherwise, update useful flags */
3636 switch (version) {
3637 case 1:
3638 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3639 continue;
3640 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3641 check_id = (atoi(params[0]) == curproxy->uuid);
3642 check_name = (strcmp(curproxy->id, params[1]) == 0);
3643 break;
3644 }
3645
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003646 bk = curproxy;
3647
3648 /* if backend can't be found, let's continue */
3649 if (!check_id && !check_name)
3650 continue;
3651 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003652 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 +02003653 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3654 }
3655 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003656 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 +02003657 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3658 /* if name doesn't match, we still want to update curproxy if the backend id
3659 * was forced in previous the previous configuration */
3660 if (!bk_f_forced_id)
3661 continue;
3662 }
3663
Baptiste Assmann95c2c012019-06-11 14:51:49 +02003664 /* look for the server by its name: param[3] */
3665 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003666
3667 if (!srv) {
3668 /* if no server found, then warning and continue with next line */
Baptiste Assmann95c2c012019-06-11 14:51:49 +02003669 ha_warning("can't find server '%s' in backend '%s'\n",
3670 params[3], params[1]);
3671 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3672 params[3], params[1]);
Frédéric Lécaille0bedb8a2017-06-15 14:09:10 +02003673 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003674 }
3675
3676 /* now we can proceed with server's state update */
3677 srv_update_state(srv, version, srv_params);
3678 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003679fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003680 fclose(f);
3681 }
3682}
3683
Simon Horman7d09b9a2013-02-12 10:45:51 +09003684/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003685 * update a server's current IP address.
3686 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3687 * ip is in network format.
3688 * updater is a string which contains an information about the requester of the update.
3689 * updater is used if not NULL.
3690 *
3691 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003692 *
3693 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003694 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003695int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003696{
3697 /* generates a log line and a warning on stderr */
3698 if (1) {
3699 /* book enough space for both IPv4 and IPv6 */
3700 char oldip[INET6_ADDRSTRLEN];
3701 char newip[INET6_ADDRSTRLEN];
3702
3703 memset(oldip, '\0', INET6_ADDRSTRLEN);
3704 memset(newip, '\0', INET6_ADDRSTRLEN);
3705
3706 /* copy old IP address in a string */
3707 switch (s->addr.ss_family) {
3708 case AF_INET:
3709 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3710 break;
3711 case AF_INET6:
3712 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3713 break;
3714 };
3715
3716 /* copy new IP address in a string */
3717 switch (ip_sin_family) {
3718 case AF_INET:
3719 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3720 break;
3721 case AF_INET6:
3722 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3723 break;
3724 };
3725
3726 /* save log line into a buffer */
3727 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3728 s->proxy->id, s->id, oldip, newip, updater);
3729
3730 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003731 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003732
3733 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003734 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003735 }
3736
3737 /* save the new IP family */
3738 s->addr.ss_family = ip_sin_family;
3739 /* save the new IP address */
3740 switch (ip_sin_family) {
3741 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003742 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003743 break;
3744 case AF_INET6:
3745 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3746 break;
3747 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003748 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003749
3750 return 0;
3751}
3752
3753/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003754 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3755 *
3756 * Caller can pass its name through <updater> to get it integrated in the response message
3757 * returned by the function.
3758 *
3759 * The function first does the following, in that order:
3760 * - validates the new addr and/or port
3761 * - checks if an update is required (new IP or port is different than current ones)
3762 * - checks the update is allowed:
3763 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3764 * - allow all changes if no CHECKS are configured
3765 * - if CHECK is configured:
3766 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3767 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3768 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003769 *
3770 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003771 */
3772const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3773{
3774 struct sockaddr_storage sa;
3775 int ret, port_change_required;
3776 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003777 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003778 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003779 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003780
3781 msg = get_trash_chunk();
3782 chunk_reset(msg);
3783
3784 if (addr) {
3785 memset(&sa, 0, sizeof(struct sockaddr_storage));
3786 if (str2ip2(addr, &sa, 0) == NULL) {
3787 chunk_printf(msg, "Invalid addr '%s'", addr);
3788 goto out;
3789 }
3790
3791 /* changes are allowed on AF_INET* families only */
3792 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3793 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3794 goto out;
3795 }
3796
3797 /* collecting data currently setup */
3798 memset(current_addr, '\0', sizeof(current_addr));
3799 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3800 /* changes are allowed on AF_INET* families only */
3801 if ((ret != AF_INET) && (ret != AF_INET6)) {
3802 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3803 goto out;
3804 }
3805
3806 /* applying ADDR changes if required and allowed
3807 * ipcmp returns 0 when both ADDR are the same
3808 */
3809 if (ipcmp(&s->addr, &sa) == 0) {
3810 chunk_appendf(msg, "no need to change the addr");
3811 goto port;
3812 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003813 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003814 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003815
3816 /* we also need to update check's ADDR only if it uses the server's one */
3817 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003818 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003819 }
3820
3821 /* we also need to update agent ADDR only if it use the server's one */
3822 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003823 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003824 }
3825
3826 /* update report for caller */
3827 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3828 }
3829
3830 port:
3831 if (port) {
3832 char sign = '\0';
3833 char *endptr;
3834
3835 if (addr)
3836 chunk_appendf(msg, ", ");
3837
3838 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003839 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003840
3841 /* check if PORT change is required */
3842 port_change_required = 0;
3843
3844 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003845 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003846 new_port = strtol(port, &endptr, 10);
3847 if ((errno != 0) || (port == endptr)) {
3848 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3849 goto out;
3850 }
3851
3852 /* check if caller triggers a port mapped or offset */
3853 if (sign == '-' || (sign == '+')) {
3854 /* check if server currently uses port map */
3855 if (!(s->flags & SRV_F_MAPPORTS)) {
3856 /* switch from fixed port to port map mandatorily triggers
3857 * a port change */
3858 port_change_required = 1;
3859 /* check is configured
3860 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3861 * prevent PORT change if check doesn't have it's dedicated port while switching
3862 * to port mapping */
3863 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3864 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.");
3865 goto out;
3866 }
3867 }
3868 /* we're already using port maps */
3869 else {
3870 port_change_required = current_port != new_port;
3871 }
3872 }
3873 /* fixed port */
3874 else {
3875 port_change_required = current_port != new_port;
3876 }
3877
3878 /* applying PORT changes if required and update response message */
3879 if (port_change_required) {
3880 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003881 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003882 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003883
3884 /* prepare message */
3885 chunk_appendf(msg, "port changed from '");
3886 if (s->flags & SRV_F_MAPPORTS)
3887 chunk_appendf(msg, "+");
3888 chunk_appendf(msg, "%d' to '", current_port);
3889
3890 if (sign == '-') {
3891 s->flags |= SRV_F_MAPPORTS;
3892 chunk_appendf(msg, "%c", sign);
3893 /* just use for result output */
3894 new_port = -new_port;
3895 }
3896 else if (sign == '+') {
3897 s->flags |= SRV_F_MAPPORTS;
3898 chunk_appendf(msg, "%c", sign);
3899 }
3900 else {
3901 s->flags &= ~SRV_F_MAPPORTS;
3902 }
3903
3904 chunk_appendf(msg, "%d'", new_port);
3905
3906 /* we also need to update health checks port only if it uses server's realport */
3907 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3908 s->check.port = new_port;
3909 }
3910 }
3911 else {
3912 chunk_appendf(msg, "no need to change the port");
3913 }
3914 }
3915
3916out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003917 if (changed)
3918 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003919 if (updater)
3920 chunk_appendf(msg, " by '%s'", updater);
3921 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003922 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003923}
3924
3925
3926/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003927 * update server status based on result of name resolution
3928 * returns:
3929 * 0 if server status is updated
3930 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003931 *
3932 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003933 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003934int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003935{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003936 struct dns_resolvers *resolvers = s->resolvers;
3937 struct dns_resolution *resolution = s->dns_requester->resolution;
3938 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003939
3940 switch (resolution->status) {
3941 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003942 /* status when HAProxy has just (re)started.
3943 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003944 break;
3945
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003946 case RSLV_STATUS_VALID:
3947 /*
3948 * resume health checks
3949 * server will be turned back on if health check is safe
3950 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003951 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003952 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003953 return 1;
3954 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3955 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003956 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003957 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003958
Emeric Brun52a91d32017-08-31 14:41:55 +02003959 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003960 return 1;
3961 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3962 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3963 s->proxy->id, s->id);
3964
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003965 ha_warning("%s.\n", trash.area);
3966 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003967 return 0;
3968
3969 case RSLV_STATUS_NX:
3970 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003971 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3972 if (!tick_is_expired(exp, now_ms))
3973 break;
3974
3975 if (s->next_admin & SRV_ADMF_RMAINT)
3976 return 1;
3977 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3978 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003979
3980 case RSLV_STATUS_TIMEOUT:
3981 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003982 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3983 if (!tick_is_expired(exp, now_ms))
3984 break;
3985
3986 if (s->next_admin & SRV_ADMF_RMAINT)
3987 return 1;
3988 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3989 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003990
3991 case RSLV_STATUS_REFUSED:
3992 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003993 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3994 if (!tick_is_expired(exp, now_ms))
3995 break;
3996
3997 if (s->next_admin & SRV_ADMF_RMAINT)
3998 return 1;
3999 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
4000 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004001
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004002 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004003 /* stop server if resolution failed for a long enough period */
4004 exp = tick_add(resolution->last_valid, resolvers->hold.other);
4005 if (!tick_is_expired(exp, now_ms))
4006 break;
4007
4008 if (s->next_admin & SRV_ADMF_RMAINT)
4009 return 1;
4010 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
4011 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004012 }
4013
4014 return 1;
4015}
4016
4017/*
4018 * Server Name Resolution valid response callback
4019 * It expects:
4020 * - <nameserver>: the name server which answered the valid response
4021 * - <response>: buffer containing a valid DNS response
4022 * - <response_len>: size of <response>
4023 * It performs the following actions:
4024 * - ignore response if current ip found and server family not met
4025 * - update with first new ip found if family is met and current IP is not found
4026 * returns:
4027 * 0 on error
4028 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01004029 *
4030 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004031 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004032int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004033{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004034 struct server *s = NULL;
4035 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004036 void *serverip, *firstip;
4037 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004038 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02004039 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004040 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004041
Christopher Faulet67957bd2017-09-27 11:00:59 +02004042 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004043 if (!s)
4044 return 1;
4045
Christopher Faulet67957bd2017-09-27 11:00:59 +02004046 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004047
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004048 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004049 firstip = NULL; /* pointer to the first valid response found */
4050 /* it will be used as the new IP if a change is required */
4051 firstip_sin_family = AF_UNSPEC;
4052 serverip = NULL; /* current server IP address */
4053
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004054 /* initializing server IP pointer */
4055 server_sin_family = s->addr.ss_family;
4056 switch (server_sin_family) {
4057 case AF_INET:
4058 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
4059 break;
4060
4061 case AF_INET6:
4062 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
4063 break;
4064
Willy Tarreau3acfcd12017-01-06 19:18:32 +01004065 case AF_UNSPEC:
4066 break;
4067
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004068 default:
4069 goto invalid;
4070 }
4071
Baptiste Assmann729c9012017-05-22 15:13:10 +02004072 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01004073 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004074 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004075
4076 switch (ret) {
4077 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004078 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004079
4080 case DNS_UPD_SRVIP_NOT_FOUND:
4081 goto save_ip;
4082
4083 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004084 goto invalid;
4085
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004086 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004087 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004088 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004089
Baptiste Assmannfad03182015-10-28 02:03:32 +01004090 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01004091 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004092 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004093 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01004094
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004095 default:
4096 goto invalid;
4097
4098 }
4099
4100 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004101 if (nameserver) {
4102 nameserver->counters.update++;
4103 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004104 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004105 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004106 else
4107 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004108 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004109
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004110 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004111 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004112 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004113
4114 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004115 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02004116 nameserver->counters.invalid++;
4117 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004118 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004119 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004120 return 0;
4121}
4122
4123/*
4124 * Server Name Resolution error management callback
4125 * returns:
4126 * 0 on error
4127 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004128 *
4129 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004130 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004131int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004132{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004133 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004134
Christopher Faulet67957bd2017-09-27 11:00:59 +02004135 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004136 if (!s)
4137 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004138 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004139 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004140 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004141 return 1;
4142}
4143
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004144/*
4145 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004146 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004147 * It returns a pointer to the first server found or NULL if <ip> is not yet
4148 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004149 *
4150 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004151 */
4152struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4153{
4154 struct server *tmpsrv;
4155 struct proxy *be;
4156
4157 if (!srv)
4158 return NULL;
4159
4160 be = srv->proxy;
4161 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004162 /* we found the current server is the same, ignore it */
4163 if (srv == tmpsrv)
4164 continue;
4165
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004166 /* We want to compare the IP in the record with the IP of the servers in the
4167 * same backend, only if:
4168 * * DNS resolution is enabled on the server
4169 * * the hostname used for the resolution by our server is the same than the
4170 * one used for the server found in the backend
4171 * * the server found in the backend is not our current server
4172 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004173 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004174 if ((tmpsrv->hostname_dn == NULL) ||
4175 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4176 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004177 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004178 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004179 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004180 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004181
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004182 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004183 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004184 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004185 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004186 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004187
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004188 /* At this point, we have 2 different servers using the same DNS hostname
4189 * for their respective resolution.
4190 */
4191 if (*ip_family == tmpsrv->addr.ss_family &&
4192 ((tmpsrv->addr.ss_family == AF_INET &&
4193 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4194 (tmpsrv->addr.ss_family == AF_INET6 &&
4195 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004196 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004197 return tmpsrv;
4198 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004199 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004200 }
4201
Emeric Brune9fd6b52017-11-02 17:20:39 +01004202
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004203 return NULL;
4204}
4205
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004206/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4207 * resolver. This is suited for initial address configuration. Returns 0 on
4208 * success otherwise a non-zero error code. In case of error, *err_code, if
4209 * not NULL, is filled up.
4210 */
4211int srv_set_addr_via_libc(struct server *srv, int *err_code)
4212{
4213 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004214 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004215 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004216 return 1;
4217 }
4218 return 0;
4219}
4220
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004221/* Set the server's FDQN (->hostname) from <hostname>.
4222 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004223 *
4224 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004225 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004226int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004227{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004228 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004229 char *hostname_dn;
4230 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004231
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004232 /* Note that the server lock is already held. */
4233 if (!srv->resolvers)
4234 return -1;
4235
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004236 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004237 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004238 /* run time DNS resolution was not active for this server
4239 * and we can't enable it at run time for now.
4240 */
4241 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004242 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004243
4244 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004245 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004246 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004247 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4248 hostname_dn, trash.size);
4249 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004250 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004251
Christopher Faulet67957bd2017-09-27 11:00:59 +02004252 resolution = srv->dns_requester->resolution;
4253 if (resolution &&
4254 resolution->hostname_dn &&
4255 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004256 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004257
Christopher Faulet67957bd2017-09-27 11:00:59 +02004258 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004259
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004260 free(srv->hostname);
4261 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004262 srv->hostname = strdup(hostname);
4263 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004264 srv->hostname_dn_len = hostname_dn_len;
4265 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004266 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004267
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004268 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004269 goto err;
4270
4271 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004272 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004273 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004274 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004275
4276 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004277 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004278 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004279 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004280}
4281
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004282/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4283 * from the state file. This is suited for initial address configuration.
4284 * Returns 0 on success otherwise a non-zero error code. In case of error,
4285 * *err_code, if not NULL, is filled up.
4286 */
4287static int srv_apply_lastaddr(struct server *srv, int *err_code)
4288{
4289 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4290 if (err_code)
4291 *err_code |= ERR_WARN;
4292 return 1;
4293 }
4294 return 0;
4295}
4296
Willy Tarreau25e51522016-11-04 15:10:17 +01004297/* returns 0 if no error, otherwise a combination of ERR_* flags */
4298static int srv_iterate_initaddr(struct server *srv)
4299{
4300 int return_code = 0;
4301 int err_code;
4302 unsigned int methods;
4303
4304 methods = srv->init_addr_methods;
4305 if (!methods) { // default to "last,libc"
4306 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4307 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4308 }
4309
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004310 /* "-dr" : always append "none" so that server addresses resolution
4311 * failures are silently ignored, this is convenient to validate some
4312 * configs out of their environment.
4313 */
4314 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4315 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4316
Willy Tarreau25e51522016-11-04 15:10:17 +01004317 while (methods) {
4318 err_code = 0;
4319 switch (srv_get_next_initaddr(&methods)) {
4320 case SRV_IADDR_LAST:
4321 if (!srv->lastaddr)
4322 continue;
4323 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004324 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004325 return_code |= err_code;
4326 break;
4327
4328 case SRV_IADDR_LIBC:
4329 if (!srv->hostname)
4330 continue;
4331 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004332 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004333 return_code |= err_code;
4334 break;
4335
Willy Tarreau37ebe122016-11-04 15:17:58 +01004336 case SRV_IADDR_NONE:
4337 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004338 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004339 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4340 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004341 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004342 return return_code;
4343
Willy Tarreau4310d362016-11-02 15:05:56 +01004344 case SRV_IADDR_IP:
4345 ipcpy(&srv->init_addr, &srv->addr);
4346 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004347 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4348 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004349 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004350 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004351
Willy Tarreau25e51522016-11-04 15:10:17 +01004352 default: /* unhandled method */
4353 break;
4354 }
4355 }
4356
4357 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004358 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004359 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4360 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004361 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004362 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004363 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4364 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004365
4366 return_code |= ERR_ALERT | ERR_FATAL;
4367 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004368out:
4369 srv_set_dyncookie(srv);
4370 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004371}
4372
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004373/*
4374 * This function parses all backends and all servers within each backend
4375 * and performs servers' addr resolution based on information provided by:
4376 * - configuration file
4377 * - server-state file (states provided by an 'old' haproxy process)
4378 *
4379 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4380 */
4381int srv_init_addr(void)
4382{
4383 struct proxy *curproxy;
4384 int return_code = 0;
4385
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004386 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004387 while (curproxy) {
4388 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004389
4390 /* servers are in backend only */
4391 if (!(curproxy->cap & PR_CAP_BE))
4392 goto srv_init_addr_next;
4393
Willy Tarreau25e51522016-11-04 15:10:17 +01004394 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004395 if (srv->hostname)
4396 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004397
4398 srv_init_addr_next:
4399 curproxy = curproxy->next;
4400 }
4401
4402 return return_code;
4403}
4404
Willy Tarreau46b7f532018-08-21 11:54:26 +02004405/*
4406 * Must be called with the server lock held.
4407 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004408const 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 +02004409{
4410
Willy Tarreau83061a82018-07-13 11:56:34 +02004411 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004412
4413 msg = get_trash_chunk();
4414 chunk_reset(msg);
4415
Olivier Houchard8da5f982017-08-04 18:35:36 +02004416 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004417 chunk_appendf(msg, "no need to change the FDQN");
4418 goto out;
4419 }
4420
4421 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4422 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4423 goto out;
4424 }
4425
4426 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4427 server->proxy->id, server->id, server->hostname, fqdn);
4428
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004429 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004430 chunk_reset(msg);
4431 chunk_appendf(msg, "could not update %s/%s FQDN",
4432 server->proxy->id, server->id);
4433 goto out;
4434 }
4435
4436 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004437 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004438
4439 out:
4440 if (updater)
4441 chunk_appendf(msg, " by '%s'", updater);
4442 chunk_appendf(msg, "\n");
4443
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004444 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004445}
4446
4447
Willy Tarreau21b069d2016-11-23 17:15:08 +01004448/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4449 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004450 * 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 +01004451 * used for CLI commands requiring a server name.
4452 * Important: the <arg> is modified to remove the '/'.
4453 */
4454struct server *cli_find_server(struct appctx *appctx, char *arg)
4455{
4456 struct proxy *px;
4457 struct server *sv;
4458 char *line;
4459
4460 /* split "backend/server" and make <line> point to server */
4461 for (line = arg; *line; line++)
4462 if (*line == '/') {
4463 *line++ = '\0';
4464 break;
4465 }
4466
4467 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004468 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004469 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004470 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004471 return NULL;
4472 }
4473
4474 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004475 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004476 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004477 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004478 return NULL;
4479 }
4480
4481 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004482 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004483 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004484 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004485 return NULL;
4486 }
4487
4488 return sv;
4489}
4490
William Lallemand222baf22016-11-19 02:00:33 +01004491
Willy Tarreau46b7f532018-08-21 11:54:26 +02004492/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004493static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004494{
4495 struct server *sv;
4496 const char *warning;
4497
4498 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4499 return 1;
4500
4501 sv = cli_find_server(appctx, args[2]);
4502 if (!sv)
4503 return 1;
4504
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004505 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004506
William Lallemand222baf22016-11-19 02:00:33 +01004507 if (strcmp(args[3], "weight") == 0) {
4508 warning = server_parse_weight_change_request(sv, args[4]);
4509 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004510 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004511 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004512 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004513 }
4514 }
4515 else if (strcmp(args[3], "state") == 0) {
4516 if (strcmp(args[4], "ready") == 0)
4517 srv_adm_set_ready(sv);
4518 else if (strcmp(args[4], "drain") == 0)
4519 srv_adm_set_drain(sv);
4520 else if (strcmp(args[4], "maint") == 0)
4521 srv_adm_set_maint(sv);
4522 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004523 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004524 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004525 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004526 }
4527 }
4528 else if (strcmp(args[3], "health") == 0) {
4529 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004530 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004531 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004532 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004533 }
4534 else if (strcmp(args[4], "up") == 0) {
4535 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004536 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004537 }
4538 else if (strcmp(args[4], "stopping") == 0) {
4539 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004540 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004541 }
4542 else if (strcmp(args[4], "down") == 0) {
4543 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004544 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004545 }
4546 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004547 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004548 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004549 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004550 }
4551 }
4552 else if (strcmp(args[3], "agent") == 0) {
4553 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004554 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004555 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004556 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004557 }
4558 else if (strcmp(args[4], "up") == 0) {
4559 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004560 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004561 }
4562 else if (strcmp(args[4], "down") == 0) {
4563 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004564 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004565 }
4566 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004567 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004568 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004569 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004570 }
4571 }
Misiek2da082d2017-01-09 09:40:42 +01004572 else if (strcmp(args[3], "agent-addr") == 0) {
4573 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004574 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004575 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4576 appctx->st0 = CLI_ST_PRINT;
4577 } else {
4578 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004579 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004580 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4581 appctx->st0 = CLI_ST_PRINT;
4582 }
4583 }
4584 }
4585 else if (strcmp(args[3], "agent-send") == 0) {
4586 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004587 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004588 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4589 appctx->st0 = CLI_ST_PRINT;
4590 } else {
4591 char *nss = strdup(args[4]);
4592 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004593 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004594 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4595 appctx->st0 = CLI_ST_PRINT;
4596 } else {
4597 free(sv->agent.send_string);
4598 sv->agent.send_string = nss;
4599 sv->agent.send_string_len = strlen(args[4]);
4600 }
4601 }
4602 }
William Lallemand222baf22016-11-19 02:00:33 +01004603 else if (strcmp(args[3], "check-port") == 0) {
4604 int i = 0;
4605 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004606 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004607 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004608 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004609 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004610 }
4611 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004612 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004613 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004614 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004615 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004616 }
4617 /* prevent the update of port to 0 if MAPPORTS are in use */
4618 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004619 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004620 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004621 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004622 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004623 }
4624 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004625 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004626 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004627 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004628 }
4629 else if (strcmp(args[3], "addr") == 0) {
4630 char *addr = NULL;
4631 char *port = NULL;
4632 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004633 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004634 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004635 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004636 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004637 }
4638 else {
4639 addr = args[4];
4640 }
4641 if (strcmp(args[5], "port") == 0) {
4642 port = args[6];
4643 }
4644 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4645 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004646 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004647 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004648 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004649 }
4650 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4651 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004652 else if (strcmp(args[3], "fqdn") == 0) {
4653 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004654 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004655 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4656 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004657 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004658 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004659 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004660 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004661 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004662 appctx->ctx.cli.msg = warning;
4663 appctx->st0 = CLI_ST_PRINT;
4664 }
4665 }
William Lallemand222baf22016-11-19 02:00:33 +01004666 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004667 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004668 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 +01004669 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004670 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004671 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004672 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004673 return 1;
4674}
4675
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004676static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004677{
4678 struct stream_interface *si = appctx->owner;
4679 struct proxy *px;
4680 struct server *sv;
4681 char *line;
4682
4683
4684 /* split "backend/server" and make <line> point to server */
4685 for (line = args[2]; *line; line++)
4686 if (*line == '/') {
4687 *line++ = '\0';
4688 break;
4689 }
4690
4691 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004692 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004693 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004694 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004695 return 1;
4696 }
4697
4698 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004699 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004700 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004701 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004702 return 1;
4703 }
4704
4705 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004706 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4707 sv->iweight);
4708 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004709 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004710 return 0;
4711 }
William Lallemand6b160942016-11-22 12:34:35 +01004712 return 1;
4713}
4714
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004715/* Parse a "set weight" command.
4716 *
4717 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004718 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004719static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004720{
4721 struct server *sv;
4722 const char *warning;
4723
4724 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4725 return 1;
4726
4727 sv = cli_find_server(appctx, args[2]);
4728 if (!sv)
4729 return 1;
4730
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004731 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4732
William Lallemand6b160942016-11-22 12:34:35 +01004733 warning = server_parse_weight_change_request(sv, args[3]);
4734 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004735 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004736 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004737 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004738 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004739
4740 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4741
William Lallemand6b160942016-11-22 12:34:35 +01004742 return 1;
4743}
4744
Willy Tarreau46b7f532018-08-21 11:54:26 +02004745/* parse a "set maxconn server" command. It always returns 1.
4746 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004747 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004748 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004749static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004750{
4751 struct server *sv;
4752 const char *warning;
4753
4754 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4755 return 1;
4756
4757 sv = cli_find_server(appctx, args[3]);
4758 if (!sv)
4759 return 1;
4760
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004761 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4762
Willy Tarreaub8026272016-11-23 11:26:56 +01004763 warning = server_parse_maxconn_change_request(sv, args[4]);
4764 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004765 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004766 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004767 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004768 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004769
4770 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4771
Willy Tarreaub8026272016-11-23 11:26:56 +01004772 return 1;
4773}
William Lallemand6b160942016-11-22 12:34:35 +01004774
Willy Tarreau46b7f532018-08-21 11:54:26 +02004775/* parse a "disable agent" command. It always returns 1.
4776 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004777 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004778 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004779static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004780{
4781 struct server *sv;
4782
4783 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4784 return 1;
4785
4786 sv = cli_find_server(appctx, args[2]);
4787 if (!sv)
4788 return 1;
4789
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004790 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004791 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004792 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004793 return 1;
4794}
4795
Willy Tarreau46b7f532018-08-21 11:54:26 +02004796/* parse a "disable health" command. It always returns 1.
4797 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004798 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004799 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004800static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004801{
4802 struct server *sv;
4803
4804 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4805 return 1;
4806
4807 sv = cli_find_server(appctx, args[2]);
4808 if (!sv)
4809 return 1;
4810
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004811 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004812 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004813 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004814 return 1;
4815}
4816
Willy Tarreau46b7f532018-08-21 11:54:26 +02004817/* parse a "disable server" command. It always returns 1.
4818 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004819 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004820 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004821static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004822{
4823 struct server *sv;
4824
4825 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4826 return 1;
4827
4828 sv = cli_find_server(appctx, args[2]);
4829 if (!sv)
4830 return 1;
4831
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004832 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004833 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004834 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004835 return 1;
4836}
4837
Willy Tarreau46b7f532018-08-21 11:54:26 +02004838/* parse a "enable agent" command. It always returns 1.
4839 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004840 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004841 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004842static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004843{
4844 struct server *sv;
4845
4846 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4847 return 1;
4848
4849 sv = cli_find_server(appctx, args[2]);
4850 if (!sv)
4851 return 1;
4852
4853 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004854 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004855 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004856 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004857 return 1;
4858 }
4859
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004860 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004861 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004862 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004863 return 1;
4864}
4865
Willy Tarreau46b7f532018-08-21 11:54:26 +02004866/* parse a "enable health" command. It always returns 1.
4867 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004868 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004869 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004870static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004871{
4872 struct server *sv;
4873
4874 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4875 return 1;
4876
4877 sv = cli_find_server(appctx, args[2]);
4878 if (!sv)
4879 return 1;
4880
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004881 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004882 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004883 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004884 return 1;
4885}
4886
Willy Tarreau46b7f532018-08-21 11:54:26 +02004887/* parse a "enable server" command. It always returns 1.
4888 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004889 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004890 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004891static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004892{
4893 struct server *sv;
4894
4895 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4896 return 1;
4897
4898 sv = cli_find_server(appctx, args[2]);
4899 if (!sv)
4900 return 1;
4901
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004902 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004903 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004904 if (!(sv->flags & SRV_F_COOKIESET)
4905 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4906 sv->cookie)
4907 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004908 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004909 return 1;
4910}
4911
William Lallemand222baf22016-11-19 02:00:33 +01004912/* register cli keywords */
4913static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004914 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004915 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004916 { { "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 +01004917 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004918 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004919 { { "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 +01004920 { { "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 +01004921 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004922 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4923 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4924
William Lallemand222baf22016-11-19 02:00:33 +01004925 {{},}
4926}};
4927
Willy Tarreau0108d902018-11-25 19:14:37 +01004928INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004929
Emeric Brun64cc49c2017-10-03 14:46:45 +02004930/*
4931 * This function applies server's status changes, it is
4932 * is designed to be called asynchronously.
4933 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004934 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004935 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004936static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004937{
4938 struct check *check = &s->check;
4939 int xferred;
4940 struct proxy *px = s->proxy;
4941 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4942 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4943 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004944 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004945
Emeric Brun64cc49c2017-10-03 14:46:45 +02004946 /* If currently main is not set we try to apply pending state changes */
4947 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4948 int next_admin;
4949
4950 /* Backup next admin */
4951 next_admin = s->next_admin;
4952
4953 /* restore current admin state */
4954 s->next_admin = s->cur_admin;
4955
4956 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4957 s->last_change = now.tv_sec;
4958 if (s->proxy->lbprm.set_server_status_down)
4959 s->proxy->lbprm.set_server_status_down(s);
4960
4961 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4962 srv_shutdown_streams(s, SF_ERR_DOWN);
4963
4964 /* we might have streams queued on this server and waiting for
4965 * a connection. Those which are redispatchable will be queued
4966 * to another server or to the proxy itself.
4967 */
4968 xferred = pendconn_redistribute(s);
4969
4970 tmptrash = alloc_trash_chunk();
4971 if (tmptrash) {
4972 chunk_printf(tmptrash,
4973 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4974 s->proxy->id, s->id);
4975
Emeric Brun5a133512017-10-19 14:42:30 +02004976 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004977 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004978
4979 /* we don't send an alert if the server was previously paused */
4980 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004981 send_log(s->proxy, log_level, "%s.\n",
4982 tmptrash->area);
4983 send_email_alert(s, log_level, "%s",
4984 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004985 free_trash_chunk(tmptrash);
4986 tmptrash = NULL;
4987 }
4988 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4989 set_backend_down(s->proxy);
4990
4991 s->counters.down_trans++;
4992 }
4993 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4994 s->last_change = now.tv_sec;
4995 if (s->proxy->lbprm.set_server_status_down)
4996 s->proxy->lbprm.set_server_status_down(s);
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 stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5008 s->proxy->id, s->id);
5009
Emeric Brun5a133512017-10-19 14:42:30 +02005010 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005011
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005012 ha_warning("%s.\n", tmptrash->area);
5013 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5014 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005015 free_trash_chunk(tmptrash);
5016 tmptrash = NULL;
5017 }
5018
5019 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5020 set_backend_down(s->proxy);
5021 }
5022 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5023 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
5024 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5025 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5026 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5027 s->proxy->last_change = now.tv_sec;
5028 }
5029
5030 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
5031 s->down_time += now.tv_sec - s->last_change;
5032
5033 s->last_change = now.tv_sec;
5034 if (s->next_state == SRV_ST_STARTING)
5035 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5036
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005037 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005038 /* now propagate the status change to any LB algorithms */
5039 if (px->lbprm.update_server_eweight)
5040 px->lbprm.update_server_eweight(s);
5041 else if (srv_willbe_usable(s)) {
5042 if (px->lbprm.set_server_status_up)
5043 px->lbprm.set_server_status_up(s);
5044 }
5045 else {
5046 if (px->lbprm.set_server_status_down)
5047 px->lbprm.set_server_status_down(s);
5048 }
5049
5050 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5051 * and it's not a backup server and its effective weight is > 0,
5052 * then it can accept new connections, so we shut down all streams
5053 * on all backup servers.
5054 */
5055 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5056 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5057 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5058
5059 /* check if we can handle some connections queued at the proxy. We
5060 * will take as many as we can handle.
5061 */
5062 xferred = pendconn_grab_from_px(s);
5063
5064 tmptrash = alloc_trash_chunk();
5065 if (tmptrash) {
5066 chunk_printf(tmptrash,
5067 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5068 s->proxy->id, s->id);
5069
Emeric Brun5a133512017-10-19 14:42:30 +02005070 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005071 ha_warning("%s.\n", tmptrash->area);
5072 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5073 tmptrash->area);
5074 send_email_alert(s, LOG_NOTICE, "%s",
5075 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005076 free_trash_chunk(tmptrash);
5077 tmptrash = NULL;
5078 }
5079
5080 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5081 set_backend_down(s->proxy);
5082 }
5083 else if (s->cur_eweight != s->next_eweight) {
5084 /* now propagate the status change to any LB algorithms */
5085 if (px->lbprm.update_server_eweight)
5086 px->lbprm.update_server_eweight(s);
5087 else if (srv_willbe_usable(s)) {
5088 if (px->lbprm.set_server_status_up)
5089 px->lbprm.set_server_status_up(s);
5090 }
5091 else {
5092 if (px->lbprm.set_server_status_down)
5093 px->lbprm.set_server_status_down(s);
5094 }
5095
5096 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5097 set_backend_down(s->proxy);
5098 }
5099
5100 s->next_admin = next_admin;
5101 }
5102
Emeric Brun5a133512017-10-19 14:42:30 +02005103 /* reset operational state change */
5104 *s->op_st_chg.reason = 0;
5105 s->op_st_chg.status = s->op_st_chg.code = -1;
5106 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005107
5108 /* Now we try to apply pending admin changes */
5109
5110 /* Maintenance must also disable health checks */
5111 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5112 if (s->check.state & CHK_ST_ENABLED) {
5113 s->check.state |= CHK_ST_PAUSED;
5114 check->health = 0;
5115 }
5116
5117 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005118 tmptrash = alloc_trash_chunk();
5119 if (tmptrash) {
5120 chunk_printf(tmptrash,
5121 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5122 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5123 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005124
Olivier Houchard796a2b32017-10-24 17:42:47 +02005125 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005126
Olivier Houchard796a2b32017-10-24 17:42:47 +02005127 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005128 ha_warning("%s.\n", tmptrash->area);
5129 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5130 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005131 }
5132 free_trash_chunk(tmptrash);
5133 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005134 }
Emeric Brun8f298292017-12-06 16:47:17 +01005135 /* commit new admin status */
5136
5137 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005138 }
5139 else { /* server was still running */
5140 check->health = 0; /* failure */
5141 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005142
5143 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005144 if (s->proxy->lbprm.set_server_status_down)
5145 s->proxy->lbprm.set_server_status_down(s);
5146
Emeric Brun64cc49c2017-10-03 14:46:45 +02005147 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5148 srv_shutdown_streams(s, SF_ERR_DOWN);
5149
5150 /* we might have streams queued on this server and waiting for
5151 * a connection. Those which are redispatchable will be queued
5152 * to another server or to the proxy itself.
5153 */
5154 xferred = pendconn_redistribute(s);
5155
5156 tmptrash = alloc_trash_chunk();
5157 if (tmptrash) {
5158 chunk_printf(tmptrash,
5159 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5160 s->flags & SRV_F_BACKUP ? "Backup " : "",
5161 s->proxy->id, s->id,
5162 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5163
5164 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5165
5166 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005167 ha_warning("%s.\n", tmptrash->area);
5168 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5169 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005170 }
5171 free_trash_chunk(tmptrash);
5172 tmptrash = NULL;
5173 }
5174 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5175 set_backend_down(s->proxy);
5176
5177 s->counters.down_trans++;
5178 }
5179 }
5180 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5181 /* OK here we're leaving maintenance, we have many things to check,
5182 * because the server might possibly be coming back up depending on
5183 * its state. In practice, leaving maintenance means that we should
5184 * immediately turn to UP (more or less the slowstart) under the
5185 * following conditions :
5186 * - server is neither checked nor tracked
5187 * - server tracks another server which is not checked
5188 * - server tracks another server which is already up
5189 * Which sums up as something simpler :
5190 * "either the tracking server is up or the server's checks are disabled
5191 * or up". Otherwise we only re-enable health checks. There's a special
5192 * case associated to the stopping state which can be inherited. Note
5193 * that the server might still be in drain mode, which is naturally dealt
5194 * with by the lower level functions.
5195 */
5196
5197 if (s->check.state & CHK_ST_ENABLED) {
5198 s->check.state &= ~CHK_ST_PAUSED;
5199 check->health = check->rise; /* start OK but check immediately */
5200 }
5201
5202 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5203 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5204 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5205 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5206 s->next_state = SRV_ST_STOPPING;
5207 }
5208 else {
5209 s->next_state = SRV_ST_STARTING;
5210 if (s->slowstart > 0)
5211 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5212 else
5213 s->next_state = SRV_ST_RUNNING;
5214 }
5215
5216 }
5217
5218 tmptrash = alloc_trash_chunk();
5219 if (tmptrash) {
5220 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5221 chunk_printf(tmptrash,
5222 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5223 s->flags & SRV_F_BACKUP ? "Backup " : "",
5224 s->proxy->id, s->id,
5225 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5226 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5227 }
5228 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5229 chunk_printf(tmptrash,
5230 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5231 s->flags & SRV_F_BACKUP ? "Backup " : "",
5232 s->proxy->id, s->id, s->hostname,
5233 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5234 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5235 }
5236 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5237 chunk_printf(tmptrash,
5238 "%sServer %s/%s is %s/%s (leaving maintenance)",
5239 s->flags & SRV_F_BACKUP ? "Backup " : "",
5240 s->proxy->id, s->id,
5241 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5242 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5243 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005244 ha_warning("%s.\n", tmptrash->area);
5245 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5246 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005247 free_trash_chunk(tmptrash);
5248 tmptrash = NULL;
5249 }
5250
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005251 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005252 /* now propagate the status change to any LB algorithms */
5253 if (px->lbprm.update_server_eweight)
5254 px->lbprm.update_server_eweight(s);
5255 else if (srv_willbe_usable(s)) {
5256 if (px->lbprm.set_server_status_up)
5257 px->lbprm.set_server_status_up(s);
5258 }
5259 else {
5260 if (px->lbprm.set_server_status_down)
5261 px->lbprm.set_server_status_down(s);
5262 }
5263
5264 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5265 set_backend_down(s->proxy);
5266
Willy Tarreau6a78e612018-08-07 10:14:53 +02005267 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5268 * and it's not a backup server and its effective weight is > 0,
5269 * then it can accept new connections, so we shut down all streams
5270 * on all backup servers.
5271 */
5272 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5273 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5274 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5275
5276 /* check if we can handle some connections queued at the proxy. We
5277 * will take as many as we can handle.
5278 */
5279 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005280 }
5281 else if (s->next_admin & SRV_ADMF_MAINT) {
5282 /* remaining in maintenance mode, let's inform precisely about the
5283 * situation.
5284 */
5285 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5286 tmptrash = alloc_trash_chunk();
5287 if (tmptrash) {
5288 chunk_printf(tmptrash,
5289 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5290 s->flags & SRV_F_BACKUP ? "Backup " : "",
5291 s->proxy->id, s->id);
5292
5293 if (s->track) /* normally it's mandatory here */
5294 chunk_appendf(tmptrash, " via %s/%s",
5295 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005296 ha_warning("%s.\n", tmptrash->area);
5297 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5298 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005299 free_trash_chunk(tmptrash);
5300 tmptrash = NULL;
5301 }
5302 }
5303 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5304 tmptrash = alloc_trash_chunk();
5305 if (tmptrash) {
5306 chunk_printf(tmptrash,
5307 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5308 s->flags & SRV_F_BACKUP ? "Backup " : "",
5309 s->proxy->id, s->id, s->hostname);
5310
5311 if (s->track) /* normally it's mandatory here */
5312 chunk_appendf(tmptrash, " via %s/%s",
5313 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005314 ha_warning("%s.\n", tmptrash->area);
5315 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5316 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005317 free_trash_chunk(tmptrash);
5318 tmptrash = NULL;
5319 }
5320 }
5321 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5322 tmptrash = alloc_trash_chunk();
5323 if (tmptrash) {
5324 chunk_printf(tmptrash,
5325 "%sServer %s/%s remains in forced maintenance",
5326 s->flags & SRV_F_BACKUP ? "Backup " : "",
5327 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005328 ha_warning("%s.\n", tmptrash->area);
5329 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5330 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005331 free_trash_chunk(tmptrash);
5332 tmptrash = NULL;
5333 }
5334 }
5335 /* don't report anything when leaving drain mode and remaining in maintenance */
5336
5337 s->cur_admin = s->next_admin;
5338 }
5339
5340 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5341 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5342 /* drain state is applied only if not yet in maint */
5343
5344 s->last_change = now.tv_sec;
5345 if (px->lbprm.set_server_status_down)
5346 px->lbprm.set_server_status_down(s);
5347
5348 /* we might have streams queued on this server and waiting for
5349 * a connection. Those which are redispatchable will be queued
5350 * to another server or to the proxy itself.
5351 */
5352 xferred = pendconn_redistribute(s);
5353
5354 tmptrash = alloc_trash_chunk();
5355 if (tmptrash) {
5356 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5357 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5358 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5359
5360 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5361
5362 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005363 ha_warning("%s.\n", tmptrash->area);
5364 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5365 tmptrash->area);
5366 send_email_alert(s, LOG_NOTICE, "%s",
5367 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005368 }
5369 free_trash_chunk(tmptrash);
5370 tmptrash = NULL;
5371 }
5372
5373 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5374 set_backend_down(s->proxy);
5375 }
5376 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5377 /* OK completely leaving drain mode */
5378 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5379 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5380 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5381 s->proxy->last_change = now.tv_sec;
5382 }
5383
5384 if (s->last_change < now.tv_sec) // ignore negative times
5385 s->down_time += now.tv_sec - s->last_change;
5386 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005387 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005388
5389 tmptrash = alloc_trash_chunk();
5390 if (tmptrash) {
5391 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5392 chunk_printf(tmptrash,
5393 "%sServer %s/%s is %s (leaving forced drain)",
5394 s->flags & SRV_F_BACKUP ? "Backup " : "",
5395 s->proxy->id, s->id,
5396 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5397 }
5398 else {
5399 chunk_printf(tmptrash,
5400 "%sServer %s/%s is %s (leaving drain)",
5401 s->flags & SRV_F_BACKUP ? "Backup " : "",
5402 s->proxy->id, s->id,
5403 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5404 if (s->track) /* normally it's mandatory here */
5405 chunk_appendf(tmptrash, " via %s/%s",
5406 s->track->proxy->id, s->track->id);
5407 }
5408
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005409 ha_warning("%s.\n", tmptrash->area);
5410 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5411 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005412 free_trash_chunk(tmptrash);
5413 tmptrash = NULL;
5414 }
5415
5416 /* now propagate the status change to any LB algorithms */
5417 if (px->lbprm.update_server_eweight)
5418 px->lbprm.update_server_eweight(s);
5419 else if (srv_willbe_usable(s)) {
5420 if (px->lbprm.set_server_status_up)
5421 px->lbprm.set_server_status_up(s);
5422 }
5423 else {
5424 if (px->lbprm.set_server_status_down)
5425 px->lbprm.set_server_status_down(s);
5426 }
5427 }
5428 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5429 /* remaining in drain mode after removing one of its flags */
5430
5431 tmptrash = alloc_trash_chunk();
5432 if (tmptrash) {
5433 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5434 chunk_printf(tmptrash,
5435 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5436 s->flags & SRV_F_BACKUP ? "Backup " : "",
5437 s->proxy->id, s->id);
5438
5439 if (s->track) /* normally it's mandatory here */
5440 chunk_appendf(tmptrash, " via %s/%s",
5441 s->track->proxy->id, s->track->id);
5442 }
5443 else {
5444 chunk_printf(tmptrash,
5445 "%sServer %s/%s remains in forced drain mode",
5446 s->flags & SRV_F_BACKUP ? "Backup " : "",
5447 s->proxy->id, s->id);
5448 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005449 ha_warning("%s.\n", tmptrash->area);
5450 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5451 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005452 free_trash_chunk(tmptrash);
5453 tmptrash = NULL;
5454 }
5455
5456 /* commit new admin status */
5457
5458 s->cur_admin = s->next_admin;
5459 }
5460 }
5461
5462 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005463 *s->adm_st_chg_cause = 0;
5464}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005465
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005466struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5467{
5468 struct connection *conn;
5469
5470 while ((conn = LIST_POP_LOCKED(&toremove_connections[tid],
5471 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005472 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005473 }
5474
5475 return task;
5476}
5477
Willy Tarreau980855b2019-02-07 14:59:29 +01005478struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005479{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005480 struct server *srv;
5481 struct eb32_node *eb;
5482 int i;
5483 unsigned int next_wakeup;
5484 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005485
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005486 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5487 while (1) {
5488 int srv_is_empty = 1;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005489
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005490 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5491 if (!eb) {
5492 /* we might have reached the end of the tree, typically because
5493 * <now_ms> is in the first half and we're first scanning the last
5494 * half. Let's loop back to the beginning of the tree now.
5495 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005496
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005497 eb = eb32_first(&idle_conn_srv);
5498 if (likely(!eb))
5499 break;
5500 }
5501 if (tick_is_lt(now_ms, eb->key)) {
5502 /* timer not expired yet, revisit it later */
5503 next_wakeup = eb->key;
5504 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005505 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005506 }
5507 srv = eb32_entry(eb, struct server, idle_node);
5508 for (i = 0; i < global.nbthread; i++) {
5509 int max_conn = (srv->curr_idle_thr[i] / 2) +
5510 (srv->curr_idle_thr[i] & 1);
5511 int j;
5512 int did_remove = 0;
5513
Olivier Houchardba936e52019-07-11 15:49:00 +02005514 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005515 for (j = 0; j < max_conn; j++) {
5516 struct connection *conn = LIST_POP_LOCKED(&srv->idle_orphan_conns[i], struct connection *, list);
5517 if (!conn)
5518 break;
5519 did_remove = 1;
5520 LIST_ADDQ_LOCKED(&toremove_connections[i], &conn->list);
5521 }
Olivier Houchardba936e52019-07-11 15:49:00 +02005522 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005523 if (did_remove && max_conn < srv->curr_idle_thr[i])
5524 srv_is_empty = 0;
5525 if (did_remove)
5526 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5527 }
5528 eb32_delete(&srv->idle_node);
5529 if (!srv_is_empty) {
5530 /* There are still more idle connections, add the
5531 * server back in the tree.
5532 */
5533 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5534 now_ms);
5535 eb32_insert(&idle_conn_srv, &srv->idle_node);
5536 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005537 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005538 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5539
5540 if (need_wakeup)
5541 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005542 else
5543 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005544
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005545 return task;
5546}
Olivier Houchard88698d92019-04-16 19:07:22 +02005547
5548/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5549static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5550 struct proxy *defpx, const char *file, int line,
5551 char **err)
5552{
5553 int arg = -1;
5554
5555 if (too_many_args(1, args, err, NULL))
5556 return -1;
5557
5558 if (*(args[1]) != 0)
5559 arg = atoi(args[1]);
5560
5561 if (arg < 0 || arg > 100) {
5562 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5563 return -1;
5564 }
5565
5566 if (args[0][10] == 'h')
5567 global.tune.pool_high_ratio = arg;
5568 else
5569 global.tune.pool_low_ratio = arg;
5570 return 0;
5571}
5572
5573/* config keyword parsers */
5574static struct cfg_kw_list cfg_kws = {ILH, {
5575 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5576 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5577 { 0, NULL, NULL }
5578}};
5579
5580INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5581
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005582/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005583 * Local variables:
5584 * c-indent-level: 8
5585 * c-basic-offset: 8
5586 * End:
5587 */