blob: 0104984da50bc728360a1eb1e43677dcd8e065ce [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];
65
Frédéric Lécaille7da71292019-05-20 09:47:07 +020066/* The server names dictionary */
67struct dict server_name_dict = {
68 .name = "server names",
69 .values = EB_ROOT_UNIQUE,
70};
71
Simon Hormana3608442013-11-01 16:46:15 +090072int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020073{
Emeric Brun52a91d32017-08-31 14:41:55 +020074 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020075 return s->down_time;
76
77 return now.tv_sec - s->last_change + s->down_time;
78}
Willy Tarreaubaaee002006-06-26 02:48:02 +020079
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050080int srv_lastsession(const struct server *s)
81{
82 if (s->counters.last_sess)
83 return now.tv_sec - s->counters.last_sess;
84
85 return -1;
86}
87
Simon Horman4a741432013-02-23 15:35:38 +090088int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020089{
Simon Horman4a741432013-02-23 15:35:38 +090090 const struct server *s = check->server;
91
Willy Tarreauff5ae352013-12-11 20:36:34 +010092 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090093 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010094
Emeric Brun52a91d32017-08-31 14:41:55 +020095 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090096 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010097
Simon Horman4a741432013-02-23 15:35:38 +090098 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010099}
100
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100101/*
102 * Check that we did not get a hash collision.
103 * Unlikely, but it can happen.
104 */
105static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100106{
107 struct proxy *p = s->proxy;
108 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100109
110 for (tmpserv = p->srv; tmpserv != NULL;
111 tmpserv = tmpserv->next) {
112 if (tmpserv == s)
113 continue;
114 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
115 continue;
116 if (tmpserv->cookie &&
117 strcmp(tmpserv->cookie, s->cookie) == 0) {
118 ha_warning("We generated two equal cookies for two different servers.\n"
119 "Please change the secret key for '%s'.\n",
120 s->proxy->id);
121 }
122 }
123
124}
125
Willy Tarreau46b7f532018-08-21 11:54:26 +0200126/*
127 * Must be called with the server lock held.
128 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100129void srv_set_dyncookie(struct server *s)
130{
131 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100132 char *tmpbuf;
133 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100134 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100135 size_t buffer_len;
136 int addr_len;
137 int port;
138
139 if ((s->flags & SRV_F_COOKIESET) ||
140 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
141 s->proxy->dyncookie_key == NULL)
142 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100143 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100144
145 if (s->addr.ss_family != AF_INET &&
146 s->addr.ss_family != AF_INET6)
147 return;
148 /*
149 * Buffer to calculate the cookie value.
150 * The buffer contains the secret key + the server IP address
151 * + the TCP port.
152 */
153 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
154 /*
155 * The TCP port should use only 2 bytes, but is stored in
156 * an unsigned int in struct server, so let's use 4, to be
157 * on the safe side.
158 */
159 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200160 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100161 memcpy(tmpbuf, p->dyncookie_key, key_len);
162 memcpy(&(tmpbuf[key_len]),
163 s->addr.ss_family == AF_INET ?
164 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
165 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
166 addr_len);
167 /*
168 * Make sure it's the same across all the load balancers,
169 * no matter their endianness.
170 */
171 port = htonl(s->svc_port);
172 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
173 hash_value = XXH64(tmpbuf, buffer_len, 0);
174 memprintf(&s->cookie, "%016llx", hash_value);
175 if (!s->cookie)
176 return;
177 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100178
179 /* Don't bother checking if the dyncookie is duplicated if
180 * the server is marked as "disabled", maybe it doesn't have
181 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100182 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100183 if (!(s->next_admin & SRV_ADMF_FMAINT))
184 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100185}
186
Willy Tarreau21faa912012-10-10 08:27:36 +0200187/*
188 * Registers the server keyword list <kwl> as a list of valid keywords for next
189 * parsing sessions.
190 */
191void srv_register_keywords(struct srv_kw_list *kwl)
192{
193 LIST_ADDQ(&srv_keywords.list, &kwl->list);
194}
195
196/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
197 * keyword is found with a NULL ->parse() function, then an attempt is made to
198 * find one with a valid ->parse() function. This way it is possible to declare
199 * platform-dependant, known keywords as NULL, then only declare them as valid
200 * if some options are met. Note that if the requested keyword contains an
201 * opening parenthesis, everything from this point is ignored.
202 */
203struct srv_kw *srv_find_kw(const char *kw)
204{
205 int index;
206 const char *kwend;
207 struct srv_kw_list *kwl;
208 struct srv_kw *ret = NULL;
209
210 kwend = strchr(kw, '(');
211 if (!kwend)
212 kwend = kw + strlen(kw);
213
214 list_for_each_entry(kwl, &srv_keywords.list, list) {
215 for (index = 0; kwl->kw[index].kw != NULL; index++) {
216 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
217 kwl->kw[index].kw[kwend-kw] == 0) {
218 if (kwl->kw[index].parse)
219 return &kwl->kw[index]; /* found it !*/
220 else
221 ret = &kwl->kw[index]; /* may be OK */
222 }
223 }
224 }
225 return ret;
226}
227
228/* Dumps all registered "server" keywords to the <out> string pointer. The
229 * unsupported keywords are only dumped if their supported form was not
230 * found.
231 */
232void srv_dump_kws(char **out)
233{
234 struct srv_kw_list *kwl;
235 int index;
236
237 *out = NULL;
238 list_for_each_entry(kwl, &srv_keywords.list, list) {
239 for (index = 0; kwl->kw[index].kw != NULL; index++) {
240 if (kwl->kw[index].parse ||
241 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
242 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
243 kwl->scope,
244 kwl->kw[index].kw,
245 kwl->kw[index].skip ? " <arg>" : "",
246 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
247 kwl->kw[index].parse ? "" : " (not supported)");
248 }
249 }
250 }
251}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100252
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100253/* Parse the "addr" server keyword */
254static int srv_parse_addr(char **args, int *cur_arg,
255 struct proxy *curproxy, struct server *newsrv, char **err)
256{
257 char *errmsg, *arg;
258 struct sockaddr_storage *sk;
259 int port1, port2;
260 struct protocol *proto;
261
262 errmsg = NULL;
263 arg = args[*cur_arg + 1];
264
265 if (!*arg) {
266 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
267 goto err;
268 }
269
270 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
271 if (!sk) {
272 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
273 goto err;
274 }
275
276 proto = protocol_by_family(sk->ss_family);
277 if (!proto || !proto->connect) {
278 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
279 args[*cur_arg], arg);
280 goto err;
281 }
282
283 if (port1 != port2) {
284 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
285 args[*cur_arg], arg);
286 goto err;
287 }
288
289 newsrv->check.addr = newsrv->agent.addr = *sk;
290 newsrv->flags |= SRV_F_CHECKADDR;
291 newsrv->flags |= SRV_F_AGENTADDR;
292
293 return 0;
294
295 err:
296 free(errmsg);
297 return ERR_ALERT | ERR_FATAL;
298}
299
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100300/* Parse the "agent-check" server keyword */
301static int srv_parse_agent_check(char **args, int *cur_arg,
302 struct proxy *curproxy, struct server *newsrv, char **err)
303{
304 newsrv->do_agent = 1;
305 return 0;
306}
307
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100308/* Parse the "backup" server keyword */
309static int srv_parse_backup(char **args, int *cur_arg,
310 struct proxy *curproxy, struct server *newsrv, char **err)
311{
312 newsrv->flags |= SRV_F_BACKUP;
313 return 0;
314}
315
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100316/* Parse the "check" server keyword */
317static int srv_parse_check(char **args, int *cur_arg,
318 struct proxy *curproxy, struct server *newsrv, char **err)
319{
320 newsrv->do_check = 1;
321 return 0;
322}
323
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100324/* Parse the "check-send-proxy" server keyword */
325static int srv_parse_check_send_proxy(char **args, int *cur_arg,
326 struct proxy *curproxy, struct server *newsrv, char **err)
327{
328 newsrv->check.send_proxy = 1;
329 return 0;
330}
331
Alexander Liu2a54bb72019-05-22 19:44:48 +0800332/* Parse the "check-via-socks4" server keyword */
333static int srv_parse_check_via_socks4(char **args, int *cur_arg,
334 struct proxy *curproxy, struct server *newsrv, char **err)
335{
336 newsrv->check.via_socks4 = 1;
337 return 0;
338}
339
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100340/* Parse the "cookie" server keyword */
341static int srv_parse_cookie(char **args, int *cur_arg,
342 struct proxy *curproxy, struct server *newsrv, char **err)
343{
344 char *arg;
345
346 arg = args[*cur_arg + 1];
347 if (!*arg) {
348 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
349 return ERR_ALERT | ERR_FATAL;
350 }
351
352 free(newsrv->cookie);
353 newsrv->cookie = strdup(arg);
354 newsrv->cklen = strlen(arg);
355 newsrv->flags |= SRV_F_COOKIESET;
356 return 0;
357}
358
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100359/* Parse the "disabled" server keyword */
360static int srv_parse_disabled(char **args, int *cur_arg,
361 struct proxy *curproxy, struct server *newsrv, char **err)
362{
Emeric Brun52a91d32017-08-31 14:41:55 +0200363 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
364 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100365 newsrv->check.state |= CHK_ST_PAUSED;
366 newsrv->check.health = 0;
367 return 0;
368}
369
370/* Parse the "enabled" server keyword */
371static int srv_parse_enabled(char **args, int *cur_arg,
372 struct proxy *curproxy, struct server *newsrv, char **err)
373{
Emeric Brun52a91d32017-08-31 14:41:55 +0200374 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
375 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100376 newsrv->check.state &= ~CHK_ST_PAUSED;
377 newsrv->check.health = newsrv->check.rise;
378 return 0;
379}
380
Willy Tarreau9c538e02019-01-23 10:21:49 +0100381static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
382{
383 char *arg;
384
385 arg = args[*cur_arg + 1];
386 if (!*arg) {
387 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
388 return ERR_ALERT | ERR_FATAL;
389 }
390 newsrv->max_reuse = atoi(arg);
391
392 return 0;
393}
394
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100395static 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 +0100396{
397 const char *res;
398 char *arg;
399 unsigned int time;
400
401 arg = args[*cur_arg + 1];
402 if (!*arg) {
403 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
404 return ERR_ALERT | ERR_FATAL;
405 }
406 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200407 if (res == PARSE_TIME_OVER) {
408 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
409 args[*cur_arg+1], args[*cur_arg]);
410 return ERR_ALERT | ERR_FATAL;
411 }
412 else if (res == PARSE_TIME_UNDER) {
413 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
414 args[*cur_arg+1], args[*cur_arg]);
415 return ERR_ALERT | ERR_FATAL;
416 }
417 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100418 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
419 *res, args[*cur_arg]);
420 return ERR_ALERT | ERR_FATAL;
421 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100422 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100423
424 return 0;
425}
426
Olivier Houchard006e3102018-12-10 18:30:32 +0100427static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
428{
429 char *arg;
430
431 arg = args[*cur_arg + 1];
432 if (!*arg) {
433 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
434 return ERR_ALERT | ERR_FATAL;
435 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100436
Olivier Houchard006e3102018-12-10 18:30:32 +0100437 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100438 if ((int)newsrv->max_idle_conns < -1) {
439 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
440 return ERR_ALERT | ERR_FATAL;
441 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100442
443 return 0;
444}
445
Willy Tarreaudff55432012-10-10 17:51:05 +0200446/* parse the "id" server keyword */
447static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
448{
449 struct eb32_node *node;
450
451 if (!*args[*cur_arg + 1]) {
452 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
453 return ERR_ALERT | ERR_FATAL;
454 }
455
456 newsrv->puid = atol(args[*cur_arg + 1]);
457 newsrv->conf.id.key = newsrv->puid;
458
459 if (newsrv->puid <= 0) {
460 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
461 return ERR_ALERT | ERR_FATAL;
462 }
463
464 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
465 if (node) {
466 struct server *target = container_of(node, struct server, conf.id);
467 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
468 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
469 target->id);
470 return ERR_ALERT | ERR_FATAL;
471 }
472
473 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200474 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200475 return 0;
476}
477
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100478/* Parse the "namespace" server keyword */
479static int srv_parse_namespace(char **args, int *cur_arg,
480 struct proxy *curproxy, struct server *newsrv, char **err)
481{
Willy Tarreaue5733232019-05-22 19:24:06 +0200482#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100483 char *arg;
484
485 arg = args[*cur_arg + 1];
486 if (!*arg) {
487 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
488 return ERR_ALERT | ERR_FATAL;
489 }
490
491 if (!strcmp(arg, "*")) {
492 /* Use the namespace associated with the connection (if present). */
493 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
494 return 0;
495 }
496
497 /*
498 * As this parser may be called several times for the same 'default-server'
499 * object, or for a new 'server' instance deriving from a 'default-server'
500 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
501 */
502 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
503
504 newsrv->netns = netns_store_lookup(arg, strlen(arg));
505 if (!newsrv->netns)
506 newsrv->netns = netns_store_insert(arg);
507
508 if (!newsrv->netns) {
509 memprintf(err, "Cannot open namespace '%s'", arg);
510 return ERR_ALERT | ERR_FATAL;
511 }
512
513 return 0;
514#else
515 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
516 return ERR_ALERT | ERR_FATAL;
517#endif
518}
519
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100520/* Parse the "no-agent-check" server keyword */
521static int srv_parse_no_agent_check(char **args, int *cur_arg,
522 struct proxy *curproxy, struct server *newsrv, char **err)
523{
524 free_check(&newsrv->agent);
525 newsrv->agent.inter = 0;
526 newsrv->agent.port = 0;
527 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
528 newsrv->do_agent = 0;
529 return 0;
530}
531
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100532/* Parse the "no-backup" server keyword */
533static int srv_parse_no_backup(char **args, int *cur_arg,
534 struct proxy *curproxy, struct server *newsrv, char **err)
535{
536 newsrv->flags &= ~SRV_F_BACKUP;
537 return 0;
538}
539
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100540/* Parse the "no-check" server keyword */
541static int srv_parse_no_check(char **args, int *cur_arg,
542 struct proxy *curproxy, struct server *newsrv, char **err)
543{
544 free_check(&newsrv->check);
545 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
546 newsrv->do_check = 0;
547 return 0;
548}
549
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100550/* Parse the "no-check-send-proxy" server keyword */
551static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
552 struct proxy *curproxy, struct server *newsrv, char **err)
553{
554 newsrv->check.send_proxy = 0;
555 return 0;
556}
557
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100558/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200559static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100560{
561 srv->pp_opts &= ~flags;
562 return 0;
563}
564
565/* Parse the "no-send-proxy" server keyword */
566static int srv_parse_no_send_proxy(char **args, int *cur_arg,
567 struct proxy *curproxy, struct server *newsrv, char **err)
568{
569 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
570}
571
572/* Parse the "no-send-proxy-v2" server keyword */
573static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
574 struct proxy *curproxy, struct server *newsrv, char **err)
575{
576 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
577}
578
Frédéric Lécailleaeeb1c92019-07-04 14:19:06 +0200579/* Parse the "no-tfo" server keyword */
580static int srv_parse_no_tfo(char **args, int *cur_arg,
581 struct proxy *curproxy, struct server *newsrv, char **err)
582{
583 newsrv->flags &= ~SRV_F_FASTOPEN;
584 return 0;
585}
586
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100587/* Parse the "non-stick" server keyword */
588static int srv_parse_non_stick(char **args, int *cur_arg,
589 struct proxy *curproxy, struct server *newsrv, char **err)
590{
591 newsrv->flags |= SRV_F_NON_STICK;
592 return 0;
593}
594
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100595/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200596static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100597{
598 srv->pp_opts |= flags;
599 return 0;
600}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200601/* parse the "proto" server keyword */
602static int srv_parse_proto(char **args, int *cur_arg,
603 struct proxy *px, struct server *newsrv, char **err)
604{
605 struct ist proto;
606
607 if (!*args[*cur_arg + 1]) {
608 memprintf(err, "'%s' : missing value", args[*cur_arg]);
609 return ERR_ALERT | ERR_FATAL;
610 }
611 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
612 newsrv->mux_proto = get_mux_proto(proto);
613 if (!newsrv->mux_proto) {
614 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
615 return ERR_ALERT | ERR_FATAL;
616 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200617 return 0;
618}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100619
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100620/* parse the "proxy-v2-options" */
621static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
622 struct proxy *px, struct server *newsrv, char **err)
623{
624 char *p, *n;
625 for (p = args[*cur_arg+1]; p; p = n) {
626 n = strchr(p, ',');
627 if (n)
628 *n++ = '\0';
629 if (!strcmp(p, "ssl")) {
630 newsrv->pp_opts |= SRV_PP_V2_SSL;
631 } else if (!strcmp(p, "cert-cn")) {
632 newsrv->pp_opts |= SRV_PP_V2_SSL;
633 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100634 } else if (!strcmp(p, "cert-key")) {
635 newsrv->pp_opts |= SRV_PP_V2_SSL;
636 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
637 } else if (!strcmp(p, "cert-sig")) {
638 newsrv->pp_opts |= SRV_PP_V2_SSL;
639 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
640 } else if (!strcmp(p, "ssl-cipher")) {
641 newsrv->pp_opts |= SRV_PP_V2_SSL;
642 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100643 } else if (!strcmp(p, "authority")) {
644 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100645 } else if (!strcmp(p, "crc32c")) {
646 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100647 } else
648 goto fail;
649 }
650 return 0;
651 fail:
652 if (err)
653 memprintf(err, "'%s' : proxy v2 option not implemented", p);
654 return ERR_ALERT | ERR_FATAL;
655}
656
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100657/* Parse the "observe" server keyword */
658static int srv_parse_observe(char **args, int *cur_arg,
659 struct proxy *curproxy, struct server *newsrv, char **err)
660{
661 char *arg;
662
663 arg = args[*cur_arg + 1];
664 if (!*arg) {
665 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
666 return ERR_ALERT | ERR_FATAL;
667 }
668
669 if (!strcmp(arg, "none")) {
670 newsrv->observe = HANA_OBS_NONE;
671 }
672 else if (!strcmp(arg, "layer4")) {
673 newsrv->observe = HANA_OBS_LAYER4;
674 }
675 else if (!strcmp(arg, "layer7")) {
676 if (curproxy->mode != PR_MODE_HTTP) {
677 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
678 return ERR_ALERT;
679 }
680 newsrv->observe = HANA_OBS_LAYER7;
681 }
682 else {
683 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
684 "but got '%s'\n", args[*cur_arg], arg);
685 return ERR_ALERT | ERR_FATAL;
686 }
687
688 return 0;
689}
690
Frédéric Lécaille16186232017-03-14 16:42:49 +0100691/* Parse the "redir" server keyword */
692static int srv_parse_redir(char **args, int *cur_arg,
693 struct proxy *curproxy, struct server *newsrv, char **err)
694{
695 char *arg;
696
697 arg = args[*cur_arg + 1];
698 if (!*arg) {
699 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
700 return ERR_ALERT | ERR_FATAL;
701 }
702
703 free(newsrv->rdr_pfx);
704 newsrv->rdr_pfx = strdup(arg);
705 newsrv->rdr_len = strlen(arg);
706
707 return 0;
708}
709
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100710/* Parse the "send-proxy" server keyword */
711static int srv_parse_send_proxy(char **args, int *cur_arg,
712 struct proxy *curproxy, struct server *newsrv, char **err)
713{
714 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
715}
716
717/* Parse the "send-proxy-v2" server keyword */
718static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
719 struct proxy *curproxy, struct server *newsrv, char **err)
720{
721 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
722}
723
Frédéric Lécailledba97072017-03-17 15:33:50 +0100724
725/* Parse the "source" server keyword */
726static int srv_parse_source(char **args, int *cur_arg,
727 struct proxy *curproxy, struct server *newsrv, char **err)
728{
729 char *errmsg;
730 int port_low, port_high;
731 struct sockaddr_storage *sk;
732 struct protocol *proto;
733
734 errmsg = NULL;
735
736 if (!*args[*cur_arg + 1]) {
737 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
738 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
739 goto err;
740 }
741
742 /* 'sk' is statically allocated (no need to be freed). */
743 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
744 if (!sk) {
745 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
746 goto err;
747 }
748
749 proto = protocol_by_family(sk->ss_family);
750 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100751 ha_alert("'%s %s' : connect() not supported for this address family.\n",
752 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100753 goto err;
754 }
755
756 newsrv->conn_src.opts |= CO_SRC_BIND;
757 newsrv->conn_src.source_addr = *sk;
758
759 if (port_low != port_high) {
760 int i;
761
762 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100763 ha_alert("'%s' does not support port offsets (found '%s').\n",
764 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100765 goto err;
766 }
767
768 if (port_low <= 0 || port_low > 65535 ||
769 port_high <= 0 || port_high > 65535 ||
770 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100771 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 +0100772 goto err;
773 }
774 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
775 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
776 newsrv->conn_src.sport_range->ports[i] = port_low + i;
777 }
778
779 *cur_arg += 2;
780 while (*(args[*cur_arg])) {
781 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
782#if defined(CONFIG_HAP_TRANSPARENT)
783 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100784 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
785 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100786 goto err;
787 }
788 if (!strcmp(args[*cur_arg + 1], "client")) {
789 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
790 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
791 }
792 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
793 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
794 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
795 }
796 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
797 char *name, *end;
798
799 name = args[*cur_arg + 1] + 7;
800 while (isspace(*name))
801 name++;
802
803 end = name;
804 while (*end && !isspace(*end) && *end != ',' && *end != ')')
805 end++;
806
807 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
808 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
809 free(newsrv->conn_src.bind_hdr_name);
810 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
811 newsrv->conn_src.bind_hdr_len = end - name;
812 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
813 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
814 newsrv->conn_src.bind_hdr_occ = -1;
815
816 /* now look for an occurrence number */
817 while (isspace(*end))
818 end++;
819 if (*end == ',') {
820 end++;
821 name = end;
822 if (*end == '-')
823 end++;
824 while (isdigit((int)*end))
825 end++;
826 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
827 }
828
829 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100830 ha_alert("usesrc hdr_ip(name,num) does not support negative"
831 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100832 goto err;
833 }
834 }
835 else {
836 struct sockaddr_storage *sk;
837 int port1, port2;
838
839 /* 'sk' is statically allocated (no need to be freed). */
840 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
841 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100842 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100843 goto err;
844 }
845
846 proto = protocol_by_family(sk->ss_family);
847 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100848 ha_alert("'%s %s' : connect() not supported for this address family.\n",
849 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100850 goto err;
851 }
852
853 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100854 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
855 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100856 goto err;
857 }
858 newsrv->conn_src.tproxy_addr = *sk;
859 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
860 }
861 global.last_checks |= LSTCHK_NETADM;
862 *cur_arg += 2;
863 continue;
864#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100865 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 +0100866 goto err;
867#endif /* defined(CONFIG_HAP_TRANSPARENT) */
868 } /* "usesrc" */
869
870 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
871#ifdef SO_BINDTODEVICE
872 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100873 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100874 goto err;
875 }
876 free(newsrv->conn_src.iface_name);
877 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
878 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
879 global.last_checks |= LSTCHK_NETADM;
880#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100881 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100882 goto err;
883#endif
884 *cur_arg += 2;
885 continue;
886 }
887 /* this keyword in not an option of "source" */
888 break;
889 } /* while */
890
891 return 0;
892
893 err:
894 free(errmsg);
895 return ERR_ALERT | ERR_FATAL;
896}
897
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100898/* Parse the "stick" server keyword */
899static int srv_parse_stick(char **args, int *cur_arg,
900 struct proxy *curproxy, struct server *newsrv, char **err)
901{
902 newsrv->flags &= ~SRV_F_NON_STICK;
903 return 0;
904}
905
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100906/* Parse the "track" server keyword */
907static int srv_parse_track(char **args, int *cur_arg,
908 struct proxy *curproxy, struct server *newsrv, char **err)
909{
910 char *arg;
911
912 arg = args[*cur_arg + 1];
913 if (!*arg) {
914 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
915 return ERR_ALERT | ERR_FATAL;
916 }
917
918 free(newsrv->trackit);
919 newsrv->trackit = strdup(arg);
920
921 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800922}
923
924/* Parse the "socks4" server keyword */
925static int srv_parse_socks4(char **args, int *cur_arg,
926 struct proxy *curproxy, struct server *newsrv, char **err)
927{
928 char *errmsg;
929 int port_low, port_high;
930 struct sockaddr_storage *sk;
931 struct protocol *proto;
932
933 errmsg = NULL;
934
935 if (!*args[*cur_arg + 1]) {
936 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
937 goto err;
938 }
939
940 /* 'sk' is statically allocated (no need to be freed). */
941 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
942 if (!sk) {
943 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
944 goto err;
945 }
946
947 proto = protocol_by_family(sk->ss_family);
948 if (!proto || !proto->connect) {
949 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
950 goto err;
951 }
952
953 newsrv->flags |= SRV_F_SOCKS4_PROXY;
954 newsrv->socks4_addr = *sk;
955
956 if (port_low != port_high) {
957 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
958 goto err;
959 }
960
961 if (!port_low) {
962 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
963 goto err;
964 }
965
966 return 0;
967
968 err:
969 free(errmsg);
970 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100971}
972
Frédéric Lécailledba97072017-03-17 15:33:50 +0100973
Willy Tarreau034c88c2017-01-23 23:36:45 +0100974/* parse the "tfo" server keyword */
975static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
976{
977 newsrv->flags |= SRV_F_FASTOPEN;
978 return 0;
979}
980
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200981/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200982 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200983 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200984 *
985 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200986 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200987void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200988{
Willy Tarreau87b09662015-04-03 00:22:06 +0200989 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200990
Willy Tarreau87b09662015-04-03 00:22:06 +0200991 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
992 if (stream->srv_conn == srv)
993 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200994}
995
996/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200997 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200998 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200999 *
1000 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001001 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001002void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001003{
1004 struct server *srv;
1005
1006 for (srv = px->srv; srv != NULL; srv = srv->next)
1007 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001008 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001009}
1010
Willy Tarreaubda92272014-05-20 21:55:30 +02001011/* Appends some information to a message string related to a server going UP or
1012 * DOWN. If both <forced> and <reason> are null and the server tracks another
1013 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001014 * If <check> is non-null, an entire string describing the check result will be
1015 * appended after a comma and a space (eg: to report some information from the
1016 * check that changed the state). In the other case, the string will be built
1017 * using the check results stored into the struct server if present.
1018 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001019 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001020 *
1021 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001022 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001023void srv_append_status(struct buffer *msg, struct server *s,
1024 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001025{
Emeric Brun5a133512017-10-19 14:42:30 +02001026 short status = s->op_st_chg.status;
1027 short code = s->op_st_chg.code;
1028 long duration = s->op_st_chg.duration;
1029 char *desc = s->op_st_chg.reason;
1030
1031 if (check) {
1032 status = check->status;
1033 code = check->code;
1034 duration = check->duration;
1035 desc = check->desc;
1036 }
1037
1038 if (status != -1) {
1039 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1040
1041 if (status >= HCHK_STATUS_L57DATA)
1042 chunk_appendf(msg, ", code: %d", code);
1043
1044 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001045 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001046
1047 chunk_appendf(msg, ", info: \"");
1048
1049 chunk_initlen(&src, desc, 0, strlen(desc));
1050 chunk_asciiencode(msg, &src, '"');
1051
1052 chunk_appendf(msg, "\"");
1053 }
1054
1055 if (duration >= 0)
1056 chunk_appendf(msg, ", check duration: %ldms", duration);
1057 }
1058 else if (desc && *desc) {
1059 chunk_appendf(msg, ", %s", desc);
1060 }
1061 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001062 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001063 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001064
1065 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001066 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001067 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1068 " %d sessions active, %d requeued, %d remaining in queue",
1069 s->proxy->srv_act, s->proxy->srv_bck,
1070 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1071 s->cur_sess, xferred, s->nbpend);
1072 else
1073 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1074 " %d sessions requeued, %d total in queue",
1075 s->proxy->srv_act, s->proxy->srv_bck,
1076 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1077 xferred, s->nbpend);
1078 }
1079}
1080
Emeric Brun5a133512017-10-19 14:42:30 +02001081/* Marks server <s> down, regardless of its checks' statuses. The server is
1082 * registered in a list to postpone the counting of the remaining servers on
1083 * the proxy and transfers queued streams whenever possible to other servers at
1084 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1085 * non-null as the reason for going down or the available data from the check
1086 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001087 *
1088 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001089 */
Emeric Brun5a133512017-10-19 14:42:30 +02001090void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001091{
1092 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001093
Emeric Brun64cc49c2017-10-03 14:46:45 +02001094 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001095 return;
1096
Emeric Brun52a91d32017-08-31 14:41:55 +02001097 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001098 *s->op_st_chg.reason = 0;
1099 s->op_st_chg.status = -1;
1100 if (reason) {
1101 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1102 }
1103 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001104 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001105 s->op_st_chg.code = check->code;
1106 s->op_st_chg.status = check->status;
1107 s->op_st_chg.duration = check->duration;
1108 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001109
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001110 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001111 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001112
Emeric Brun9f0b4582017-10-23 14:39:51 +02001113 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001114 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001115 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001116 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001117 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001118}
1119
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001120/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001121 * in maintenance. The server is registered in a list to postpone the counting
1122 * of the remaining servers on the proxy and tries to grab requests from the
1123 * proxy at a sync point. Maintenance servers are ignored. It stores the
1124 * <reason> if non-null as the reason for going down or the available data
1125 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001126 *
1127 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001128 */
Emeric Brun5a133512017-10-19 14:42:30 +02001129void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001130{
1131 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001132
Emeric Brun64cc49c2017-10-03 14:46:45 +02001133 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001134 return;
1135
Emeric Brun52a91d32017-08-31 14:41:55 +02001136 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001137 return;
1138
Emeric Brun52a91d32017-08-31 14:41:55 +02001139 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001140 *s->op_st_chg.reason = 0;
1141 s->op_st_chg.status = -1;
1142 if (reason) {
1143 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1144 }
1145 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001146 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001147 s->op_st_chg.code = check->code;
1148 s->op_st_chg.status = check->status;
1149 s->op_st_chg.duration = check->duration;
1150 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001151
Emeric Brun64cc49c2017-10-03 14:46:45 +02001152 if (s->slowstart <= 0)
1153 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001154
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001155 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001156 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001157
Emeric Brun9f0b4582017-10-23 14:39:51 +02001158 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001159 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001160 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001161 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001162 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001163}
1164
Willy Tarreau8eb77842014-05-21 13:54:57 +02001165/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001166 * isn't in maintenance. The server is registered in a list to postpone the
1167 * counting of the remaining servers on the proxy and tries to grab requests
1168 * from the proxy. Maintenance servers are ignored. It stores the
1169 * <reason> if non-null as the reason for going down or the available data
1170 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001171 * up. Note that it makes use of the trash to build the log strings, so <reason>
1172 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001173 *
1174 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001175 */
Emeric Brun5a133512017-10-19 14:42:30 +02001176void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001177{
1178 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001179
Emeric Brun64cc49c2017-10-03 14:46:45 +02001180 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001181 return;
1182
Emeric Brun52a91d32017-08-31 14:41:55 +02001183 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001184 return;
1185
Emeric Brun52a91d32017-08-31 14:41:55 +02001186 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001187 *s->op_st_chg.reason = 0;
1188 s->op_st_chg.status = -1;
1189 if (reason) {
1190 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1191 }
1192 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001193 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001194 s->op_st_chg.code = check->code;
1195 s->op_st_chg.status = check->status;
1196 s->op_st_chg.duration = check->duration;
1197 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001198
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001199 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001200 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001201
Emeric Brun9f0b4582017-10-23 14:39:51 +02001202 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001203 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001204 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001205 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001206 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001207}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001208
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001209/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1210 * enforce either maint mode or drain mode. It is not allowed to set more than
1211 * one flag at once. The equivalent "inherited" flag is propagated to all
1212 * tracking servers. Maintenance mode disables health checks (but not agent
1213 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001214 * is done. If <cause> is non-null, it will be displayed at the end of the log
1215 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001216 *
1217 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001218 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001219void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001220{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001221 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001222
1223 if (!mode)
1224 return;
1225
1226 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001227 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001228 return;
1229
Emeric Brun52a91d32017-08-31 14:41:55 +02001230 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001231 if (cause)
1232 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1233
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001234 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001235 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001236
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001237 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001238 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1239 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001240 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001241
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001242 /* compute the inherited flag to propagate */
1243 if (mode & SRV_ADMF_MAINT)
1244 mode = SRV_ADMF_IMAINT;
1245 else if (mode & SRV_ADMF_DRAIN)
1246 mode = SRV_ADMF_IDRAIN;
1247
Emeric Brun9f0b4582017-10-23 14:39:51 +02001248 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001249 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001250 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001251 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001252 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001253}
1254
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001255/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1256 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1257 * than one flag at once. The equivalent "inherited" flag is propagated to all
1258 * tracking servers. Leaving maintenance mode re-enables health checks. When
1259 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001260 *
1261 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001262 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001263void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001264{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001265 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001266
1267 if (!mode)
1268 return;
1269
1270 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001271 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001272 return;
1273
Emeric Brun52a91d32017-08-31 14:41:55 +02001274 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001275
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001276 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001277 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001278
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001279 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001280 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1281 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001282 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001283
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001284 if (mode & SRV_ADMF_MAINT)
1285 mode = SRV_ADMF_IMAINT;
1286 else if (mode & SRV_ADMF_DRAIN)
1287 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001288
Emeric Brun9f0b4582017-10-23 14:39:51 +02001289 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001290 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001291 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001292 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001293 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001294}
1295
Willy Tarreau757478e2016-11-03 19:22:19 +01001296/* principle: propagate maint and drain to tracking servers. This is useful
1297 * upon startup so that inherited states are correct.
1298 */
1299static void srv_propagate_admin_state(struct server *srv)
1300{
1301 struct server *srv2;
1302
1303 if (!srv->trackers)
1304 return;
1305
1306 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001307 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001308 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001309 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001310
Emeric Brun52a91d32017-08-31 14:41:55 +02001311 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001312 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001313 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001314 }
1315}
1316
1317/* Compute and propagate the admin states for all servers in proxy <px>.
1318 * Only servers *not* tracking another one are considered, because other
1319 * ones will be handled when the server they track is visited.
1320 */
1321void srv_compute_all_admin_states(struct proxy *px)
1322{
1323 struct server *srv;
1324
1325 for (srv = px->srv; srv; srv = srv->next) {
1326 if (srv->track)
1327 continue;
1328 srv_propagate_admin_state(srv);
1329 }
1330}
1331
Willy Tarreaudff55432012-10-10 17:51:05 +02001332/* Note: must not be declared <const> as its list will be overwritten.
1333 * Please take care of keeping this list alphabetically sorted, doing so helps
1334 * all code contributors.
1335 * Optional keywords are also declared with a NULL ->parse() function so that
1336 * the config parser can report an appropriate error when a known keyword was
1337 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001338 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001339 */
1340static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001341 { "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 +01001342 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001343 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001344 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001345 { "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 +01001346 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001347 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1348 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001349 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001350 { "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 +01001351 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001352 { "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 +01001353 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001354 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001355 { "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 +01001356 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1357 { "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 +02001358 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001359 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001360 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001361 { "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 +01001362 { "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 +02001363 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001364 { "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 +01001365 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001366 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1367 { "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 +02001368 { "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 +01001369 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard1a9c25f2019-07-04 13:34:10 +02001370 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001371 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001372 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
1373 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* enable socks4 proxy for health checks */
Willy Tarreaudff55432012-10-10 17:51:05 +02001374 { NULL, NULL, 0 },
1375}};
1376
Willy Tarreau0108d902018-11-25 19:14:37 +01001377INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001378
Willy Tarreau004e0452013-11-21 11:22:01 +01001379/* Recomputes the server's eweight based on its state, uweight, the current time,
1380 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001381 * state is automatically disabled if the time is elapsed. If <must_update> is
1382 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001383 *
1384 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001385 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001386void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001387{
1388 struct proxy *px = sv->proxy;
1389 unsigned w;
1390
1391 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1392 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001393 if (sv->next_state == SRV_ST_STARTING)
1394 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001395 }
1396
1397 /* We must take care of not pushing the server to full throttle during slow starts.
1398 * It must also start immediately, at least at the minimal step when leaving maintenance.
1399 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001400 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001401 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1402 else
1403 w = px->lbprm.wdiv;
1404
Emeric Brun52a91d32017-08-31 14:41:55 +02001405 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001406
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001407 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001408 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001409 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001410}
1411
Willy Tarreaubaaee002006-06-26 02:48:02 +02001412/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001413 * Parses weight_str and configures sv accordingly.
1414 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001415 *
1416 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001417 */
1418const char *server_parse_weight_change_request(struct server *sv,
1419 const char *weight_str)
1420{
1421 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001422 long int w;
1423 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001424
1425 px = sv->proxy;
1426
1427 /* if the weight is terminated with '%', it is set relative to
1428 * the initial weight, otherwise it is absolute.
1429 */
1430 if (!*weight_str)
1431 return "Require <weight> or <weight%>.\n";
1432
Simon Hormanb796afa2013-02-12 10:45:53 +09001433 w = strtol(weight_str, &end, 10);
1434 if (end == weight_str)
1435 return "Empty weight string empty or preceded by garbage";
1436 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001437 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001438 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001439 /* Avoid integer overflow */
1440 if (w > 25600)
1441 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001442 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001443 if (w > 256)
1444 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001445 }
1446 else if (w < 0 || w > 256)
1447 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001448 else if (end[0] != '\0')
1449 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001450
1451 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1452 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1453
1454 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001455 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001456
1457 return NULL;
1458}
1459
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001460/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001461 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1462 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001463 * Returns:
1464 * - error string on error
1465 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001466 *
1467 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001468 */
1469const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001470 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001471{
1472 unsigned char ip[INET6_ADDRSTRLEN];
1473
1474 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001475 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001476 return NULL;
1477 }
1478 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001479 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001480 return NULL;
1481 }
1482
1483 return "Could not understand IP address format.\n";
1484}
1485
Willy Tarreau46b7f532018-08-21 11:54:26 +02001486/*
1487 * Must be called with the server lock held.
1488 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001489const char *server_parse_maxconn_change_request(struct server *sv,
1490 const char *maxconn_str)
1491{
1492 long int v;
1493 char *end;
1494
1495 if (!*maxconn_str)
1496 return "Require <maxconn>.\n";
1497
1498 v = strtol(maxconn_str, &end, 10);
1499 if (end == maxconn_str)
1500 return "maxconn string empty or preceded by garbage";
1501 else if (end[0] != '\0')
1502 return "Trailing garbage in maxconn string";
1503
1504 if (sv->maxconn == sv->minconn) { // static maxconn
1505 sv->maxconn = sv->minconn = v;
1506 } else { // dynamic maxconn
1507 sv->maxconn = v;
1508 }
1509
1510 if (may_dequeue_tasks(sv, sv->proxy))
1511 process_srv_queue(sv);
1512
1513 return NULL;
1514}
1515
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001516#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001517static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1518 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001519{
1520 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001521 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001522 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001523 NULL,
1524 };
1525
1526 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001527 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001528
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001529 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1530}
1531
1532static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1533{
1534 struct sample_expr *expr;
1535
1536 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 +01001537 if (!expr) {
1538 memprintf(err, "error detected while parsing sni expression : %s", *err);
1539 return ERR_ALERT | ERR_FATAL;
1540 }
1541
1542 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1543 memprintf(err, "error detected while parsing sni expression : "
1544 " fetch method '%s' extracts information from '%s', "
1545 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001546 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001547 return ERR_ALERT | ERR_FATAL;
1548 }
1549
1550 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1551 release_sample_expr(newsrv->ssl_ctx.sni);
1552 newsrv->ssl_ctx.sni = expr;
1553
1554 return 0;
1555}
1556#endif
1557
1558static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1559{
1560 if (err && *err) {
1561 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001562 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 +01001563 }
1564 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001565 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1566 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001567}
1568
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001569static void srv_conn_src_sport_range_cpy(struct server *srv,
1570 struct server *src)
1571{
1572 int range_sz;
1573
1574 range_sz = src->conn_src.sport_range->size;
1575 if (range_sz > 0) {
1576 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1577 if (srv->conn_src.sport_range != NULL) {
1578 int i;
1579
1580 for (i = 0; i < range_sz; i++) {
1581 srv->conn_src.sport_range->ports[i] =
1582 src->conn_src.sport_range->ports[i];
1583 }
1584 }
1585 }
1586}
1587
1588/*
1589 * Copy <src> server connection source settings to <srv> server everything needed.
1590 */
1591static void srv_conn_src_cpy(struct server *srv, struct server *src)
1592{
1593 srv->conn_src.opts = src->conn_src.opts;
1594 srv->conn_src.source_addr = src->conn_src.source_addr;
1595
1596 /* Source port range copy. */
1597 if (src->conn_src.sport_range != NULL)
1598 srv_conn_src_sport_range_cpy(srv, src);
1599
1600#ifdef CONFIG_HAP_TRANSPARENT
1601 if (src->conn_src.bind_hdr_name != NULL) {
1602 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1603 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1604 }
1605 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1606 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1607#endif
1608 if (src->conn_src.iface_name != NULL)
1609 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1610}
1611
1612/*
1613 * Copy <src> server SSL settings to <srv> server allocating
1614 * everything needed.
1615 */
1616#if defined(USE_OPENSSL)
1617static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1618{
1619 if (src->ssl_ctx.ca_file != NULL)
1620 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1621 if (src->ssl_ctx.crl_file != NULL)
1622 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1623 if (src->ssl_ctx.client_crt != NULL)
1624 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1625
1626 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1627
1628 if (src->ssl_ctx.verify_host != NULL)
1629 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1630 if (src->ssl_ctx.ciphers != NULL)
1631 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Willy Tarreau5db847a2019-05-09 14:13:35 +02001632#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001633 if (src->ssl_ctx.ciphersuites != NULL)
1634 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1635#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001636 if (src->sni_expr != NULL)
1637 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001638
1639#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1640 if (src->ssl_ctx.alpn_str) {
1641 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1642 if (srv->ssl_ctx.alpn_str) {
1643 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1644 src->ssl_ctx.alpn_len);
1645 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1646 }
1647 }
1648#endif
1649#ifdef OPENSSL_NPN_NEGOTIATED
1650 if (src->ssl_ctx.npn_str) {
1651 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1652 if (srv->ssl_ctx.npn_str) {
1653 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1654 src->ssl_ctx.npn_len);
1655 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1656 }
1657 }
1658#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001659}
1660#endif
1661
1662/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001663 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001664 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001665 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001666 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001667static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001668{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001669 char *hostname_dn;
1670 int hostname_len, hostname_dn_len;
1671
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001672 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001673 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001674
Christopher Faulet67957bd2017-09-27 11:00:59 +02001675 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001676 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001677 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1678 hostname_dn, trash.size);
1679 if (hostname_dn_len == -1)
1680 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001681
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001682
Christopher Faulet67957bd2017-09-27 11:00:59 +02001683 free(srv->hostname);
1684 free(srv->hostname_dn);
1685 srv->hostname = strdup(hostname);
1686 srv->hostname_dn = strdup(hostname_dn);
1687 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001688 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001689 goto err;
1690
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001691 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001692
1693 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001694 free(srv->hostname); srv->hostname = NULL;
1695 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001696 return -1;
1697}
1698
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001699/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001700 * Copy <src> server settings to <srv> server allocating
1701 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001702 * This function is not supposed to be called at any time, but only
1703 * during server settings parsing or during server allocations from
1704 * a server template, and just after having calloc()'ed a new server.
1705 * So, <src> may only be a default server (when parsing server settings)
1706 * or a server template (during server allocations from a server template).
1707 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1708 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001709 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001710static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001711{
1712 /* Connection source settings copy */
1713 srv_conn_src_cpy(srv, src);
1714
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001715 if (srv_tmpl) {
1716 srv->addr = src->addr;
1717 srv->svc_port = src->svc_port;
1718 }
1719
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001720 srv->pp_opts = src->pp_opts;
1721 if (src->rdr_pfx != NULL) {
1722 srv->rdr_pfx = strdup(src->rdr_pfx);
1723 srv->rdr_len = src->rdr_len;
1724 }
1725 if (src->cookie != NULL) {
1726 srv->cookie = strdup(src->cookie);
1727 srv->cklen = src->cklen;
1728 }
1729 srv->use_ssl = src->use_ssl;
1730 srv->check.addr = srv->agent.addr = src->check.addr;
1731 srv->check.use_ssl = src->check.use_ssl;
1732 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001733 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001734 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001735 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001736 /* Note: 'flags' field has potentially been already initialized. */
1737 srv->flags |= src->flags;
1738 srv->do_check = src->do_check;
1739 srv->do_agent = src->do_agent;
1740 if (srv->check.port)
1741 srv->flags |= SRV_F_CHECKPORT;
1742 srv->check.inter = src->check.inter;
1743 srv->check.fastinter = src->check.fastinter;
1744 srv->check.downinter = src->check.downinter;
1745 srv->agent.use_ssl = src->agent.use_ssl;
1746 srv->agent.port = src->agent.port;
1747 if (src->agent.send_string != NULL)
1748 srv->agent.send_string = strdup(src->agent.send_string);
1749 srv->agent.send_string_len = src->agent.send_string_len;
1750 srv->agent.inter = src->agent.inter;
1751 srv->agent.fastinter = src->agent.fastinter;
1752 srv->agent.downinter = src->agent.downinter;
1753 srv->maxqueue = src->maxqueue;
1754 srv->minconn = src->minconn;
1755 srv->maxconn = src->maxconn;
1756 srv->slowstart = src->slowstart;
1757 srv->observe = src->observe;
1758 srv->onerror = src->onerror;
1759 srv->onmarkeddown = src->onmarkeddown;
1760 srv->onmarkedup = src->onmarkedup;
1761 if (src->trackit != NULL)
1762 srv->trackit = strdup(src->trackit);
1763 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1764 srv->uweight = srv->iweight = src->iweight;
1765
1766 srv->check.send_proxy = src->check.send_proxy;
1767 /* health: up, but will fall down at first failure */
1768 srv->check.rise = srv->check.health = src->check.rise;
1769 srv->check.fall = src->check.fall;
1770
1771 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001772 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1773 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1774 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001775 srv->check.state |= CHK_ST_PAUSED;
1776 srv->check.health = 0;
1777 }
1778
1779 /* health: up but will fall down at first failure */
1780 srv->agent.rise = srv->agent.health = src->agent.rise;
1781 srv->agent.fall = src->agent.fall;
1782
1783 if (src->resolvers_id != NULL)
1784 srv->resolvers_id = strdup(src->resolvers_id);
1785 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001786 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001787 if (srv->dns_opts.family_prio == AF_UNSPEC)
1788 srv->dns_opts.family_prio = AF_INET6;
1789 memcpy(srv->dns_opts.pref_net,
1790 src->dns_opts.pref_net,
1791 sizeof srv->dns_opts.pref_net);
1792 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1793
1794 srv->init_addr_methods = src->init_addr_methods;
1795 srv->init_addr = src->init_addr;
1796#if defined(USE_OPENSSL)
1797 srv_ssl_settings_cpy(srv, src);
1798#endif
1799#ifdef TCP_USER_TIMEOUT
1800 srv->tcp_ut = src->tcp_ut;
1801#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001802 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001803 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001804 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001805 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001806
Olivier Houchard8da5f982017-08-04 18:35:36 +02001807 if (srv_tmpl)
1808 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001809
1810 srv->check.via_socks4 = src->check.via_socks4;
1811 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001812}
1813
William Lallemand313bfd12018-10-26 14:47:32 +02001814struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001815{
1816 struct server *srv;
1817
1818 srv = calloc(1, sizeof *srv);
1819 if (!srv)
1820 return NULL;
1821
1822 srv->obj_type = OBJ_TYPE_SERVER;
1823 srv->proxy = proxy;
1824 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001825 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001826
Emeric Brun52a91d32017-08-31 14:41:55 +02001827 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001828 srv->last_change = now.tv_sec;
1829
1830 srv->check.status = HCHK_STATUS_INI;
1831 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001832 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001833 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1834
1835 srv->agent.status = HCHK_STATUS_INI;
1836 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001837 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001838 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1839
Willy Tarreau975b1552019-06-06 16:25:55 +02001840 /* please don't put default server settings here, they are set in
1841 * init_default_instance().
1842 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001843 return srv;
1844}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001845
1846/*
1847 * Validate <srv> server health-check settings.
1848 * Returns 0 if everything is OK, -1 if not.
1849 */
1850static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1851{
1852 struct tcpcheck_rule *r = NULL;
1853 struct list *l;
1854
1855 /*
1856 * We need at least a service port, a check port or the first tcp-check rule must
1857 * be a 'connect' one when checking an IPv4/IPv6 server.
1858 */
1859 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1860 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1861 return 0;
1862
1863 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1864 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001865 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1866 "Check has been disabled.\n",
1867 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001868 return -1;
1869 }
1870
1871 /* search the first action (connect / send / expect) in the list */
1872 l = &srv->proxy->tcpcheck_rules;
1873 list_for_each_entry(r, l, list) {
1874 if (r->action != TCPCHK_ACT_COMMENT)
1875 break;
1876 }
1877
1878 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001879 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1880 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1881 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001882 return -1;
1883 }
1884
1885 /* scan the tcp-check ruleset to ensure a port has been configured */
1886 l = &srv->proxy->tcpcheck_rules;
1887 list_for_each_entry(r, l, list) {
1888 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001889 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1890 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1891 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001892 return -1;
1893 }
1894 }
1895
1896 return 0;
1897}
1898
1899/*
1900 * Initialize <srv> health-check structure.
1901 * Returns the error string in case of memory allocation failure, NULL if not.
1902 */
1903static const char *do_health_check_init(struct server *srv, int check_type, int state)
1904{
1905 const char *ret;
1906
1907 if (!srv->do_check)
1908 return NULL;
1909
1910 ret = init_check(&srv->check, check_type);
1911 if (ret)
1912 return ret;
1913
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001914 srv->check.state |= state;
1915 global.maxsock++;
1916
1917 return NULL;
1918}
1919
1920static int server_health_check_init(const char *file, int linenum,
1921 struct server *srv, struct proxy *curproxy)
1922{
1923 const char *ret;
1924
1925 if (!srv->do_check)
1926 return 0;
1927
1928 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001929 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1930 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001931 return ERR_ALERT | ERR_FATAL;
1932 }
1933
1934 if (server_healthcheck_validate(file, linenum, srv) < 0)
1935 return ERR_ALERT | ERR_ABORT;
1936
1937 /* note: check type will be set during the config review phase */
1938 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1939 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001940 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001941 return ERR_ALERT | ERR_ABORT;
1942 }
1943
1944 return 0;
1945}
1946
1947/*
1948 * Initialize <srv> agent check structure.
1949 * Returns the error string in case of memory allocation failure, NULL if not.
1950 */
1951static const char *do_server_agent_check_init(struct server *srv, int state)
1952{
1953 const char *ret;
1954
1955 if (!srv->do_agent)
1956 return NULL;
1957
1958 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1959 if (ret)
1960 return ret;
1961
1962 if (!srv->agent.inter)
1963 srv->agent.inter = srv->check.inter;
1964
1965 srv->agent.state |= state;
1966 global.maxsock++;
1967
1968 return NULL;
1969}
1970
1971static int server_agent_check_init(const char *file, int linenum,
1972 struct server *srv, struct proxy *curproxy)
1973{
1974 const char *ret;
1975
1976 if (!srv->do_agent)
1977 return 0;
1978
1979 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001980 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 +02001981 file, linenum, srv->id);
1982 return ERR_ALERT | ERR_FATAL;
1983 }
1984
1985 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1986 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001987 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001988 return ERR_ALERT | ERR_ABORT;
1989 }
1990
1991 return 0;
1992}
1993
1994#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1995static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1996 struct server *srv, struct proxy *proxy)
1997{
1998 int ret;
1999 char *err = NULL;
2000
2001 if (!srv->sni_expr)
2002 return 0;
2003
2004 ret = server_parse_sni_expr(srv, proxy, &err);
2005 if (!ret)
2006 return 0;
2007
2008 display_parser_err(file, linenum, args, cur_arg, &err);
2009 free(err);
2010
2011 return ret;
2012}
2013#endif
2014
2015/*
2016 * Server initializations finalization.
2017 * Initialize health check, agent check and SNI expression if enabled.
2018 * Must not be called for a default server instance.
2019 */
2020static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2021 struct server *srv, struct proxy *px)
2022{
2023 int ret;
2024
2025 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2026 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2027 return ret;
2028 }
2029
2030#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2031 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2032 return ret;
2033#endif
2034
2035 if (srv->flags & SRV_F_BACKUP)
2036 px->srv_bck++;
2037 else
2038 px->srv_act++;
2039 srv_lb_commit_status(srv);
2040
2041 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002042err:
2043 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002044}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002045
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002046/*
2047 * Parse as much as possible such a range string argument: low[-high]
2048 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2049 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2050 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2051 * Returns 0 if succeeded, -1 if not.
2052 */
2053static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2054{
2055 char *nb_high_arg;
2056
2057 *nb_high = 0;
2058 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002059 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002060
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002061 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002062 *nb_high_arg++ = '\0';
2063 *nb_high = atoi(nb_high_arg);
2064 }
2065 else {
2066 *nb_high += *nb_low;
2067 *nb_low = 1;
2068 }
2069
2070 if (*nb_low < 0 || *nb_high < *nb_low)
2071 return -1;
2072
2073 return 0;
2074}
2075
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002076static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2077{
2078 chunk_printf(&trash, "%s%d", prefix, nb);
2079 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002080 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002081}
2082
2083/*
2084 * Initialize as much as possible servers from <srv> server template.
2085 * Note that a server template is a special server with
2086 * a few different parameters than a server which has
2087 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002088 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002089 * 'srv' template included.
2090 */
2091static int server_template_init(struct server *srv, struct proxy *px)
2092{
2093 int i;
2094 struct server *newsrv;
2095
2096 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2097 int check_init_state;
2098 int agent_init_state;
2099
2100 newsrv = new_server(px);
2101 if (!newsrv)
2102 goto err;
2103
2104 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002105 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002106#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2107 if (newsrv->sni_expr) {
2108 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2109 if (!newsrv->ssl_ctx.sni)
2110 goto err;
2111 }
2112#endif
2113 /* Set this new server ID. */
2114 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2115
2116 /* Initial checks states. */
2117 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2118 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2119
2120 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2121 do_server_agent_check_init(newsrv, agent_init_state))
2122 goto err;
2123
2124 /* Linked backwards first. This will be restablished after parsing. */
2125 newsrv->next = px->srv;
2126 px->srv = newsrv;
2127 }
2128 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2129
2130 return i - srv->tmpl_info.nb_low;
2131
2132 err:
2133 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2134 if (newsrv) {
2135#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2136 release_sample_expr(newsrv->ssl_ctx.sni);
2137#endif
2138 free_check(&newsrv->agent);
2139 free_check(&newsrv->check);
2140 }
2141 free(newsrv);
2142 return i - srv->tmpl_info.nb_low;
2143}
2144
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002145int 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 +02002146{
2147 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002148 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002149 char *errmsg = NULL;
2150 int err_code = 0;
2151 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002152 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002153
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002154 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002155 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002156 !strcmp(args[0], "default-server") ||
2157 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002158 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002159 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002160 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002161 int srv_tmpl = !defsrv && !srv;
2162 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002163
2164 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002165 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002166 err_code |= ERR_ALERT | ERR_FATAL;
2167 goto out;
2168 }
2169 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002170 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002171
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002172 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002173 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002174 if (!*args[2]) {
2175 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002176 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002177 file, linenum, args[0]);
2178 err_code |= ERR_ALERT | ERR_FATAL;
2179 goto out;
2180 }
2181
2182 err = invalid_char(args[1]);
2183 }
2184 else if (srv_tmpl) {
2185 if (!*args[3]) {
2186 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002187 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 +02002188 file, linenum, args[0]);
2189 err_code |= ERR_ALERT | ERR_FATAL;
2190 goto out;
2191 }
2192
2193 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002194 }
2195
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002196 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002197 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 +02002198 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002199 err_code |= ERR_ALERT | ERR_FATAL;
2200 goto out;
2201 }
2202
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002203 cur_arg = 2;
2204 if (srv_tmpl) {
2205 /* Parse server-template <nb | range> arg. */
2206 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002207 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002208 file, linenum, args[0], args[cur_arg]);
2209 err_code |= ERR_ALERT | ERR_FATAL;
2210 goto out;
2211 }
2212 cur_arg++;
2213 }
2214
Willy Tarreau272adea2014-03-31 10:39:59 +02002215 if (!defsrv) {
2216 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002217 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002218 struct protocol *proto;
2219
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002220 newsrv = new_server(curproxy);
2221 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002222 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002223 err_code |= ERR_ALERT | ERR_ABORT;
2224 goto out;
2225 }
2226
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002227 if (srv_tmpl) {
2228 newsrv->tmpl_info.nb_low = tmpl_range_low;
2229 newsrv->tmpl_info.nb_high = tmpl_range_high;
2230 }
2231
Willy Tarreau272adea2014-03-31 10:39:59 +02002232 /* the servers are linked backwards first */
2233 newsrv->next = curproxy->srv;
2234 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002235 newsrv->conf.file = strdup(file);
2236 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002237 /* Note: for a server template, its id is its prefix.
2238 * This is a temporary id which will be used for server allocations to come
2239 * after parsing.
2240 */
2241 if (srv)
2242 newsrv->id = strdup(args[1]);
2243 else
2244 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002245
2246 /* several ways to check the port component :
2247 * - IP => port=+0, relative (IPv4 only)
2248 * - IP: => port=+0, relative
2249 * - IP:N => port=N, absolute
2250 * - IP:+N => port=+N, relative
2251 * - IP:-N => port=-N, relative
2252 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002253 if (!parse_addr)
2254 goto skip_addr;
2255
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002256 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002257 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002258 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002259 err_code |= ERR_ALERT | ERR_FATAL;
2260 goto out;
2261 }
2262
2263 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002264 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002265 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002266 file, linenum, args[0], args[1]);
2267 err_code |= ERR_ALERT | ERR_FATAL;
2268 goto out;
2269 }
2270
2271 if (!port1 || !port2) {
2272 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002273 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002274 }
2275 else if (port1 != port2) {
2276 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002277 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002278 file, linenum, args[0], args[1], args[2]);
2279 err_code |= ERR_ALERT | ERR_FATAL;
2280 goto out;
2281 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002282
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002283 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002284 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002285 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002286 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002287 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2288 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2289 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002290 err_code |= ERR_ALERT | ERR_FATAL;
2291 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002292 }
2293 }
2294 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002295 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002296 file, linenum, newsrv->id);
2297 err_code |= ERR_ALERT | ERR_FATAL;
2298 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002299 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002300 }
2301
Willy Tarreau272adea2014-03-31 10:39:59 +02002302 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002303 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002304
Olivier Houchard8da5f982017-08-04 18:35:36 +02002305 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002306 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002307 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002308 err_code |= ERR_ALERT | ERR_FATAL;
2309 goto out;
2310 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002311
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002312 cur_arg++;
2313 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002314 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002315 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002316 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002317 } else {
2318 newsrv = &curproxy->defsrv;
2319 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002320 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002321 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002322 }
2323
2324 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002325 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002326 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002327
2328 if (err == PARSE_TIME_OVER) {
2329 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2330 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2331 err_code |= ERR_ALERT | ERR_FATAL;
2332 goto out;
2333 }
2334 else if (err == PARSE_TIME_UNDER) {
2335 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2336 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2337 err_code |= ERR_ALERT | ERR_FATAL;
2338 goto out;
2339 }
2340 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002341 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002342 file, linenum, *err, newsrv->id);
2343 err_code |= ERR_ALERT | ERR_FATAL;
2344 goto out;
2345 }
2346 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002347 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002348 file, linenum, val, args[cur_arg], newsrv->id);
2349 err_code |= ERR_ALERT | ERR_FATAL;
2350 goto out;
2351 }
2352 newsrv->agent.inter = val;
2353 cur_arg += 2;
2354 }
Misiekea849332017-01-09 09:39:51 +01002355 else if (!strcmp(args[cur_arg], "agent-addr")) {
2356 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002357 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002358 goto out;
2359 }
2360
2361 cur_arg += 2;
2362 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002363 else if (!strcmp(args[cur_arg], "agent-port")) {
2364 global.maxsock++;
2365 newsrv->agent.port = atol(args[cur_arg + 1]);
2366 cur_arg += 2;
2367 }
James Brown55f9ff12015-10-21 18:19:05 -07002368 else if (!strcmp(args[cur_arg], "agent-send")) {
2369 global.maxsock++;
2370 free(newsrv->agent.send_string);
2371 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2372 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2373 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2374 cur_arg += 2;
2375 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002376 else if (!strcmp(args[cur_arg], "init-addr")) {
2377 char *p, *end;
2378 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002379 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002380
2381 newsrv->init_addr_methods = 0;
2382 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2383
2384 for (p = args[cur_arg + 1]; *p; p = end) {
2385 /* cut on next comma */
2386 for (end = p; *end && *end != ','; end++);
2387 if (*end)
2388 *(end++) = 0;
2389
Willy Tarreau4310d362016-11-02 15:05:56 +01002390 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002391 if (!strcmp(p, "libc")) {
2392 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2393 }
2394 else if (!strcmp(p, "last")) {
2395 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2396 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002397 else if (!strcmp(p, "none")) {
2398 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2399 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002400 else if (str2ip2(p, &sa, 0)) {
2401 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002402 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002403 file, linenum, args[cur_arg], p);
2404 err_code |= ERR_ALERT | ERR_FATAL;
2405 goto out;
2406 }
2407 newsrv->init_addr = sa;
2408 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2409 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002410 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002411 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 +02002412 file, linenum, args[cur_arg], p);
2413 err_code |= ERR_ALERT | ERR_FATAL;
2414 goto out;
2415 }
2416 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002417 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002418 file, linenum, args[cur_arg], p);
2419 err_code |= ERR_ALERT | ERR_FATAL;
2420 goto out;
2421 }
2422 }
2423 cur_arg += 2;
2424 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002425 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002426 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002427 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2428 cur_arg += 2;
2429 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002430 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2431 char *p, *end;
2432
2433 for (p = args[cur_arg + 1]; *p; p = end) {
2434 /* cut on next comma */
2435 for (end = p; *end && *end != ','; end++);
2436 if (*end)
2437 *(end++) = 0;
2438
2439 if (!strcmp(p, "allow-dup-ip")) {
2440 newsrv->dns_opts.accept_duplicate_ip = 1;
2441 }
2442 else if (!strcmp(p, "prevent-dup-ip")) {
2443 newsrv->dns_opts.accept_duplicate_ip = 0;
2444 }
2445 else {
2446 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2447 file, linenum, args[cur_arg], p);
2448 err_code |= ERR_ALERT | ERR_FATAL;
2449 goto out;
2450 }
2451 }
2452
2453 cur_arg += 2;
2454 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002455 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2456 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002457 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002458 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002459 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002460 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002461 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002462 file, linenum, args[cur_arg]);
2463 err_code |= ERR_ALERT | ERR_FATAL;
2464 goto out;
2465 }
2466 cur_arg += 2;
2467 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002468 else if (!strcmp(args[cur_arg], "resolve-net")) {
2469 char *p, *e;
2470 unsigned char mask;
2471 struct dns_options *opt;
2472
2473 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002474 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002475 file, linenum, args[cur_arg]);
2476 err_code |= ERR_ALERT | ERR_FATAL;
2477 goto out;
2478 }
2479
2480 opt = &newsrv->dns_opts;
2481
2482 /* Split arguments by comma, and convert it from ipv4 or ipv6
2483 * string network in in_addr or in6_addr.
2484 */
2485 p = args[cur_arg + 1];
2486 e = p;
2487 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002488 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002489 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002490 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002491 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2492 err_code |= ERR_ALERT | ERR_FATAL;
2493 goto out;
2494 }
2495 /* look for end or comma. */
2496 while (*e != ',' && *e != '\0')
2497 e++;
2498 if (*e == ',') {
2499 *e = '\0';
2500 e++;
2501 }
2502 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2503 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2504 /* Try to convert input string from ipv4 or ipv6 network. */
2505 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2506 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2507 &mask)) {
2508 /* Try to convert input string from ipv6 network. */
2509 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2510 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2511 } else {
2512 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002513 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002514 file, linenum, args[cur_arg], p);
2515 err_code |= ERR_ALERT | ERR_FATAL;
2516 goto out;
2517 }
2518 opt->pref_net_nb++;
2519 p = e;
2520 }
2521
2522 cur_arg += 2;
2523 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002524 else if (!strcmp(args[cur_arg], "rise")) {
2525 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002526 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002527 file, linenum, args[cur_arg]);
2528 err_code |= ERR_ALERT | ERR_FATAL;
2529 goto out;
2530 }
2531
2532 newsrv->check.rise = atol(args[cur_arg + 1]);
2533 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002534 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002535 file, linenum, args[cur_arg]);
2536 err_code |= ERR_ALERT | ERR_FATAL;
2537 goto out;
2538 }
2539
2540 if (newsrv->check.health)
2541 newsrv->check.health = newsrv->check.rise;
2542 cur_arg += 2;
2543 }
2544 else if (!strcmp(args[cur_arg], "fall")) {
2545 newsrv->check.fall = atol(args[cur_arg + 1]);
2546
2547 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002548 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002549 file, linenum, args[cur_arg]);
2550 err_code |= ERR_ALERT | ERR_FATAL;
2551 goto out;
2552 }
2553
2554 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002555 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002556 file, linenum, args[cur_arg]);
2557 err_code |= ERR_ALERT | ERR_FATAL;
2558 goto out;
2559 }
2560
2561 cur_arg += 2;
2562 }
2563 else if (!strcmp(args[cur_arg], "inter")) {
2564 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002565
2566 if (err == PARSE_TIME_OVER) {
2567 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2568 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2569 err_code |= ERR_ALERT | ERR_FATAL;
2570 goto out;
2571 }
2572 else if (err == PARSE_TIME_UNDER) {
2573 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2574 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2575 err_code |= ERR_ALERT | ERR_FATAL;
2576 goto out;
2577 }
2578 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002579 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002580 file, linenum, *err, newsrv->id);
2581 err_code |= ERR_ALERT | ERR_FATAL;
2582 goto out;
2583 }
2584 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002585 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002586 file, linenum, val, args[cur_arg], newsrv->id);
2587 err_code |= ERR_ALERT | ERR_FATAL;
2588 goto out;
2589 }
2590 newsrv->check.inter = val;
2591 cur_arg += 2;
2592 }
2593 else if (!strcmp(args[cur_arg], "fastinter")) {
2594 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002595
2596 if (err == PARSE_TIME_OVER) {
2597 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2598 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2599 err_code |= ERR_ALERT | ERR_FATAL;
2600 goto out;
2601 }
2602 else if (err == PARSE_TIME_UNDER) {
2603 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2604 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2605 err_code |= ERR_ALERT | ERR_FATAL;
2606 goto out;
2607 }
2608 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002609 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002610 file, linenum, *err, newsrv->id);
2611 err_code |= ERR_ALERT | ERR_FATAL;
2612 goto out;
2613 }
2614 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002615 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002616 file, linenum, val, args[cur_arg], newsrv->id);
2617 err_code |= ERR_ALERT | ERR_FATAL;
2618 goto out;
2619 }
2620 newsrv->check.fastinter = val;
2621 cur_arg += 2;
2622 }
2623 else if (!strcmp(args[cur_arg], "downinter")) {
2624 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002625
2626 if (err == PARSE_TIME_OVER) {
2627 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2628 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2629 err_code |= ERR_ALERT | ERR_FATAL;
2630 goto out;
2631 }
2632 else if (err == PARSE_TIME_UNDER) {
2633 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2634 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2635 err_code |= ERR_ALERT | ERR_FATAL;
2636 goto out;
2637 }
2638 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002639 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002640 file, linenum, *err, newsrv->id);
2641 err_code |= ERR_ALERT | ERR_FATAL;
2642 goto out;
2643 }
2644 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002645 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002646 file, linenum, val, args[cur_arg], newsrv->id);
2647 err_code |= ERR_ALERT | ERR_FATAL;
2648 goto out;
2649 }
2650 newsrv->check.downinter = val;
2651 cur_arg += 2;
2652 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002653 else if (!strcmp(args[cur_arg], "port")) {
2654 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002655 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002656 cur_arg += 2;
2657 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002658 else if (!strcmp(args[cur_arg], "weight")) {
2659 int w;
2660 w = atol(args[cur_arg + 1]);
2661 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002662 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002663 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2664 err_code |= ERR_ALERT | ERR_FATAL;
2665 goto out;
2666 }
2667 newsrv->uweight = newsrv->iweight = w;
2668 cur_arg += 2;
2669 }
2670 else if (!strcmp(args[cur_arg], "minconn")) {
2671 newsrv->minconn = atol(args[cur_arg + 1]);
2672 cur_arg += 2;
2673 }
2674 else if (!strcmp(args[cur_arg], "maxconn")) {
2675 newsrv->maxconn = atol(args[cur_arg + 1]);
2676 cur_arg += 2;
2677 }
2678 else if (!strcmp(args[cur_arg], "maxqueue")) {
2679 newsrv->maxqueue = atol(args[cur_arg + 1]);
2680 cur_arg += 2;
2681 }
2682 else if (!strcmp(args[cur_arg], "slowstart")) {
2683 /* slowstart is stored in seconds */
2684 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002685
2686 if (err == PARSE_TIME_OVER) {
2687 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2688 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2689 err_code |= ERR_ALERT | ERR_FATAL;
2690 goto out;
2691 }
2692 else if (err == PARSE_TIME_UNDER) {
2693 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2694 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2695 err_code |= ERR_ALERT | ERR_FATAL;
2696 goto out;
2697 }
2698 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002699 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002700 file, linenum, *err, newsrv->id);
2701 err_code |= ERR_ALERT | ERR_FATAL;
2702 goto out;
2703 }
2704 newsrv->slowstart = (val + 999) / 1000;
2705 cur_arg += 2;
2706 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002707 else if (!strcmp(args[cur_arg], "on-error")) {
2708 if (!strcmp(args[cur_arg + 1], "fastinter"))
2709 newsrv->onerror = HANA_ONERR_FASTINTER;
2710 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2711 newsrv->onerror = HANA_ONERR_FAILCHK;
2712 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2713 newsrv->onerror = HANA_ONERR_SUDDTH;
2714 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2715 newsrv->onerror = HANA_ONERR_MARKDWN;
2716 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002717 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002718 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2719 file, linenum, args[cur_arg], args[cur_arg + 1]);
2720 err_code |= ERR_ALERT | ERR_FATAL;
2721 goto out;
2722 }
2723
2724 cur_arg += 2;
2725 }
2726 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2727 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2728 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2729 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002730 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002731 file, linenum, args[cur_arg], args[cur_arg + 1]);
2732 err_code |= ERR_ALERT | ERR_FATAL;
2733 goto out;
2734 }
2735
2736 cur_arg += 2;
2737 }
2738 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2739 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2740 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2741 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002742 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002743 file, linenum, args[cur_arg], args[cur_arg + 1]);
2744 err_code |= ERR_ALERT | ERR_FATAL;
2745 goto out;
2746 }
2747
2748 cur_arg += 2;
2749 }
2750 else if (!strcmp(args[cur_arg], "error-limit")) {
2751 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002752 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002753 file, linenum, args[cur_arg]);
2754 err_code |= ERR_ALERT | ERR_FATAL;
2755 goto out;
2756 }
2757
2758 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2759
2760 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002761 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002762 file, linenum, args[cur_arg]);
2763 err_code |= ERR_ALERT | ERR_FATAL;
2764 goto out;
2765 }
2766 cur_arg += 2;
2767 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002768 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002769 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002770 file, linenum, "usesrc", "source");
2771 err_code |= ERR_ALERT | ERR_FATAL;
2772 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002773 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002774 else {
2775 static int srv_dumped;
2776 struct srv_kw *kw;
2777 char *err;
2778
2779 kw = srv_find_kw(args[cur_arg]);
2780 if (kw) {
2781 char *err = NULL;
2782 int code;
2783
2784 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002785 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 +02002786 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002787 if (kw->skip != -1)
2788 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002789 err_code |= ERR_ALERT | ERR_FATAL;
2790 goto out;
2791 }
2792
2793 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002794 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002795 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002796 if (kw->skip != -1)
2797 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002798 err_code |= ERR_ALERT;
2799 continue;
2800 }
2801
2802 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2803 err_code |= code;
2804
2805 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002806 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002807 if (code & ERR_FATAL) {
2808 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002809 if (kw->skip != -1)
2810 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002811 goto out;
2812 }
2813 }
2814 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002815 if (kw->skip != -1)
2816 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002817 continue;
2818 }
2819
2820 err = NULL;
2821 if (!srv_dumped) {
2822 srv_dump_kws(&err);
2823 indent_msg(&err, 4);
2824 srv_dumped = 1;
2825 }
2826
Christopher Faulet767a84b2017-11-24 16:50:31 +01002827 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002828 file, linenum, args[0], args[1], args[cur_arg],
2829 err ? " Registered keywords :" : "", err ? err : "");
2830 free(err);
2831
2832 err_code |= ERR_ALERT | ERR_FATAL;
2833 goto out;
2834 }
2835 }
2836
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002837 if (!defsrv)
2838 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2839 if (err_code & ERR_FATAL)
2840 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002841 if (srv_tmpl)
2842 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002843 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002844 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002845 return 0;
2846
2847 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002848 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002849 free(errmsg);
2850 return err_code;
2851}
2852
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002853/* Returns a pointer to the first server matching either id <id>.
2854 * NULL is returned if no match is found.
2855 * the lookup is performed in the backend <bk>
2856 */
2857struct server *server_find_by_id(struct proxy *bk, int id)
2858{
2859 struct eb32_node *eb32;
2860 struct server *curserver;
2861
2862 if (!bk || (id ==0))
2863 return NULL;
2864
2865 /* <bk> has no backend capabilities, so it can't have a server */
2866 if (!(bk->cap & PR_CAP_BE))
2867 return NULL;
2868
2869 curserver = NULL;
2870
2871 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2872 if (eb32)
2873 curserver = container_of(eb32, struct server, conf.id);
2874
2875 return curserver;
2876}
2877
2878/* Returns a pointer to the first server matching either name <name>, or id
2879 * if <name> starts with a '#'. NULL is returned if no match is found.
2880 * the lookup is performed in the backend <bk>
2881 */
2882struct server *server_find_by_name(struct proxy *bk, const char *name)
2883{
2884 struct server *curserver;
2885
2886 if (!bk || !name)
2887 return NULL;
2888
2889 /* <bk> has no backend capabilities, so it can't have a server */
2890 if (!(bk->cap & PR_CAP_BE))
2891 return NULL;
2892
2893 curserver = NULL;
2894 if (*name == '#') {
2895 curserver = server_find_by_id(bk, atoi(name + 1));
2896 if (curserver)
2897 return curserver;
2898 }
2899 else {
2900 curserver = bk->srv;
2901
2902 while (curserver && (strcmp(curserver->id, name) != 0))
2903 curserver = curserver->next;
2904
2905 if (curserver)
2906 return curserver;
2907 }
2908
2909 return NULL;
2910}
2911
2912struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2913{
2914 struct server *byname;
2915 struct server *byid;
2916
2917 if (!name && !id)
2918 return NULL;
2919
2920 if (diff)
2921 *diff = 0;
2922
2923 byname = byid = NULL;
2924
2925 if (name) {
2926 byname = server_find_by_name(bk, name);
2927 if (byname && (!id || byname->puid == id))
2928 return byname;
2929 }
2930
2931 /* remaining possibilities :
2932 * - name not set
2933 * - name set but not found
2934 * - name found but ID doesn't match
2935 */
2936 if (id) {
2937 byid = server_find_by_id(bk, id);
2938 if (byid) {
2939 if (byname) {
2940 /* use id only if forced by configuration */
2941 if (byid->flags & SRV_F_FORCED_ID) {
2942 if (diff)
2943 *diff |= 2;
2944 return byid;
2945 }
2946 else {
2947 if (diff)
2948 *diff |= 1;
2949 return byname;
2950 }
2951 }
2952
2953 /* remaining possibilities:
2954 * - name not set
2955 * - name set but not found
2956 */
2957 if (name && diff)
2958 *diff |= 2;
2959 return byid;
2960 }
2961
2962 /* id bot found */
2963 if (byname) {
2964 if (diff)
2965 *diff |= 1;
2966 return byname;
2967 }
2968 }
2969
2970 return NULL;
2971}
2972
Willy Tarreau46b7f532018-08-21 11:54:26 +02002973/* Update a server state using the parameters available in the params list.
2974 *
2975 * Grabs the server lock during operation.
2976 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002977static void srv_update_state(struct server *srv, int version, char **params)
2978{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002979 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002980 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002981
2982 /* fields since version 1
2983 * and common to all other upcoming versions
2984 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002985 enum srv_state srv_op_state;
2986 enum srv_admin srv_admin_state;
2987 unsigned srv_uweight, srv_iweight;
2988 unsigned long srv_last_time_change;
2989 short srv_check_status;
2990 enum chk_result srv_check_result;
2991 int srv_check_health;
2992 int srv_check_state, srv_agent_state;
2993 int bk_f_forced_id;
2994 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002995 int fqdn_set_by_cli;
2996 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002997 const char *port_str;
2998 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002999 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003000
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003001 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02003002 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02003003 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003004 switch (version) {
3005 case 1:
3006 /*
3007 * now we can proceed with server's state update:
3008 * srv_addr: params[0]
3009 * srv_op_state: params[1]
3010 * srv_admin_state: params[2]
3011 * srv_uweight: params[3]
3012 * srv_iweight: params[4]
3013 * srv_last_time_change: params[5]
3014 * srv_check_status: params[6]
3015 * srv_check_result: params[7]
3016 * srv_check_health: params[8]
3017 * srv_check_state: params[9]
3018 * srv_agent_state: params[10]
3019 * bk_f_forced_id: params[11]
3020 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003021 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003022 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003023 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003024 */
3025
3026 /* validating srv_op_state */
3027 p = NULL;
3028 errno = 0;
3029 srv_op_state = strtol(params[1], &p, 10);
3030 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
3031 (srv_op_state != SRV_ST_STOPPED &&
3032 srv_op_state != SRV_ST_STARTING &&
3033 srv_op_state != SRV_ST_RUNNING &&
3034 srv_op_state != SRV_ST_STOPPING)) {
3035 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
3036 }
3037
3038 /* validating srv_admin_state */
3039 p = NULL;
3040 errno = 0;
3041 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003042 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01003043
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003044 /* inherited statuses will be recomputed later.
3045 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
3046 */
3047 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01003048
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003049 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
3050 (srv_admin_state != 0 &&
3051 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003052 srv_admin_state != SRV_ADMF_CMAINT &&
3053 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01003054 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01003055 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003056 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
3057 }
3058
3059 /* validating srv_uweight */
3060 p = NULL;
3061 errno = 0;
3062 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003063 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003064 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
3065
3066 /* validating srv_iweight */
3067 p = NULL;
3068 errno = 0;
3069 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003070 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003071 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
3072
3073 /* validating srv_last_time_change */
3074 p = NULL;
3075 errno = 0;
3076 srv_last_time_change = strtol(params[5], &p, 10);
3077 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
3078 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
3079
3080 /* validating srv_check_status */
3081 p = NULL;
3082 errno = 0;
3083 srv_check_status = strtol(params[6], &p, 10);
3084 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3085 (srv_check_status >= HCHK_STATUS_SIZE))
3086 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3087
3088 /* validating srv_check_result */
3089 p = NULL;
3090 errno = 0;
3091 srv_check_result = strtol(params[7], &p, 10);
3092 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3093 (srv_check_result != CHK_RES_UNKNOWN &&
3094 srv_check_result != CHK_RES_NEUTRAL &&
3095 srv_check_result != CHK_RES_FAILED &&
3096 srv_check_result != CHK_RES_PASSED &&
3097 srv_check_result != CHK_RES_CONDPASS)) {
3098 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3099 }
3100
3101 /* validating srv_check_health */
3102 p = NULL;
3103 errno = 0;
3104 srv_check_health = strtol(params[8], &p, 10);
3105 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3106 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3107
3108 /* validating srv_check_state */
3109 p = NULL;
3110 errno = 0;
3111 srv_check_state = strtol(params[9], &p, 10);
3112 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3113 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3114 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3115
3116 /* validating srv_agent_state */
3117 p = NULL;
3118 errno = 0;
3119 srv_agent_state = strtol(params[10], &p, 10);
3120 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3121 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3122 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3123
3124 /* validating bk_f_forced_id */
3125 p = NULL;
3126 errno = 0;
3127 bk_f_forced_id = strtol(params[11], &p, 10);
3128 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3129 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3130
3131 /* validating srv_f_forced_id */
3132 p = NULL;
3133 errno = 0;
3134 srv_f_forced_id = strtol(params[12], &p, 10);
3135 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3136 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3137
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003138 /* validating srv_fqdn */
3139 fqdn = params[13];
3140 if (fqdn && *fqdn == '-')
3141 fqdn = NULL;
3142 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3143 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3144 fqdn = NULL;
3145 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003146
Frédéric Lécaille31694712017-08-01 08:47:19 +02003147 port_str = params[14];
3148 if (port_str) {
3149 port = strl2uic(port_str, strlen(port_str));
3150 if (port > USHRT_MAX) {
3151 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3152 port_str = NULL;
3153 }
3154 }
3155
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003156 /* SRV record
3157 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3158 */
3159 srvrecord = params[15];
3160 if (srvrecord && *srvrecord != '_')
3161 srvrecord = NULL;
3162
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003163 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003164 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003165 goto out;
3166
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003167 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003168 /* recover operational state and apply it to this server
3169 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003170 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003171 switch (srv_op_state) {
3172 case SRV_ST_STOPPED:
3173 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003174 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003175 break;
3176 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003177 /* If rise == 1 there is no STARTING state, let's switch to
3178 * RUNNING
3179 */
3180 if (srv->check.rise == 1) {
3181 srv->check.health = srv->check.rise + srv->check.fall - 1;
3182 srv_set_running(srv, "", NULL);
3183 break;
3184 }
3185 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3186 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003187 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003188 break;
3189 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003190 /* If fall == 1 there is no STOPPING state, let's switch to
3191 * STOPPED
3192 */
3193 if (srv->check.fall == 1) {
3194 srv->check.health = 0;
3195 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3196 break;
3197 }
3198 if (srv->check.health < srv->check.rise ||
3199 srv->check.health > srv->check.rise + srv->check.fall - 2)
3200 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003201 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003202 break;
3203 case SRV_ST_RUNNING:
3204 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003205 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003206 break;
3207 }
3208
3209 /* When applying server state, the following rules apply:
3210 * - in case of a configuration change, we apply the setting from the new
3211 * configuration, regardless of old running state
3212 * - if no configuration change, we apply old running state only if old running
3213 * state is different from new configuration state
3214 */
3215 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003216 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3217 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003218 srv_adm_set_maint(srv);
3219 else
3220 srv_adm_set_ready(srv);
3221 }
3222 /* configuration is the same, let's compate old running state and new conf state */
3223 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003224 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003225 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003226 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003227 srv_adm_set_ready(srv);
3228 }
3229 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003230 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003231 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003232 * (srv->iweight is the weight set up in configuration).
3233 * There are two possible reasons for FDRAIN to have been present :
3234 * - previous config weight was zero
3235 * - "set server b/s drain" was sent to the CLI
3236 *
3237 * In the first case, we simply want to drop this drain state
3238 * if the new weight is not zero anymore, meaning the administrator
3239 * has intentionally turned the weight back to a positive value to
3240 * enable the server again after an operation. In the second case,
3241 * the drain state was forced on the CLI regardless of the config's
3242 * weight so we don't want a change to the config weight to lose this
3243 * status. What this means is :
3244 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3245 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003246 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003247 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003248 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003249 }
3250
3251 srv->last_change = date.tv_sec - srv_last_time_change;
3252 srv->check.status = srv_check_status;
3253 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003254
3255 /* Only case we want to apply is removing ENABLED flag which could have been
3256 * done by the "disable health" command over the stats socket
3257 */
3258 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3259 (srv_check_state & CHK_ST_CONFIGURED) &&
3260 !(srv_check_state & CHK_ST_ENABLED))
3261 srv->check.state &= ~CHK_ST_ENABLED;
3262
3263 /* Only case we want to apply is removing ENABLED flag which could have been
3264 * done by the "disable agent" command over the stats socket
3265 */
3266 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3267 (srv_agent_state & CHK_ST_CONFIGURED) &&
3268 !(srv_agent_state & CHK_ST_ENABLED))
3269 srv->agent.state &= ~CHK_ST_ENABLED;
3270
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003271 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3272 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003273 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003274 * It means that a configuration file change has precedence over a unix socket change
3275 * for server's weight
3276 *
3277 * by default, HAProxy applies the following weight when parsing the configuration
3278 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003279 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003280 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003281 srv->uweight = srv_uweight;
3282 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003283 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003284
Willy Tarreaue5a60682016-11-09 14:54:53 +01003285 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003286 if (strcmp(params[0], "-"))
3287 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003288
3289 if (fqdn && srv->hostname) {
3290 if (!strcmp(srv->hostname, fqdn)) {
3291 /* Here we reset the 'set from stats socket FQDN' flag
3292 * to support such transitions:
3293 * Let's say initial FQDN value is foo1 (in configuration file).
3294 * - FQDN changed from stats socket, from foo1 to foo2 value,
3295 * - FQDN changed again from file configuration (with the same previous value
3296 set from stats socket, from foo1 to foo2 value),
3297 * - reload for any other reason than a FQDN modification,
3298 * the configuration file FQDN matches the fqdn server state file value.
3299 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003300 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003301 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003302 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003303 }
3304 else {
3305 /* If the FDQN has been changed from stats socket,
3306 * apply fqdn state file value (which is the value set
3307 * from stats socket).
3308 */
3309 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003310 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003311 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003312 }
3313 }
3314 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003315 /* If all the conditions below are validated, this means
3316 * we're evaluating a server managed by SRV resolution
3317 */
3318 else if (fqdn && !srv->hostname && srvrecord) {
3319 int res;
3320
3321 /* we can't apply previous state if SRV record has changed */
3322 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3323 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);
3324 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3325 goto out;
3326 }
3327
3328 /* create or find a SRV resolution for this srv record */
3329 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3330 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3331 if (srv->srvrq == NULL) {
3332 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3333 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3334 goto out;
3335 }
3336
3337 /* prepare DNS resolution for this server */
3338 res = srv_prepare_for_resolution(srv, fqdn);
3339 if (res == -1) {
3340 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3341 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3342 goto out;
3343 }
3344
3345 /* configure check.port accordingly */
3346 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3347 !(srv->flags & SRV_F_CHECKPORT))
3348 srv->check.port = port;
3349
3350 /* Unset SRV_F_MAPPORTS for SRV records.
3351 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3352 * because no ports are provided in the configuration file.
3353 * This is because HAProxy will use the port found into the SRV record.
3354 */
3355 srv->flags &= ~SRV_F_MAPPORTS;
3356 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003357
Frédéric Lécaille31694712017-08-01 08:47:19 +02003358 if (port_str)
3359 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003360 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003361
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003362 break;
3363 default:
3364 chunk_appendf(msg, ", version '%d' not supported", version);
3365 }
3366
3367 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003368 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003369 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003370 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003371 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003372 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003373}
3374
3375/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3376 * For each proxy, it does the following:
3377 * - opens its server state file (either one or local one)
3378 * - read whole file, line by line
3379 * - analyse each line to check if it matches our current backend:
3380 * - backend name matches
3381 * - backend id matches if id is forced and name doesn't match
3382 * - if the server pointed by the line is found, then state is applied
3383 *
3384 * If the running backend uuid or id differs from the state file, then HAProxy reports
3385 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003386 *
3387 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003388 */
3389void apply_server_state(void)
3390{
3391 char *cur, *end;
3392 char mybuf[SRV_STATE_LINE_MAXLEN];
3393 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003394 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3395 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmann95c2c012019-06-11 14:51:49 +02003396 int arg, srv_arg, version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003397 FILE *f;
3398 char *filepath;
3399 char globalfilepath[MAXPATHLEN + 1];
3400 char localfilepath[MAXPATHLEN + 1];
3401 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003402 struct proxy *curproxy, *bk;
3403 struct server *srv;
3404
3405 globalfilepathlen = 0;
3406 /* create the globalfilepath variable */
3407 if (global.server_state_file) {
3408 /* absolute path or no base directory provided */
3409 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3410 len = strlen(global.server_state_file);
3411 if (len > MAXPATHLEN) {
3412 globalfilepathlen = 0;
3413 goto globalfileerror;
3414 }
3415 memcpy(globalfilepath, global.server_state_file, len);
3416 globalfilepath[len] = '\0';
3417 globalfilepathlen = len;
3418 }
3419 else if (global.server_state_base) {
3420 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003421 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003422 globalfilepathlen = 0;
3423 goto globalfileerror;
3424 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003425 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003426 globalfilepath[len] = 0;
3427 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003428
3429 /* append a slash if needed */
3430 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3431 if (globalfilepathlen + 1 > MAXPATHLEN) {
3432 globalfilepathlen = 0;
3433 goto globalfileerror;
3434 }
3435 globalfilepath[globalfilepathlen++] = '/';
3436 }
3437
3438 len = strlen(global.server_state_file);
3439 if (globalfilepathlen + len > MAXPATHLEN) {
3440 globalfilepathlen = 0;
3441 goto globalfileerror;
3442 }
3443 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3444 globalfilepathlen += len;
3445 globalfilepath[globalfilepathlen++] = 0;
3446 }
3447 }
3448 globalfileerror:
3449 if (globalfilepathlen == 0)
3450 globalfilepath[0] = '\0';
3451
3452 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003453 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003454 /* servers are only in backends */
3455 if (!(curproxy->cap & PR_CAP_BE))
3456 continue;
3457 fileopenerr = 0;
3458 filepath = NULL;
3459
3460 /* search server state file path and name */
3461 switch (curproxy->load_server_state_from_file) {
3462 /* read servers state from global file */
3463 case PR_SRV_STATE_FILE_GLOBAL:
3464 /* there was an error while generating global server state file path */
3465 if (globalfilepathlen == 0)
3466 continue;
3467 filepath = globalfilepath;
3468 fileopenerr = 1;
3469 break;
3470 /* this backend has its own file */
3471 case PR_SRV_STATE_FILE_LOCAL:
3472 localfilepathlen = 0;
3473 localfilepath[0] = '\0';
3474 len = 0;
3475 /* create the localfilepath variable */
3476 /* absolute path or no base directory provided */
3477 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3478 len = strlen(curproxy->server_state_file_name);
3479 if (len > MAXPATHLEN) {
3480 localfilepathlen = 0;
3481 goto localfileerror;
3482 }
3483 memcpy(localfilepath, curproxy->server_state_file_name, len);
3484 localfilepath[len] = '\0';
3485 localfilepathlen = len;
3486 }
3487 else if (global.server_state_base) {
3488 len = strlen(global.server_state_base);
3489 localfilepathlen += len;
3490
3491 if (localfilepathlen > MAXPATHLEN) {
3492 localfilepathlen = 0;
3493 goto localfileerror;
3494 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003495 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003496 localfilepath[localfilepathlen] = 0;
3497
3498 /* append a slash if needed */
3499 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3500 if (localfilepathlen + 1 > MAXPATHLEN) {
3501 localfilepathlen = 0;
3502 goto localfileerror;
3503 }
3504 localfilepath[localfilepathlen++] = '/';
3505 }
3506
3507 len = strlen(curproxy->server_state_file_name);
3508 if (localfilepathlen + len > MAXPATHLEN) {
3509 localfilepathlen = 0;
3510 goto localfileerror;
3511 }
3512 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3513 localfilepathlen += len;
3514 localfilepath[localfilepathlen++] = 0;
3515 }
3516 filepath = localfilepath;
3517 localfileerror:
3518 if (localfilepathlen == 0)
3519 localfilepath[0] = '\0';
3520
3521 break;
3522 case PR_SRV_STATE_FILE_NONE:
3523 default:
3524 continue;
3525 }
3526
3527 /* preload global state file */
3528 errno = 0;
3529 f = fopen(filepath, "r");
3530 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003531 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003532 if (!f)
3533 continue;
3534
3535 mybuf[0] = '\0';
3536 mybuflen = 0;
3537 version = 0;
3538
3539 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003540 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003541 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003542 goto fileclose;
3543 }
3544
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003545 cur = mybuf;
3546 version = atoi(cur);
3547 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3548 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003549 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003550
3551 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3552 int bk_f_forced_id = 0;
3553 int check_id = 0;
3554 int check_name = 0;
3555
3556 mybuflen = strlen(mybuf);
3557 cur = mybuf;
3558 end = cur + mybuflen;
3559
3560 bk = NULL;
3561 srv = NULL;
3562
3563 /* we need at least one character */
3564 if (mybuflen == 0)
3565 continue;
3566
3567 /* ignore blank characters at the beginning of the line */
3568 while (isspace(*cur))
3569 ++cur;
3570
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003571 /* Ignore empty or commented lines */
3572 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003573 continue;
3574
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003575 /* truncated lines */
3576 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003577 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003578 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003579 }
3580
3581 /* Removes trailing '\n' */
3582 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003583
3584 /* we're now ready to move the line into *srv_params[] */
3585 params[0] = cur;
3586 arg = 1;
3587 srv_arg = 0;
3588 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3589 if (isspace(*cur)) {
3590 *cur = '\0';
3591 ++cur;
3592 while (isspace(*cur))
3593 ++cur;
3594 switch (version) {
3595 case 1:
3596 /*
3597 * srv_addr: params[4] => srv_params[0]
3598 * srv_op_state: params[5] => srv_params[1]
3599 * srv_admin_state: params[6] => srv_params[2]
3600 * srv_uweight: params[7] => srv_params[3]
3601 * srv_iweight: params[8] => srv_params[4]
3602 * srv_last_time_change: params[9] => srv_params[5]
3603 * srv_check_status: params[10] => srv_params[6]
3604 * srv_check_result: params[11] => srv_params[7]
3605 * srv_check_health: params[12] => srv_params[8]
3606 * srv_check_state: params[13] => srv_params[9]
3607 * srv_agent_state: params[14] => srv_params[10]
3608 * bk_f_forced_id: params[15] => srv_params[11]
3609 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003610 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003611 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003612 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003613 */
3614 if (arg >= 4) {
3615 srv_params[srv_arg] = cur;
3616 ++srv_arg;
3617 }
3618 break;
3619 }
3620
3621 params[arg] = cur;
3622 ++arg;
3623 }
3624 else {
3625 ++cur;
3626 }
3627 }
3628
3629 /* if line is incomplete line, then ignore it.
3630 * otherwise, update useful flags */
3631 switch (version) {
3632 case 1:
3633 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3634 continue;
3635 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3636 check_id = (atoi(params[0]) == curproxy->uuid);
3637 check_name = (strcmp(curproxy->id, params[1]) == 0);
3638 break;
3639 }
3640
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003641 bk = curproxy;
3642
3643 /* if backend can't be found, let's continue */
3644 if (!check_id && !check_name)
3645 continue;
3646 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003647 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 +02003648 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3649 }
3650 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003651 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 +02003652 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3653 /* if name doesn't match, we still want to update curproxy if the backend id
3654 * was forced in previous the previous configuration */
3655 if (!bk_f_forced_id)
3656 continue;
3657 }
3658
Baptiste Assmann95c2c012019-06-11 14:51:49 +02003659 /* look for the server by its name: param[3] */
3660 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003661
3662 if (!srv) {
3663 /* if no server found, then warning and continue with next line */
Baptiste Assmann95c2c012019-06-11 14:51:49 +02003664 ha_warning("can't find server '%s' in backend '%s'\n",
3665 params[3], params[1]);
3666 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3667 params[3], params[1]);
Frédéric Lécaille0bedb8a2017-06-15 14:09:10 +02003668 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003669 }
3670
3671 /* now we can proceed with server's state update */
3672 srv_update_state(srv, version, srv_params);
3673 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003674fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003675 fclose(f);
3676 }
3677}
3678
Simon Horman7d09b9a2013-02-12 10:45:51 +09003679/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003680 * update a server's current IP address.
3681 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3682 * ip is in network format.
3683 * updater is a string which contains an information about the requester of the update.
3684 * updater is used if not NULL.
3685 *
3686 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003687 *
3688 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003689 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003690int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003691{
3692 /* generates a log line and a warning on stderr */
3693 if (1) {
3694 /* book enough space for both IPv4 and IPv6 */
3695 char oldip[INET6_ADDRSTRLEN];
3696 char newip[INET6_ADDRSTRLEN];
3697
3698 memset(oldip, '\0', INET6_ADDRSTRLEN);
3699 memset(newip, '\0', INET6_ADDRSTRLEN);
3700
3701 /* copy old IP address in a string */
3702 switch (s->addr.ss_family) {
3703 case AF_INET:
3704 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3705 break;
3706 case AF_INET6:
3707 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3708 break;
3709 };
3710
3711 /* copy new IP address in a string */
3712 switch (ip_sin_family) {
3713 case AF_INET:
3714 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3715 break;
3716 case AF_INET6:
3717 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3718 break;
3719 };
3720
3721 /* save log line into a buffer */
3722 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3723 s->proxy->id, s->id, oldip, newip, updater);
3724
3725 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003726 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003727
3728 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003729 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003730 }
3731
3732 /* save the new IP family */
3733 s->addr.ss_family = ip_sin_family;
3734 /* save the new IP address */
3735 switch (ip_sin_family) {
3736 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003737 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003738 break;
3739 case AF_INET6:
3740 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3741 break;
3742 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003743 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003744
3745 return 0;
3746}
3747
3748/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003749 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3750 *
3751 * Caller can pass its name through <updater> to get it integrated in the response message
3752 * returned by the function.
3753 *
3754 * The function first does the following, in that order:
3755 * - validates the new addr and/or port
3756 * - checks if an update is required (new IP or port is different than current ones)
3757 * - checks the update is allowed:
3758 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3759 * - allow all changes if no CHECKS are configured
3760 * - if CHECK is configured:
3761 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3762 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3763 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003764 *
3765 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003766 */
3767const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3768{
3769 struct sockaddr_storage sa;
3770 int ret, port_change_required;
3771 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003772 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003773 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003774 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003775
3776 msg = get_trash_chunk();
3777 chunk_reset(msg);
3778
3779 if (addr) {
3780 memset(&sa, 0, sizeof(struct sockaddr_storage));
3781 if (str2ip2(addr, &sa, 0) == NULL) {
3782 chunk_printf(msg, "Invalid addr '%s'", addr);
3783 goto out;
3784 }
3785
3786 /* changes are allowed on AF_INET* families only */
3787 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3788 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3789 goto out;
3790 }
3791
3792 /* collecting data currently setup */
3793 memset(current_addr, '\0', sizeof(current_addr));
3794 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3795 /* changes are allowed on AF_INET* families only */
3796 if ((ret != AF_INET) && (ret != AF_INET6)) {
3797 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3798 goto out;
3799 }
3800
3801 /* applying ADDR changes if required and allowed
3802 * ipcmp returns 0 when both ADDR are the same
3803 */
3804 if (ipcmp(&s->addr, &sa) == 0) {
3805 chunk_appendf(msg, "no need to change the addr");
3806 goto port;
3807 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003808 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003809 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003810
3811 /* we also need to update check's ADDR only if it uses the server's one */
3812 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003813 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003814 }
3815
3816 /* we also need to update agent ADDR only if it use the server's one */
3817 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003818 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003819 }
3820
3821 /* update report for caller */
3822 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3823 }
3824
3825 port:
3826 if (port) {
3827 char sign = '\0';
3828 char *endptr;
3829
3830 if (addr)
3831 chunk_appendf(msg, ", ");
3832
3833 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003834 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003835
3836 /* check if PORT change is required */
3837 port_change_required = 0;
3838
3839 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003840 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003841 new_port = strtol(port, &endptr, 10);
3842 if ((errno != 0) || (port == endptr)) {
3843 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3844 goto out;
3845 }
3846
3847 /* check if caller triggers a port mapped or offset */
3848 if (sign == '-' || (sign == '+')) {
3849 /* check if server currently uses port map */
3850 if (!(s->flags & SRV_F_MAPPORTS)) {
3851 /* switch from fixed port to port map mandatorily triggers
3852 * a port change */
3853 port_change_required = 1;
3854 /* check is configured
3855 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3856 * prevent PORT change if check doesn't have it's dedicated port while switching
3857 * to port mapping */
3858 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3859 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.");
3860 goto out;
3861 }
3862 }
3863 /* we're already using port maps */
3864 else {
3865 port_change_required = current_port != new_port;
3866 }
3867 }
3868 /* fixed port */
3869 else {
3870 port_change_required = current_port != new_port;
3871 }
3872
3873 /* applying PORT changes if required and update response message */
3874 if (port_change_required) {
3875 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003876 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003877 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003878
3879 /* prepare message */
3880 chunk_appendf(msg, "port changed from '");
3881 if (s->flags & SRV_F_MAPPORTS)
3882 chunk_appendf(msg, "+");
3883 chunk_appendf(msg, "%d' to '", current_port);
3884
3885 if (sign == '-') {
3886 s->flags |= SRV_F_MAPPORTS;
3887 chunk_appendf(msg, "%c", sign);
3888 /* just use for result output */
3889 new_port = -new_port;
3890 }
3891 else if (sign == '+') {
3892 s->flags |= SRV_F_MAPPORTS;
3893 chunk_appendf(msg, "%c", sign);
3894 }
3895 else {
3896 s->flags &= ~SRV_F_MAPPORTS;
3897 }
3898
3899 chunk_appendf(msg, "%d'", new_port);
3900
3901 /* we also need to update health checks port only if it uses server's realport */
3902 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3903 s->check.port = new_port;
3904 }
3905 }
3906 else {
3907 chunk_appendf(msg, "no need to change the port");
3908 }
3909 }
3910
3911out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003912 if (changed)
3913 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003914 if (updater)
3915 chunk_appendf(msg, " by '%s'", updater);
3916 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003917 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003918}
3919
3920
3921/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003922 * update server status based on result of name resolution
3923 * returns:
3924 * 0 if server status is updated
3925 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003926 *
3927 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003928 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003929int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003930{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003931 struct dns_resolvers *resolvers = s->resolvers;
3932 struct dns_resolution *resolution = s->dns_requester->resolution;
3933 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003934
3935 switch (resolution->status) {
3936 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003937 /* status when HAProxy has just (re)started.
3938 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003939 break;
3940
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003941 case RSLV_STATUS_VALID:
3942 /*
3943 * resume health checks
3944 * server will be turned back on if health check is safe
3945 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003946 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003947 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003948 return 1;
3949 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3950 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003951 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003952 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003953
Emeric Brun52a91d32017-08-31 14:41:55 +02003954 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003955 return 1;
3956 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3957 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3958 s->proxy->id, s->id);
3959
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003960 ha_warning("%s.\n", trash.area);
3961 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003962 return 0;
3963
3964 case RSLV_STATUS_NX:
3965 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003966 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3967 if (!tick_is_expired(exp, now_ms))
3968 break;
3969
3970 if (s->next_admin & SRV_ADMF_RMAINT)
3971 return 1;
3972 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3973 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003974
3975 case RSLV_STATUS_TIMEOUT:
3976 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003977 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3978 if (!tick_is_expired(exp, now_ms))
3979 break;
3980
3981 if (s->next_admin & SRV_ADMF_RMAINT)
3982 return 1;
3983 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3984 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003985
3986 case RSLV_STATUS_REFUSED:
3987 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003988 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3989 if (!tick_is_expired(exp, now_ms))
3990 break;
3991
3992 if (s->next_admin & SRV_ADMF_RMAINT)
3993 return 1;
3994 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3995 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003996
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003997 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003998 /* stop server if resolution failed for a long enough period */
3999 exp = tick_add(resolution->last_valid, resolvers->hold.other);
4000 if (!tick_is_expired(exp, now_ms))
4001 break;
4002
4003 if (s->next_admin & SRV_ADMF_RMAINT)
4004 return 1;
4005 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
4006 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004007 }
4008
4009 return 1;
4010}
4011
4012/*
4013 * Server Name Resolution valid response callback
4014 * It expects:
4015 * - <nameserver>: the name server which answered the valid response
4016 * - <response>: buffer containing a valid DNS response
4017 * - <response_len>: size of <response>
4018 * It performs the following actions:
4019 * - ignore response if current ip found and server family not met
4020 * - update with first new ip found if family is met and current IP is not found
4021 * returns:
4022 * 0 on error
4023 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01004024 *
4025 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004026 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004027int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004028{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004029 struct server *s = NULL;
4030 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004031 void *serverip, *firstip;
4032 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004033 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02004034 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004035 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004036
Christopher Faulet67957bd2017-09-27 11:00:59 +02004037 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004038 if (!s)
4039 return 1;
4040
Christopher Faulet67957bd2017-09-27 11:00:59 +02004041 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004042
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004043 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004044 firstip = NULL; /* pointer to the first valid response found */
4045 /* it will be used as the new IP if a change is required */
4046 firstip_sin_family = AF_UNSPEC;
4047 serverip = NULL; /* current server IP address */
4048
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004049 /* initializing server IP pointer */
4050 server_sin_family = s->addr.ss_family;
4051 switch (server_sin_family) {
4052 case AF_INET:
4053 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
4054 break;
4055
4056 case AF_INET6:
4057 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
4058 break;
4059
Willy Tarreau3acfcd12017-01-06 19:18:32 +01004060 case AF_UNSPEC:
4061 break;
4062
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004063 default:
4064 goto invalid;
4065 }
4066
Baptiste Assmann729c9012017-05-22 15:13:10 +02004067 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01004068 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004069 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004070
4071 switch (ret) {
4072 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004073 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004074
4075 case DNS_UPD_SRVIP_NOT_FOUND:
4076 goto save_ip;
4077
4078 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004079 goto invalid;
4080
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004081 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004082 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004083 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004084
Baptiste Assmannfad03182015-10-28 02:03:32 +01004085 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01004086 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004087 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004088 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01004089
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004090 default:
4091 goto invalid;
4092
4093 }
4094
4095 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004096 if (nameserver) {
4097 nameserver->counters.update++;
4098 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004099 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004100 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004101 else
4102 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004103 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004104
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004105 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004106 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004107 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004108
4109 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004110 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02004111 nameserver->counters.invalid++;
4112 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004113 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004114 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004115 return 0;
4116}
4117
4118/*
4119 * Server Name Resolution error management callback
4120 * returns:
4121 * 0 on error
4122 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004123 *
4124 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004125 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004126int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004127{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004128 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004129
Christopher Faulet67957bd2017-09-27 11:00:59 +02004130 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004131 if (!s)
4132 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004133 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004134 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004135 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004136 return 1;
4137}
4138
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004139/*
4140 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004141 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004142 * It returns a pointer to the first server found or NULL if <ip> is not yet
4143 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004144 *
4145 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004146 */
4147struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4148{
4149 struct server *tmpsrv;
4150 struct proxy *be;
4151
4152 if (!srv)
4153 return NULL;
4154
4155 be = srv->proxy;
4156 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004157 /* we found the current server is the same, ignore it */
4158 if (srv == tmpsrv)
4159 continue;
4160
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004161 /* We want to compare the IP in the record with the IP of the servers in the
4162 * same backend, only if:
4163 * * DNS resolution is enabled on the server
4164 * * the hostname used for the resolution by our server is the same than the
4165 * one used for the server found in the backend
4166 * * the server found in the backend is not our current server
4167 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004168 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004169 if ((tmpsrv->hostname_dn == NULL) ||
4170 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4171 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004172 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004173 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004174 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004175 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004176
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004177 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004178 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004179 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004180 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004181 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004182
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004183 /* At this point, we have 2 different servers using the same DNS hostname
4184 * for their respective resolution.
4185 */
4186 if (*ip_family == tmpsrv->addr.ss_family &&
4187 ((tmpsrv->addr.ss_family == AF_INET &&
4188 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4189 (tmpsrv->addr.ss_family == AF_INET6 &&
4190 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004191 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004192 return tmpsrv;
4193 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004194 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004195 }
4196
Emeric Brune9fd6b52017-11-02 17:20:39 +01004197
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004198 return NULL;
4199}
4200
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004201/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4202 * resolver. This is suited for initial address configuration. Returns 0 on
4203 * success otherwise a non-zero error code. In case of error, *err_code, if
4204 * not NULL, is filled up.
4205 */
4206int srv_set_addr_via_libc(struct server *srv, int *err_code)
4207{
4208 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004209 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004210 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004211 return 1;
4212 }
4213 return 0;
4214}
4215
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004216/* Set the server's FDQN (->hostname) from <hostname>.
4217 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004218 *
4219 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004220 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004221int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004222{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004223 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004224 char *hostname_dn;
4225 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004226
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004227 /* Note that the server lock is already held. */
4228 if (!srv->resolvers)
4229 return -1;
4230
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004231 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004232 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004233 /* run time DNS resolution was not active for this server
4234 * and we can't enable it at run time for now.
4235 */
4236 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004237 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004238
4239 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004240 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004241 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004242 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4243 hostname_dn, trash.size);
4244 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004245 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004246
Christopher Faulet67957bd2017-09-27 11:00:59 +02004247 resolution = srv->dns_requester->resolution;
4248 if (resolution &&
4249 resolution->hostname_dn &&
4250 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004251 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004252
Christopher Faulet67957bd2017-09-27 11:00:59 +02004253 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004254
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004255 free(srv->hostname);
4256 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004257 srv->hostname = strdup(hostname);
4258 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004259 srv->hostname_dn_len = hostname_dn_len;
4260 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004261 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004262
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004263 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004264 goto err;
4265
4266 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004267 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004268 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004269 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004270
4271 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004272 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004273 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004274 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004275}
4276
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004277/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4278 * from the state file. This is suited for initial address configuration.
4279 * Returns 0 on success otherwise a non-zero error code. In case of error,
4280 * *err_code, if not NULL, is filled up.
4281 */
4282static int srv_apply_lastaddr(struct server *srv, int *err_code)
4283{
4284 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4285 if (err_code)
4286 *err_code |= ERR_WARN;
4287 return 1;
4288 }
4289 return 0;
4290}
4291
Willy Tarreau25e51522016-11-04 15:10:17 +01004292/* returns 0 if no error, otherwise a combination of ERR_* flags */
4293static int srv_iterate_initaddr(struct server *srv)
4294{
4295 int return_code = 0;
4296 int err_code;
4297 unsigned int methods;
4298
4299 methods = srv->init_addr_methods;
4300 if (!methods) { // default to "last,libc"
4301 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4302 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4303 }
4304
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004305 /* "-dr" : always append "none" so that server addresses resolution
4306 * failures are silently ignored, this is convenient to validate some
4307 * configs out of their environment.
4308 */
4309 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4310 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4311
Willy Tarreau25e51522016-11-04 15:10:17 +01004312 while (methods) {
4313 err_code = 0;
4314 switch (srv_get_next_initaddr(&methods)) {
4315 case SRV_IADDR_LAST:
4316 if (!srv->lastaddr)
4317 continue;
4318 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004319 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004320 return_code |= err_code;
4321 break;
4322
4323 case SRV_IADDR_LIBC:
4324 if (!srv->hostname)
4325 continue;
4326 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004327 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004328 return_code |= err_code;
4329 break;
4330
Willy Tarreau37ebe122016-11-04 15:17:58 +01004331 case SRV_IADDR_NONE:
4332 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004333 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004334 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4335 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004336 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004337 return return_code;
4338
Willy Tarreau4310d362016-11-02 15:05:56 +01004339 case SRV_IADDR_IP:
4340 ipcpy(&srv->init_addr, &srv->addr);
4341 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004342 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4343 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004344 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004345 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004346
Willy Tarreau25e51522016-11-04 15:10:17 +01004347 default: /* unhandled method */
4348 break;
4349 }
4350 }
4351
4352 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004353 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004354 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4355 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004356 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004357 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004358 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4359 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004360
4361 return_code |= ERR_ALERT | ERR_FATAL;
4362 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004363out:
4364 srv_set_dyncookie(srv);
4365 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004366}
4367
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004368/*
4369 * This function parses all backends and all servers within each backend
4370 * and performs servers' addr resolution based on information provided by:
4371 * - configuration file
4372 * - server-state file (states provided by an 'old' haproxy process)
4373 *
4374 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4375 */
4376int srv_init_addr(void)
4377{
4378 struct proxy *curproxy;
4379 int return_code = 0;
4380
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004381 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004382 while (curproxy) {
4383 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004384
4385 /* servers are in backend only */
4386 if (!(curproxy->cap & PR_CAP_BE))
4387 goto srv_init_addr_next;
4388
Willy Tarreau25e51522016-11-04 15:10:17 +01004389 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004390 if (srv->hostname)
4391 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004392
4393 srv_init_addr_next:
4394 curproxy = curproxy->next;
4395 }
4396
4397 return return_code;
4398}
4399
Willy Tarreau46b7f532018-08-21 11:54:26 +02004400/*
4401 * Must be called with the server lock held.
4402 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004403const 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 +02004404{
4405
Willy Tarreau83061a82018-07-13 11:56:34 +02004406 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004407
4408 msg = get_trash_chunk();
4409 chunk_reset(msg);
4410
Olivier Houchard8da5f982017-08-04 18:35:36 +02004411 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004412 chunk_appendf(msg, "no need to change the FDQN");
4413 goto out;
4414 }
4415
4416 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4417 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4418 goto out;
4419 }
4420
4421 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4422 server->proxy->id, server->id, server->hostname, fqdn);
4423
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004424 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004425 chunk_reset(msg);
4426 chunk_appendf(msg, "could not update %s/%s FQDN",
4427 server->proxy->id, server->id);
4428 goto out;
4429 }
4430
4431 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004432 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004433
4434 out:
4435 if (updater)
4436 chunk_appendf(msg, " by '%s'", updater);
4437 chunk_appendf(msg, "\n");
4438
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004439 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004440}
4441
4442
Willy Tarreau21b069d2016-11-23 17:15:08 +01004443/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4444 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004445 * 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 +01004446 * used for CLI commands requiring a server name.
4447 * Important: the <arg> is modified to remove the '/'.
4448 */
4449struct server *cli_find_server(struct appctx *appctx, char *arg)
4450{
4451 struct proxy *px;
4452 struct server *sv;
4453 char *line;
4454
4455 /* split "backend/server" and make <line> point to server */
4456 for (line = arg; *line; line++)
4457 if (*line == '/') {
4458 *line++ = '\0';
4459 break;
4460 }
4461
4462 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004463 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004464 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004465 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004466 return NULL;
4467 }
4468
4469 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004470 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004471 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004472 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004473 return NULL;
4474 }
4475
4476 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004477 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004478 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004479 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004480 return NULL;
4481 }
4482
4483 return sv;
4484}
4485
William Lallemand222baf22016-11-19 02:00:33 +01004486
Willy Tarreau46b7f532018-08-21 11:54:26 +02004487/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004488static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004489{
4490 struct server *sv;
4491 const char *warning;
4492
4493 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4494 return 1;
4495
4496 sv = cli_find_server(appctx, args[2]);
4497 if (!sv)
4498 return 1;
4499
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004500 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004501
William Lallemand222baf22016-11-19 02:00:33 +01004502 if (strcmp(args[3], "weight") == 0) {
4503 warning = server_parse_weight_change_request(sv, args[4]);
4504 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004505 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004506 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004507 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004508 }
4509 }
4510 else if (strcmp(args[3], "state") == 0) {
4511 if (strcmp(args[4], "ready") == 0)
4512 srv_adm_set_ready(sv);
4513 else if (strcmp(args[4], "drain") == 0)
4514 srv_adm_set_drain(sv);
4515 else if (strcmp(args[4], "maint") == 0)
4516 srv_adm_set_maint(sv);
4517 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004518 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004519 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004520 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004521 }
4522 }
4523 else if (strcmp(args[3], "health") == 0) {
4524 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004525 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004526 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004527 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004528 }
4529 else if (strcmp(args[4], "up") == 0) {
4530 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004531 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004532 }
4533 else if (strcmp(args[4], "stopping") == 0) {
4534 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004535 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004536 }
4537 else if (strcmp(args[4], "down") == 0) {
4538 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004539 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004540 }
4541 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004542 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004543 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004544 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004545 }
4546 }
4547 else if (strcmp(args[3], "agent") == 0) {
4548 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004549 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004550 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004551 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004552 }
4553 else if (strcmp(args[4], "up") == 0) {
4554 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004555 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004556 }
4557 else if (strcmp(args[4], "down") == 0) {
4558 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004559 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004560 }
4561 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004562 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004563 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004564 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004565 }
4566 }
Misiek2da082d2017-01-09 09:40:42 +01004567 else if (strcmp(args[3], "agent-addr") == 0) {
4568 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004569 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004570 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4571 appctx->st0 = CLI_ST_PRINT;
4572 } else {
4573 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004574 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004575 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4576 appctx->st0 = CLI_ST_PRINT;
4577 }
4578 }
4579 }
4580 else if (strcmp(args[3], "agent-send") == 0) {
4581 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004582 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004583 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4584 appctx->st0 = CLI_ST_PRINT;
4585 } else {
4586 char *nss = strdup(args[4]);
4587 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004588 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004589 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4590 appctx->st0 = CLI_ST_PRINT;
4591 } else {
4592 free(sv->agent.send_string);
4593 sv->agent.send_string = nss;
4594 sv->agent.send_string_len = strlen(args[4]);
4595 }
4596 }
4597 }
William Lallemand222baf22016-11-19 02:00:33 +01004598 else if (strcmp(args[3], "check-port") == 0) {
4599 int i = 0;
4600 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004601 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004602 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004603 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004604 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004605 }
4606 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004607 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004608 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004609 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004610 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004611 }
4612 /* prevent the update of port to 0 if MAPPORTS are in use */
4613 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004614 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004615 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004616 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004617 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004618 }
4619 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004620 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004621 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004622 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004623 }
4624 else if (strcmp(args[3], "addr") == 0) {
4625 char *addr = NULL;
4626 char *port = NULL;
4627 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004628 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004629 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004630 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004631 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004632 }
4633 else {
4634 addr = args[4];
4635 }
4636 if (strcmp(args[5], "port") == 0) {
4637 port = args[6];
4638 }
4639 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4640 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004641 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004642 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004643 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004644 }
4645 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4646 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004647 else if (strcmp(args[3], "fqdn") == 0) {
4648 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004649 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004650 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4651 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004652 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004653 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004654 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004655 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004656 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004657 appctx->ctx.cli.msg = warning;
4658 appctx->st0 = CLI_ST_PRINT;
4659 }
4660 }
William Lallemand222baf22016-11-19 02:00:33 +01004661 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004662 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004663 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 +01004664 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004665 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004666 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004667 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004668 return 1;
4669}
4670
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004671static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004672{
4673 struct stream_interface *si = appctx->owner;
4674 struct proxy *px;
4675 struct server *sv;
4676 char *line;
4677
4678
4679 /* split "backend/server" and make <line> point to server */
4680 for (line = args[2]; *line; line++)
4681 if (*line == '/') {
4682 *line++ = '\0';
4683 break;
4684 }
4685
4686 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004687 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004688 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004689 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004690 return 1;
4691 }
4692
4693 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004694 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004695 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004696 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004697 return 1;
4698 }
4699
4700 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004701 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4702 sv->iweight);
4703 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004704 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004705 return 0;
4706 }
William Lallemand6b160942016-11-22 12:34:35 +01004707 return 1;
4708}
4709
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004710/* Parse a "set weight" command.
4711 *
4712 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004713 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004714static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004715{
4716 struct server *sv;
4717 const char *warning;
4718
4719 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4720 return 1;
4721
4722 sv = cli_find_server(appctx, args[2]);
4723 if (!sv)
4724 return 1;
4725
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004726 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4727
William Lallemand6b160942016-11-22 12:34:35 +01004728 warning = server_parse_weight_change_request(sv, args[3]);
4729 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004730 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004731 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004732 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004733 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004734
4735 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4736
William Lallemand6b160942016-11-22 12:34:35 +01004737 return 1;
4738}
4739
Willy Tarreau46b7f532018-08-21 11:54:26 +02004740/* parse a "set maxconn server" command. It always returns 1.
4741 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004742 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004743 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004744static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004745{
4746 struct server *sv;
4747 const char *warning;
4748
4749 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4750 return 1;
4751
4752 sv = cli_find_server(appctx, args[3]);
4753 if (!sv)
4754 return 1;
4755
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004756 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4757
Willy Tarreaub8026272016-11-23 11:26:56 +01004758 warning = server_parse_maxconn_change_request(sv, args[4]);
4759 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004760 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004761 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004762 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004763 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004764
4765 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4766
Willy Tarreaub8026272016-11-23 11:26:56 +01004767 return 1;
4768}
William Lallemand6b160942016-11-22 12:34:35 +01004769
Willy Tarreau46b7f532018-08-21 11:54:26 +02004770/* parse a "disable agent" command. It always returns 1.
4771 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004772 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004773 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004774static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004775{
4776 struct server *sv;
4777
4778 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4779 return 1;
4780
4781 sv = cli_find_server(appctx, args[2]);
4782 if (!sv)
4783 return 1;
4784
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004785 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004786 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004787 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004788 return 1;
4789}
4790
Willy Tarreau46b7f532018-08-21 11:54:26 +02004791/* parse a "disable health" command. It always returns 1.
4792 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004793 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004794 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004795static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004796{
4797 struct server *sv;
4798
4799 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4800 return 1;
4801
4802 sv = cli_find_server(appctx, args[2]);
4803 if (!sv)
4804 return 1;
4805
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004806 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004807 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004808 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004809 return 1;
4810}
4811
Willy Tarreau46b7f532018-08-21 11:54:26 +02004812/* parse a "disable server" command. It always returns 1.
4813 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004814 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004815 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004816static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004817{
4818 struct server *sv;
4819
4820 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4821 return 1;
4822
4823 sv = cli_find_server(appctx, args[2]);
4824 if (!sv)
4825 return 1;
4826
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004827 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004828 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004829 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004830 return 1;
4831}
4832
Willy Tarreau46b7f532018-08-21 11:54:26 +02004833/* parse a "enable agent" command. It always returns 1.
4834 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004835 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004836 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004837static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004838{
4839 struct server *sv;
4840
4841 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4842 return 1;
4843
4844 sv = cli_find_server(appctx, args[2]);
4845 if (!sv)
4846 return 1;
4847
4848 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004849 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004850 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004851 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004852 return 1;
4853 }
4854
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004855 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004856 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004857 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004858 return 1;
4859}
4860
Willy Tarreau46b7f532018-08-21 11:54:26 +02004861/* parse a "enable health" command. It always returns 1.
4862 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004863 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004864 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004865static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004866{
4867 struct server *sv;
4868
4869 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4870 return 1;
4871
4872 sv = cli_find_server(appctx, args[2]);
4873 if (!sv)
4874 return 1;
4875
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004876 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004877 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004878 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004879 return 1;
4880}
4881
Willy Tarreau46b7f532018-08-21 11:54:26 +02004882/* parse a "enable server" command. It always returns 1.
4883 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004884 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004885 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004886static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004887{
4888 struct server *sv;
4889
4890 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4891 return 1;
4892
4893 sv = cli_find_server(appctx, args[2]);
4894 if (!sv)
4895 return 1;
4896
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004897 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004898 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004899 if (!(sv->flags & SRV_F_COOKIESET)
4900 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4901 sv->cookie)
4902 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004903 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004904 return 1;
4905}
4906
William Lallemand222baf22016-11-19 02:00:33 +01004907/* register cli keywords */
4908static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004909 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004910 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004911 { { "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 +01004912 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004913 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004914 { { "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 +01004915 { { "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 +01004916 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004917 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4918 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4919
William Lallemand222baf22016-11-19 02:00:33 +01004920 {{},}
4921}};
4922
Willy Tarreau0108d902018-11-25 19:14:37 +01004923INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004924
Emeric Brun64cc49c2017-10-03 14:46:45 +02004925/*
4926 * This function applies server's status changes, it is
4927 * is designed to be called asynchronously.
4928 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004929 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004930 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004931static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004932{
4933 struct check *check = &s->check;
4934 int xferred;
4935 struct proxy *px = s->proxy;
4936 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4937 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4938 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004939 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004940
Emeric Brun64cc49c2017-10-03 14:46:45 +02004941 /* If currently main is not set we try to apply pending state changes */
4942 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4943 int next_admin;
4944
4945 /* Backup next admin */
4946 next_admin = s->next_admin;
4947
4948 /* restore current admin state */
4949 s->next_admin = s->cur_admin;
4950
4951 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4952 s->last_change = now.tv_sec;
4953 if (s->proxy->lbprm.set_server_status_down)
4954 s->proxy->lbprm.set_server_status_down(s);
4955
4956 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4957 srv_shutdown_streams(s, SF_ERR_DOWN);
4958
4959 /* we might have streams queued on this server and waiting for
4960 * a connection. Those which are redispatchable will be queued
4961 * to another server or to the proxy itself.
4962 */
4963 xferred = pendconn_redistribute(s);
4964
4965 tmptrash = alloc_trash_chunk();
4966 if (tmptrash) {
4967 chunk_printf(tmptrash,
4968 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4969 s->proxy->id, s->id);
4970
Emeric Brun5a133512017-10-19 14:42:30 +02004971 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004972 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004973
4974 /* we don't send an alert if the server was previously paused */
4975 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004976 send_log(s->proxy, log_level, "%s.\n",
4977 tmptrash->area);
4978 send_email_alert(s, log_level, "%s",
4979 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004980 free_trash_chunk(tmptrash);
4981 tmptrash = NULL;
4982 }
4983 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4984 set_backend_down(s->proxy);
4985
4986 s->counters.down_trans++;
4987 }
4988 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4989 s->last_change = now.tv_sec;
4990 if (s->proxy->lbprm.set_server_status_down)
4991 s->proxy->lbprm.set_server_status_down(s);
4992
4993 /* we might have streams queued on this server and waiting for
4994 * a connection. Those which are redispatchable will be queued
4995 * to another server or to the proxy itself.
4996 */
4997 xferred = pendconn_redistribute(s);
4998
4999 tmptrash = alloc_trash_chunk();
5000 if (tmptrash) {
5001 chunk_printf(tmptrash,
5002 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5003 s->proxy->id, s->id);
5004
Emeric Brun5a133512017-10-19 14:42:30 +02005005 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005006
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005007 ha_warning("%s.\n", tmptrash->area);
5008 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5009 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005010 free_trash_chunk(tmptrash);
5011 tmptrash = NULL;
5012 }
5013
5014 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5015 set_backend_down(s->proxy);
5016 }
5017 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5018 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
5019 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5020 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5021 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5022 s->proxy->last_change = now.tv_sec;
5023 }
5024
5025 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
5026 s->down_time += now.tv_sec - s->last_change;
5027
5028 s->last_change = now.tv_sec;
5029 if (s->next_state == SRV_ST_STARTING)
5030 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5031
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005032 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005033 /* now propagate the status change to any LB algorithms */
5034 if (px->lbprm.update_server_eweight)
5035 px->lbprm.update_server_eweight(s);
5036 else if (srv_willbe_usable(s)) {
5037 if (px->lbprm.set_server_status_up)
5038 px->lbprm.set_server_status_up(s);
5039 }
5040 else {
5041 if (px->lbprm.set_server_status_down)
5042 px->lbprm.set_server_status_down(s);
5043 }
5044
5045 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5046 * and it's not a backup server and its effective weight is > 0,
5047 * then it can accept new connections, so we shut down all streams
5048 * on all backup servers.
5049 */
5050 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5051 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5052 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5053
5054 /* check if we can handle some connections queued at the proxy. We
5055 * will take as many as we can handle.
5056 */
5057 xferred = pendconn_grab_from_px(s);
5058
5059 tmptrash = alloc_trash_chunk();
5060 if (tmptrash) {
5061 chunk_printf(tmptrash,
5062 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5063 s->proxy->id, s->id);
5064
Emeric Brun5a133512017-10-19 14:42:30 +02005065 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005066 ha_warning("%s.\n", tmptrash->area);
5067 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5068 tmptrash->area);
5069 send_email_alert(s, LOG_NOTICE, "%s",
5070 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005071 free_trash_chunk(tmptrash);
5072 tmptrash = NULL;
5073 }
5074
5075 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5076 set_backend_down(s->proxy);
5077 }
5078 else if (s->cur_eweight != s->next_eweight) {
5079 /* now propagate the status change to any LB algorithms */
5080 if (px->lbprm.update_server_eweight)
5081 px->lbprm.update_server_eweight(s);
5082 else if (srv_willbe_usable(s)) {
5083 if (px->lbprm.set_server_status_up)
5084 px->lbprm.set_server_status_up(s);
5085 }
5086 else {
5087 if (px->lbprm.set_server_status_down)
5088 px->lbprm.set_server_status_down(s);
5089 }
5090
5091 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5092 set_backend_down(s->proxy);
5093 }
5094
5095 s->next_admin = next_admin;
5096 }
5097
Emeric Brun5a133512017-10-19 14:42:30 +02005098 /* reset operational state change */
5099 *s->op_st_chg.reason = 0;
5100 s->op_st_chg.status = s->op_st_chg.code = -1;
5101 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005102
5103 /* Now we try to apply pending admin changes */
5104
5105 /* Maintenance must also disable health checks */
5106 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5107 if (s->check.state & CHK_ST_ENABLED) {
5108 s->check.state |= CHK_ST_PAUSED;
5109 check->health = 0;
5110 }
5111
5112 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005113 tmptrash = alloc_trash_chunk();
5114 if (tmptrash) {
5115 chunk_printf(tmptrash,
5116 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5117 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5118 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005119
Olivier Houchard796a2b32017-10-24 17:42:47 +02005120 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005121
Olivier Houchard796a2b32017-10-24 17:42:47 +02005122 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005123 ha_warning("%s.\n", tmptrash->area);
5124 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5125 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005126 }
5127 free_trash_chunk(tmptrash);
5128 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005129 }
Emeric Brun8f298292017-12-06 16:47:17 +01005130 /* commit new admin status */
5131
5132 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005133 }
5134 else { /* server was still running */
5135 check->health = 0; /* failure */
5136 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005137
5138 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005139 if (s->proxy->lbprm.set_server_status_down)
5140 s->proxy->lbprm.set_server_status_down(s);
5141
Emeric Brun64cc49c2017-10-03 14:46:45 +02005142 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5143 srv_shutdown_streams(s, SF_ERR_DOWN);
5144
5145 /* we might have streams queued on this server and waiting for
5146 * a connection. Those which are redispatchable will be queued
5147 * to another server or to the proxy itself.
5148 */
5149 xferred = pendconn_redistribute(s);
5150
5151 tmptrash = alloc_trash_chunk();
5152 if (tmptrash) {
5153 chunk_printf(tmptrash,
5154 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5155 s->flags & SRV_F_BACKUP ? "Backup " : "",
5156 s->proxy->id, s->id,
5157 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5158
5159 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5160
5161 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005162 ha_warning("%s.\n", tmptrash->area);
5163 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5164 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005165 }
5166 free_trash_chunk(tmptrash);
5167 tmptrash = NULL;
5168 }
5169 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5170 set_backend_down(s->proxy);
5171
5172 s->counters.down_trans++;
5173 }
5174 }
5175 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5176 /* OK here we're leaving maintenance, we have many things to check,
5177 * because the server might possibly be coming back up depending on
5178 * its state. In practice, leaving maintenance means that we should
5179 * immediately turn to UP (more or less the slowstart) under the
5180 * following conditions :
5181 * - server is neither checked nor tracked
5182 * - server tracks another server which is not checked
5183 * - server tracks another server which is already up
5184 * Which sums up as something simpler :
5185 * "either the tracking server is up or the server's checks are disabled
5186 * or up". Otherwise we only re-enable health checks. There's a special
5187 * case associated to the stopping state which can be inherited. Note
5188 * that the server might still be in drain mode, which is naturally dealt
5189 * with by the lower level functions.
5190 */
5191
5192 if (s->check.state & CHK_ST_ENABLED) {
5193 s->check.state &= ~CHK_ST_PAUSED;
5194 check->health = check->rise; /* start OK but check immediately */
5195 }
5196
5197 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5198 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5199 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5200 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5201 s->next_state = SRV_ST_STOPPING;
5202 }
5203 else {
5204 s->next_state = SRV_ST_STARTING;
5205 if (s->slowstart > 0)
5206 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5207 else
5208 s->next_state = SRV_ST_RUNNING;
5209 }
5210
5211 }
5212
5213 tmptrash = alloc_trash_chunk();
5214 if (tmptrash) {
5215 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5216 chunk_printf(tmptrash,
5217 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5218 s->flags & SRV_F_BACKUP ? "Backup " : "",
5219 s->proxy->id, s->id,
5220 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5221 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5222 }
5223 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5224 chunk_printf(tmptrash,
5225 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5226 s->flags & SRV_F_BACKUP ? "Backup " : "",
5227 s->proxy->id, s->id, s->hostname,
5228 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5229 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5230 }
5231 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5232 chunk_printf(tmptrash,
5233 "%sServer %s/%s is %s/%s (leaving maintenance)",
5234 s->flags & SRV_F_BACKUP ? "Backup " : "",
5235 s->proxy->id, s->id,
5236 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5237 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5238 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005239 ha_warning("%s.\n", tmptrash->area);
5240 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5241 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005242 free_trash_chunk(tmptrash);
5243 tmptrash = NULL;
5244 }
5245
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005246 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005247 /* now propagate the status change to any LB algorithms */
5248 if (px->lbprm.update_server_eweight)
5249 px->lbprm.update_server_eweight(s);
5250 else if (srv_willbe_usable(s)) {
5251 if (px->lbprm.set_server_status_up)
5252 px->lbprm.set_server_status_up(s);
5253 }
5254 else {
5255 if (px->lbprm.set_server_status_down)
5256 px->lbprm.set_server_status_down(s);
5257 }
5258
5259 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5260 set_backend_down(s->proxy);
5261
Willy Tarreau6a78e612018-08-07 10:14:53 +02005262 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5263 * and it's not a backup server and its effective weight is > 0,
5264 * then it can accept new connections, so we shut down all streams
5265 * on all backup servers.
5266 */
5267 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5268 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5269 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5270
5271 /* check if we can handle some connections queued at the proxy. We
5272 * will take as many as we can handle.
5273 */
5274 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005275 }
5276 else if (s->next_admin & SRV_ADMF_MAINT) {
5277 /* remaining in maintenance mode, let's inform precisely about the
5278 * situation.
5279 */
5280 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5281 tmptrash = alloc_trash_chunk();
5282 if (tmptrash) {
5283 chunk_printf(tmptrash,
5284 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5285 s->flags & SRV_F_BACKUP ? "Backup " : "",
5286 s->proxy->id, s->id);
5287
5288 if (s->track) /* normally it's mandatory here */
5289 chunk_appendf(tmptrash, " via %s/%s",
5290 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005291 ha_warning("%s.\n", tmptrash->area);
5292 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5293 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005294 free_trash_chunk(tmptrash);
5295 tmptrash = NULL;
5296 }
5297 }
5298 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5299 tmptrash = alloc_trash_chunk();
5300 if (tmptrash) {
5301 chunk_printf(tmptrash,
5302 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5303 s->flags & SRV_F_BACKUP ? "Backup " : "",
5304 s->proxy->id, s->id, s->hostname);
5305
5306 if (s->track) /* normally it's mandatory here */
5307 chunk_appendf(tmptrash, " via %s/%s",
5308 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005309 ha_warning("%s.\n", tmptrash->area);
5310 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5311 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005312 free_trash_chunk(tmptrash);
5313 tmptrash = NULL;
5314 }
5315 }
5316 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5317 tmptrash = alloc_trash_chunk();
5318 if (tmptrash) {
5319 chunk_printf(tmptrash,
5320 "%sServer %s/%s remains in forced maintenance",
5321 s->flags & SRV_F_BACKUP ? "Backup " : "",
5322 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005323 ha_warning("%s.\n", tmptrash->area);
5324 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5325 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005326 free_trash_chunk(tmptrash);
5327 tmptrash = NULL;
5328 }
5329 }
5330 /* don't report anything when leaving drain mode and remaining in maintenance */
5331
5332 s->cur_admin = s->next_admin;
5333 }
5334
5335 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5336 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5337 /* drain state is applied only if not yet in maint */
5338
5339 s->last_change = now.tv_sec;
5340 if (px->lbprm.set_server_status_down)
5341 px->lbprm.set_server_status_down(s);
5342
5343 /* we might have streams queued on this server and waiting for
5344 * a connection. Those which are redispatchable will be queued
5345 * to another server or to the proxy itself.
5346 */
5347 xferred = pendconn_redistribute(s);
5348
5349 tmptrash = alloc_trash_chunk();
5350 if (tmptrash) {
5351 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5352 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5353 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5354
5355 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5356
5357 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005358 ha_warning("%s.\n", tmptrash->area);
5359 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5360 tmptrash->area);
5361 send_email_alert(s, LOG_NOTICE, "%s",
5362 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005363 }
5364 free_trash_chunk(tmptrash);
5365 tmptrash = NULL;
5366 }
5367
5368 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5369 set_backend_down(s->proxy);
5370 }
5371 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5372 /* OK completely leaving drain mode */
5373 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5374 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5375 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5376 s->proxy->last_change = now.tv_sec;
5377 }
5378
5379 if (s->last_change < now.tv_sec) // ignore negative times
5380 s->down_time += now.tv_sec - s->last_change;
5381 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005382 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005383
5384 tmptrash = alloc_trash_chunk();
5385 if (tmptrash) {
5386 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5387 chunk_printf(tmptrash,
5388 "%sServer %s/%s is %s (leaving forced drain)",
5389 s->flags & SRV_F_BACKUP ? "Backup " : "",
5390 s->proxy->id, s->id,
5391 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5392 }
5393 else {
5394 chunk_printf(tmptrash,
5395 "%sServer %s/%s is %s (leaving drain)",
5396 s->flags & SRV_F_BACKUP ? "Backup " : "",
5397 s->proxy->id, s->id,
5398 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5399 if (s->track) /* normally it's mandatory here */
5400 chunk_appendf(tmptrash, " via %s/%s",
5401 s->track->proxy->id, s->track->id);
5402 }
5403
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005404 ha_warning("%s.\n", tmptrash->area);
5405 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5406 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005407 free_trash_chunk(tmptrash);
5408 tmptrash = NULL;
5409 }
5410
5411 /* now propagate the status change to any LB algorithms */
5412 if (px->lbprm.update_server_eweight)
5413 px->lbprm.update_server_eweight(s);
5414 else if (srv_willbe_usable(s)) {
5415 if (px->lbprm.set_server_status_up)
5416 px->lbprm.set_server_status_up(s);
5417 }
5418 else {
5419 if (px->lbprm.set_server_status_down)
5420 px->lbprm.set_server_status_down(s);
5421 }
5422 }
5423 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5424 /* remaining in drain mode after removing one of its flags */
5425
5426 tmptrash = alloc_trash_chunk();
5427 if (tmptrash) {
5428 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5429 chunk_printf(tmptrash,
5430 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5431 s->flags & SRV_F_BACKUP ? "Backup " : "",
5432 s->proxy->id, s->id);
5433
5434 if (s->track) /* normally it's mandatory here */
5435 chunk_appendf(tmptrash, " via %s/%s",
5436 s->track->proxy->id, s->track->id);
5437 }
5438 else {
5439 chunk_printf(tmptrash,
5440 "%sServer %s/%s remains in forced drain mode",
5441 s->flags & SRV_F_BACKUP ? "Backup " : "",
5442 s->proxy->id, s->id);
5443 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005444 ha_warning("%s.\n", tmptrash->area);
5445 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5446 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005447 free_trash_chunk(tmptrash);
5448 tmptrash = NULL;
5449 }
5450
5451 /* commit new admin status */
5452
5453 s->cur_admin = s->next_admin;
5454 }
5455 }
5456
5457 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005458 *s->adm_st_chg_cause = 0;
5459}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005460
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005461struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5462{
5463 struct connection *conn;
5464
5465 while ((conn = LIST_POP_LOCKED(&toremove_connections[tid],
5466 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005467 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005468 }
5469
5470 return task;
5471}
5472
Willy Tarreau980855b2019-02-07 14:59:29 +01005473struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005474{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005475 struct server *srv;
5476 struct eb32_node *eb;
5477 int i;
5478 unsigned int next_wakeup;
5479 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005480
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005481 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5482 while (1) {
5483 int srv_is_empty = 1;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005484
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005485 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5486 if (!eb) {
5487 /* we might have reached the end of the tree, typically because
5488 * <now_ms> is in the first half and we're first scanning the last
5489 * half. Let's loop back to the beginning of the tree now.
5490 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005491
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005492 eb = eb32_first(&idle_conn_srv);
5493 if (likely(!eb))
5494 break;
5495 }
5496 if (tick_is_lt(now_ms, eb->key)) {
5497 /* timer not expired yet, revisit it later */
5498 next_wakeup = eb->key;
5499 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005500 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005501 }
5502 srv = eb32_entry(eb, struct server, idle_node);
5503 for (i = 0; i < global.nbthread; i++) {
5504 int max_conn = (srv->curr_idle_thr[i] / 2) +
5505 (srv->curr_idle_thr[i] & 1);
5506 int j;
5507 int did_remove = 0;
5508
5509 for (j = 0; j < max_conn; j++) {
5510 struct connection *conn = LIST_POP_LOCKED(&srv->idle_orphan_conns[i], struct connection *, list);
5511 if (!conn)
5512 break;
5513 did_remove = 1;
5514 LIST_ADDQ_LOCKED(&toremove_connections[i], &conn->list);
5515 }
5516 if (did_remove && max_conn < srv->curr_idle_thr[i])
5517 srv_is_empty = 0;
5518 if (did_remove)
5519 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5520 }
5521 eb32_delete(&srv->idle_node);
5522 if (!srv_is_empty) {
5523 /* There are still more idle connections, add the
5524 * server back in the tree.
5525 */
5526 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5527 now_ms);
5528 eb32_insert(&idle_conn_srv, &srv->idle_node);
5529 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005530 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005531 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5532
5533 if (need_wakeup)
5534 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005535 else
5536 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005537
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005538 return task;
5539}
Olivier Houchard88698d92019-04-16 19:07:22 +02005540
5541/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5542static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5543 struct proxy *defpx, const char *file, int line,
5544 char **err)
5545{
5546 int arg = -1;
5547
5548 if (too_many_args(1, args, err, NULL))
5549 return -1;
5550
5551 if (*(args[1]) != 0)
5552 arg = atoi(args[1]);
5553
5554 if (arg < 0 || arg > 100) {
5555 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5556 return -1;
5557 }
5558
5559 if (args[0][10] == 'h')
5560 global.tune.pool_high_ratio = arg;
5561 else
5562 global.tune.pool_low_ratio = arg;
5563 return 0;
5564}
5565
5566/* config keyword parsers */
5567static struct cfg_kw_list cfg_kws = {ILH, {
5568 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5569 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5570 { 0, NULL, NULL }
5571}};
5572
5573INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5574
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005575/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005576 * Local variables:
5577 * c-indent-level: 8
5578 * c-basic-offset: 8
5579 * End:
5580 */