blob: a96f1ef663086aa46ad2cf232715581f4d56142a [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/*
128 * Must be called with the server lock held.
129 */
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
140 if ((s->flags & SRV_F_COOKIESET) ||
141 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
142 s->proxy->dyncookie_key == NULL)
143 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100144 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100145
146 if (s->addr.ss_family != AF_INET &&
147 s->addr.ss_family != AF_INET6)
148 return;
149 /*
150 * Buffer to calculate the cookie value.
151 * The buffer contains the secret key + the server IP address
152 * + the TCP port.
153 */
154 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
155 /*
156 * The TCP port should use only 2 bytes, but is stored in
157 * an unsigned int in struct server, so let's use 4, to be
158 * on the safe side.
159 */
160 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200161 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100162 memcpy(tmpbuf, p->dyncookie_key, key_len);
163 memcpy(&(tmpbuf[key_len]),
164 s->addr.ss_family == AF_INET ?
165 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
166 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
167 addr_len);
168 /*
169 * Make sure it's the same across all the load balancers,
170 * no matter their endianness.
171 */
172 port = htonl(s->svc_port);
173 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
174 hash_value = XXH64(tmpbuf, buffer_len, 0);
175 memprintf(&s->cookie, "%016llx", hash_value);
176 if (!s->cookie)
177 return;
178 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100179
180 /* Don't bother checking if the dyncookie is duplicated if
181 * the server is marked as "disabled", maybe it doesn't have
182 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100183 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100184 if (!(s->next_admin & SRV_ADMF_FMAINT))
185 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100186}
187
Willy Tarreau21faa912012-10-10 08:27:36 +0200188/*
189 * Registers the server keyword list <kwl> as a list of valid keywords for next
190 * parsing sessions.
191 */
192void srv_register_keywords(struct srv_kw_list *kwl)
193{
194 LIST_ADDQ(&srv_keywords.list, &kwl->list);
195}
196
197/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
198 * keyword is found with a NULL ->parse() function, then an attempt is made to
199 * find one with a valid ->parse() function. This way it is possible to declare
200 * platform-dependant, known keywords as NULL, then only declare them as valid
201 * if some options are met. Note that if the requested keyword contains an
202 * opening parenthesis, everything from this point is ignored.
203 */
204struct srv_kw *srv_find_kw(const char *kw)
205{
206 int index;
207 const char *kwend;
208 struct srv_kw_list *kwl;
209 struct srv_kw *ret = NULL;
210
211 kwend = strchr(kw, '(');
212 if (!kwend)
213 kwend = kw + strlen(kw);
214
215 list_for_each_entry(kwl, &srv_keywords.list, list) {
216 for (index = 0; kwl->kw[index].kw != NULL; index++) {
217 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
218 kwl->kw[index].kw[kwend-kw] == 0) {
219 if (kwl->kw[index].parse)
220 return &kwl->kw[index]; /* found it !*/
221 else
222 ret = &kwl->kw[index]; /* may be OK */
223 }
224 }
225 }
226 return ret;
227}
228
229/* Dumps all registered "server" keywords to the <out> string pointer. The
230 * unsupported keywords are only dumped if their supported form was not
231 * found.
232 */
233void srv_dump_kws(char **out)
234{
235 struct srv_kw_list *kwl;
236 int index;
237
238 *out = NULL;
239 list_for_each_entry(kwl, &srv_keywords.list, list) {
240 for (index = 0; kwl->kw[index].kw != NULL; index++) {
241 if (kwl->kw[index].parse ||
242 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
243 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
244 kwl->scope,
245 kwl->kw[index].kw,
246 kwl->kw[index].skip ? " <arg>" : "",
247 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
248 kwl->kw[index].parse ? "" : " (not supported)");
249 }
250 }
251 }
252}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100253
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100254/* Parse the "addr" server keyword */
255static int srv_parse_addr(char **args, int *cur_arg,
256 struct proxy *curproxy, struct server *newsrv, char **err)
257{
258 char *errmsg, *arg;
259 struct sockaddr_storage *sk;
260 int port1, port2;
261 struct protocol *proto;
262
263 errmsg = NULL;
264 arg = args[*cur_arg + 1];
265
266 if (!*arg) {
267 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
268 goto err;
269 }
270
271 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
272 if (!sk) {
273 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
274 goto err;
275 }
276
277 proto = protocol_by_family(sk->ss_family);
278 if (!proto || !proto->connect) {
279 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
280 args[*cur_arg], arg);
281 goto err;
282 }
283
284 if (port1 != port2) {
285 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
286 args[*cur_arg], arg);
287 goto err;
288 }
289
290 newsrv->check.addr = newsrv->agent.addr = *sk;
291 newsrv->flags |= SRV_F_CHECKADDR;
292 newsrv->flags |= SRV_F_AGENTADDR;
293
294 return 0;
295
296 err:
297 free(errmsg);
298 return ERR_ALERT | ERR_FATAL;
299}
300
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100301/* Parse the "agent-check" server keyword */
302static int srv_parse_agent_check(char **args, int *cur_arg,
303 struct proxy *curproxy, struct server *newsrv, char **err)
304{
305 newsrv->do_agent = 1;
306 return 0;
307}
308
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100309/* Parse the "backup" server keyword */
310static int srv_parse_backup(char **args, int *cur_arg,
311 struct proxy *curproxy, struct server *newsrv, char **err)
312{
313 newsrv->flags |= SRV_F_BACKUP;
314 return 0;
315}
316
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100317/* Parse the "check" server keyword */
318static int srv_parse_check(char **args, int *cur_arg,
319 struct proxy *curproxy, struct server *newsrv, char **err)
320{
321 newsrv->do_check = 1;
322 return 0;
323}
324
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100325/* Parse the "check-send-proxy" server keyword */
326static int srv_parse_check_send_proxy(char **args, int *cur_arg,
327 struct proxy *curproxy, struct server *newsrv, char **err)
328{
329 newsrv->check.send_proxy = 1;
330 return 0;
331}
332
Alexander Liu2a54bb72019-05-22 19:44:48 +0800333/* Parse the "check-via-socks4" server keyword */
334static int srv_parse_check_via_socks4(char **args, int *cur_arg,
335 struct proxy *curproxy, struct server *newsrv, char **err)
336{
337 newsrv->check.via_socks4 = 1;
338 return 0;
339}
340
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100341/* Parse the "cookie" server keyword */
342static int srv_parse_cookie(char **args, int *cur_arg,
343 struct proxy *curproxy, struct server *newsrv, char **err)
344{
345 char *arg;
346
347 arg = args[*cur_arg + 1];
348 if (!*arg) {
349 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
350 return ERR_ALERT | ERR_FATAL;
351 }
352
353 free(newsrv->cookie);
354 newsrv->cookie = strdup(arg);
355 newsrv->cklen = strlen(arg);
356 newsrv->flags |= SRV_F_COOKIESET;
357 return 0;
358}
359
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100360/* Parse the "disabled" server keyword */
361static int srv_parse_disabled(char **args, int *cur_arg,
362 struct proxy *curproxy, struct server *newsrv, char **err)
363{
Emeric Brun52a91d32017-08-31 14:41:55 +0200364 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
365 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100366 newsrv->check.state |= CHK_ST_PAUSED;
367 newsrv->check.health = 0;
368 return 0;
369}
370
371/* Parse the "enabled" server keyword */
372static int srv_parse_enabled(char **args, int *cur_arg,
373 struct proxy *curproxy, struct server *newsrv, char **err)
374{
Emeric Brun52a91d32017-08-31 14:41:55 +0200375 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
376 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100377 newsrv->check.state &= ~CHK_ST_PAUSED;
378 newsrv->check.health = newsrv->check.rise;
379 return 0;
380}
381
Willy Tarreau9c538e02019-01-23 10:21:49 +0100382static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
383{
384 char *arg;
385
386 arg = args[*cur_arg + 1];
387 if (!*arg) {
388 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
389 return ERR_ALERT | ERR_FATAL;
390 }
391 newsrv->max_reuse = atoi(arg);
392
393 return 0;
394}
395
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100396static 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 +0100397{
398 const char *res;
399 char *arg;
400 unsigned int time;
401
402 arg = args[*cur_arg + 1];
403 if (!*arg) {
404 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
405 return ERR_ALERT | ERR_FATAL;
406 }
407 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200408 if (res == PARSE_TIME_OVER) {
409 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
410 args[*cur_arg+1], args[*cur_arg]);
411 return ERR_ALERT | ERR_FATAL;
412 }
413 else if (res == PARSE_TIME_UNDER) {
414 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
415 args[*cur_arg+1], args[*cur_arg]);
416 return ERR_ALERT | ERR_FATAL;
417 }
418 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100419 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
420 *res, args[*cur_arg]);
421 return ERR_ALERT | ERR_FATAL;
422 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100423 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100424
425 return 0;
426}
427
Olivier Houchard006e3102018-12-10 18:30:32 +0100428static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
429{
430 char *arg;
431
432 arg = args[*cur_arg + 1];
433 if (!*arg) {
434 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
435 return ERR_ALERT | ERR_FATAL;
436 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100437
Olivier Houchard006e3102018-12-10 18:30:32 +0100438 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100439 if ((int)newsrv->max_idle_conns < -1) {
440 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
441 return ERR_ALERT | ERR_FATAL;
442 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100443
444 return 0;
445}
446
Willy Tarreaudff55432012-10-10 17:51:05 +0200447/* parse the "id" server keyword */
448static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
449{
450 struct eb32_node *node;
451
452 if (!*args[*cur_arg + 1]) {
453 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
454 return ERR_ALERT | ERR_FATAL;
455 }
456
457 newsrv->puid = atol(args[*cur_arg + 1]);
458 newsrv->conf.id.key = newsrv->puid;
459
460 if (newsrv->puid <= 0) {
461 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
462 return ERR_ALERT | ERR_FATAL;
463 }
464
465 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
466 if (node) {
467 struct server *target = container_of(node, struct server, conf.id);
468 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
469 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
470 target->id);
471 return ERR_ALERT | ERR_FATAL;
472 }
473
474 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200475 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200476 return 0;
477}
478
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100479/* Parse the "namespace" server keyword */
480static int srv_parse_namespace(char **args, int *cur_arg,
481 struct proxy *curproxy, struct server *newsrv, char **err)
482{
Willy Tarreaue5733232019-05-22 19:24:06 +0200483#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100484 char *arg;
485
486 arg = args[*cur_arg + 1];
487 if (!*arg) {
488 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
489 return ERR_ALERT | ERR_FATAL;
490 }
491
492 if (!strcmp(arg, "*")) {
493 /* Use the namespace associated with the connection (if present). */
494 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
495 return 0;
496 }
497
498 /*
499 * As this parser may be called several times for the same 'default-server'
500 * object, or for a new 'server' instance deriving from a 'default-server'
501 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
502 */
503 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
504
505 newsrv->netns = netns_store_lookup(arg, strlen(arg));
506 if (!newsrv->netns)
507 newsrv->netns = netns_store_insert(arg);
508
509 if (!newsrv->netns) {
510 memprintf(err, "Cannot open namespace '%s'", arg);
511 return ERR_ALERT | ERR_FATAL;
512 }
513
514 return 0;
515#else
516 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
517 return ERR_ALERT | ERR_FATAL;
518#endif
519}
520
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100521/* Parse the "no-agent-check" server keyword */
522static int srv_parse_no_agent_check(char **args, int *cur_arg,
523 struct proxy *curproxy, struct server *newsrv, char **err)
524{
525 free_check(&newsrv->agent);
526 newsrv->agent.inter = 0;
527 newsrv->agent.port = 0;
528 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
529 newsrv->do_agent = 0;
530 return 0;
531}
532
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100533/* Parse the "no-backup" server keyword */
534static int srv_parse_no_backup(char **args, int *cur_arg,
535 struct proxy *curproxy, struct server *newsrv, char **err)
536{
537 newsrv->flags &= ~SRV_F_BACKUP;
538 return 0;
539}
540
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100541/* Parse the "no-check" server keyword */
542static int srv_parse_no_check(char **args, int *cur_arg,
543 struct proxy *curproxy, struct server *newsrv, char **err)
544{
545 free_check(&newsrv->check);
546 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
547 newsrv->do_check = 0;
548 return 0;
549}
550
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100551/* Parse the "no-check-send-proxy" server keyword */
552static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
553 struct proxy *curproxy, struct server *newsrv, char **err)
554{
555 newsrv->check.send_proxy = 0;
556 return 0;
557}
558
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100559/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200560static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100561{
562 srv->pp_opts &= ~flags;
563 return 0;
564}
565
566/* Parse the "no-send-proxy" server keyword */
567static int srv_parse_no_send_proxy(char **args, int *cur_arg,
568 struct proxy *curproxy, struct server *newsrv, char **err)
569{
570 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
571}
572
573/* Parse the "no-send-proxy-v2" server keyword */
574static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
575 struct proxy *curproxy, struct server *newsrv, char **err)
576{
577 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
578}
579
Frédéric Lécailleaeeb1c92019-07-04 14:19:06 +0200580/* Parse the "no-tfo" server keyword */
581static int srv_parse_no_tfo(char **args, int *cur_arg,
582 struct proxy *curproxy, struct server *newsrv, char **err)
583{
584 newsrv->flags &= ~SRV_F_FASTOPEN;
585 return 0;
586}
587
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100588/* Parse the "non-stick" server keyword */
589static int srv_parse_non_stick(char **args, int *cur_arg,
590 struct proxy *curproxy, struct server *newsrv, char **err)
591{
592 newsrv->flags |= SRV_F_NON_STICK;
593 return 0;
594}
595
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100596/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200597static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100598{
599 srv->pp_opts |= flags;
600 return 0;
601}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200602/* parse the "proto" server keyword */
603static int srv_parse_proto(char **args, int *cur_arg,
604 struct proxy *px, struct server *newsrv, char **err)
605{
606 struct ist proto;
607
608 if (!*args[*cur_arg + 1]) {
609 memprintf(err, "'%s' : missing value", args[*cur_arg]);
610 return ERR_ALERT | ERR_FATAL;
611 }
612 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
613 newsrv->mux_proto = get_mux_proto(proto);
614 if (!newsrv->mux_proto) {
615 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
616 return ERR_ALERT | ERR_FATAL;
617 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200618 return 0;
619}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100620
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100621/* parse the "proxy-v2-options" */
622static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
623 struct proxy *px, struct server *newsrv, char **err)
624{
625 char *p, *n;
626 for (p = args[*cur_arg+1]; p; p = n) {
627 n = strchr(p, ',');
628 if (n)
629 *n++ = '\0';
630 if (!strcmp(p, "ssl")) {
631 newsrv->pp_opts |= SRV_PP_V2_SSL;
632 } else if (!strcmp(p, "cert-cn")) {
633 newsrv->pp_opts |= SRV_PP_V2_SSL;
634 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100635 } else if (!strcmp(p, "cert-key")) {
636 newsrv->pp_opts |= SRV_PP_V2_SSL;
637 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
638 } else if (!strcmp(p, "cert-sig")) {
639 newsrv->pp_opts |= SRV_PP_V2_SSL;
640 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
641 } else if (!strcmp(p, "ssl-cipher")) {
642 newsrv->pp_opts |= SRV_PP_V2_SSL;
643 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100644 } else if (!strcmp(p, "authority")) {
645 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100646 } else if (!strcmp(p, "crc32c")) {
647 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100648 } else
649 goto fail;
650 }
651 return 0;
652 fail:
653 if (err)
654 memprintf(err, "'%s' : proxy v2 option not implemented", p);
655 return ERR_ALERT | ERR_FATAL;
656}
657
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100658/* Parse the "observe" server keyword */
659static int srv_parse_observe(char **args, int *cur_arg,
660 struct proxy *curproxy, struct server *newsrv, char **err)
661{
662 char *arg;
663
664 arg = args[*cur_arg + 1];
665 if (!*arg) {
666 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
667 return ERR_ALERT | ERR_FATAL;
668 }
669
670 if (!strcmp(arg, "none")) {
671 newsrv->observe = HANA_OBS_NONE;
672 }
673 else if (!strcmp(arg, "layer4")) {
674 newsrv->observe = HANA_OBS_LAYER4;
675 }
676 else if (!strcmp(arg, "layer7")) {
677 if (curproxy->mode != PR_MODE_HTTP) {
678 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
679 return ERR_ALERT;
680 }
681 newsrv->observe = HANA_OBS_LAYER7;
682 }
683 else {
684 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
685 "but got '%s'\n", args[*cur_arg], arg);
686 return ERR_ALERT | ERR_FATAL;
687 }
688
689 return 0;
690}
691
Frédéric Lécaille16186232017-03-14 16:42:49 +0100692/* Parse the "redir" server keyword */
693static int srv_parse_redir(char **args, int *cur_arg,
694 struct proxy *curproxy, struct server *newsrv, char **err)
695{
696 char *arg;
697
698 arg = args[*cur_arg + 1];
699 if (!*arg) {
700 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
701 return ERR_ALERT | ERR_FATAL;
702 }
703
704 free(newsrv->rdr_pfx);
705 newsrv->rdr_pfx = strdup(arg);
706 newsrv->rdr_len = strlen(arg);
707
708 return 0;
709}
710
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100711/* Parse the "send-proxy" server keyword */
712static int srv_parse_send_proxy(char **args, int *cur_arg,
713 struct proxy *curproxy, struct server *newsrv, char **err)
714{
715 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
716}
717
718/* Parse the "send-proxy-v2" server keyword */
719static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
720 struct proxy *curproxy, struct server *newsrv, char **err)
721{
722 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
723}
724
Frédéric Lécailledba97072017-03-17 15:33:50 +0100725
726/* Parse the "source" server keyword */
727static int srv_parse_source(char **args, int *cur_arg,
728 struct proxy *curproxy, struct server *newsrv, char **err)
729{
730 char *errmsg;
731 int port_low, port_high;
732 struct sockaddr_storage *sk;
733 struct protocol *proto;
734
735 errmsg = NULL;
736
737 if (!*args[*cur_arg + 1]) {
738 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
739 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
740 goto err;
741 }
742
743 /* 'sk' is statically allocated (no need to be freed). */
744 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
745 if (!sk) {
746 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
747 goto err;
748 }
749
750 proto = protocol_by_family(sk->ss_family);
751 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100752 ha_alert("'%s %s' : connect() not supported for this address family.\n",
753 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100754 goto err;
755 }
756
757 newsrv->conn_src.opts |= CO_SRC_BIND;
758 newsrv->conn_src.source_addr = *sk;
759
760 if (port_low != port_high) {
761 int i;
762
763 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100764 ha_alert("'%s' does not support port offsets (found '%s').\n",
765 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100766 goto err;
767 }
768
769 if (port_low <= 0 || port_low > 65535 ||
770 port_high <= 0 || port_high > 65535 ||
771 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100772 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 +0100773 goto err;
774 }
775 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
776 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
777 newsrv->conn_src.sport_range->ports[i] = port_low + i;
778 }
779
780 *cur_arg += 2;
781 while (*(args[*cur_arg])) {
782 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
783#if defined(CONFIG_HAP_TRANSPARENT)
784 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100785 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
786 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100787 goto err;
788 }
789 if (!strcmp(args[*cur_arg + 1], "client")) {
790 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
791 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
792 }
793 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
794 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
795 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
796 }
797 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
798 char *name, *end;
799
800 name = args[*cur_arg + 1] + 7;
801 while (isspace(*name))
802 name++;
803
804 end = name;
805 while (*end && !isspace(*end) && *end != ',' && *end != ')')
806 end++;
807
808 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
809 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
810 free(newsrv->conn_src.bind_hdr_name);
811 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
812 newsrv->conn_src.bind_hdr_len = end - name;
813 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
814 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
815 newsrv->conn_src.bind_hdr_occ = -1;
816
817 /* now look for an occurrence number */
818 while (isspace(*end))
819 end++;
820 if (*end == ',') {
821 end++;
822 name = end;
823 if (*end == '-')
824 end++;
825 while (isdigit((int)*end))
826 end++;
827 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
828 }
829
830 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100831 ha_alert("usesrc hdr_ip(name,num) does not support negative"
832 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100833 goto err;
834 }
835 }
836 else {
837 struct sockaddr_storage *sk;
838 int port1, port2;
839
840 /* 'sk' is statically allocated (no need to be freed). */
841 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
842 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100843 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100844 goto err;
845 }
846
847 proto = protocol_by_family(sk->ss_family);
848 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100849 ha_alert("'%s %s' : connect() not supported for this address family.\n",
850 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100851 goto err;
852 }
853
854 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100855 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
856 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100857 goto err;
858 }
859 newsrv->conn_src.tproxy_addr = *sk;
860 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
861 }
862 global.last_checks |= LSTCHK_NETADM;
863 *cur_arg += 2;
864 continue;
865#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100866 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 +0100867 goto err;
868#endif /* defined(CONFIG_HAP_TRANSPARENT) */
869 } /* "usesrc" */
870
871 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
872#ifdef SO_BINDTODEVICE
873 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100874 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100875 goto err;
876 }
877 free(newsrv->conn_src.iface_name);
878 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
879 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
880 global.last_checks |= LSTCHK_NETADM;
881#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100882 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100883 goto err;
884#endif
885 *cur_arg += 2;
886 continue;
887 }
888 /* this keyword in not an option of "source" */
889 break;
890 } /* while */
891
892 return 0;
893
894 err:
895 free(errmsg);
896 return ERR_ALERT | ERR_FATAL;
897}
898
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100899/* Parse the "stick" server keyword */
900static int srv_parse_stick(char **args, int *cur_arg,
901 struct proxy *curproxy, struct server *newsrv, char **err)
902{
903 newsrv->flags &= ~SRV_F_NON_STICK;
904 return 0;
905}
906
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100907/* Parse the "track" server keyword */
908static int srv_parse_track(char **args, int *cur_arg,
909 struct proxy *curproxy, struct server *newsrv, char **err)
910{
911 char *arg;
912
913 arg = args[*cur_arg + 1];
914 if (!*arg) {
915 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
916 return ERR_ALERT | ERR_FATAL;
917 }
918
919 free(newsrv->trackit);
920 newsrv->trackit = strdup(arg);
921
922 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800923}
924
925/* Parse the "socks4" server keyword */
926static int srv_parse_socks4(char **args, int *cur_arg,
927 struct proxy *curproxy, struct server *newsrv, char **err)
928{
929 char *errmsg;
930 int port_low, port_high;
931 struct sockaddr_storage *sk;
932 struct protocol *proto;
933
934 errmsg = NULL;
935
936 if (!*args[*cur_arg + 1]) {
937 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
938 goto err;
939 }
940
941 /* 'sk' is statically allocated (no need to be freed). */
942 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
943 if (!sk) {
944 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
945 goto err;
946 }
947
948 proto = protocol_by_family(sk->ss_family);
949 if (!proto || !proto->connect) {
950 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
951 goto err;
952 }
953
954 newsrv->flags |= SRV_F_SOCKS4_PROXY;
955 newsrv->socks4_addr = *sk;
956
957 if (port_low != port_high) {
958 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
959 goto err;
960 }
961
962 if (!port_low) {
963 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
964 goto err;
965 }
966
967 return 0;
968
969 err:
970 free(errmsg);
971 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100972}
973
Frédéric Lécailledba97072017-03-17 15:33:50 +0100974
Willy Tarreau034c88c2017-01-23 23:36:45 +0100975/* parse the "tfo" server keyword */
976static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
977{
978 newsrv->flags |= SRV_F_FASTOPEN;
979 return 0;
980}
981
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200982/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200983 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200984 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200985 *
986 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200987 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200988void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200989{
Willy Tarreau87b09662015-04-03 00:22:06 +0200990 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200991
Willy Tarreau87b09662015-04-03 00:22:06 +0200992 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
993 if (stream->srv_conn == srv)
994 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200995}
996
997/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200998 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200999 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001000 *
1001 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001002 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001003void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001004{
1005 struct server *srv;
1006
1007 for (srv = px->srv; srv != NULL; srv = srv->next)
1008 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001009 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001010}
1011
Willy Tarreaubda92272014-05-20 21:55:30 +02001012/* Appends some information to a message string related to a server going UP or
1013 * DOWN. If both <forced> and <reason> are null and the server tracks another
1014 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001015 * If <check> is non-null, an entire string describing the check result will be
1016 * appended after a comma and a space (eg: to report some information from the
1017 * check that changed the state). In the other case, the string will be built
1018 * using the check results stored into the struct server if present.
1019 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001020 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001021 *
1022 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001023 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001024void srv_append_status(struct buffer *msg, struct server *s,
1025 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001026{
Emeric Brun5a133512017-10-19 14:42:30 +02001027 short status = s->op_st_chg.status;
1028 short code = s->op_st_chg.code;
1029 long duration = s->op_st_chg.duration;
1030 char *desc = s->op_st_chg.reason;
1031
1032 if (check) {
1033 status = check->status;
1034 code = check->code;
1035 duration = check->duration;
1036 desc = check->desc;
1037 }
1038
1039 if (status != -1) {
1040 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1041
1042 if (status >= HCHK_STATUS_L57DATA)
1043 chunk_appendf(msg, ", code: %d", code);
1044
1045 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001046 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001047
1048 chunk_appendf(msg, ", info: \"");
1049
1050 chunk_initlen(&src, desc, 0, strlen(desc));
1051 chunk_asciiencode(msg, &src, '"');
1052
1053 chunk_appendf(msg, "\"");
1054 }
1055
1056 if (duration >= 0)
1057 chunk_appendf(msg, ", check duration: %ldms", duration);
1058 }
1059 else if (desc && *desc) {
1060 chunk_appendf(msg, ", %s", desc);
1061 }
1062 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001063 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001064 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001065
1066 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001067 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001068 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1069 " %d sessions active, %d requeued, %d remaining in queue",
1070 s->proxy->srv_act, s->proxy->srv_bck,
1071 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1072 s->cur_sess, xferred, s->nbpend);
1073 else
1074 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1075 " %d sessions requeued, %d total in queue",
1076 s->proxy->srv_act, s->proxy->srv_bck,
1077 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1078 xferred, s->nbpend);
1079 }
1080}
1081
Emeric Brun5a133512017-10-19 14:42:30 +02001082/* Marks server <s> down, regardless of its checks' statuses. The server is
1083 * registered in a list to postpone the counting of the remaining servers on
1084 * the proxy and transfers queued streams whenever possible to other servers at
1085 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1086 * non-null as the reason for going down or the available data from the check
1087 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001088 *
1089 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001090 */
Emeric Brun5a133512017-10-19 14:42:30 +02001091void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001092{
1093 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001094
Emeric Brun64cc49c2017-10-03 14:46:45 +02001095 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001096 return;
1097
Emeric Brun52a91d32017-08-31 14:41:55 +02001098 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001099 *s->op_st_chg.reason = 0;
1100 s->op_st_chg.status = -1;
1101 if (reason) {
1102 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1103 }
1104 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001105 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001106 s->op_st_chg.code = check->code;
1107 s->op_st_chg.status = check->status;
1108 s->op_st_chg.duration = check->duration;
1109 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001110
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001111 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001112 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001113
Emeric Brun9f0b4582017-10-23 14:39:51 +02001114 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001115 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001116 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001117 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001118 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001119}
1120
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001121/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001122 * in maintenance. The server is registered in a list to postpone the counting
1123 * of the remaining servers on the proxy and tries to grab requests from the
1124 * proxy at a sync point. Maintenance servers are ignored. It stores the
1125 * <reason> if non-null as the reason for going down or the available data
1126 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001127 *
1128 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001129 */
Emeric Brun5a133512017-10-19 14:42:30 +02001130void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001131{
1132 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001133
Emeric Brun64cc49c2017-10-03 14:46:45 +02001134 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001135 return;
1136
Emeric Brun52a91d32017-08-31 14:41:55 +02001137 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001138 return;
1139
Emeric Brun52a91d32017-08-31 14:41:55 +02001140 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001141 *s->op_st_chg.reason = 0;
1142 s->op_st_chg.status = -1;
1143 if (reason) {
1144 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1145 }
1146 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001147 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001148 s->op_st_chg.code = check->code;
1149 s->op_st_chg.status = check->status;
1150 s->op_st_chg.duration = check->duration;
1151 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001152
Emeric Brun64cc49c2017-10-03 14:46:45 +02001153 if (s->slowstart <= 0)
1154 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001155
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001156 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001157 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001158
Emeric Brun9f0b4582017-10-23 14:39:51 +02001159 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001160 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001161 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001162 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001163 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001164}
1165
Willy Tarreau8eb77842014-05-21 13:54:57 +02001166/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001167 * isn't in maintenance. The server is registered in a list to postpone the
1168 * counting of the remaining servers on the proxy and tries to grab requests
1169 * from the proxy. Maintenance servers are ignored. It stores the
1170 * <reason> if non-null as the reason for going down or the available data
1171 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001172 * up. Note that it makes use of the trash to build the log strings, so <reason>
1173 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001174 *
1175 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001176 */
Emeric Brun5a133512017-10-19 14:42:30 +02001177void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001178{
1179 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001180
Emeric Brun64cc49c2017-10-03 14:46:45 +02001181 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001182 return;
1183
Emeric Brun52a91d32017-08-31 14:41:55 +02001184 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001185 return;
1186
Emeric Brun52a91d32017-08-31 14:41:55 +02001187 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001188 *s->op_st_chg.reason = 0;
1189 s->op_st_chg.status = -1;
1190 if (reason) {
1191 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1192 }
1193 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001194 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001195 s->op_st_chg.code = check->code;
1196 s->op_st_chg.status = check->status;
1197 s->op_st_chg.duration = check->duration;
1198 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001199
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001200 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001201 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001202
Emeric Brun9f0b4582017-10-23 14:39:51 +02001203 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001204 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001205 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001206 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001207 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001208}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001209
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001210/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1211 * enforce either maint mode or drain mode. It is not allowed to set more than
1212 * one flag at once. The equivalent "inherited" flag is propagated to all
1213 * tracking servers. Maintenance mode disables health checks (but not agent
1214 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001215 * is done. If <cause> is non-null, it will be displayed at the end of the log
1216 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001217 *
1218 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001219 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001220void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001221{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001222 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001223
1224 if (!mode)
1225 return;
1226
1227 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001228 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001229 return;
1230
Emeric Brun52a91d32017-08-31 14:41:55 +02001231 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001232 if (cause)
1233 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1234
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001235 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001236 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001237
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001238 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001239 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1240 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001241 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001242
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001243 /* compute the inherited flag to propagate */
1244 if (mode & SRV_ADMF_MAINT)
1245 mode = SRV_ADMF_IMAINT;
1246 else if (mode & SRV_ADMF_DRAIN)
1247 mode = SRV_ADMF_IDRAIN;
1248
Emeric Brun9f0b4582017-10-23 14:39:51 +02001249 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001250 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001251 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001252 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001253 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001254}
1255
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001256/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1257 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1258 * than one flag at once. The equivalent "inherited" flag is propagated to all
1259 * tracking servers. Leaving maintenance mode re-enables health checks. When
1260 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001261 *
1262 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001263 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001264void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001265{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001266 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001267
1268 if (!mode)
1269 return;
1270
1271 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001272 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001273 return;
1274
Emeric Brun52a91d32017-08-31 14:41:55 +02001275 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001276
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001277 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001278 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001279
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001280 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001281 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1282 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001283 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001284
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001285 if (mode & SRV_ADMF_MAINT)
1286 mode = SRV_ADMF_IMAINT;
1287 else if (mode & SRV_ADMF_DRAIN)
1288 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001289
Emeric Brun9f0b4582017-10-23 14:39:51 +02001290 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001291 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001292 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001293 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001294 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001295}
1296
Willy Tarreau757478e2016-11-03 19:22:19 +01001297/* principle: propagate maint and drain to tracking servers. This is useful
1298 * upon startup so that inherited states are correct.
1299 */
1300static void srv_propagate_admin_state(struct server *srv)
1301{
1302 struct server *srv2;
1303
1304 if (!srv->trackers)
1305 return;
1306
1307 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001308 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001309 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001310 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001311
Emeric Brun52a91d32017-08-31 14:41:55 +02001312 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001313 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001314 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001315 }
1316}
1317
1318/* Compute and propagate the admin states for all servers in proxy <px>.
1319 * Only servers *not* tracking another one are considered, because other
1320 * ones will be handled when the server they track is visited.
1321 */
1322void srv_compute_all_admin_states(struct proxy *px)
1323{
1324 struct server *srv;
1325
1326 for (srv = px->srv; srv; srv = srv->next) {
1327 if (srv->track)
1328 continue;
1329 srv_propagate_admin_state(srv);
1330 }
1331}
1332
Willy Tarreaudff55432012-10-10 17:51:05 +02001333/* Note: must not be declared <const> as its list will be overwritten.
1334 * Please take care of keeping this list alphabetically sorted, doing so helps
1335 * all code contributors.
1336 * Optional keywords are also declared with a NULL ->parse() function so that
1337 * the config parser can report an appropriate error when a known keyword was
1338 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001339 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001340 */
1341static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001342 { "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 +01001343 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001344 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001345 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001346 { "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 +01001347 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001348 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1349 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001350 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001351 { "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 +01001352 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001353 { "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 +01001354 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001355 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001356 { "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 +01001357 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1358 { "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 +02001359 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001360 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001361 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001362 { "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 +01001363 { "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 +02001364 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001365 { "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 +01001366 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001367 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1368 { "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 +02001369 { "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 +01001370 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard1a9c25f2019-07-04 13:34:10 +02001371 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001372 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001373 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
1374 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* enable socks4 proxy for health checks */
Willy Tarreaudff55432012-10-10 17:51:05 +02001375 { NULL, NULL, 0 },
1376}};
1377
Willy Tarreau0108d902018-11-25 19:14:37 +01001378INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001379
Willy Tarreau004e0452013-11-21 11:22:01 +01001380/* Recomputes the server's eweight based on its state, uweight, the current time,
1381 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001382 * state is automatically disabled if the time is elapsed. If <must_update> is
1383 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001384 *
1385 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001386 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001387void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001388{
1389 struct proxy *px = sv->proxy;
1390 unsigned w;
1391
1392 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1393 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001394 if (sv->next_state == SRV_ST_STARTING)
1395 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001396 }
1397
1398 /* We must take care of not pushing the server to full throttle during slow starts.
1399 * It must also start immediately, at least at the minimal step when leaving maintenance.
1400 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001401 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001402 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1403 else
1404 w = px->lbprm.wdiv;
1405
Emeric Brun52a91d32017-08-31 14:41:55 +02001406 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001407
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001408 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001409 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001410 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001411}
1412
Willy Tarreaubaaee002006-06-26 02:48:02 +02001413/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001414 * Parses weight_str and configures sv accordingly.
1415 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001416 *
1417 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001418 */
1419const char *server_parse_weight_change_request(struct server *sv,
1420 const char *weight_str)
1421{
1422 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001423 long int w;
1424 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001425
1426 px = sv->proxy;
1427
1428 /* if the weight is terminated with '%', it is set relative to
1429 * the initial weight, otherwise it is absolute.
1430 */
1431 if (!*weight_str)
1432 return "Require <weight> or <weight%>.\n";
1433
Simon Hormanb796afa2013-02-12 10:45:53 +09001434 w = strtol(weight_str, &end, 10);
1435 if (end == weight_str)
1436 return "Empty weight string empty or preceded by garbage";
1437 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001438 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001439 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001440 /* Avoid integer overflow */
1441 if (w > 25600)
1442 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001443 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001444 if (w > 256)
1445 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001446 }
1447 else if (w < 0 || w > 256)
1448 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001449 else if (end[0] != '\0')
1450 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001451
1452 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1453 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1454
1455 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001456 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001457
1458 return NULL;
1459}
1460
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001461/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001462 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1463 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001464 * Returns:
1465 * - error string on error
1466 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001467 *
1468 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001469 */
1470const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001471 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001472{
1473 unsigned char ip[INET6_ADDRSTRLEN];
1474
1475 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001476 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001477 return NULL;
1478 }
1479 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001480 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001481 return NULL;
1482 }
1483
1484 return "Could not understand IP address format.\n";
1485}
1486
Willy Tarreau46b7f532018-08-21 11:54:26 +02001487/*
1488 * Must be called with the server lock held.
1489 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001490const char *server_parse_maxconn_change_request(struct server *sv,
1491 const char *maxconn_str)
1492{
1493 long int v;
1494 char *end;
1495
1496 if (!*maxconn_str)
1497 return "Require <maxconn>.\n";
1498
1499 v = strtol(maxconn_str, &end, 10);
1500 if (end == maxconn_str)
1501 return "maxconn string empty or preceded by garbage";
1502 else if (end[0] != '\0')
1503 return "Trailing garbage in maxconn string";
1504
1505 if (sv->maxconn == sv->minconn) { // static maxconn
1506 sv->maxconn = sv->minconn = v;
1507 } else { // dynamic maxconn
1508 sv->maxconn = v;
1509 }
1510
1511 if (may_dequeue_tasks(sv, sv->proxy))
1512 process_srv_queue(sv);
1513
1514 return NULL;
1515}
1516
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001517#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001518static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1519 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001520{
1521 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001522 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001523 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001524 NULL,
1525 };
1526
1527 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001528 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001529
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001530 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1531}
1532
1533static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1534{
1535 struct sample_expr *expr;
1536
1537 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 +01001538 if (!expr) {
1539 memprintf(err, "error detected while parsing sni expression : %s", *err);
1540 return ERR_ALERT | ERR_FATAL;
1541 }
1542
1543 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1544 memprintf(err, "error detected while parsing sni expression : "
1545 " fetch method '%s' extracts information from '%s', "
1546 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001547 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001548 return ERR_ALERT | ERR_FATAL;
1549 }
1550
1551 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1552 release_sample_expr(newsrv->ssl_ctx.sni);
1553 newsrv->ssl_ctx.sni = expr;
1554
1555 return 0;
1556}
1557#endif
1558
1559static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1560{
1561 if (err && *err) {
1562 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001563 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 +01001564 }
1565 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001566 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1567 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001568}
1569
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001570static void srv_conn_src_sport_range_cpy(struct server *srv,
1571 struct server *src)
1572{
1573 int range_sz;
1574
1575 range_sz = src->conn_src.sport_range->size;
1576 if (range_sz > 0) {
1577 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1578 if (srv->conn_src.sport_range != NULL) {
1579 int i;
1580
1581 for (i = 0; i < range_sz; i++) {
1582 srv->conn_src.sport_range->ports[i] =
1583 src->conn_src.sport_range->ports[i];
1584 }
1585 }
1586 }
1587}
1588
1589/*
1590 * Copy <src> server connection source settings to <srv> server everything needed.
1591 */
1592static void srv_conn_src_cpy(struct server *srv, struct server *src)
1593{
1594 srv->conn_src.opts = src->conn_src.opts;
1595 srv->conn_src.source_addr = src->conn_src.source_addr;
1596
1597 /* Source port range copy. */
1598 if (src->conn_src.sport_range != NULL)
1599 srv_conn_src_sport_range_cpy(srv, src);
1600
1601#ifdef CONFIG_HAP_TRANSPARENT
1602 if (src->conn_src.bind_hdr_name != NULL) {
1603 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1604 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1605 }
1606 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1607 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1608#endif
1609 if (src->conn_src.iface_name != NULL)
1610 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1611}
1612
1613/*
1614 * Copy <src> server SSL settings to <srv> server allocating
1615 * everything needed.
1616 */
1617#if defined(USE_OPENSSL)
1618static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1619{
1620 if (src->ssl_ctx.ca_file != NULL)
1621 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1622 if (src->ssl_ctx.crl_file != NULL)
1623 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1624 if (src->ssl_ctx.client_crt != NULL)
1625 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1626
1627 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1628
1629 if (src->ssl_ctx.verify_host != NULL)
1630 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1631 if (src->ssl_ctx.ciphers != NULL)
1632 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Willy Tarreau5db847a2019-05-09 14:13:35 +02001633#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001634 if (src->ssl_ctx.ciphersuites != NULL)
1635 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1636#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001637 if (src->sni_expr != NULL)
1638 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001639
1640#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1641 if (src->ssl_ctx.alpn_str) {
1642 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1643 if (srv->ssl_ctx.alpn_str) {
1644 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1645 src->ssl_ctx.alpn_len);
1646 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1647 }
1648 }
1649#endif
1650#ifdef OPENSSL_NPN_NEGOTIATED
1651 if (src->ssl_ctx.npn_str) {
1652 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1653 if (srv->ssl_ctx.npn_str) {
1654 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1655 src->ssl_ctx.npn_len);
1656 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1657 }
1658 }
1659#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001660}
1661#endif
1662
1663/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001664 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001665 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001666 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001667 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001668static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001669{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001670 char *hostname_dn;
1671 int hostname_len, hostname_dn_len;
1672
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001673 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001674 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001675
Christopher Faulet67957bd2017-09-27 11:00:59 +02001676 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001677 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001678 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1679 hostname_dn, trash.size);
1680 if (hostname_dn_len == -1)
1681 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001682
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001683
Christopher Faulet67957bd2017-09-27 11:00:59 +02001684 free(srv->hostname);
1685 free(srv->hostname_dn);
1686 srv->hostname = strdup(hostname);
1687 srv->hostname_dn = strdup(hostname_dn);
1688 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001689 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001690 goto err;
1691
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001692 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001693
1694 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001695 free(srv->hostname); srv->hostname = NULL;
1696 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001697 return -1;
1698}
1699
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001700/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001701 * Copy <src> server settings to <srv> server allocating
1702 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001703 * This function is not supposed to be called at any time, but only
1704 * during server settings parsing or during server allocations from
1705 * a server template, and just after having calloc()'ed a new server.
1706 * So, <src> may only be a default server (when parsing server settings)
1707 * or a server template (during server allocations from a server template).
1708 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1709 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001710 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001711static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001712{
1713 /* Connection source settings copy */
1714 srv_conn_src_cpy(srv, src);
1715
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001716 if (srv_tmpl) {
1717 srv->addr = src->addr;
1718 srv->svc_port = src->svc_port;
1719 }
1720
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001721 srv->pp_opts = src->pp_opts;
1722 if (src->rdr_pfx != NULL) {
1723 srv->rdr_pfx = strdup(src->rdr_pfx);
1724 srv->rdr_len = src->rdr_len;
1725 }
1726 if (src->cookie != NULL) {
1727 srv->cookie = strdup(src->cookie);
1728 srv->cklen = src->cklen;
1729 }
1730 srv->use_ssl = src->use_ssl;
1731 srv->check.addr = srv->agent.addr = src->check.addr;
1732 srv->check.use_ssl = src->check.use_ssl;
1733 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001734 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001735 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001736 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001737 /* Note: 'flags' field has potentially been already initialized. */
1738 srv->flags |= src->flags;
1739 srv->do_check = src->do_check;
1740 srv->do_agent = src->do_agent;
1741 if (srv->check.port)
1742 srv->flags |= SRV_F_CHECKPORT;
1743 srv->check.inter = src->check.inter;
1744 srv->check.fastinter = src->check.fastinter;
1745 srv->check.downinter = src->check.downinter;
1746 srv->agent.use_ssl = src->agent.use_ssl;
1747 srv->agent.port = src->agent.port;
1748 if (src->agent.send_string != NULL)
1749 srv->agent.send_string = strdup(src->agent.send_string);
1750 srv->agent.send_string_len = src->agent.send_string_len;
1751 srv->agent.inter = src->agent.inter;
1752 srv->agent.fastinter = src->agent.fastinter;
1753 srv->agent.downinter = src->agent.downinter;
1754 srv->maxqueue = src->maxqueue;
1755 srv->minconn = src->minconn;
1756 srv->maxconn = src->maxconn;
1757 srv->slowstart = src->slowstart;
1758 srv->observe = src->observe;
1759 srv->onerror = src->onerror;
1760 srv->onmarkeddown = src->onmarkeddown;
1761 srv->onmarkedup = src->onmarkedup;
1762 if (src->trackit != NULL)
1763 srv->trackit = strdup(src->trackit);
1764 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1765 srv->uweight = srv->iweight = src->iweight;
1766
1767 srv->check.send_proxy = src->check.send_proxy;
1768 /* health: up, but will fall down at first failure */
1769 srv->check.rise = srv->check.health = src->check.rise;
1770 srv->check.fall = src->check.fall;
1771
1772 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001773 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1774 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1775 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001776 srv->check.state |= CHK_ST_PAUSED;
1777 srv->check.health = 0;
1778 }
1779
1780 /* health: up but will fall down at first failure */
1781 srv->agent.rise = srv->agent.health = src->agent.rise;
1782 srv->agent.fall = src->agent.fall;
1783
1784 if (src->resolvers_id != NULL)
1785 srv->resolvers_id = strdup(src->resolvers_id);
1786 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001787 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001788 if (srv->dns_opts.family_prio == AF_UNSPEC)
1789 srv->dns_opts.family_prio = AF_INET6;
1790 memcpy(srv->dns_opts.pref_net,
1791 src->dns_opts.pref_net,
1792 sizeof srv->dns_opts.pref_net);
1793 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1794
1795 srv->init_addr_methods = src->init_addr_methods;
1796 srv->init_addr = src->init_addr;
1797#if defined(USE_OPENSSL)
1798 srv_ssl_settings_cpy(srv, src);
1799#endif
1800#ifdef TCP_USER_TIMEOUT
1801 srv->tcp_ut = src->tcp_ut;
1802#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001803 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001804 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001805 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001806 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001807
Olivier Houchard8da5f982017-08-04 18:35:36 +02001808 if (srv_tmpl)
1809 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001810
1811 srv->check.via_socks4 = src->check.via_socks4;
1812 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001813}
1814
William Lallemand313bfd12018-10-26 14:47:32 +02001815struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001816{
1817 struct server *srv;
1818
1819 srv = calloc(1, sizeof *srv);
1820 if (!srv)
1821 return NULL;
1822
1823 srv->obj_type = OBJ_TYPE_SERVER;
1824 srv->proxy = proxy;
1825 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001826 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001827
Emeric Brun52a91d32017-08-31 14:41:55 +02001828 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001829 srv->last_change = now.tv_sec;
1830
1831 srv->check.status = HCHK_STATUS_INI;
1832 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001833 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001834 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1835
1836 srv->agent.status = HCHK_STATUS_INI;
1837 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001838 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001839 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1840
Willy Tarreau975b1552019-06-06 16:25:55 +02001841 /* please don't put default server settings here, they are set in
1842 * init_default_instance().
1843 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001844 return srv;
1845}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001846
1847/*
1848 * Validate <srv> server health-check settings.
1849 * Returns 0 if everything is OK, -1 if not.
1850 */
1851static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1852{
1853 struct tcpcheck_rule *r = NULL;
1854 struct list *l;
1855
1856 /*
1857 * We need at least a service port, a check port or the first tcp-check rule must
1858 * be a 'connect' one when checking an IPv4/IPv6 server.
1859 */
1860 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1861 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1862 return 0;
1863
1864 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1865 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001866 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1867 "Check has been disabled.\n",
1868 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001869 return -1;
1870 }
1871
1872 /* search the first action (connect / send / expect) in the list */
1873 l = &srv->proxy->tcpcheck_rules;
1874 list_for_each_entry(r, l, list) {
1875 if (r->action != TCPCHK_ACT_COMMENT)
1876 break;
1877 }
1878
1879 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001880 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1881 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1882 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001883 return -1;
1884 }
1885
1886 /* scan the tcp-check ruleset to ensure a port has been configured */
1887 l = &srv->proxy->tcpcheck_rules;
1888 list_for_each_entry(r, l, list) {
1889 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001890 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1891 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1892 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001893 return -1;
1894 }
1895 }
1896
1897 return 0;
1898}
1899
1900/*
1901 * Initialize <srv> health-check structure.
1902 * Returns the error string in case of memory allocation failure, NULL if not.
1903 */
1904static const char *do_health_check_init(struct server *srv, int check_type, int state)
1905{
1906 const char *ret;
1907
1908 if (!srv->do_check)
1909 return NULL;
1910
1911 ret = init_check(&srv->check, check_type);
1912 if (ret)
1913 return ret;
1914
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001915 srv->check.state |= state;
1916 global.maxsock++;
1917
1918 return NULL;
1919}
1920
1921static int server_health_check_init(const char *file, int linenum,
1922 struct server *srv, struct proxy *curproxy)
1923{
1924 const char *ret;
1925
1926 if (!srv->do_check)
1927 return 0;
1928
1929 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001930 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1931 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001932 return ERR_ALERT | ERR_FATAL;
1933 }
1934
1935 if (server_healthcheck_validate(file, linenum, srv) < 0)
1936 return ERR_ALERT | ERR_ABORT;
1937
1938 /* note: check type will be set during the config review phase */
1939 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1940 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001941 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001942 return ERR_ALERT | ERR_ABORT;
1943 }
1944
1945 return 0;
1946}
1947
1948/*
1949 * Initialize <srv> agent check structure.
1950 * Returns the error string in case of memory allocation failure, NULL if not.
1951 */
1952static const char *do_server_agent_check_init(struct server *srv, int state)
1953{
1954 const char *ret;
1955
1956 if (!srv->do_agent)
1957 return NULL;
1958
1959 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1960 if (ret)
1961 return ret;
1962
1963 if (!srv->agent.inter)
1964 srv->agent.inter = srv->check.inter;
1965
1966 srv->agent.state |= state;
1967 global.maxsock++;
1968
1969 return NULL;
1970}
1971
1972static int server_agent_check_init(const char *file, int linenum,
1973 struct server *srv, struct proxy *curproxy)
1974{
1975 const char *ret;
1976
1977 if (!srv->do_agent)
1978 return 0;
1979
1980 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001981 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 +02001982 file, linenum, srv->id);
1983 return ERR_ALERT | ERR_FATAL;
1984 }
1985
1986 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1987 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001988 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001989 return ERR_ALERT | ERR_ABORT;
1990 }
1991
1992 return 0;
1993}
1994
1995#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1996static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1997 struct server *srv, struct proxy *proxy)
1998{
1999 int ret;
2000 char *err = NULL;
2001
2002 if (!srv->sni_expr)
2003 return 0;
2004
2005 ret = server_parse_sni_expr(srv, proxy, &err);
2006 if (!ret)
2007 return 0;
2008
2009 display_parser_err(file, linenum, args, cur_arg, &err);
2010 free(err);
2011
2012 return ret;
2013}
2014#endif
2015
2016/*
2017 * Server initializations finalization.
2018 * Initialize health check, agent check and SNI expression if enabled.
2019 * Must not be called for a default server instance.
2020 */
2021static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2022 struct server *srv, struct proxy *px)
2023{
2024 int ret;
2025
2026 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2027 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2028 return ret;
2029 }
2030
2031#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2032 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2033 return ret;
2034#endif
2035
2036 if (srv->flags & SRV_F_BACKUP)
2037 px->srv_bck++;
2038 else
2039 px->srv_act++;
2040 srv_lb_commit_status(srv);
2041
2042 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002043err:
2044 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002045}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002046
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002047/*
2048 * Parse as much as possible such a range string argument: low[-high]
2049 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2050 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2051 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2052 * Returns 0 if succeeded, -1 if not.
2053 */
2054static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2055{
2056 char *nb_high_arg;
2057
2058 *nb_high = 0;
2059 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002060 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002061
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002062 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002063 *nb_high_arg++ = '\0';
2064 *nb_high = atoi(nb_high_arg);
2065 }
2066 else {
2067 *nb_high += *nb_low;
2068 *nb_low = 1;
2069 }
2070
2071 if (*nb_low < 0 || *nb_high < *nb_low)
2072 return -1;
2073
2074 return 0;
2075}
2076
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002077static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2078{
2079 chunk_printf(&trash, "%s%d", prefix, nb);
2080 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002081 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002082}
2083
2084/*
2085 * Initialize as much as possible servers from <srv> server template.
2086 * Note that a server template is a special server with
2087 * a few different parameters than a server which has
2088 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002089 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002090 * 'srv' template included.
2091 */
2092static int server_template_init(struct server *srv, struct proxy *px)
2093{
2094 int i;
2095 struct server *newsrv;
2096
2097 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2098 int check_init_state;
2099 int agent_init_state;
2100
2101 newsrv = new_server(px);
2102 if (!newsrv)
2103 goto err;
2104
2105 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002106 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002107#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2108 if (newsrv->sni_expr) {
2109 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2110 if (!newsrv->ssl_ctx.sni)
2111 goto err;
2112 }
2113#endif
2114 /* Set this new server ID. */
2115 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2116
2117 /* Initial checks states. */
2118 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2119 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2120
2121 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2122 do_server_agent_check_init(newsrv, agent_init_state))
2123 goto err;
2124
2125 /* Linked backwards first. This will be restablished after parsing. */
2126 newsrv->next = px->srv;
2127 px->srv = newsrv;
2128 }
2129 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2130
2131 return i - srv->tmpl_info.nb_low;
2132
2133 err:
2134 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2135 if (newsrv) {
2136#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2137 release_sample_expr(newsrv->ssl_ctx.sni);
2138#endif
2139 free_check(&newsrv->agent);
2140 free_check(&newsrv->check);
2141 }
2142 free(newsrv);
2143 return i - srv->tmpl_info.nb_low;
2144}
2145
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002146int 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 +02002147{
2148 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002149 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002150 char *errmsg = NULL;
2151 int err_code = 0;
2152 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002153 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002154
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002155 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002156 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002157 !strcmp(args[0], "default-server") ||
2158 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002159 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002160 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002161 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002162 int srv_tmpl = !defsrv && !srv;
2163 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002164
2165 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002166 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002167 err_code |= ERR_ALERT | ERR_FATAL;
2168 goto out;
2169 }
2170 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002171 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002172
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002173 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002174 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002175 if (!*args[2]) {
2176 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002177 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002178 file, linenum, args[0]);
2179 err_code |= ERR_ALERT | ERR_FATAL;
2180 goto out;
2181 }
2182
2183 err = invalid_char(args[1]);
2184 }
2185 else if (srv_tmpl) {
2186 if (!*args[3]) {
2187 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002188 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 +02002189 file, linenum, args[0]);
2190 err_code |= ERR_ALERT | ERR_FATAL;
2191 goto out;
2192 }
2193
2194 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002195 }
2196
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002197 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002198 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 +02002199 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002200 err_code |= ERR_ALERT | ERR_FATAL;
2201 goto out;
2202 }
2203
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002204 cur_arg = 2;
2205 if (srv_tmpl) {
2206 /* Parse server-template <nb | range> arg. */
2207 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002208 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002209 file, linenum, args[0], args[cur_arg]);
2210 err_code |= ERR_ALERT | ERR_FATAL;
2211 goto out;
2212 }
2213 cur_arg++;
2214 }
2215
Willy Tarreau272adea2014-03-31 10:39:59 +02002216 if (!defsrv) {
2217 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002218 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002219 struct protocol *proto;
2220
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002221 newsrv = new_server(curproxy);
2222 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002223 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002224 err_code |= ERR_ALERT | ERR_ABORT;
2225 goto out;
2226 }
2227
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002228 if (srv_tmpl) {
2229 newsrv->tmpl_info.nb_low = tmpl_range_low;
2230 newsrv->tmpl_info.nb_high = tmpl_range_high;
2231 }
2232
Willy Tarreau272adea2014-03-31 10:39:59 +02002233 /* the servers are linked backwards first */
2234 newsrv->next = curproxy->srv;
2235 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002236 newsrv->conf.file = strdup(file);
2237 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002238 /* Note: for a server template, its id is its prefix.
2239 * This is a temporary id which will be used for server allocations to come
2240 * after parsing.
2241 */
2242 if (srv)
2243 newsrv->id = strdup(args[1]);
2244 else
2245 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002246
2247 /* several ways to check the port component :
2248 * - IP => port=+0, relative (IPv4 only)
2249 * - IP: => port=+0, relative
2250 * - IP:N => port=N, absolute
2251 * - IP:+N => port=+N, relative
2252 * - IP:-N => port=-N, relative
2253 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002254 if (!parse_addr)
2255 goto skip_addr;
2256
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002257 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002258 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002259 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002260 err_code |= ERR_ALERT | ERR_FATAL;
2261 goto out;
2262 }
2263
2264 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002265 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002266 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002267 file, linenum, args[0], args[1]);
2268 err_code |= ERR_ALERT | ERR_FATAL;
2269 goto out;
2270 }
2271
2272 if (!port1 || !port2) {
2273 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002274 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002275 }
2276 else if (port1 != port2) {
2277 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002278 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002279 file, linenum, args[0], args[1], args[2]);
2280 err_code |= ERR_ALERT | ERR_FATAL;
2281 goto out;
2282 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002283
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002284 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002285 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002286 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002287 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002288 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2289 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2290 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002291 err_code |= ERR_ALERT | ERR_FATAL;
2292 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002293 }
2294 }
2295 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002296 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002297 file, linenum, newsrv->id);
2298 err_code |= ERR_ALERT | ERR_FATAL;
2299 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002300 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002301 }
2302
Willy Tarreau272adea2014-03-31 10:39:59 +02002303 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002304 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002305
Olivier Houchard8da5f982017-08-04 18:35:36 +02002306 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002307 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002308 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002309 err_code |= ERR_ALERT | ERR_FATAL;
2310 goto out;
2311 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002312
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002313 cur_arg++;
2314 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002315 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002316 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002317 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002318 } else {
2319 newsrv = &curproxy->defsrv;
2320 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002321 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002322 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002323 }
2324
2325 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002326 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002327 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002328
2329 if (err == PARSE_TIME_OVER) {
2330 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2331 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2332 err_code |= ERR_ALERT | ERR_FATAL;
2333 goto out;
2334 }
2335 else if (err == PARSE_TIME_UNDER) {
2336 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2337 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2338 err_code |= ERR_ALERT | ERR_FATAL;
2339 goto out;
2340 }
2341 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002342 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002343 file, linenum, *err, newsrv->id);
2344 err_code |= ERR_ALERT | ERR_FATAL;
2345 goto out;
2346 }
2347 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002348 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002349 file, linenum, val, args[cur_arg], newsrv->id);
2350 err_code |= ERR_ALERT | ERR_FATAL;
2351 goto out;
2352 }
2353 newsrv->agent.inter = val;
2354 cur_arg += 2;
2355 }
Misiekea849332017-01-09 09:39:51 +01002356 else if (!strcmp(args[cur_arg], "agent-addr")) {
2357 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002358 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002359 goto out;
2360 }
2361
2362 cur_arg += 2;
2363 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002364 else if (!strcmp(args[cur_arg], "agent-port")) {
2365 global.maxsock++;
2366 newsrv->agent.port = atol(args[cur_arg + 1]);
2367 cur_arg += 2;
2368 }
James Brown55f9ff12015-10-21 18:19:05 -07002369 else if (!strcmp(args[cur_arg], "agent-send")) {
2370 global.maxsock++;
2371 free(newsrv->agent.send_string);
2372 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2373 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2374 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2375 cur_arg += 2;
2376 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002377 else if (!strcmp(args[cur_arg], "init-addr")) {
2378 char *p, *end;
2379 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002380 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002381
2382 newsrv->init_addr_methods = 0;
2383 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2384
2385 for (p = args[cur_arg + 1]; *p; p = end) {
2386 /* cut on next comma */
2387 for (end = p; *end && *end != ','; end++);
2388 if (*end)
2389 *(end++) = 0;
2390
Willy Tarreau4310d362016-11-02 15:05:56 +01002391 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002392 if (!strcmp(p, "libc")) {
2393 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2394 }
2395 else if (!strcmp(p, "last")) {
2396 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2397 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002398 else if (!strcmp(p, "none")) {
2399 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2400 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002401 else if (str2ip2(p, &sa, 0)) {
2402 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002403 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002404 file, linenum, args[cur_arg], p);
2405 err_code |= ERR_ALERT | ERR_FATAL;
2406 goto out;
2407 }
2408 newsrv->init_addr = sa;
2409 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2410 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002411 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002412 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 +02002413 file, linenum, args[cur_arg], p);
2414 err_code |= ERR_ALERT | ERR_FATAL;
2415 goto out;
2416 }
2417 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002418 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002419 file, linenum, args[cur_arg], p);
2420 err_code |= ERR_ALERT | ERR_FATAL;
2421 goto out;
2422 }
2423 }
2424 cur_arg += 2;
2425 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002426 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002427 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002428 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2429 cur_arg += 2;
2430 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002431 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2432 char *p, *end;
2433
2434 for (p = args[cur_arg + 1]; *p; p = end) {
2435 /* cut on next comma */
2436 for (end = p; *end && *end != ','; end++);
2437 if (*end)
2438 *(end++) = 0;
2439
2440 if (!strcmp(p, "allow-dup-ip")) {
2441 newsrv->dns_opts.accept_duplicate_ip = 1;
2442 }
2443 else if (!strcmp(p, "prevent-dup-ip")) {
2444 newsrv->dns_opts.accept_duplicate_ip = 0;
2445 }
2446 else {
2447 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2448 file, linenum, args[cur_arg], p);
2449 err_code |= ERR_ALERT | ERR_FATAL;
2450 goto out;
2451 }
2452 }
2453
2454 cur_arg += 2;
2455 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002456 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2457 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002458 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002459 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002460 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002461 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002462 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002463 file, linenum, args[cur_arg]);
2464 err_code |= ERR_ALERT | ERR_FATAL;
2465 goto out;
2466 }
2467 cur_arg += 2;
2468 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002469 else if (!strcmp(args[cur_arg], "resolve-net")) {
2470 char *p, *e;
2471 unsigned char mask;
2472 struct dns_options *opt;
2473
2474 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002475 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002476 file, linenum, args[cur_arg]);
2477 err_code |= ERR_ALERT | ERR_FATAL;
2478 goto out;
2479 }
2480
2481 opt = &newsrv->dns_opts;
2482
2483 /* Split arguments by comma, and convert it from ipv4 or ipv6
2484 * string network in in_addr or in6_addr.
2485 */
2486 p = args[cur_arg + 1];
2487 e = p;
2488 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002489 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002490 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002491 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002492 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2493 err_code |= ERR_ALERT | ERR_FATAL;
2494 goto out;
2495 }
2496 /* look for end or comma. */
2497 while (*e != ',' && *e != '\0')
2498 e++;
2499 if (*e == ',') {
2500 *e = '\0';
2501 e++;
2502 }
2503 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2504 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2505 /* Try to convert input string from ipv4 or ipv6 network. */
2506 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2507 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2508 &mask)) {
2509 /* Try to convert input string from ipv6 network. */
2510 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2511 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2512 } else {
2513 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002514 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002515 file, linenum, args[cur_arg], p);
2516 err_code |= ERR_ALERT | ERR_FATAL;
2517 goto out;
2518 }
2519 opt->pref_net_nb++;
2520 p = e;
2521 }
2522
2523 cur_arg += 2;
2524 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002525 else if (!strcmp(args[cur_arg], "rise")) {
2526 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002527 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002528 file, linenum, args[cur_arg]);
2529 err_code |= ERR_ALERT | ERR_FATAL;
2530 goto out;
2531 }
2532
2533 newsrv->check.rise = atol(args[cur_arg + 1]);
2534 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002535 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002536 file, linenum, args[cur_arg]);
2537 err_code |= ERR_ALERT | ERR_FATAL;
2538 goto out;
2539 }
2540
2541 if (newsrv->check.health)
2542 newsrv->check.health = newsrv->check.rise;
2543 cur_arg += 2;
2544 }
2545 else if (!strcmp(args[cur_arg], "fall")) {
2546 newsrv->check.fall = atol(args[cur_arg + 1]);
2547
2548 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002549 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002550 file, linenum, args[cur_arg]);
2551 err_code |= ERR_ALERT | ERR_FATAL;
2552 goto out;
2553 }
2554
2555 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002556 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002557 file, linenum, args[cur_arg]);
2558 err_code |= ERR_ALERT | ERR_FATAL;
2559 goto out;
2560 }
2561
2562 cur_arg += 2;
2563 }
2564 else if (!strcmp(args[cur_arg], "inter")) {
2565 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002566
2567 if (err == PARSE_TIME_OVER) {
2568 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2569 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2570 err_code |= ERR_ALERT | ERR_FATAL;
2571 goto out;
2572 }
2573 else if (err == PARSE_TIME_UNDER) {
2574 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2575 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2576 err_code |= ERR_ALERT | ERR_FATAL;
2577 goto out;
2578 }
2579 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002580 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002581 file, linenum, *err, newsrv->id);
2582 err_code |= ERR_ALERT | ERR_FATAL;
2583 goto out;
2584 }
2585 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002586 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002587 file, linenum, val, args[cur_arg], newsrv->id);
2588 err_code |= ERR_ALERT | ERR_FATAL;
2589 goto out;
2590 }
2591 newsrv->check.inter = val;
2592 cur_arg += 2;
2593 }
2594 else if (!strcmp(args[cur_arg], "fastinter")) {
2595 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002596
2597 if (err == PARSE_TIME_OVER) {
2598 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2599 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2600 err_code |= ERR_ALERT | ERR_FATAL;
2601 goto out;
2602 }
2603 else if (err == PARSE_TIME_UNDER) {
2604 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2605 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2606 err_code |= ERR_ALERT | ERR_FATAL;
2607 goto out;
2608 }
2609 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002610 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002611 file, linenum, *err, newsrv->id);
2612 err_code |= ERR_ALERT | ERR_FATAL;
2613 goto out;
2614 }
2615 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002616 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002617 file, linenum, val, args[cur_arg], newsrv->id);
2618 err_code |= ERR_ALERT | ERR_FATAL;
2619 goto out;
2620 }
2621 newsrv->check.fastinter = val;
2622 cur_arg += 2;
2623 }
2624 else if (!strcmp(args[cur_arg], "downinter")) {
2625 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002626
2627 if (err == PARSE_TIME_OVER) {
2628 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2629 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2630 err_code |= ERR_ALERT | ERR_FATAL;
2631 goto out;
2632 }
2633 else if (err == PARSE_TIME_UNDER) {
2634 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2635 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2636 err_code |= ERR_ALERT | ERR_FATAL;
2637 goto out;
2638 }
2639 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002640 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002641 file, linenum, *err, newsrv->id);
2642 err_code |= ERR_ALERT | ERR_FATAL;
2643 goto out;
2644 }
2645 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002646 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002647 file, linenum, val, args[cur_arg], newsrv->id);
2648 err_code |= ERR_ALERT | ERR_FATAL;
2649 goto out;
2650 }
2651 newsrv->check.downinter = val;
2652 cur_arg += 2;
2653 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002654 else if (!strcmp(args[cur_arg], "port")) {
2655 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002656 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002657 cur_arg += 2;
2658 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002659 else if (!strcmp(args[cur_arg], "weight")) {
2660 int w;
2661 w = atol(args[cur_arg + 1]);
2662 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002663 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002664 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2665 err_code |= ERR_ALERT | ERR_FATAL;
2666 goto out;
2667 }
2668 newsrv->uweight = newsrv->iweight = w;
2669 cur_arg += 2;
2670 }
2671 else if (!strcmp(args[cur_arg], "minconn")) {
2672 newsrv->minconn = atol(args[cur_arg + 1]);
2673 cur_arg += 2;
2674 }
2675 else if (!strcmp(args[cur_arg], "maxconn")) {
2676 newsrv->maxconn = atol(args[cur_arg + 1]);
2677 cur_arg += 2;
2678 }
2679 else if (!strcmp(args[cur_arg], "maxqueue")) {
2680 newsrv->maxqueue = atol(args[cur_arg + 1]);
2681 cur_arg += 2;
2682 }
2683 else if (!strcmp(args[cur_arg], "slowstart")) {
2684 /* slowstart is stored in seconds */
2685 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002686
2687 if (err == PARSE_TIME_OVER) {
2688 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2689 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2690 err_code |= ERR_ALERT | ERR_FATAL;
2691 goto out;
2692 }
2693 else if (err == PARSE_TIME_UNDER) {
2694 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2695 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2696 err_code |= ERR_ALERT | ERR_FATAL;
2697 goto out;
2698 }
2699 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002700 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002701 file, linenum, *err, newsrv->id);
2702 err_code |= ERR_ALERT | ERR_FATAL;
2703 goto out;
2704 }
2705 newsrv->slowstart = (val + 999) / 1000;
2706 cur_arg += 2;
2707 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002708 else if (!strcmp(args[cur_arg], "on-error")) {
2709 if (!strcmp(args[cur_arg + 1], "fastinter"))
2710 newsrv->onerror = HANA_ONERR_FASTINTER;
2711 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2712 newsrv->onerror = HANA_ONERR_FAILCHK;
2713 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2714 newsrv->onerror = HANA_ONERR_SUDDTH;
2715 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2716 newsrv->onerror = HANA_ONERR_MARKDWN;
2717 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002718 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002719 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2720 file, linenum, args[cur_arg], args[cur_arg + 1]);
2721 err_code |= ERR_ALERT | ERR_FATAL;
2722 goto out;
2723 }
2724
2725 cur_arg += 2;
2726 }
2727 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2728 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2729 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2730 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002731 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002732 file, linenum, args[cur_arg], args[cur_arg + 1]);
2733 err_code |= ERR_ALERT | ERR_FATAL;
2734 goto out;
2735 }
2736
2737 cur_arg += 2;
2738 }
2739 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2740 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2741 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2742 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002743 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002744 file, linenum, args[cur_arg], args[cur_arg + 1]);
2745 err_code |= ERR_ALERT | ERR_FATAL;
2746 goto out;
2747 }
2748
2749 cur_arg += 2;
2750 }
2751 else if (!strcmp(args[cur_arg], "error-limit")) {
2752 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002753 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002754 file, linenum, args[cur_arg]);
2755 err_code |= ERR_ALERT | ERR_FATAL;
2756 goto out;
2757 }
2758
2759 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2760
2761 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002762 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002763 file, linenum, args[cur_arg]);
2764 err_code |= ERR_ALERT | ERR_FATAL;
2765 goto out;
2766 }
2767 cur_arg += 2;
2768 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002769 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002770 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002771 file, linenum, "usesrc", "source");
2772 err_code |= ERR_ALERT | ERR_FATAL;
2773 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002774 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002775 else {
2776 static int srv_dumped;
2777 struct srv_kw *kw;
2778 char *err;
2779
2780 kw = srv_find_kw(args[cur_arg]);
2781 if (kw) {
2782 char *err = NULL;
2783 int code;
2784
2785 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002786 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 +02002787 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002788 if (kw->skip != -1)
2789 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002790 err_code |= ERR_ALERT | ERR_FATAL;
2791 goto out;
2792 }
2793
2794 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002795 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002796 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002797 if (kw->skip != -1)
2798 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002799 err_code |= ERR_ALERT;
2800 continue;
2801 }
2802
2803 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2804 err_code |= code;
2805
2806 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002807 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002808 if (code & ERR_FATAL) {
2809 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002810 if (kw->skip != -1)
2811 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002812 goto out;
2813 }
2814 }
2815 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002816 if (kw->skip != -1)
2817 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002818 continue;
2819 }
2820
2821 err = NULL;
2822 if (!srv_dumped) {
2823 srv_dump_kws(&err);
2824 indent_msg(&err, 4);
2825 srv_dumped = 1;
2826 }
2827
Christopher Faulet767a84b2017-11-24 16:50:31 +01002828 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002829 file, linenum, args[0], args[1], args[cur_arg],
2830 err ? " Registered keywords :" : "", err ? err : "");
2831 free(err);
2832
2833 err_code |= ERR_ALERT | ERR_FATAL;
2834 goto out;
2835 }
2836 }
2837
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002838 if (!defsrv)
2839 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2840 if (err_code & ERR_FATAL)
2841 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002842 if (srv_tmpl)
2843 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002844 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002845 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002846 return 0;
2847
2848 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002849 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002850 free(errmsg);
2851 return err_code;
2852}
2853
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002854/* Returns a pointer to the first server matching either id <id>.
2855 * NULL is returned if no match is found.
2856 * the lookup is performed in the backend <bk>
2857 */
2858struct server *server_find_by_id(struct proxy *bk, int id)
2859{
2860 struct eb32_node *eb32;
2861 struct server *curserver;
2862
2863 if (!bk || (id ==0))
2864 return NULL;
2865
2866 /* <bk> has no backend capabilities, so it can't have a server */
2867 if (!(bk->cap & PR_CAP_BE))
2868 return NULL;
2869
2870 curserver = NULL;
2871
2872 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2873 if (eb32)
2874 curserver = container_of(eb32, struct server, conf.id);
2875
2876 return curserver;
2877}
2878
2879/* Returns a pointer to the first server matching either name <name>, or id
2880 * if <name> starts with a '#'. NULL is returned if no match is found.
2881 * the lookup is performed in the backend <bk>
2882 */
2883struct server *server_find_by_name(struct proxy *bk, const char *name)
2884{
2885 struct server *curserver;
2886
2887 if (!bk || !name)
2888 return NULL;
2889
2890 /* <bk> has no backend capabilities, so it can't have a server */
2891 if (!(bk->cap & PR_CAP_BE))
2892 return NULL;
2893
2894 curserver = NULL;
2895 if (*name == '#') {
2896 curserver = server_find_by_id(bk, atoi(name + 1));
2897 if (curserver)
2898 return curserver;
2899 }
2900 else {
2901 curserver = bk->srv;
2902
2903 while (curserver && (strcmp(curserver->id, name) != 0))
2904 curserver = curserver->next;
2905
2906 if (curserver)
2907 return curserver;
2908 }
2909
2910 return NULL;
2911}
2912
2913struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2914{
2915 struct server *byname;
2916 struct server *byid;
2917
2918 if (!name && !id)
2919 return NULL;
2920
2921 if (diff)
2922 *diff = 0;
2923
2924 byname = byid = NULL;
2925
2926 if (name) {
2927 byname = server_find_by_name(bk, name);
2928 if (byname && (!id || byname->puid == id))
2929 return byname;
2930 }
2931
2932 /* remaining possibilities :
2933 * - name not set
2934 * - name set but not found
2935 * - name found but ID doesn't match
2936 */
2937 if (id) {
2938 byid = server_find_by_id(bk, id);
2939 if (byid) {
2940 if (byname) {
2941 /* use id only if forced by configuration */
2942 if (byid->flags & SRV_F_FORCED_ID) {
2943 if (diff)
2944 *diff |= 2;
2945 return byid;
2946 }
2947 else {
2948 if (diff)
2949 *diff |= 1;
2950 return byname;
2951 }
2952 }
2953
2954 /* remaining possibilities:
2955 * - name not set
2956 * - name set but not found
2957 */
2958 if (name && diff)
2959 *diff |= 2;
2960 return byid;
2961 }
2962
2963 /* id bot found */
2964 if (byname) {
2965 if (diff)
2966 *diff |= 1;
2967 return byname;
2968 }
2969 }
2970
2971 return NULL;
2972}
2973
Willy Tarreau46b7f532018-08-21 11:54:26 +02002974/* Update a server state using the parameters available in the params list.
2975 *
2976 * Grabs the server lock during operation.
2977 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002978static void srv_update_state(struct server *srv, int version, char **params)
2979{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002980 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002981 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002982
2983 /* fields since version 1
2984 * and common to all other upcoming versions
2985 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002986 enum srv_state srv_op_state;
2987 enum srv_admin srv_admin_state;
2988 unsigned srv_uweight, srv_iweight;
2989 unsigned long srv_last_time_change;
2990 short srv_check_status;
2991 enum chk_result srv_check_result;
2992 int srv_check_health;
2993 int srv_check_state, srv_agent_state;
2994 int bk_f_forced_id;
2995 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002996 int fqdn_set_by_cli;
2997 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002998 const char *port_str;
2999 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003000 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003001
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003002 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02003003 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02003004 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003005 switch (version) {
3006 case 1:
3007 /*
3008 * now we can proceed with server's state update:
3009 * srv_addr: params[0]
3010 * srv_op_state: params[1]
3011 * srv_admin_state: params[2]
3012 * srv_uweight: params[3]
3013 * srv_iweight: params[4]
3014 * srv_last_time_change: params[5]
3015 * srv_check_status: params[6]
3016 * srv_check_result: params[7]
3017 * srv_check_health: params[8]
3018 * srv_check_state: params[9]
3019 * srv_agent_state: params[10]
3020 * bk_f_forced_id: params[11]
3021 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003022 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003023 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003024 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003025 */
3026
3027 /* validating srv_op_state */
3028 p = NULL;
3029 errno = 0;
3030 srv_op_state = strtol(params[1], &p, 10);
3031 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
3032 (srv_op_state != SRV_ST_STOPPED &&
3033 srv_op_state != SRV_ST_STARTING &&
3034 srv_op_state != SRV_ST_RUNNING &&
3035 srv_op_state != SRV_ST_STOPPING)) {
3036 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
3037 }
3038
3039 /* validating srv_admin_state */
3040 p = NULL;
3041 errno = 0;
3042 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003043 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01003044
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003045 /* inherited statuses will be recomputed later.
3046 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
3047 */
3048 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01003049
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003050 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
3051 (srv_admin_state != 0 &&
3052 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003053 srv_admin_state != SRV_ADMF_CMAINT &&
3054 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01003055 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01003056 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003057 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
3058 }
3059
3060 /* validating srv_uweight */
3061 p = NULL;
3062 errno = 0;
3063 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003064 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003065 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
3066
3067 /* validating srv_iweight */
3068 p = NULL;
3069 errno = 0;
3070 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003071 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003072 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
3073
3074 /* validating srv_last_time_change */
3075 p = NULL;
3076 errno = 0;
3077 srv_last_time_change = strtol(params[5], &p, 10);
3078 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
3079 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
3080
3081 /* validating srv_check_status */
3082 p = NULL;
3083 errno = 0;
3084 srv_check_status = strtol(params[6], &p, 10);
3085 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3086 (srv_check_status >= HCHK_STATUS_SIZE))
3087 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3088
3089 /* validating srv_check_result */
3090 p = NULL;
3091 errno = 0;
3092 srv_check_result = strtol(params[7], &p, 10);
3093 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3094 (srv_check_result != CHK_RES_UNKNOWN &&
3095 srv_check_result != CHK_RES_NEUTRAL &&
3096 srv_check_result != CHK_RES_FAILED &&
3097 srv_check_result != CHK_RES_PASSED &&
3098 srv_check_result != CHK_RES_CONDPASS)) {
3099 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3100 }
3101
3102 /* validating srv_check_health */
3103 p = NULL;
3104 errno = 0;
3105 srv_check_health = strtol(params[8], &p, 10);
3106 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3107 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3108
3109 /* validating srv_check_state */
3110 p = NULL;
3111 errno = 0;
3112 srv_check_state = strtol(params[9], &p, 10);
3113 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3114 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3115 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3116
3117 /* validating srv_agent_state */
3118 p = NULL;
3119 errno = 0;
3120 srv_agent_state = strtol(params[10], &p, 10);
3121 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3122 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3123 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3124
3125 /* validating bk_f_forced_id */
3126 p = NULL;
3127 errno = 0;
3128 bk_f_forced_id = strtol(params[11], &p, 10);
3129 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3130 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3131
3132 /* validating srv_f_forced_id */
3133 p = NULL;
3134 errno = 0;
3135 srv_f_forced_id = strtol(params[12], &p, 10);
3136 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3137 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3138
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003139 /* validating srv_fqdn */
3140 fqdn = params[13];
3141 if (fqdn && *fqdn == '-')
3142 fqdn = NULL;
3143 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3144 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3145 fqdn = NULL;
3146 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003147
Frédéric Lécaille31694712017-08-01 08:47:19 +02003148 port_str = params[14];
3149 if (port_str) {
3150 port = strl2uic(port_str, strlen(port_str));
3151 if (port > USHRT_MAX) {
3152 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3153 port_str = NULL;
3154 }
3155 }
3156
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003157 /* SRV record
3158 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3159 */
3160 srvrecord = params[15];
3161 if (srvrecord && *srvrecord != '_')
3162 srvrecord = NULL;
3163
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003164 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003165 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003166 goto out;
3167
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003168 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003169 /* recover operational state and apply it to this server
3170 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003171 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003172 switch (srv_op_state) {
3173 case SRV_ST_STOPPED:
3174 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003175 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003176 break;
3177 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003178 /* If rise == 1 there is no STARTING state, let's switch to
3179 * RUNNING
3180 */
3181 if (srv->check.rise == 1) {
3182 srv->check.health = srv->check.rise + srv->check.fall - 1;
3183 srv_set_running(srv, "", NULL);
3184 break;
3185 }
3186 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3187 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003188 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003189 break;
3190 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003191 /* If fall == 1 there is no STOPPING state, let's switch to
3192 * STOPPED
3193 */
3194 if (srv->check.fall == 1) {
3195 srv->check.health = 0;
3196 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3197 break;
3198 }
3199 if (srv->check.health < srv->check.rise ||
3200 srv->check.health > srv->check.rise + srv->check.fall - 2)
3201 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003202 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003203 break;
3204 case SRV_ST_RUNNING:
3205 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003206 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003207 break;
3208 }
3209
3210 /* When applying server state, the following rules apply:
3211 * - in case of a configuration change, we apply the setting from the new
3212 * configuration, regardless of old running state
3213 * - if no configuration change, we apply old running state only if old running
3214 * state is different from new configuration state
3215 */
3216 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003217 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3218 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003219 srv_adm_set_maint(srv);
3220 else
3221 srv_adm_set_ready(srv);
3222 }
3223 /* configuration is the same, let's compate old running state and new conf state */
3224 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003225 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003226 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003227 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003228 srv_adm_set_ready(srv);
3229 }
3230 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003231 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003232 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003233 * (srv->iweight is the weight set up in configuration).
3234 * There are two possible reasons for FDRAIN to have been present :
3235 * - previous config weight was zero
3236 * - "set server b/s drain" was sent to the CLI
3237 *
3238 * In the first case, we simply want to drop this drain state
3239 * if the new weight is not zero anymore, meaning the administrator
3240 * has intentionally turned the weight back to a positive value to
3241 * enable the server again after an operation. In the second case,
3242 * the drain state was forced on the CLI regardless of the config's
3243 * weight so we don't want a change to the config weight to lose this
3244 * status. What this means is :
3245 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3246 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003247 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003248 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003249 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003250 }
3251
3252 srv->last_change = date.tv_sec - srv_last_time_change;
3253 srv->check.status = srv_check_status;
3254 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003255
3256 /* Only case we want to apply is removing ENABLED flag which could have been
3257 * done by the "disable health" command over the stats socket
3258 */
3259 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3260 (srv_check_state & CHK_ST_CONFIGURED) &&
3261 !(srv_check_state & CHK_ST_ENABLED))
3262 srv->check.state &= ~CHK_ST_ENABLED;
3263
3264 /* Only case we want to apply is removing ENABLED flag which could have been
3265 * done by the "disable agent" command over the stats socket
3266 */
3267 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3268 (srv_agent_state & CHK_ST_CONFIGURED) &&
3269 !(srv_agent_state & CHK_ST_ENABLED))
3270 srv->agent.state &= ~CHK_ST_ENABLED;
3271
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003272 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3273 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003274 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003275 * It means that a configuration file change has precedence over a unix socket change
3276 * for server's weight
3277 *
3278 * by default, HAProxy applies the following weight when parsing the configuration
3279 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003280 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003281 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003282 srv->uweight = srv_uweight;
3283 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003284 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003285
Willy Tarreaue5a60682016-11-09 14:54:53 +01003286 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003287 if (strcmp(params[0], "-"))
3288 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003289
3290 if (fqdn && srv->hostname) {
3291 if (!strcmp(srv->hostname, fqdn)) {
3292 /* Here we reset the 'set from stats socket FQDN' flag
3293 * to support such transitions:
3294 * Let's say initial FQDN value is foo1 (in configuration file).
3295 * - FQDN changed from stats socket, from foo1 to foo2 value,
3296 * - FQDN changed again from file configuration (with the same previous value
3297 set from stats socket, from foo1 to foo2 value),
3298 * - reload for any other reason than a FQDN modification,
3299 * the configuration file FQDN matches the fqdn server state file value.
3300 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003301 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003302 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003303 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003304 }
3305 else {
3306 /* If the FDQN has been changed from stats socket,
3307 * apply fqdn state file value (which is the value set
3308 * from stats socket).
3309 */
3310 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003311 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003312 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003313 }
3314 }
3315 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003316 /* If all the conditions below are validated, this means
3317 * we're evaluating a server managed by SRV resolution
3318 */
3319 else if (fqdn && !srv->hostname && srvrecord) {
3320 int res;
3321
3322 /* we can't apply previous state if SRV record has changed */
3323 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3324 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);
3325 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3326 goto out;
3327 }
3328
3329 /* create or find a SRV resolution for this srv record */
3330 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3331 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3332 if (srv->srvrq == NULL) {
3333 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3334 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3335 goto out;
3336 }
3337
3338 /* prepare DNS resolution for this server */
3339 res = srv_prepare_for_resolution(srv, fqdn);
3340 if (res == -1) {
3341 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3342 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3343 goto out;
3344 }
3345
3346 /* configure check.port accordingly */
3347 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3348 !(srv->flags & SRV_F_CHECKPORT))
3349 srv->check.port = port;
3350
3351 /* Unset SRV_F_MAPPORTS for SRV records.
3352 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3353 * because no ports are provided in the configuration file.
3354 * This is because HAProxy will use the port found into the SRV record.
3355 */
3356 srv->flags &= ~SRV_F_MAPPORTS;
3357 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003358
Frédéric Lécaille31694712017-08-01 08:47:19 +02003359 if (port_str)
3360 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003361 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003362
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003363 break;
3364 default:
3365 chunk_appendf(msg, ", version '%d' not supported", version);
3366 }
3367
3368 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003369 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003370 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003371 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003372 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003373 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003374}
3375
3376/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3377 * For each proxy, it does the following:
3378 * - opens its server state file (either one or local one)
3379 * - read whole file, line by line
3380 * - analyse each line to check if it matches our current backend:
3381 * - backend name matches
3382 * - backend id matches if id is forced and name doesn't match
3383 * - if the server pointed by the line is found, then state is applied
3384 *
3385 * If the running backend uuid or id differs from the state file, then HAProxy reports
3386 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003387 *
3388 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003389 */
3390void apply_server_state(void)
3391{
3392 char *cur, *end;
3393 char mybuf[SRV_STATE_LINE_MAXLEN];
3394 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003395 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3396 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmann95c2c012019-06-11 14:51:49 +02003397 int arg, srv_arg, version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003398 FILE *f;
3399 char *filepath;
3400 char globalfilepath[MAXPATHLEN + 1];
3401 char localfilepath[MAXPATHLEN + 1];
3402 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003403 struct proxy *curproxy, *bk;
3404 struct server *srv;
3405
3406 globalfilepathlen = 0;
3407 /* create the globalfilepath variable */
3408 if (global.server_state_file) {
3409 /* absolute path or no base directory provided */
3410 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3411 len = strlen(global.server_state_file);
3412 if (len > MAXPATHLEN) {
3413 globalfilepathlen = 0;
3414 goto globalfileerror;
3415 }
3416 memcpy(globalfilepath, global.server_state_file, len);
3417 globalfilepath[len] = '\0';
3418 globalfilepathlen = len;
3419 }
3420 else if (global.server_state_base) {
3421 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003422 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003423 globalfilepathlen = 0;
3424 goto globalfileerror;
3425 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003426 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003427 globalfilepath[len] = 0;
3428 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003429
3430 /* append a slash if needed */
3431 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3432 if (globalfilepathlen + 1 > MAXPATHLEN) {
3433 globalfilepathlen = 0;
3434 goto globalfileerror;
3435 }
3436 globalfilepath[globalfilepathlen++] = '/';
3437 }
3438
3439 len = strlen(global.server_state_file);
3440 if (globalfilepathlen + len > MAXPATHLEN) {
3441 globalfilepathlen = 0;
3442 goto globalfileerror;
3443 }
3444 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3445 globalfilepathlen += len;
3446 globalfilepath[globalfilepathlen++] = 0;
3447 }
3448 }
3449 globalfileerror:
3450 if (globalfilepathlen == 0)
3451 globalfilepath[0] = '\0';
3452
3453 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003454 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003455 /* servers are only in backends */
3456 if (!(curproxy->cap & PR_CAP_BE))
3457 continue;
3458 fileopenerr = 0;
3459 filepath = NULL;
3460
3461 /* search server state file path and name */
3462 switch (curproxy->load_server_state_from_file) {
3463 /* read servers state from global file */
3464 case PR_SRV_STATE_FILE_GLOBAL:
3465 /* there was an error while generating global server state file path */
3466 if (globalfilepathlen == 0)
3467 continue;
3468 filepath = globalfilepath;
3469 fileopenerr = 1;
3470 break;
3471 /* this backend has its own file */
3472 case PR_SRV_STATE_FILE_LOCAL:
3473 localfilepathlen = 0;
3474 localfilepath[0] = '\0';
3475 len = 0;
3476 /* create the localfilepath variable */
3477 /* absolute path or no base directory provided */
3478 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3479 len = strlen(curproxy->server_state_file_name);
3480 if (len > MAXPATHLEN) {
3481 localfilepathlen = 0;
3482 goto localfileerror;
3483 }
3484 memcpy(localfilepath, curproxy->server_state_file_name, len);
3485 localfilepath[len] = '\0';
3486 localfilepathlen = len;
3487 }
3488 else if (global.server_state_base) {
3489 len = strlen(global.server_state_base);
3490 localfilepathlen += len;
3491
3492 if (localfilepathlen > MAXPATHLEN) {
3493 localfilepathlen = 0;
3494 goto localfileerror;
3495 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003496 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003497 localfilepath[localfilepathlen] = 0;
3498
3499 /* append a slash if needed */
3500 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3501 if (localfilepathlen + 1 > MAXPATHLEN) {
3502 localfilepathlen = 0;
3503 goto localfileerror;
3504 }
3505 localfilepath[localfilepathlen++] = '/';
3506 }
3507
3508 len = strlen(curproxy->server_state_file_name);
3509 if (localfilepathlen + len > MAXPATHLEN) {
3510 localfilepathlen = 0;
3511 goto localfileerror;
3512 }
3513 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3514 localfilepathlen += len;
3515 localfilepath[localfilepathlen++] = 0;
3516 }
3517 filepath = localfilepath;
3518 localfileerror:
3519 if (localfilepathlen == 0)
3520 localfilepath[0] = '\0';
3521
3522 break;
3523 case PR_SRV_STATE_FILE_NONE:
3524 default:
3525 continue;
3526 }
3527
3528 /* preload global state file */
3529 errno = 0;
3530 f = fopen(filepath, "r");
3531 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003532 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003533 if (!f)
3534 continue;
3535
3536 mybuf[0] = '\0';
3537 mybuflen = 0;
3538 version = 0;
3539
3540 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003541 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003542 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003543 goto fileclose;
3544 }
3545
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003546 cur = mybuf;
3547 version = atoi(cur);
3548 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3549 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003550 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003551
3552 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3553 int bk_f_forced_id = 0;
3554 int check_id = 0;
3555 int check_name = 0;
3556
3557 mybuflen = strlen(mybuf);
3558 cur = mybuf;
3559 end = cur + mybuflen;
3560
3561 bk = NULL;
3562 srv = NULL;
3563
3564 /* we need at least one character */
3565 if (mybuflen == 0)
3566 continue;
3567
3568 /* ignore blank characters at the beginning of the line */
3569 while (isspace(*cur))
3570 ++cur;
3571
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003572 /* Ignore empty or commented lines */
3573 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003574 continue;
3575
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003576 /* truncated lines */
3577 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003578 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003579 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003580 }
3581
3582 /* Removes trailing '\n' */
3583 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003584
3585 /* we're now ready to move the line into *srv_params[] */
3586 params[0] = cur;
3587 arg = 1;
3588 srv_arg = 0;
3589 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3590 if (isspace(*cur)) {
3591 *cur = '\0';
3592 ++cur;
3593 while (isspace(*cur))
3594 ++cur;
3595 switch (version) {
3596 case 1:
3597 /*
3598 * srv_addr: params[4] => srv_params[0]
3599 * srv_op_state: params[5] => srv_params[1]
3600 * srv_admin_state: params[6] => srv_params[2]
3601 * srv_uweight: params[7] => srv_params[3]
3602 * srv_iweight: params[8] => srv_params[4]
3603 * srv_last_time_change: params[9] => srv_params[5]
3604 * srv_check_status: params[10] => srv_params[6]
3605 * srv_check_result: params[11] => srv_params[7]
3606 * srv_check_health: params[12] => srv_params[8]
3607 * srv_check_state: params[13] => srv_params[9]
3608 * srv_agent_state: params[14] => srv_params[10]
3609 * bk_f_forced_id: params[15] => srv_params[11]
3610 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003611 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003612 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003613 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003614 */
3615 if (arg >= 4) {
3616 srv_params[srv_arg] = cur;
3617 ++srv_arg;
3618 }
3619 break;
3620 }
3621
3622 params[arg] = cur;
3623 ++arg;
3624 }
3625 else {
3626 ++cur;
3627 }
3628 }
3629
3630 /* if line is incomplete line, then ignore it.
3631 * otherwise, update useful flags */
3632 switch (version) {
3633 case 1:
3634 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3635 continue;
3636 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3637 check_id = (atoi(params[0]) == curproxy->uuid);
3638 check_name = (strcmp(curproxy->id, params[1]) == 0);
3639 break;
3640 }
3641
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003642 bk = curproxy;
3643
3644 /* if backend can't be found, let's continue */
3645 if (!check_id && !check_name)
3646 continue;
3647 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003648 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 +02003649 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3650 }
3651 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003652 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 +02003653 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3654 /* if name doesn't match, we still want to update curproxy if the backend id
3655 * was forced in previous the previous configuration */
3656 if (!bk_f_forced_id)
3657 continue;
3658 }
3659
Baptiste Assmann95c2c012019-06-11 14:51:49 +02003660 /* look for the server by its name: param[3] */
3661 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003662
3663 if (!srv) {
3664 /* if no server found, then warning and continue with next line */
Baptiste Assmann95c2c012019-06-11 14:51:49 +02003665 ha_warning("can't find server '%s' in backend '%s'\n",
3666 params[3], params[1]);
3667 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3668 params[3], params[1]);
Frédéric Lécaille0bedb8a2017-06-15 14:09:10 +02003669 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003670 }
3671
3672 /* now we can proceed with server's state update */
3673 srv_update_state(srv, version, srv_params);
3674 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003675fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003676 fclose(f);
3677 }
3678}
3679
Simon Horman7d09b9a2013-02-12 10:45:51 +09003680/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003681 * update a server's current IP address.
3682 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3683 * ip is in network format.
3684 * updater is a string which contains an information about the requester of the update.
3685 * updater is used if not NULL.
3686 *
3687 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003688 *
3689 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003690 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003691int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003692{
3693 /* generates a log line and a warning on stderr */
3694 if (1) {
3695 /* book enough space for both IPv4 and IPv6 */
3696 char oldip[INET6_ADDRSTRLEN];
3697 char newip[INET6_ADDRSTRLEN];
3698
3699 memset(oldip, '\0', INET6_ADDRSTRLEN);
3700 memset(newip, '\0', INET6_ADDRSTRLEN);
3701
3702 /* copy old IP address in a string */
3703 switch (s->addr.ss_family) {
3704 case AF_INET:
3705 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3706 break;
3707 case AF_INET6:
3708 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3709 break;
3710 };
3711
3712 /* copy new IP address in a string */
3713 switch (ip_sin_family) {
3714 case AF_INET:
3715 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3716 break;
3717 case AF_INET6:
3718 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3719 break;
3720 };
3721
3722 /* save log line into a buffer */
3723 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3724 s->proxy->id, s->id, oldip, newip, updater);
3725
3726 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003727 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003728
3729 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003730 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003731 }
3732
3733 /* save the new IP family */
3734 s->addr.ss_family = ip_sin_family;
3735 /* save the new IP address */
3736 switch (ip_sin_family) {
3737 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003738 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003739 break;
3740 case AF_INET6:
3741 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3742 break;
3743 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003744 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003745
3746 return 0;
3747}
3748
3749/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003750 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3751 *
3752 * Caller can pass its name through <updater> to get it integrated in the response message
3753 * returned by the function.
3754 *
3755 * The function first does the following, in that order:
3756 * - validates the new addr and/or port
3757 * - checks if an update is required (new IP or port is different than current ones)
3758 * - checks the update is allowed:
3759 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3760 * - allow all changes if no CHECKS are configured
3761 * - if CHECK is configured:
3762 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3763 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3764 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003765 *
3766 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003767 */
3768const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3769{
3770 struct sockaddr_storage sa;
3771 int ret, port_change_required;
3772 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003773 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003774 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003775 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003776
3777 msg = get_trash_chunk();
3778 chunk_reset(msg);
3779
3780 if (addr) {
3781 memset(&sa, 0, sizeof(struct sockaddr_storage));
3782 if (str2ip2(addr, &sa, 0) == NULL) {
3783 chunk_printf(msg, "Invalid addr '%s'", addr);
3784 goto out;
3785 }
3786
3787 /* changes are allowed on AF_INET* families only */
3788 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3789 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3790 goto out;
3791 }
3792
3793 /* collecting data currently setup */
3794 memset(current_addr, '\0', sizeof(current_addr));
3795 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3796 /* changes are allowed on AF_INET* families only */
3797 if ((ret != AF_INET) && (ret != AF_INET6)) {
3798 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3799 goto out;
3800 }
3801
3802 /* applying ADDR changes if required and allowed
3803 * ipcmp returns 0 when both ADDR are the same
3804 */
3805 if (ipcmp(&s->addr, &sa) == 0) {
3806 chunk_appendf(msg, "no need to change the addr");
3807 goto port;
3808 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003809 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003810 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003811
3812 /* we also need to update check's ADDR only if it uses the server's one */
3813 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003814 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003815 }
3816
3817 /* we also need to update agent ADDR only if it use the server's one */
3818 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003819 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003820 }
3821
3822 /* update report for caller */
3823 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3824 }
3825
3826 port:
3827 if (port) {
3828 char sign = '\0';
3829 char *endptr;
3830
3831 if (addr)
3832 chunk_appendf(msg, ", ");
3833
3834 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003835 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003836
3837 /* check if PORT change is required */
3838 port_change_required = 0;
3839
3840 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003841 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003842 new_port = strtol(port, &endptr, 10);
3843 if ((errno != 0) || (port == endptr)) {
3844 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3845 goto out;
3846 }
3847
3848 /* check if caller triggers a port mapped or offset */
3849 if (sign == '-' || (sign == '+')) {
3850 /* check if server currently uses port map */
3851 if (!(s->flags & SRV_F_MAPPORTS)) {
3852 /* switch from fixed port to port map mandatorily triggers
3853 * a port change */
3854 port_change_required = 1;
3855 /* check is configured
3856 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3857 * prevent PORT change if check doesn't have it's dedicated port while switching
3858 * to port mapping */
3859 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3860 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.");
3861 goto out;
3862 }
3863 }
3864 /* we're already using port maps */
3865 else {
3866 port_change_required = current_port != new_port;
3867 }
3868 }
3869 /* fixed port */
3870 else {
3871 port_change_required = current_port != new_port;
3872 }
3873
3874 /* applying PORT changes if required and update response message */
3875 if (port_change_required) {
3876 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003877 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003878 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003879
3880 /* prepare message */
3881 chunk_appendf(msg, "port changed from '");
3882 if (s->flags & SRV_F_MAPPORTS)
3883 chunk_appendf(msg, "+");
3884 chunk_appendf(msg, "%d' to '", current_port);
3885
3886 if (sign == '-') {
3887 s->flags |= SRV_F_MAPPORTS;
3888 chunk_appendf(msg, "%c", sign);
3889 /* just use for result output */
3890 new_port = -new_port;
3891 }
3892 else if (sign == '+') {
3893 s->flags |= SRV_F_MAPPORTS;
3894 chunk_appendf(msg, "%c", sign);
3895 }
3896 else {
3897 s->flags &= ~SRV_F_MAPPORTS;
3898 }
3899
3900 chunk_appendf(msg, "%d'", new_port);
3901
3902 /* we also need to update health checks port only if it uses server's realport */
3903 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3904 s->check.port = new_port;
3905 }
3906 }
3907 else {
3908 chunk_appendf(msg, "no need to change the port");
3909 }
3910 }
3911
3912out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003913 if (changed)
3914 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003915 if (updater)
3916 chunk_appendf(msg, " by '%s'", updater);
3917 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003918 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003919}
3920
3921
3922/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003923 * update server status based on result of name resolution
3924 * returns:
3925 * 0 if server status is updated
3926 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003927 *
3928 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003929 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003930int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003931{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003932 struct dns_resolvers *resolvers = s->resolvers;
3933 struct dns_resolution *resolution = s->dns_requester->resolution;
3934 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003935
3936 switch (resolution->status) {
3937 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003938 /* status when HAProxy has just (re)started.
3939 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003940 break;
3941
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003942 case RSLV_STATUS_VALID:
3943 /*
3944 * resume health checks
3945 * server will be turned back on if health check is safe
3946 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003947 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003948 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003949 return 1;
3950 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3951 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003952 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003953 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003954
Emeric Brun52a91d32017-08-31 14:41:55 +02003955 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003956 return 1;
3957 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3958 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3959 s->proxy->id, s->id);
3960
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003961 ha_warning("%s.\n", trash.area);
3962 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003963 return 0;
3964
3965 case RSLV_STATUS_NX:
3966 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003967 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3968 if (!tick_is_expired(exp, now_ms))
3969 break;
3970
3971 if (s->next_admin & SRV_ADMF_RMAINT)
3972 return 1;
3973 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3974 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003975
3976 case RSLV_STATUS_TIMEOUT:
3977 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003978 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3979 if (!tick_is_expired(exp, now_ms))
3980 break;
3981
3982 if (s->next_admin & SRV_ADMF_RMAINT)
3983 return 1;
3984 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3985 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003986
3987 case RSLV_STATUS_REFUSED:
3988 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003989 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3990 if (!tick_is_expired(exp, now_ms))
3991 break;
3992
3993 if (s->next_admin & SRV_ADMF_RMAINT)
3994 return 1;
3995 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3996 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003997
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003998 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003999 /* stop server if resolution failed for a long enough period */
4000 exp = tick_add(resolution->last_valid, resolvers->hold.other);
4001 if (!tick_is_expired(exp, now_ms))
4002 break;
4003
4004 if (s->next_admin & SRV_ADMF_RMAINT)
4005 return 1;
4006 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
4007 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004008 }
4009
4010 return 1;
4011}
4012
4013/*
4014 * Server Name Resolution valid response callback
4015 * It expects:
4016 * - <nameserver>: the name server which answered the valid response
4017 * - <response>: buffer containing a valid DNS response
4018 * - <response_len>: size of <response>
4019 * It performs the following actions:
4020 * - ignore response if current ip found and server family not met
4021 * - update with first new ip found if family is met and current IP is not found
4022 * returns:
4023 * 0 on error
4024 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01004025 *
4026 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004027 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004028int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004029{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004030 struct server *s = NULL;
4031 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004032 void *serverip, *firstip;
4033 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004034 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02004035 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004036 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004037
Christopher Faulet67957bd2017-09-27 11:00:59 +02004038 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004039 if (!s)
4040 return 1;
4041
Christopher Faulet67957bd2017-09-27 11:00:59 +02004042 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004043
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004044 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004045 firstip = NULL; /* pointer to the first valid response found */
4046 /* it will be used as the new IP if a change is required */
4047 firstip_sin_family = AF_UNSPEC;
4048 serverip = NULL; /* current server IP address */
4049
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004050 /* initializing server IP pointer */
4051 server_sin_family = s->addr.ss_family;
4052 switch (server_sin_family) {
4053 case AF_INET:
4054 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
4055 break;
4056
4057 case AF_INET6:
4058 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
4059 break;
4060
Willy Tarreau3acfcd12017-01-06 19:18:32 +01004061 case AF_UNSPEC:
4062 break;
4063
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004064 default:
4065 goto invalid;
4066 }
4067
Baptiste Assmann729c9012017-05-22 15:13:10 +02004068 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01004069 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004070 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004071
4072 switch (ret) {
4073 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004074 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004075
4076 case DNS_UPD_SRVIP_NOT_FOUND:
4077 goto save_ip;
4078
4079 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004080 goto invalid;
4081
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004082 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004083 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004084 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004085
Baptiste Assmannfad03182015-10-28 02:03:32 +01004086 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01004087 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004088 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004089 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01004090
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004091 default:
4092 goto invalid;
4093
4094 }
4095
4096 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004097 if (nameserver) {
4098 nameserver->counters.update++;
4099 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004100 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004101 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004102 else
4103 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004104 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004105
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004106 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004107 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004108 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004109
4110 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004111 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02004112 nameserver->counters.invalid++;
4113 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004114 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004115 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004116 return 0;
4117}
4118
4119/*
4120 * Server Name Resolution error management callback
4121 * returns:
4122 * 0 on error
4123 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004124 *
4125 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004126 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004127int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004128{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004129 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004130
Christopher Faulet67957bd2017-09-27 11:00:59 +02004131 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004132 if (!s)
4133 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004134 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004135 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004136 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004137 return 1;
4138}
4139
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004140/*
4141 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004142 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004143 * It returns a pointer to the first server found or NULL if <ip> is not yet
4144 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004145 *
4146 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004147 */
4148struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4149{
4150 struct server *tmpsrv;
4151 struct proxy *be;
4152
4153 if (!srv)
4154 return NULL;
4155
4156 be = srv->proxy;
4157 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004158 /* we found the current server is the same, ignore it */
4159 if (srv == tmpsrv)
4160 continue;
4161
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004162 /* We want to compare the IP in the record with the IP of the servers in the
4163 * same backend, only if:
4164 * * DNS resolution is enabled on the server
4165 * * the hostname used for the resolution by our server is the same than the
4166 * one used for the server found in the backend
4167 * * the server found in the backend is not our current server
4168 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004169 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004170 if ((tmpsrv->hostname_dn == NULL) ||
4171 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4172 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004173 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004174 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004175 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004176 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004177
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004178 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004179 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004180 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004181 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004182 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004183
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004184 /* At this point, we have 2 different servers using the same DNS hostname
4185 * for their respective resolution.
4186 */
4187 if (*ip_family == tmpsrv->addr.ss_family &&
4188 ((tmpsrv->addr.ss_family == AF_INET &&
4189 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4190 (tmpsrv->addr.ss_family == AF_INET6 &&
4191 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004192 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004193 return tmpsrv;
4194 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004195 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004196 }
4197
Emeric Brune9fd6b52017-11-02 17:20:39 +01004198
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004199 return NULL;
4200}
4201
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004202/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4203 * resolver. This is suited for initial address configuration. Returns 0 on
4204 * success otherwise a non-zero error code. In case of error, *err_code, if
4205 * not NULL, is filled up.
4206 */
4207int srv_set_addr_via_libc(struct server *srv, int *err_code)
4208{
4209 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004210 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004211 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004212 return 1;
4213 }
4214 return 0;
4215}
4216
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004217/* Set the server's FDQN (->hostname) from <hostname>.
4218 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004219 *
4220 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004221 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004222int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004223{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004224 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004225 char *hostname_dn;
4226 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004227
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004228 /* Note that the server lock is already held. */
4229 if (!srv->resolvers)
4230 return -1;
4231
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004232 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004233 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004234 /* run time DNS resolution was not active for this server
4235 * and we can't enable it at run time for now.
4236 */
4237 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004238 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004239
4240 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004241 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004242 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004243 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4244 hostname_dn, trash.size);
4245 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004246 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004247
Christopher Faulet67957bd2017-09-27 11:00:59 +02004248 resolution = srv->dns_requester->resolution;
4249 if (resolution &&
4250 resolution->hostname_dn &&
4251 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004252 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004253
Christopher Faulet67957bd2017-09-27 11:00:59 +02004254 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004255
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004256 free(srv->hostname);
4257 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004258 srv->hostname = strdup(hostname);
4259 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004260 srv->hostname_dn_len = hostname_dn_len;
4261 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004262 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004263
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004264 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004265 goto err;
4266
4267 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004268 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004269 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004270 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004271
4272 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004273 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004274 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004275 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004276}
4277
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004278/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4279 * from the state file. This is suited for initial address configuration.
4280 * Returns 0 on success otherwise a non-zero error code. In case of error,
4281 * *err_code, if not NULL, is filled up.
4282 */
4283static int srv_apply_lastaddr(struct server *srv, int *err_code)
4284{
4285 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4286 if (err_code)
4287 *err_code |= ERR_WARN;
4288 return 1;
4289 }
4290 return 0;
4291}
4292
Willy Tarreau25e51522016-11-04 15:10:17 +01004293/* returns 0 if no error, otherwise a combination of ERR_* flags */
4294static int srv_iterate_initaddr(struct server *srv)
4295{
4296 int return_code = 0;
4297 int err_code;
4298 unsigned int methods;
4299
4300 methods = srv->init_addr_methods;
4301 if (!methods) { // default to "last,libc"
4302 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4303 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4304 }
4305
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004306 /* "-dr" : always append "none" so that server addresses resolution
4307 * failures are silently ignored, this is convenient to validate some
4308 * configs out of their environment.
4309 */
4310 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4311 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4312
Willy Tarreau25e51522016-11-04 15:10:17 +01004313 while (methods) {
4314 err_code = 0;
4315 switch (srv_get_next_initaddr(&methods)) {
4316 case SRV_IADDR_LAST:
4317 if (!srv->lastaddr)
4318 continue;
4319 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004320 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004321 return_code |= err_code;
4322 break;
4323
4324 case SRV_IADDR_LIBC:
4325 if (!srv->hostname)
4326 continue;
4327 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004328 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004329 return_code |= err_code;
4330 break;
4331
Willy Tarreau37ebe122016-11-04 15:17:58 +01004332 case SRV_IADDR_NONE:
4333 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004334 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004335 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4336 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004337 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004338 return return_code;
4339
Willy Tarreau4310d362016-11-02 15:05:56 +01004340 case SRV_IADDR_IP:
4341 ipcpy(&srv->init_addr, &srv->addr);
4342 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004343 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4344 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004345 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004346 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004347
Willy Tarreau25e51522016-11-04 15:10:17 +01004348 default: /* unhandled method */
4349 break;
4350 }
4351 }
4352
4353 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004354 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004355 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4356 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004357 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004358 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004359 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4360 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004361
4362 return_code |= ERR_ALERT | ERR_FATAL;
4363 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004364out:
4365 srv_set_dyncookie(srv);
4366 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004367}
4368
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004369/*
4370 * This function parses all backends and all servers within each backend
4371 * and performs servers' addr resolution based on information provided by:
4372 * - configuration file
4373 * - server-state file (states provided by an 'old' haproxy process)
4374 *
4375 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4376 */
4377int srv_init_addr(void)
4378{
4379 struct proxy *curproxy;
4380 int return_code = 0;
4381
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004382 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004383 while (curproxy) {
4384 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004385
4386 /* servers are in backend only */
4387 if (!(curproxy->cap & PR_CAP_BE))
4388 goto srv_init_addr_next;
4389
Willy Tarreau25e51522016-11-04 15:10:17 +01004390 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004391 if (srv->hostname)
4392 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004393
4394 srv_init_addr_next:
4395 curproxy = curproxy->next;
4396 }
4397
4398 return return_code;
4399}
4400
Willy Tarreau46b7f532018-08-21 11:54:26 +02004401/*
4402 * Must be called with the server lock held.
4403 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004404const 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 +02004405{
4406
Willy Tarreau83061a82018-07-13 11:56:34 +02004407 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004408
4409 msg = get_trash_chunk();
4410 chunk_reset(msg);
4411
Olivier Houchard8da5f982017-08-04 18:35:36 +02004412 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004413 chunk_appendf(msg, "no need to change the FDQN");
4414 goto out;
4415 }
4416
4417 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4418 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4419 goto out;
4420 }
4421
4422 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4423 server->proxy->id, server->id, server->hostname, fqdn);
4424
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004425 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004426 chunk_reset(msg);
4427 chunk_appendf(msg, "could not update %s/%s FQDN",
4428 server->proxy->id, server->id);
4429 goto out;
4430 }
4431
4432 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004433 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004434
4435 out:
4436 if (updater)
4437 chunk_appendf(msg, " by '%s'", updater);
4438 chunk_appendf(msg, "\n");
4439
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004440 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004441}
4442
4443
Willy Tarreau21b069d2016-11-23 17:15:08 +01004444/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4445 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004446 * 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 +01004447 * used for CLI commands requiring a server name.
4448 * Important: the <arg> is modified to remove the '/'.
4449 */
4450struct server *cli_find_server(struct appctx *appctx, char *arg)
4451{
4452 struct proxy *px;
4453 struct server *sv;
4454 char *line;
4455
4456 /* split "backend/server" and make <line> point to server */
4457 for (line = arg; *line; line++)
4458 if (*line == '/') {
4459 *line++ = '\0';
4460 break;
4461 }
4462
4463 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004464 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004465 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004466 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004467 return NULL;
4468 }
4469
4470 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004471 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004472 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004473 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004474 return NULL;
4475 }
4476
4477 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004478 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004479 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004480 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004481 return NULL;
4482 }
4483
4484 return sv;
4485}
4486
William Lallemand222baf22016-11-19 02:00:33 +01004487
Willy Tarreau46b7f532018-08-21 11:54:26 +02004488/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004489static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004490{
4491 struct server *sv;
4492 const char *warning;
4493
4494 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4495 return 1;
4496
4497 sv = cli_find_server(appctx, args[2]);
4498 if (!sv)
4499 return 1;
4500
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004501 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004502
William Lallemand222baf22016-11-19 02:00:33 +01004503 if (strcmp(args[3], "weight") == 0) {
4504 warning = server_parse_weight_change_request(sv, args[4]);
4505 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004506 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004507 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004508 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004509 }
4510 }
4511 else if (strcmp(args[3], "state") == 0) {
4512 if (strcmp(args[4], "ready") == 0)
4513 srv_adm_set_ready(sv);
4514 else if (strcmp(args[4], "drain") == 0)
4515 srv_adm_set_drain(sv);
4516 else if (strcmp(args[4], "maint") == 0)
4517 srv_adm_set_maint(sv);
4518 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004519 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004520 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004521 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004522 }
4523 }
4524 else if (strcmp(args[3], "health") == 0) {
4525 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004526 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004527 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004528 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004529 }
4530 else if (strcmp(args[4], "up") == 0) {
4531 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004532 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004533 }
4534 else if (strcmp(args[4], "stopping") == 0) {
4535 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004536 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004537 }
4538 else if (strcmp(args[4], "down") == 0) {
4539 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004540 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004541 }
4542 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004543 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004544 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004545 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004546 }
4547 }
4548 else if (strcmp(args[3], "agent") == 0) {
4549 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004550 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004551 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004552 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004553 }
4554 else if (strcmp(args[4], "up") == 0) {
4555 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004556 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004557 }
4558 else if (strcmp(args[4], "down") == 0) {
4559 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004560 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004561 }
4562 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004563 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004564 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004565 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004566 }
4567 }
Misiek2da082d2017-01-09 09:40:42 +01004568 else if (strcmp(args[3], "agent-addr") == 0) {
4569 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004570 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004571 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4572 appctx->st0 = CLI_ST_PRINT;
4573 } else {
4574 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004575 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004576 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4577 appctx->st0 = CLI_ST_PRINT;
4578 }
4579 }
4580 }
4581 else if (strcmp(args[3], "agent-send") == 0) {
4582 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004583 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004584 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4585 appctx->st0 = CLI_ST_PRINT;
4586 } else {
4587 char *nss = strdup(args[4]);
4588 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004589 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004590 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4591 appctx->st0 = CLI_ST_PRINT;
4592 } else {
4593 free(sv->agent.send_string);
4594 sv->agent.send_string = nss;
4595 sv->agent.send_string_len = strlen(args[4]);
4596 }
4597 }
4598 }
William Lallemand222baf22016-11-19 02:00:33 +01004599 else if (strcmp(args[3], "check-port") == 0) {
4600 int i = 0;
4601 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004602 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004603 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004604 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004605 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004606 }
4607 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004608 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004609 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004610 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004611 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004612 }
4613 /* prevent the update of port to 0 if MAPPORTS are in use */
4614 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004615 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004616 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004617 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004618 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004619 }
4620 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004621 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004622 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004623 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004624 }
4625 else if (strcmp(args[3], "addr") == 0) {
4626 char *addr = NULL;
4627 char *port = NULL;
4628 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004629 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004630 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004631 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004632 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004633 }
4634 else {
4635 addr = args[4];
4636 }
4637 if (strcmp(args[5], "port") == 0) {
4638 port = args[6];
4639 }
4640 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4641 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004642 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004643 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004644 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004645 }
4646 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4647 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004648 else if (strcmp(args[3], "fqdn") == 0) {
4649 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004650 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004651 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4652 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004653 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004654 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004655 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004656 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004657 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004658 appctx->ctx.cli.msg = warning;
4659 appctx->st0 = CLI_ST_PRINT;
4660 }
4661 }
William Lallemand222baf22016-11-19 02:00:33 +01004662 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004663 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004664 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 +01004665 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004666 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004667 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004668 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004669 return 1;
4670}
4671
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004672static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004673{
4674 struct stream_interface *si = appctx->owner;
4675 struct proxy *px;
4676 struct server *sv;
4677 char *line;
4678
4679
4680 /* split "backend/server" and make <line> point to server */
4681 for (line = args[2]; *line; line++)
4682 if (*line == '/') {
4683 *line++ = '\0';
4684 break;
4685 }
4686
4687 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004688 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004689 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004690 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004691 return 1;
4692 }
4693
4694 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004695 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004696 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004697 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004698 return 1;
4699 }
4700
4701 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004702 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4703 sv->iweight);
4704 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004705 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004706 return 0;
4707 }
William Lallemand6b160942016-11-22 12:34:35 +01004708 return 1;
4709}
4710
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004711/* Parse a "set weight" command.
4712 *
4713 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004714 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004715static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004716{
4717 struct server *sv;
4718 const char *warning;
4719
4720 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4721 return 1;
4722
4723 sv = cli_find_server(appctx, args[2]);
4724 if (!sv)
4725 return 1;
4726
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004727 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4728
William Lallemand6b160942016-11-22 12:34:35 +01004729 warning = server_parse_weight_change_request(sv, args[3]);
4730 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004731 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004732 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004733 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004734 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004735
4736 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4737
William Lallemand6b160942016-11-22 12:34:35 +01004738 return 1;
4739}
4740
Willy Tarreau46b7f532018-08-21 11:54:26 +02004741/* parse a "set maxconn server" command. It always returns 1.
4742 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004743 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004744 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004745static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004746{
4747 struct server *sv;
4748 const char *warning;
4749
4750 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4751 return 1;
4752
4753 sv = cli_find_server(appctx, args[3]);
4754 if (!sv)
4755 return 1;
4756
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004757 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4758
Willy Tarreaub8026272016-11-23 11:26:56 +01004759 warning = server_parse_maxconn_change_request(sv, args[4]);
4760 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004761 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004762 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004763 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004764 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004765
4766 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4767
Willy Tarreaub8026272016-11-23 11:26:56 +01004768 return 1;
4769}
William Lallemand6b160942016-11-22 12:34:35 +01004770
Willy Tarreau46b7f532018-08-21 11:54:26 +02004771/* parse a "disable agent" command. It always returns 1.
4772 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004773 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004774 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004775static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004776{
4777 struct server *sv;
4778
4779 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4780 return 1;
4781
4782 sv = cli_find_server(appctx, args[2]);
4783 if (!sv)
4784 return 1;
4785
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004786 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004787 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004788 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004789 return 1;
4790}
4791
Willy Tarreau46b7f532018-08-21 11:54:26 +02004792/* parse a "disable health" command. It always returns 1.
4793 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004794 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004795 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004796static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004797{
4798 struct server *sv;
4799
4800 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4801 return 1;
4802
4803 sv = cli_find_server(appctx, args[2]);
4804 if (!sv)
4805 return 1;
4806
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004807 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004808 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004809 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004810 return 1;
4811}
4812
Willy Tarreau46b7f532018-08-21 11:54:26 +02004813/* parse a "disable server" command. It always returns 1.
4814 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004815 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004816 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004817static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004818{
4819 struct server *sv;
4820
4821 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4822 return 1;
4823
4824 sv = cli_find_server(appctx, args[2]);
4825 if (!sv)
4826 return 1;
4827
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004828 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004829 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004830 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004831 return 1;
4832}
4833
Willy Tarreau46b7f532018-08-21 11:54:26 +02004834/* parse a "enable agent" command. It always returns 1.
4835 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004836 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004837 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004838static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004839{
4840 struct server *sv;
4841
4842 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4843 return 1;
4844
4845 sv = cli_find_server(appctx, args[2]);
4846 if (!sv)
4847 return 1;
4848
4849 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004850 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004851 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004852 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004853 return 1;
4854 }
4855
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004856 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004857 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004858 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004859 return 1;
4860}
4861
Willy Tarreau46b7f532018-08-21 11:54:26 +02004862/* parse a "enable health" command. It always returns 1.
4863 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004864 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004865 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004866static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004867{
4868 struct server *sv;
4869
4870 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4871 return 1;
4872
4873 sv = cli_find_server(appctx, args[2]);
4874 if (!sv)
4875 return 1;
4876
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004877 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004878 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004879 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004880 return 1;
4881}
4882
Willy Tarreau46b7f532018-08-21 11:54:26 +02004883/* parse a "enable server" command. It always returns 1.
4884 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004885 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004886 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004887static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004888{
4889 struct server *sv;
4890
4891 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4892 return 1;
4893
4894 sv = cli_find_server(appctx, args[2]);
4895 if (!sv)
4896 return 1;
4897
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004898 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004899 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004900 if (!(sv->flags & SRV_F_COOKIESET)
4901 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4902 sv->cookie)
4903 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004904 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004905 return 1;
4906}
4907
William Lallemand222baf22016-11-19 02:00:33 +01004908/* register cli keywords */
4909static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004910 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004911 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004912 { { "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 +01004913 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004914 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004915 { { "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 +01004916 { { "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 +01004917 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004918 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4919 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4920
William Lallemand222baf22016-11-19 02:00:33 +01004921 {{},}
4922}};
4923
Willy Tarreau0108d902018-11-25 19:14:37 +01004924INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004925
Emeric Brun64cc49c2017-10-03 14:46:45 +02004926/*
4927 * This function applies server's status changes, it is
4928 * is designed to be called asynchronously.
4929 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004930 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004931 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004932static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004933{
4934 struct check *check = &s->check;
4935 int xferred;
4936 struct proxy *px = s->proxy;
4937 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4938 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4939 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004940 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004941
Emeric Brun64cc49c2017-10-03 14:46:45 +02004942 /* If currently main is not set we try to apply pending state changes */
4943 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4944 int next_admin;
4945
4946 /* Backup next admin */
4947 next_admin = s->next_admin;
4948
4949 /* restore current admin state */
4950 s->next_admin = s->cur_admin;
4951
4952 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4953 s->last_change = now.tv_sec;
4954 if (s->proxy->lbprm.set_server_status_down)
4955 s->proxy->lbprm.set_server_status_down(s);
4956
4957 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4958 srv_shutdown_streams(s, SF_ERR_DOWN);
4959
4960 /* we might have streams queued on this server and waiting for
4961 * a connection. Those which are redispatchable will be queued
4962 * to another server or to the proxy itself.
4963 */
4964 xferred = pendconn_redistribute(s);
4965
4966 tmptrash = alloc_trash_chunk();
4967 if (tmptrash) {
4968 chunk_printf(tmptrash,
4969 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4970 s->proxy->id, s->id);
4971
Emeric Brun5a133512017-10-19 14:42:30 +02004972 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004973 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004974
4975 /* we don't send an alert if the server was previously paused */
4976 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004977 send_log(s->proxy, log_level, "%s.\n",
4978 tmptrash->area);
4979 send_email_alert(s, log_level, "%s",
4980 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004981 free_trash_chunk(tmptrash);
4982 tmptrash = NULL;
4983 }
4984 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4985 set_backend_down(s->proxy);
4986
4987 s->counters.down_trans++;
4988 }
4989 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4990 s->last_change = now.tv_sec;
4991 if (s->proxy->lbprm.set_server_status_down)
4992 s->proxy->lbprm.set_server_status_down(s);
4993
4994 /* we might have streams queued on this server and waiting for
4995 * a connection. Those which are redispatchable will be queued
4996 * to another server or to the proxy itself.
4997 */
4998 xferred = pendconn_redistribute(s);
4999
5000 tmptrash = alloc_trash_chunk();
5001 if (tmptrash) {
5002 chunk_printf(tmptrash,
5003 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5004 s->proxy->id, s->id);
5005
Emeric Brun5a133512017-10-19 14:42:30 +02005006 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005007
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005008 ha_warning("%s.\n", tmptrash->area);
5009 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5010 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005011 free_trash_chunk(tmptrash);
5012 tmptrash = NULL;
5013 }
5014
5015 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5016 set_backend_down(s->proxy);
5017 }
5018 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5019 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
5020 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5021 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5022 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5023 s->proxy->last_change = now.tv_sec;
5024 }
5025
5026 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
5027 s->down_time += now.tv_sec - s->last_change;
5028
5029 s->last_change = now.tv_sec;
5030 if (s->next_state == SRV_ST_STARTING)
5031 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5032
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005033 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005034 /* now propagate the status change to any LB algorithms */
5035 if (px->lbprm.update_server_eweight)
5036 px->lbprm.update_server_eweight(s);
5037 else if (srv_willbe_usable(s)) {
5038 if (px->lbprm.set_server_status_up)
5039 px->lbprm.set_server_status_up(s);
5040 }
5041 else {
5042 if (px->lbprm.set_server_status_down)
5043 px->lbprm.set_server_status_down(s);
5044 }
5045
5046 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5047 * and it's not a backup server and its effective weight is > 0,
5048 * then it can accept new connections, so we shut down all streams
5049 * on all backup servers.
5050 */
5051 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5052 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5053 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5054
5055 /* check if we can handle some connections queued at the proxy. We
5056 * will take as many as we can handle.
5057 */
5058 xferred = pendconn_grab_from_px(s);
5059
5060 tmptrash = alloc_trash_chunk();
5061 if (tmptrash) {
5062 chunk_printf(tmptrash,
5063 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5064 s->proxy->id, s->id);
5065
Emeric Brun5a133512017-10-19 14:42:30 +02005066 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005067 ha_warning("%s.\n", tmptrash->area);
5068 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5069 tmptrash->area);
5070 send_email_alert(s, LOG_NOTICE, "%s",
5071 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005072 free_trash_chunk(tmptrash);
5073 tmptrash = NULL;
5074 }
5075
5076 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5077 set_backend_down(s->proxy);
5078 }
5079 else if (s->cur_eweight != s->next_eweight) {
5080 /* now propagate the status change to any LB algorithms */
5081 if (px->lbprm.update_server_eweight)
5082 px->lbprm.update_server_eweight(s);
5083 else if (srv_willbe_usable(s)) {
5084 if (px->lbprm.set_server_status_up)
5085 px->lbprm.set_server_status_up(s);
5086 }
5087 else {
5088 if (px->lbprm.set_server_status_down)
5089 px->lbprm.set_server_status_down(s);
5090 }
5091
5092 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5093 set_backend_down(s->proxy);
5094 }
5095
5096 s->next_admin = next_admin;
5097 }
5098
Emeric Brun5a133512017-10-19 14:42:30 +02005099 /* reset operational state change */
5100 *s->op_st_chg.reason = 0;
5101 s->op_st_chg.status = s->op_st_chg.code = -1;
5102 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005103
5104 /* Now we try to apply pending admin changes */
5105
5106 /* Maintenance must also disable health checks */
5107 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5108 if (s->check.state & CHK_ST_ENABLED) {
5109 s->check.state |= CHK_ST_PAUSED;
5110 check->health = 0;
5111 }
5112
5113 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005114 tmptrash = alloc_trash_chunk();
5115 if (tmptrash) {
5116 chunk_printf(tmptrash,
5117 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5118 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5119 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005120
Olivier Houchard796a2b32017-10-24 17:42:47 +02005121 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005122
Olivier Houchard796a2b32017-10-24 17:42:47 +02005123 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005124 ha_warning("%s.\n", tmptrash->area);
5125 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5126 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005127 }
5128 free_trash_chunk(tmptrash);
5129 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005130 }
Emeric Brun8f298292017-12-06 16:47:17 +01005131 /* commit new admin status */
5132
5133 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005134 }
5135 else { /* server was still running */
5136 check->health = 0; /* failure */
5137 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005138
5139 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005140 if (s->proxy->lbprm.set_server_status_down)
5141 s->proxy->lbprm.set_server_status_down(s);
5142
Emeric Brun64cc49c2017-10-03 14:46:45 +02005143 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5144 srv_shutdown_streams(s, SF_ERR_DOWN);
5145
5146 /* we might have streams queued on this server and waiting for
5147 * a connection. Those which are redispatchable will be queued
5148 * to another server or to the proxy itself.
5149 */
5150 xferred = pendconn_redistribute(s);
5151
5152 tmptrash = alloc_trash_chunk();
5153 if (tmptrash) {
5154 chunk_printf(tmptrash,
5155 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5156 s->flags & SRV_F_BACKUP ? "Backup " : "",
5157 s->proxy->id, s->id,
5158 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5159
5160 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5161
5162 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005163 ha_warning("%s.\n", tmptrash->area);
5164 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5165 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005166 }
5167 free_trash_chunk(tmptrash);
5168 tmptrash = NULL;
5169 }
5170 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5171 set_backend_down(s->proxy);
5172
5173 s->counters.down_trans++;
5174 }
5175 }
5176 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5177 /* OK here we're leaving maintenance, we have many things to check,
5178 * because the server might possibly be coming back up depending on
5179 * its state. In practice, leaving maintenance means that we should
5180 * immediately turn to UP (more or less the slowstart) under the
5181 * following conditions :
5182 * - server is neither checked nor tracked
5183 * - server tracks another server which is not checked
5184 * - server tracks another server which is already up
5185 * Which sums up as something simpler :
5186 * "either the tracking server is up or the server's checks are disabled
5187 * or up". Otherwise we only re-enable health checks. There's a special
5188 * case associated to the stopping state which can be inherited. Note
5189 * that the server might still be in drain mode, which is naturally dealt
5190 * with by the lower level functions.
5191 */
5192
5193 if (s->check.state & CHK_ST_ENABLED) {
5194 s->check.state &= ~CHK_ST_PAUSED;
5195 check->health = check->rise; /* start OK but check immediately */
5196 }
5197
5198 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5199 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5200 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5201 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5202 s->next_state = SRV_ST_STOPPING;
5203 }
5204 else {
5205 s->next_state = SRV_ST_STARTING;
5206 if (s->slowstart > 0)
5207 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5208 else
5209 s->next_state = SRV_ST_RUNNING;
5210 }
5211
5212 }
5213
5214 tmptrash = alloc_trash_chunk();
5215 if (tmptrash) {
5216 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5217 chunk_printf(tmptrash,
5218 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5219 s->flags & SRV_F_BACKUP ? "Backup " : "",
5220 s->proxy->id, s->id,
5221 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5222 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5223 }
5224 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5225 chunk_printf(tmptrash,
5226 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5227 s->flags & SRV_F_BACKUP ? "Backup " : "",
5228 s->proxy->id, s->id, s->hostname,
5229 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5230 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5231 }
5232 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5233 chunk_printf(tmptrash,
5234 "%sServer %s/%s is %s/%s (leaving maintenance)",
5235 s->flags & SRV_F_BACKUP ? "Backup " : "",
5236 s->proxy->id, s->id,
5237 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5238 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5239 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005240 ha_warning("%s.\n", tmptrash->area);
5241 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5242 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005243 free_trash_chunk(tmptrash);
5244 tmptrash = NULL;
5245 }
5246
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005247 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005248 /* now propagate the status change to any LB algorithms */
5249 if (px->lbprm.update_server_eweight)
5250 px->lbprm.update_server_eweight(s);
5251 else if (srv_willbe_usable(s)) {
5252 if (px->lbprm.set_server_status_up)
5253 px->lbprm.set_server_status_up(s);
5254 }
5255 else {
5256 if (px->lbprm.set_server_status_down)
5257 px->lbprm.set_server_status_down(s);
5258 }
5259
5260 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5261 set_backend_down(s->proxy);
5262
Willy Tarreau6a78e612018-08-07 10:14:53 +02005263 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5264 * and it's not a backup server and its effective weight is > 0,
5265 * then it can accept new connections, so we shut down all streams
5266 * on all backup servers.
5267 */
5268 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5269 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5270 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5271
5272 /* check if we can handle some connections queued at the proxy. We
5273 * will take as many as we can handle.
5274 */
5275 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005276 }
5277 else if (s->next_admin & SRV_ADMF_MAINT) {
5278 /* remaining in maintenance mode, let's inform precisely about the
5279 * situation.
5280 */
5281 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5282 tmptrash = alloc_trash_chunk();
5283 if (tmptrash) {
5284 chunk_printf(tmptrash,
5285 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5286 s->flags & SRV_F_BACKUP ? "Backup " : "",
5287 s->proxy->id, s->id);
5288
5289 if (s->track) /* normally it's mandatory here */
5290 chunk_appendf(tmptrash, " via %s/%s",
5291 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005292 ha_warning("%s.\n", tmptrash->area);
5293 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5294 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005295 free_trash_chunk(tmptrash);
5296 tmptrash = NULL;
5297 }
5298 }
5299 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5300 tmptrash = alloc_trash_chunk();
5301 if (tmptrash) {
5302 chunk_printf(tmptrash,
5303 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5304 s->flags & SRV_F_BACKUP ? "Backup " : "",
5305 s->proxy->id, s->id, s->hostname);
5306
5307 if (s->track) /* normally it's mandatory here */
5308 chunk_appendf(tmptrash, " via %s/%s",
5309 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005310 ha_warning("%s.\n", tmptrash->area);
5311 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5312 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005313 free_trash_chunk(tmptrash);
5314 tmptrash = NULL;
5315 }
5316 }
5317 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5318 tmptrash = alloc_trash_chunk();
5319 if (tmptrash) {
5320 chunk_printf(tmptrash,
5321 "%sServer %s/%s remains in forced maintenance",
5322 s->flags & SRV_F_BACKUP ? "Backup " : "",
5323 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005324 ha_warning("%s.\n", tmptrash->area);
5325 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5326 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005327 free_trash_chunk(tmptrash);
5328 tmptrash = NULL;
5329 }
5330 }
5331 /* don't report anything when leaving drain mode and remaining in maintenance */
5332
5333 s->cur_admin = s->next_admin;
5334 }
5335
5336 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5337 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5338 /* drain state is applied only if not yet in maint */
5339
5340 s->last_change = now.tv_sec;
5341 if (px->lbprm.set_server_status_down)
5342 px->lbprm.set_server_status_down(s);
5343
5344 /* we might have streams queued on this server and waiting for
5345 * a connection. Those which are redispatchable will be queued
5346 * to another server or to the proxy itself.
5347 */
5348 xferred = pendconn_redistribute(s);
5349
5350 tmptrash = alloc_trash_chunk();
5351 if (tmptrash) {
5352 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5353 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5354 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5355
5356 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5357
5358 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005359 ha_warning("%s.\n", tmptrash->area);
5360 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5361 tmptrash->area);
5362 send_email_alert(s, LOG_NOTICE, "%s",
5363 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005364 }
5365 free_trash_chunk(tmptrash);
5366 tmptrash = NULL;
5367 }
5368
5369 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5370 set_backend_down(s->proxy);
5371 }
5372 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5373 /* OK completely leaving drain mode */
5374 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5375 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5376 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5377 s->proxy->last_change = now.tv_sec;
5378 }
5379
5380 if (s->last_change < now.tv_sec) // ignore negative times
5381 s->down_time += now.tv_sec - s->last_change;
5382 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005383 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005384
5385 tmptrash = alloc_trash_chunk();
5386 if (tmptrash) {
5387 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5388 chunk_printf(tmptrash,
5389 "%sServer %s/%s is %s (leaving forced drain)",
5390 s->flags & SRV_F_BACKUP ? "Backup " : "",
5391 s->proxy->id, s->id,
5392 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5393 }
5394 else {
5395 chunk_printf(tmptrash,
5396 "%sServer %s/%s is %s (leaving drain)",
5397 s->flags & SRV_F_BACKUP ? "Backup " : "",
5398 s->proxy->id, s->id,
5399 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5400 if (s->track) /* normally it's mandatory here */
5401 chunk_appendf(tmptrash, " via %s/%s",
5402 s->track->proxy->id, s->track->id);
5403 }
5404
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005405 ha_warning("%s.\n", tmptrash->area);
5406 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5407 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005408 free_trash_chunk(tmptrash);
5409 tmptrash = NULL;
5410 }
5411
5412 /* now propagate the status change to any LB algorithms */
5413 if (px->lbprm.update_server_eweight)
5414 px->lbprm.update_server_eweight(s);
5415 else if (srv_willbe_usable(s)) {
5416 if (px->lbprm.set_server_status_up)
5417 px->lbprm.set_server_status_up(s);
5418 }
5419 else {
5420 if (px->lbprm.set_server_status_down)
5421 px->lbprm.set_server_status_down(s);
5422 }
5423 }
5424 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5425 /* remaining in drain mode after removing one of its flags */
5426
5427 tmptrash = alloc_trash_chunk();
5428 if (tmptrash) {
5429 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5430 chunk_printf(tmptrash,
5431 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5432 s->flags & SRV_F_BACKUP ? "Backup " : "",
5433 s->proxy->id, s->id);
5434
5435 if (s->track) /* normally it's mandatory here */
5436 chunk_appendf(tmptrash, " via %s/%s",
5437 s->track->proxy->id, s->track->id);
5438 }
5439 else {
5440 chunk_printf(tmptrash,
5441 "%sServer %s/%s remains in forced drain mode",
5442 s->flags & SRV_F_BACKUP ? "Backup " : "",
5443 s->proxy->id, s->id);
5444 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005445 ha_warning("%s.\n", tmptrash->area);
5446 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5447 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005448 free_trash_chunk(tmptrash);
5449 tmptrash = NULL;
5450 }
5451
5452 /* commit new admin status */
5453
5454 s->cur_admin = s->next_admin;
5455 }
5456 }
5457
5458 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005459 *s->adm_st_chg_cause = 0;
5460}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005461
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005462struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5463{
5464 struct connection *conn;
5465
5466 while ((conn = LIST_POP_LOCKED(&toremove_connections[tid],
5467 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005468 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005469 }
5470
5471 return task;
5472}
5473
Willy Tarreau980855b2019-02-07 14:59:29 +01005474struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005475{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005476 struct server *srv;
5477 struct eb32_node *eb;
5478 int i;
5479 unsigned int next_wakeup;
5480 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005481
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005482 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5483 while (1) {
5484 int srv_is_empty = 1;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005485
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005486 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5487 if (!eb) {
5488 /* we might have reached the end of the tree, typically because
5489 * <now_ms> is in the first half and we're first scanning the last
5490 * half. Let's loop back to the beginning of the tree now.
5491 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005492
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005493 eb = eb32_first(&idle_conn_srv);
5494 if (likely(!eb))
5495 break;
5496 }
5497 if (tick_is_lt(now_ms, eb->key)) {
5498 /* timer not expired yet, revisit it later */
5499 next_wakeup = eb->key;
5500 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005501 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005502 }
5503 srv = eb32_entry(eb, struct server, idle_node);
5504 for (i = 0; i < global.nbthread; i++) {
5505 int max_conn = (srv->curr_idle_thr[i] / 2) +
5506 (srv->curr_idle_thr[i] & 1);
5507 int j;
5508 int did_remove = 0;
5509
Olivier Houchardba936e52019-07-11 15:49:00 +02005510 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005511 for (j = 0; j < max_conn; j++) {
5512 struct connection *conn = LIST_POP_LOCKED(&srv->idle_orphan_conns[i], struct connection *, list);
5513 if (!conn)
5514 break;
5515 did_remove = 1;
5516 LIST_ADDQ_LOCKED(&toremove_connections[i], &conn->list);
5517 }
Olivier Houchardba936e52019-07-11 15:49:00 +02005518 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005519 if (did_remove && max_conn < srv->curr_idle_thr[i])
5520 srv_is_empty = 0;
5521 if (did_remove)
5522 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5523 }
5524 eb32_delete(&srv->idle_node);
5525 if (!srv_is_empty) {
5526 /* There are still more idle connections, add the
5527 * server back in the tree.
5528 */
5529 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5530 now_ms);
5531 eb32_insert(&idle_conn_srv, &srv->idle_node);
5532 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005533 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005534 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5535
5536 if (need_wakeup)
5537 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005538 else
5539 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005540
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005541 return task;
5542}
Olivier Houchard88698d92019-04-16 19:07:22 +02005543
5544/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5545static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5546 struct proxy *defpx, const char *file, int line,
5547 char **err)
5548{
5549 int arg = -1;
5550
5551 if (too_many_args(1, args, err, NULL))
5552 return -1;
5553
5554 if (*(args[1]) != 0)
5555 arg = atoi(args[1]);
5556
5557 if (arg < 0 || arg > 100) {
5558 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5559 return -1;
5560 }
5561
5562 if (args[0][10] == 'h')
5563 global.tune.pool_high_ratio = arg;
5564 else
5565 global.tune.pool_low_ratio = arg;
5566 return 0;
5567}
5568
5569/* config keyword parsers */
5570static struct cfg_kw_list cfg_kws = {ILH, {
5571 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5572 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5573 { 0, NULL, NULL }
5574}};
5575
5576INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5577
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005578/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005579 * Local variables:
5580 * c-indent-level: 8
5581 * c-basic-offset: 8
5582 * End:
5583 */