blob: 8307ae2ca21b3a7054695049381ed5dd72c042e1 [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
Baptiste Assmannda29fe22019-06-13 13:24:29 +020050#include <ebsttree.h>
51
Willy Tarreau3ff577e2018-08-02 11:48:52 +020052static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020053static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010054static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010055static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Baptiste Assmannda29fe22019-06-13 13:24:29 +020056static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params);
57static int srv_state_get_version(FILE *f);
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau21faa912012-10-10 08:27:36 +020059/* List head of all known server keywords */
60static struct srv_kw_list srv_keywords = {
61 .list = LIST_HEAD_INIT(srv_keywords.list)
62};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020063
Olivier Houchard9ea5d362019-02-14 18:29:09 +010064__decl_hathreads(HA_SPINLOCK_T idle_conn_srv_lock);
65struct eb_root idle_conn_srv = EB_ROOT;
66struct task *idle_conn_task = NULL;
67struct task *idle_conn_cleanup[MAX_THREADS] = { NULL };
Olivier Houchard859dc802019-08-08 15:47:21 +020068struct mt_list toremove_connections[MAX_THREADS];
Olivier Houchard4be71902019-07-11 15:49:00 +020069__decl_hathreads(HA_SPINLOCK_T toremove_lock[MAX_THREADS]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010070
Frédéric Lécaille7da71292019-05-20 09:47:07 +020071/* The server names dictionary */
72struct dict server_name_dict = {
73 .name = "server names",
74 .values = EB_ROOT_UNIQUE,
75};
76
Baptiste Assmannda29fe22019-06-13 13:24:29 +020077/* tree where global state_file is loaded */
Willy Tarreaufd1aa012019-12-20 17:23:40 +010078struct eb_root state_file = EB_ROOT_UNIQUE;
Baptiste Assmannda29fe22019-06-13 13:24:29 +020079
Simon Hormana3608442013-11-01 16:46:15 +090080int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020081{
Emeric Brun52a91d32017-08-31 14:41:55 +020082 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020083 return s->down_time;
84
85 return now.tv_sec - s->last_change + s->down_time;
86}
Willy Tarreaubaaee002006-06-26 02:48:02 +020087
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050088int srv_lastsession(const struct server *s)
89{
90 if (s->counters.last_sess)
91 return now.tv_sec - s->counters.last_sess;
92
93 return -1;
94}
95
Simon Horman4a741432013-02-23 15:35:38 +090096int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020097{
Simon Horman4a741432013-02-23 15:35:38 +090098 const struct server *s = check->server;
99
Willy Tarreauff5ae352013-12-11 20:36:34 +0100100 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +0900101 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100102
Emeric Brun52a91d32017-08-31 14:41:55 +0200103 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900104 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100105
Simon Horman4a741432013-02-23 15:35:38 +0900106 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100107}
108
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100109/*
110 * Check that we did not get a hash collision.
111 * Unlikely, but it can happen.
112 */
113static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100114{
115 struct proxy *p = s->proxy;
116 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100117
118 for (tmpserv = p->srv; tmpserv != NULL;
119 tmpserv = tmpserv->next) {
120 if (tmpserv == s)
121 continue;
122 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
123 continue;
124 if (tmpserv->cookie &&
125 strcmp(tmpserv->cookie, s->cookie) == 0) {
126 ha_warning("We generated two equal cookies for two different servers.\n"
127 "Please change the secret key for '%s'.\n",
128 s->proxy->id);
129 }
130 }
131
132}
133
Willy Tarreau46b7f532018-08-21 11:54:26 +0200134/*
Willy Tarreau5e83d992019-07-30 11:59:34 +0200135 * Must be called with the server lock held, and will grab the proxy lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200136 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100137void srv_set_dyncookie(struct server *s)
138{
139 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100140 char *tmpbuf;
141 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100142 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100143 size_t buffer_len;
144 int addr_len;
145 int port;
146
Willy Tarreau5e83d992019-07-30 11:59:34 +0200147 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
148
Olivier Houchard4e694042017-03-14 20:01:29 +0100149 if ((s->flags & SRV_F_COOKIESET) ||
150 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
151 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200152 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100153 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100154
155 if (s->addr.ss_family != AF_INET &&
156 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200157 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100158 /*
159 * Buffer to calculate the cookie value.
160 * The buffer contains the secret key + the server IP address
161 * + the TCP port.
162 */
163 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
164 /*
165 * The TCP port should use only 2 bytes, but is stored in
166 * an unsigned int in struct server, so let's use 4, to be
167 * on the safe side.
168 */
169 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200170 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100171 memcpy(tmpbuf, p->dyncookie_key, key_len);
172 memcpy(&(tmpbuf[key_len]),
173 s->addr.ss_family == AF_INET ?
174 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
175 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
176 addr_len);
177 /*
178 * Make sure it's the same across all the load balancers,
179 * no matter their endianness.
180 */
181 port = htonl(s->svc_port);
182 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
183 hash_value = XXH64(tmpbuf, buffer_len, 0);
184 memprintf(&s->cookie, "%016llx", hash_value);
185 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200186 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100187 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100188
189 /* Don't bother checking if the dyncookie is duplicated if
190 * the server is marked as "disabled", maybe it doesn't have
191 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100192 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100193 if (!(s->next_admin & SRV_ADMF_FMAINT))
194 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200195 out:
196 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100197}
198
Willy Tarreau21faa912012-10-10 08:27:36 +0200199/*
200 * Registers the server keyword list <kwl> as a list of valid keywords for next
201 * parsing sessions.
202 */
203void srv_register_keywords(struct srv_kw_list *kwl)
204{
205 LIST_ADDQ(&srv_keywords.list, &kwl->list);
206}
207
208/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
209 * keyword is found with a NULL ->parse() function, then an attempt is made to
210 * find one with a valid ->parse() function. This way it is possible to declare
211 * platform-dependant, known keywords as NULL, then only declare them as valid
212 * if some options are met. Note that if the requested keyword contains an
213 * opening parenthesis, everything from this point is ignored.
214 */
215struct srv_kw *srv_find_kw(const char *kw)
216{
217 int index;
218 const char *kwend;
219 struct srv_kw_list *kwl;
220 struct srv_kw *ret = NULL;
221
222 kwend = strchr(kw, '(');
223 if (!kwend)
224 kwend = kw + strlen(kw);
225
226 list_for_each_entry(kwl, &srv_keywords.list, list) {
227 for (index = 0; kwl->kw[index].kw != NULL; index++) {
228 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
229 kwl->kw[index].kw[kwend-kw] == 0) {
230 if (kwl->kw[index].parse)
231 return &kwl->kw[index]; /* found it !*/
232 else
233 ret = &kwl->kw[index]; /* may be OK */
234 }
235 }
236 }
237 return ret;
238}
239
240/* Dumps all registered "server" keywords to the <out> string pointer. The
241 * unsupported keywords are only dumped if their supported form was not
242 * found.
243 */
244void srv_dump_kws(char **out)
245{
246 struct srv_kw_list *kwl;
247 int index;
248
249 *out = NULL;
250 list_for_each_entry(kwl, &srv_keywords.list, list) {
251 for (index = 0; kwl->kw[index].kw != NULL; index++) {
252 if (kwl->kw[index].parse ||
253 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
254 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
255 kwl->scope,
256 kwl->kw[index].kw,
257 kwl->kw[index].skip ? " <arg>" : "",
258 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
259 kwl->kw[index].parse ? "" : " (not supported)");
260 }
261 }
262 }
263}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100264
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100265/* Parse the "addr" server keyword */
266static int srv_parse_addr(char **args, int *cur_arg,
267 struct proxy *curproxy, struct server *newsrv, char **err)
268{
269 char *errmsg, *arg;
270 struct sockaddr_storage *sk;
271 int port1, port2;
272 struct protocol *proto;
273
274 errmsg = NULL;
275 arg = args[*cur_arg + 1];
276
277 if (!*arg) {
278 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
279 goto err;
280 }
281
282 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
283 if (!sk) {
284 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
285 goto err;
286 }
287
288 proto = protocol_by_family(sk->ss_family);
289 if (!proto || !proto->connect) {
290 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
291 args[*cur_arg], arg);
292 goto err;
293 }
294
295 if (port1 != port2) {
296 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
297 args[*cur_arg], arg);
298 goto err;
299 }
300
301 newsrv->check.addr = newsrv->agent.addr = *sk;
302 newsrv->flags |= SRV_F_CHECKADDR;
303 newsrv->flags |= SRV_F_AGENTADDR;
304
305 return 0;
306
307 err:
308 free(errmsg);
309 return ERR_ALERT | ERR_FATAL;
310}
311
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100312/* Parse the "agent-check" server keyword */
313static int srv_parse_agent_check(char **args, int *cur_arg,
314 struct proxy *curproxy, struct server *newsrv, char **err)
315{
316 newsrv->do_agent = 1;
317 return 0;
318}
319
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100320/* Parse the "backup" server keyword */
321static int srv_parse_backup(char **args, int *cur_arg,
322 struct proxy *curproxy, struct server *newsrv, char **err)
323{
324 newsrv->flags |= SRV_F_BACKUP;
325 return 0;
326}
327
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100328/* Parse the "check" server keyword */
329static int srv_parse_check(char **args, int *cur_arg,
330 struct proxy *curproxy, struct server *newsrv, char **err)
331{
332 newsrv->do_check = 1;
333 return 0;
334}
335
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100336/* Parse the "check-send-proxy" server keyword */
337static int srv_parse_check_send_proxy(char **args, int *cur_arg,
338 struct proxy *curproxy, struct server *newsrv, char **err)
339{
340 newsrv->check.send_proxy = 1;
341 return 0;
342}
343
Alexander Liu2a54bb72019-05-22 19:44:48 +0800344/* Parse the "check-via-socks4" server keyword */
345static int srv_parse_check_via_socks4(char **args, int *cur_arg,
346 struct proxy *curproxy, struct server *newsrv, char **err)
347{
348 newsrv->check.via_socks4 = 1;
349 return 0;
350}
351
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100352/* Parse the "cookie" server keyword */
353static int srv_parse_cookie(char **args, int *cur_arg,
354 struct proxy *curproxy, struct server *newsrv, char **err)
355{
356 char *arg;
357
358 arg = args[*cur_arg + 1];
359 if (!*arg) {
360 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
361 return ERR_ALERT | ERR_FATAL;
362 }
363
364 free(newsrv->cookie);
365 newsrv->cookie = strdup(arg);
366 newsrv->cklen = strlen(arg);
367 newsrv->flags |= SRV_F_COOKIESET;
368 return 0;
369}
370
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100371/* Parse the "disabled" server keyword */
372static int srv_parse_disabled(char **args, int *cur_arg,
373 struct proxy *curproxy, struct server *newsrv, char **err)
374{
Emeric Brun52a91d32017-08-31 14:41:55 +0200375 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
376 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100377 newsrv->check.state |= CHK_ST_PAUSED;
378 newsrv->check.health = 0;
379 return 0;
380}
381
382/* Parse the "enabled" server keyword */
383static int srv_parse_enabled(char **args, int *cur_arg,
384 struct proxy *curproxy, struct server *newsrv, char **err)
385{
Emeric Brun52a91d32017-08-31 14:41:55 +0200386 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
387 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100388 newsrv->check.state &= ~CHK_ST_PAUSED;
389 newsrv->check.health = newsrv->check.rise;
390 return 0;
391}
392
Willy Tarreau9c538e02019-01-23 10:21:49 +0100393static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
394{
395 char *arg;
396
397 arg = args[*cur_arg + 1];
398 if (!*arg) {
399 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
400 return ERR_ALERT | ERR_FATAL;
401 }
402 newsrv->max_reuse = atoi(arg);
403
404 return 0;
405}
406
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100407static 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 +0100408{
409 const char *res;
410 char *arg;
411 unsigned int time;
412
413 arg = args[*cur_arg + 1];
414 if (!*arg) {
415 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
416 return ERR_ALERT | ERR_FATAL;
417 }
418 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200419 if (res == PARSE_TIME_OVER) {
420 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
421 args[*cur_arg+1], args[*cur_arg]);
422 return ERR_ALERT | ERR_FATAL;
423 }
424 else if (res == PARSE_TIME_UNDER) {
425 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
426 args[*cur_arg+1], args[*cur_arg]);
427 return ERR_ALERT | ERR_FATAL;
428 }
429 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100430 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
431 *res, args[*cur_arg]);
432 return ERR_ALERT | ERR_FATAL;
433 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100434 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100435
436 return 0;
437}
438
Olivier Houchard006e3102018-12-10 18:30:32 +0100439static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
440{
441 char *arg;
442
443 arg = args[*cur_arg + 1];
444 if (!*arg) {
445 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
446 return ERR_ALERT | ERR_FATAL;
447 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100448
Olivier Houchard006e3102018-12-10 18:30:32 +0100449 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100450 if ((int)newsrv->max_idle_conns < -1) {
451 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
452 return ERR_ALERT | ERR_FATAL;
453 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100454
455 return 0;
456}
457
Willy Tarreaudff55432012-10-10 17:51:05 +0200458/* parse the "id" server keyword */
459static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
460{
461 struct eb32_node *node;
462
463 if (!*args[*cur_arg + 1]) {
464 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
465 return ERR_ALERT | ERR_FATAL;
466 }
467
468 newsrv->puid = atol(args[*cur_arg + 1]);
469 newsrv->conf.id.key = newsrv->puid;
470
471 if (newsrv->puid <= 0) {
472 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
473 return ERR_ALERT | ERR_FATAL;
474 }
475
476 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
477 if (node) {
478 struct server *target = container_of(node, struct server, conf.id);
479 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
480 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
481 target->id);
482 return ERR_ALERT | ERR_FATAL;
483 }
484
485 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200486 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200487 return 0;
488}
489
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100490/* Parse the "namespace" server keyword */
491static int srv_parse_namespace(char **args, int *cur_arg,
492 struct proxy *curproxy, struct server *newsrv, char **err)
493{
Willy Tarreaue5733232019-05-22 19:24:06 +0200494#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100495 char *arg;
496
497 arg = args[*cur_arg + 1];
498 if (!*arg) {
499 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
500 return ERR_ALERT | ERR_FATAL;
501 }
502
503 if (!strcmp(arg, "*")) {
504 /* Use the namespace associated with the connection (if present). */
505 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
506 return 0;
507 }
508
509 /*
510 * As this parser may be called several times for the same 'default-server'
511 * object, or for a new 'server' instance deriving from a 'default-server'
512 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
513 */
514 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
515
516 newsrv->netns = netns_store_lookup(arg, strlen(arg));
517 if (!newsrv->netns)
518 newsrv->netns = netns_store_insert(arg);
519
520 if (!newsrv->netns) {
521 memprintf(err, "Cannot open namespace '%s'", arg);
522 return ERR_ALERT | ERR_FATAL;
523 }
524
525 return 0;
526#else
527 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
528 return ERR_ALERT | ERR_FATAL;
529#endif
530}
531
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100532/* Parse the "no-agent-check" server keyword */
533static int srv_parse_no_agent_check(char **args, int *cur_arg,
534 struct proxy *curproxy, struct server *newsrv, char **err)
535{
536 free_check(&newsrv->agent);
537 newsrv->agent.inter = 0;
538 newsrv->agent.port = 0;
539 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
540 newsrv->do_agent = 0;
541 return 0;
542}
543
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100544/* Parse the "no-backup" server keyword */
545static int srv_parse_no_backup(char **args, int *cur_arg,
546 struct proxy *curproxy, struct server *newsrv, char **err)
547{
548 newsrv->flags &= ~SRV_F_BACKUP;
549 return 0;
550}
551
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100552/* Parse the "no-check" server keyword */
553static int srv_parse_no_check(char **args, int *cur_arg,
554 struct proxy *curproxy, struct server *newsrv, char **err)
555{
556 free_check(&newsrv->check);
557 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
558 newsrv->do_check = 0;
559 return 0;
560}
561
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100562/* Parse the "no-check-send-proxy" server keyword */
563static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
564 struct proxy *curproxy, struct server *newsrv, char **err)
565{
566 newsrv->check.send_proxy = 0;
567 return 0;
568}
569
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100570/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200571static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100572{
573 srv->pp_opts &= ~flags;
574 return 0;
575}
576
577/* Parse the "no-send-proxy" server keyword */
578static int srv_parse_no_send_proxy(char **args, int *cur_arg,
579 struct proxy *curproxy, struct server *newsrv, char **err)
580{
581 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
582}
583
584/* Parse the "no-send-proxy-v2" server keyword */
585static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
586 struct proxy *curproxy, struct server *newsrv, char **err)
587{
588 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
589}
590
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200591/* Parse the "no-tfo" server keyword */
592static int srv_parse_no_tfo(char **args, int *cur_arg,
593 struct proxy *curproxy, struct server *newsrv, char **err)
594{
595 newsrv->flags &= ~SRV_F_FASTOPEN;
596 return 0;
597}
598
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100599/* Parse the "non-stick" server keyword */
600static int srv_parse_non_stick(char **args, int *cur_arg,
601 struct proxy *curproxy, struct server *newsrv, char **err)
602{
603 newsrv->flags |= SRV_F_NON_STICK;
604 return 0;
605}
606
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100607/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200608static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100609{
610 srv->pp_opts |= flags;
611 return 0;
612}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200613/* parse the "proto" server keyword */
614static int srv_parse_proto(char **args, int *cur_arg,
615 struct proxy *px, struct server *newsrv, char **err)
616{
617 struct ist proto;
618
619 if (!*args[*cur_arg + 1]) {
620 memprintf(err, "'%s' : missing value", args[*cur_arg]);
621 return ERR_ALERT | ERR_FATAL;
622 }
623 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
624 newsrv->mux_proto = get_mux_proto(proto);
625 if (!newsrv->mux_proto) {
626 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
627 return ERR_ALERT | ERR_FATAL;
628 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200629 return 0;
630}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100631
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100632/* parse the "proxy-v2-options" */
633static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
634 struct proxy *px, struct server *newsrv, char **err)
635{
636 char *p, *n;
637 for (p = args[*cur_arg+1]; p; p = n) {
638 n = strchr(p, ',');
639 if (n)
640 *n++ = '\0';
641 if (!strcmp(p, "ssl")) {
642 newsrv->pp_opts |= SRV_PP_V2_SSL;
643 } else if (!strcmp(p, "cert-cn")) {
644 newsrv->pp_opts |= SRV_PP_V2_SSL;
645 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100646 } else if (!strcmp(p, "cert-key")) {
647 newsrv->pp_opts |= SRV_PP_V2_SSL;
648 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
649 } else if (!strcmp(p, "cert-sig")) {
650 newsrv->pp_opts |= SRV_PP_V2_SSL;
651 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
652 } else if (!strcmp(p, "ssl-cipher")) {
653 newsrv->pp_opts |= SRV_PP_V2_SSL;
654 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100655 } else if (!strcmp(p, "authority")) {
656 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100657 } else if (!strcmp(p, "crc32c")) {
658 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100659 } else if (!strcmp(p, "unique-id")) {
660 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100661 } else
662 goto fail;
663 }
664 return 0;
665 fail:
666 if (err)
667 memprintf(err, "'%s' : proxy v2 option not implemented", p);
668 return ERR_ALERT | ERR_FATAL;
669}
670
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100671/* Parse the "observe" server keyword */
672static int srv_parse_observe(char **args, int *cur_arg,
673 struct proxy *curproxy, struct server *newsrv, char **err)
674{
675 char *arg;
676
677 arg = args[*cur_arg + 1];
678 if (!*arg) {
679 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
680 return ERR_ALERT | ERR_FATAL;
681 }
682
683 if (!strcmp(arg, "none")) {
684 newsrv->observe = HANA_OBS_NONE;
685 }
686 else if (!strcmp(arg, "layer4")) {
687 newsrv->observe = HANA_OBS_LAYER4;
688 }
689 else if (!strcmp(arg, "layer7")) {
690 if (curproxy->mode != PR_MODE_HTTP) {
691 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
692 return ERR_ALERT;
693 }
694 newsrv->observe = HANA_OBS_LAYER7;
695 }
696 else {
697 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
698 "but got '%s'\n", args[*cur_arg], arg);
699 return ERR_ALERT | ERR_FATAL;
700 }
701
702 return 0;
703}
704
Frédéric Lécaille16186232017-03-14 16:42:49 +0100705/* Parse the "redir" server keyword */
706static int srv_parse_redir(char **args, int *cur_arg,
707 struct proxy *curproxy, struct server *newsrv, char **err)
708{
709 char *arg;
710
711 arg = args[*cur_arg + 1];
712 if (!*arg) {
713 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
714 return ERR_ALERT | ERR_FATAL;
715 }
716
717 free(newsrv->rdr_pfx);
718 newsrv->rdr_pfx = strdup(arg);
719 newsrv->rdr_len = strlen(arg);
720
721 return 0;
722}
723
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100724/* Parse the "send-proxy" server keyword */
725static int srv_parse_send_proxy(char **args, int *cur_arg,
726 struct proxy *curproxy, struct server *newsrv, char **err)
727{
728 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
729}
730
731/* Parse the "send-proxy-v2" server keyword */
732static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
733 struct proxy *curproxy, struct server *newsrv, char **err)
734{
735 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
736}
737
Frédéric Lécailledba97072017-03-17 15:33:50 +0100738
739/* Parse the "source" server keyword */
740static int srv_parse_source(char **args, int *cur_arg,
741 struct proxy *curproxy, struct server *newsrv, char **err)
742{
743 char *errmsg;
744 int port_low, port_high;
745 struct sockaddr_storage *sk;
746 struct protocol *proto;
747
748 errmsg = NULL;
749
750 if (!*args[*cur_arg + 1]) {
751 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
752 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
753 goto err;
754 }
755
756 /* 'sk' is statically allocated (no need to be freed). */
757 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
758 if (!sk) {
759 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
760 goto err;
761 }
762
763 proto = protocol_by_family(sk->ss_family);
764 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100765 ha_alert("'%s %s' : connect() not supported for this address family.\n",
766 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100767 goto err;
768 }
769
770 newsrv->conn_src.opts |= CO_SRC_BIND;
771 newsrv->conn_src.source_addr = *sk;
772
773 if (port_low != port_high) {
774 int i;
775
776 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100777 ha_alert("'%s' does not support port offsets (found '%s').\n",
778 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100779 goto err;
780 }
781
782 if (port_low <= 0 || port_low > 65535 ||
783 port_high <= 0 || port_high > 65535 ||
784 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100785 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 +0100786 goto err;
787 }
788 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
789 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
790 newsrv->conn_src.sport_range->ports[i] = port_low + i;
791 }
792
793 *cur_arg += 2;
794 while (*(args[*cur_arg])) {
795 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
796#if defined(CONFIG_HAP_TRANSPARENT)
797 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100798 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
799 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100800 goto err;
801 }
802 if (!strcmp(args[*cur_arg + 1], "client")) {
803 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
804 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
805 }
806 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
807 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
808 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
809 }
810 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
811 char *name, *end;
812
813 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +0100814 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100815 name++;
816
817 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +0100818 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +0100819 end++;
820
821 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
822 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
823 free(newsrv->conn_src.bind_hdr_name);
824 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
825 newsrv->conn_src.bind_hdr_len = end - name;
826 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
827 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
828 newsrv->conn_src.bind_hdr_occ = -1;
829
830 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +0100831 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100832 end++;
833 if (*end == ',') {
834 end++;
835 name = end;
836 if (*end == '-')
837 end++;
Willy Tarreau90807112020-02-25 08:16:33 +0100838 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100839 end++;
840 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
841 }
842
843 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100844 ha_alert("usesrc hdr_ip(name,num) does not support negative"
845 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100846 goto err;
847 }
848 }
849 else {
850 struct sockaddr_storage *sk;
851 int port1, port2;
852
853 /* 'sk' is statically allocated (no need to be freed). */
854 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
855 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100856 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100857 goto err;
858 }
859
860 proto = protocol_by_family(sk->ss_family);
861 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100862 ha_alert("'%s %s' : connect() not supported for this address family.\n",
863 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100864 goto err;
865 }
866
867 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100868 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
869 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100870 goto err;
871 }
872 newsrv->conn_src.tproxy_addr = *sk;
873 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
874 }
875 global.last_checks |= LSTCHK_NETADM;
876 *cur_arg += 2;
877 continue;
878#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100879 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 +0100880 goto err;
881#endif /* defined(CONFIG_HAP_TRANSPARENT) */
882 } /* "usesrc" */
883
884 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
885#ifdef SO_BINDTODEVICE
886 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100887 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100888 goto err;
889 }
890 free(newsrv->conn_src.iface_name);
891 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
892 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
893 global.last_checks |= LSTCHK_NETADM;
894#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100895 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100896 goto err;
897#endif
898 *cur_arg += 2;
899 continue;
900 }
901 /* this keyword in not an option of "source" */
902 break;
903 } /* while */
904
905 return 0;
906
907 err:
908 free(errmsg);
909 return ERR_ALERT | ERR_FATAL;
910}
911
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100912/* Parse the "stick" server keyword */
913static int srv_parse_stick(char **args, int *cur_arg,
914 struct proxy *curproxy, struct server *newsrv, char **err)
915{
916 newsrv->flags &= ~SRV_F_NON_STICK;
917 return 0;
918}
919
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100920/* Parse the "track" server keyword */
921static int srv_parse_track(char **args, int *cur_arg,
922 struct proxy *curproxy, struct server *newsrv, char **err)
923{
924 char *arg;
925
926 arg = args[*cur_arg + 1];
927 if (!*arg) {
928 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
929 return ERR_ALERT | ERR_FATAL;
930 }
931
932 free(newsrv->trackit);
933 newsrv->trackit = strdup(arg);
934
935 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800936}
937
938/* Parse the "socks4" server keyword */
939static int srv_parse_socks4(char **args, int *cur_arg,
940 struct proxy *curproxy, struct server *newsrv, char **err)
941{
942 char *errmsg;
943 int port_low, port_high;
944 struct sockaddr_storage *sk;
945 struct protocol *proto;
946
947 errmsg = NULL;
948
949 if (!*args[*cur_arg + 1]) {
950 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
951 goto err;
952 }
953
954 /* 'sk' is statically allocated (no need to be freed). */
955 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
956 if (!sk) {
957 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
958 goto err;
959 }
960
961 proto = protocol_by_family(sk->ss_family);
962 if (!proto || !proto->connect) {
963 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
964 goto err;
965 }
966
967 newsrv->flags |= SRV_F_SOCKS4_PROXY;
968 newsrv->socks4_addr = *sk;
969
970 if (port_low != port_high) {
971 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
972 goto err;
973 }
974
975 if (!port_low) {
976 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
977 goto err;
978 }
979
980 return 0;
981
982 err:
983 free(errmsg);
984 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100985}
986
Frédéric Lécailledba97072017-03-17 15:33:50 +0100987
Willy Tarreau034c88c2017-01-23 23:36:45 +0100988/* parse the "tfo" server keyword */
989static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
990{
991 newsrv->flags |= SRV_F_FASTOPEN;
992 return 0;
993}
994
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200995/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200996 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200997 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200998 *
999 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001000 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001001void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001002{
Willy Tarreau87b09662015-04-03 00:22:06 +02001003 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001004
Willy Tarreau87b09662015-04-03 00:22:06 +02001005 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
1006 if (stream->srv_conn == srv)
1007 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001008}
1009
1010/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001011 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001012 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001013 *
1014 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001015 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001016void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001017{
1018 struct server *srv;
1019
1020 for (srv = px->srv; srv != NULL; srv = srv->next)
1021 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001022 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001023}
1024
Willy Tarreaubda92272014-05-20 21:55:30 +02001025/* Appends some information to a message string related to a server going UP or
1026 * DOWN. If both <forced> and <reason> are null and the server tracks another
1027 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001028 * If <check> is non-null, an entire string describing the check result will be
1029 * appended after a comma and a space (eg: to report some information from the
1030 * check that changed the state). In the other case, the string will be built
1031 * using the check results stored into the struct server if present.
1032 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001033 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001034 *
1035 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001036 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001037void srv_append_status(struct buffer *msg, struct server *s,
1038 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001039{
Emeric Brun5a133512017-10-19 14:42:30 +02001040 short status = s->op_st_chg.status;
1041 short code = s->op_st_chg.code;
1042 long duration = s->op_st_chg.duration;
1043 char *desc = s->op_st_chg.reason;
1044
1045 if (check) {
1046 status = check->status;
1047 code = check->code;
1048 duration = check->duration;
1049 desc = check->desc;
1050 }
1051
1052 if (status != -1) {
1053 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1054
1055 if (status >= HCHK_STATUS_L57DATA)
1056 chunk_appendf(msg, ", code: %d", code);
1057
1058 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001059 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001060
1061 chunk_appendf(msg, ", info: \"");
1062
1063 chunk_initlen(&src, desc, 0, strlen(desc));
1064 chunk_asciiencode(msg, &src, '"');
1065
1066 chunk_appendf(msg, "\"");
1067 }
1068
1069 if (duration >= 0)
1070 chunk_appendf(msg, ", check duration: %ldms", duration);
1071 }
1072 else if (desc && *desc) {
1073 chunk_appendf(msg, ", %s", desc);
1074 }
1075 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001076 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001077 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001078
1079 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001080 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001081 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1082 " %d sessions active, %d requeued, %d remaining in queue",
1083 s->proxy->srv_act, s->proxy->srv_bck,
1084 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1085 s->cur_sess, xferred, s->nbpend);
1086 else
1087 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1088 " %d sessions requeued, %d total in queue",
1089 s->proxy->srv_act, s->proxy->srv_bck,
1090 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1091 xferred, s->nbpend);
1092 }
1093}
1094
Emeric Brun5a133512017-10-19 14:42:30 +02001095/* Marks server <s> down, regardless of its checks' statuses. The server is
1096 * registered in a list to postpone the counting of the remaining servers on
1097 * the proxy and transfers queued streams whenever possible to other servers at
1098 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1099 * non-null as the reason for going down or the available data from the check
1100 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001101 *
1102 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001103 */
Emeric Brun5a133512017-10-19 14:42:30 +02001104void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001105{
1106 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001107
Emeric Brun64cc49c2017-10-03 14:46:45 +02001108 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001109 return;
1110
Emeric Brun52a91d32017-08-31 14:41:55 +02001111 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001112 *s->op_st_chg.reason = 0;
1113 s->op_st_chg.status = -1;
1114 if (reason) {
1115 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1116 }
1117 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001118 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001119 s->op_st_chg.code = check->code;
1120 s->op_st_chg.status = check->status;
1121 s->op_st_chg.duration = check->duration;
1122 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001123
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001124 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001125 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001126
Emeric Brun9f0b4582017-10-23 14:39:51 +02001127 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001128 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001129 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001130 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001131 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001132}
1133
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001134/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001135 * in maintenance. The server is registered in a list to postpone the counting
1136 * of the remaining servers on the proxy and tries to grab requests from the
1137 * proxy at a sync point. Maintenance servers are ignored. It stores the
1138 * <reason> if non-null as the reason for going down or the available data
1139 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001140 *
1141 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001142 */
Emeric Brun5a133512017-10-19 14:42:30 +02001143void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001144{
1145 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001146
Emeric Brun64cc49c2017-10-03 14:46:45 +02001147 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001148 return;
1149
Emeric Brun52a91d32017-08-31 14:41:55 +02001150 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001151 return;
1152
Emeric Brun52a91d32017-08-31 14:41:55 +02001153 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001154 *s->op_st_chg.reason = 0;
1155 s->op_st_chg.status = -1;
1156 if (reason) {
1157 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1158 }
1159 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001160 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001161 s->op_st_chg.code = check->code;
1162 s->op_st_chg.status = check->status;
1163 s->op_st_chg.duration = check->duration;
1164 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001165
Emeric Brun64cc49c2017-10-03 14:46:45 +02001166 if (s->slowstart <= 0)
1167 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001168
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001169 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001170 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001171
Emeric Brun9f0b4582017-10-23 14:39:51 +02001172 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001173 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001174 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001175 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001176 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001177}
1178
Willy Tarreau8eb77842014-05-21 13:54:57 +02001179/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001180 * isn't in maintenance. The server is registered in a list to postpone the
1181 * counting of the remaining servers on the proxy and tries to grab requests
1182 * from the proxy. Maintenance servers are ignored. It stores the
1183 * <reason> if non-null as the reason for going down or the available data
1184 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001185 * up. Note that it makes use of the trash to build the log strings, so <reason>
1186 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001187 *
1188 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001189 */
Emeric Brun5a133512017-10-19 14:42:30 +02001190void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001191{
1192 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001193
Emeric Brun64cc49c2017-10-03 14:46:45 +02001194 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001195 return;
1196
Emeric Brun52a91d32017-08-31 14:41:55 +02001197 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001198 return;
1199
Emeric Brun52a91d32017-08-31 14:41:55 +02001200 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001201 *s->op_st_chg.reason = 0;
1202 s->op_st_chg.status = -1;
1203 if (reason) {
1204 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1205 }
1206 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001207 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001208 s->op_st_chg.code = check->code;
1209 s->op_st_chg.status = check->status;
1210 s->op_st_chg.duration = check->duration;
1211 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001212
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001213 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001214 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001215
Emeric Brun9f0b4582017-10-23 14:39:51 +02001216 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001217 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001218 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001219 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001220 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001221}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001222
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001223/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1224 * enforce either maint mode or drain mode. It is not allowed to set more than
1225 * one flag at once. The equivalent "inherited" flag is propagated to all
1226 * tracking servers. Maintenance mode disables health checks (but not agent
1227 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001228 * is done. If <cause> is non-null, it will be displayed at the end of the log
1229 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001230 *
1231 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001232 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001233void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001234{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001235 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001236
1237 if (!mode)
1238 return;
1239
1240 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001241 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001242 return;
1243
Emeric Brun52a91d32017-08-31 14:41:55 +02001244 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001245 if (cause)
1246 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1247
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001248 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001249 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001250
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001251 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001252 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1253 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001254 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001255
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001256 /* compute the inherited flag to propagate */
1257 if (mode & SRV_ADMF_MAINT)
1258 mode = SRV_ADMF_IMAINT;
1259 else if (mode & SRV_ADMF_DRAIN)
1260 mode = SRV_ADMF_IDRAIN;
1261
Emeric Brun9f0b4582017-10-23 14:39:51 +02001262 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001263 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001264 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001265 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001266 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001267}
1268
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001269/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1270 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1271 * than one flag at once. The equivalent "inherited" flag is propagated to all
1272 * tracking servers. Leaving maintenance mode re-enables health checks. When
1273 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001274 *
1275 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001276 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001277void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001278{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001279 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001280
1281 if (!mode)
1282 return;
1283
1284 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001285 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001286 return;
1287
Emeric Brun52a91d32017-08-31 14:41:55 +02001288 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001289
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001290 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001291 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001292
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001293 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001294 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1295 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001296 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001297
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001298 if (mode & SRV_ADMF_MAINT)
1299 mode = SRV_ADMF_IMAINT;
1300 else if (mode & SRV_ADMF_DRAIN)
1301 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001302
Emeric Brun9f0b4582017-10-23 14:39:51 +02001303 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001304 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001305 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001306 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001307 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001308}
1309
Willy Tarreau757478e2016-11-03 19:22:19 +01001310/* principle: propagate maint and drain to tracking servers. This is useful
1311 * upon startup so that inherited states are correct.
1312 */
1313static void srv_propagate_admin_state(struct server *srv)
1314{
1315 struct server *srv2;
1316
1317 if (!srv->trackers)
1318 return;
1319
1320 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001321 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001322 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001323 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001324
Emeric Brun52a91d32017-08-31 14:41:55 +02001325 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001326 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001327 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001328 }
1329}
1330
1331/* Compute and propagate the admin states for all servers in proxy <px>.
1332 * Only servers *not* tracking another one are considered, because other
1333 * ones will be handled when the server they track is visited.
1334 */
1335void srv_compute_all_admin_states(struct proxy *px)
1336{
1337 struct server *srv;
1338
1339 for (srv = px->srv; srv; srv = srv->next) {
1340 if (srv->track)
1341 continue;
1342 srv_propagate_admin_state(srv);
1343 }
1344}
1345
Willy Tarreaudff55432012-10-10 17:51:05 +02001346/* Note: must not be declared <const> as its list will be overwritten.
1347 * Please take care of keeping this list alphabetically sorted, doing so helps
1348 * all code contributors.
1349 * Optional keywords are also declared with a NULL ->parse() function so that
1350 * the config parser can report an appropriate error when a known keyword was
1351 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001352 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001353 */
1354static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001355 { "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 +01001356 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001357 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001358 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001359 { "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 +01001360 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001361 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1362 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001363 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001364 { "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 +01001365 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001366 { "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 +01001367 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001368 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001369 { "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 +01001370 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1371 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +02001372 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001373 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001374 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001375 { "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 +01001376 { "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 +02001377 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001378 { "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 +01001379 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001380 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1381 { "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 +02001382 { "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 +01001383 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001384 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001385 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001386 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
1387 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* enable socks4 proxy for health checks */
Willy Tarreaudff55432012-10-10 17:51:05 +02001388 { NULL, NULL, 0 },
1389}};
1390
Willy Tarreau0108d902018-11-25 19:14:37 +01001391INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001392
Willy Tarreau004e0452013-11-21 11:22:01 +01001393/* Recomputes the server's eweight based on its state, uweight, the current time,
1394 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001395 * state is automatically disabled if the time is elapsed. If <must_update> is
1396 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001397 *
1398 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001399 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001400void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001401{
1402 struct proxy *px = sv->proxy;
1403 unsigned w;
1404
1405 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1406 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001407 if (sv->next_state == SRV_ST_STARTING)
1408 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001409 }
1410
1411 /* We must take care of not pushing the server to full throttle during slow starts.
1412 * It must also start immediately, at least at the minimal step when leaving maintenance.
1413 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001414 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001415 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1416 else
1417 w = px->lbprm.wdiv;
1418
Emeric Brun52a91d32017-08-31 14:41:55 +02001419 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001420
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001421 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001422 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001423 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001424}
1425
Willy Tarreaubaaee002006-06-26 02:48:02 +02001426/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001427 * Parses weight_str and configures sv accordingly.
1428 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001429 *
1430 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001431 */
1432const char *server_parse_weight_change_request(struct server *sv,
1433 const char *weight_str)
1434{
1435 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001436 long int w;
1437 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001438
1439 px = sv->proxy;
1440
1441 /* if the weight is terminated with '%', it is set relative to
1442 * the initial weight, otherwise it is absolute.
1443 */
1444 if (!*weight_str)
1445 return "Require <weight> or <weight%>.\n";
1446
Simon Hormanb796afa2013-02-12 10:45:53 +09001447 w = strtol(weight_str, &end, 10);
1448 if (end == weight_str)
1449 return "Empty weight string empty or preceded by garbage";
1450 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001451 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001452 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001453 /* Avoid integer overflow */
1454 if (w > 25600)
1455 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001456 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001457 if (w > 256)
1458 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001459 }
1460 else if (w < 0 || w > 256)
1461 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001462 else if (end[0] != '\0')
1463 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001464
1465 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1466 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1467
1468 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001469 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001470
1471 return NULL;
1472}
1473
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001474/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001475 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1476 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001477 * Returns:
1478 * - error string on error
1479 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001480 *
1481 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001482 */
1483const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001484 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001485{
1486 unsigned char ip[INET6_ADDRSTRLEN];
1487
1488 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001489 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001490 return NULL;
1491 }
1492 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001493 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001494 return NULL;
1495 }
1496
1497 return "Could not understand IP address format.\n";
1498}
1499
Willy Tarreau46b7f532018-08-21 11:54:26 +02001500/*
1501 * Must be called with the server lock held.
1502 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001503const char *server_parse_maxconn_change_request(struct server *sv,
1504 const char *maxconn_str)
1505{
1506 long int v;
1507 char *end;
1508
1509 if (!*maxconn_str)
1510 return "Require <maxconn>.\n";
1511
1512 v = strtol(maxconn_str, &end, 10);
1513 if (end == maxconn_str)
1514 return "maxconn string empty or preceded by garbage";
1515 else if (end[0] != '\0')
1516 return "Trailing garbage in maxconn string";
1517
1518 if (sv->maxconn == sv->minconn) { // static maxconn
1519 sv->maxconn = sv->minconn = v;
1520 } else { // dynamic maxconn
1521 sv->maxconn = v;
1522 }
1523
1524 if (may_dequeue_tasks(sv, sv->proxy))
1525 process_srv_queue(sv);
1526
1527 return NULL;
1528}
1529
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001530#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001531static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1532 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001533{
1534 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001535 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001536 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001537 NULL,
1538 };
1539
1540 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001541 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001542
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001543 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001544}
1545
1546static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1547{
1548 struct sample_expr *expr;
1549
1550 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 +01001551 if (!expr) {
1552 memprintf(err, "error detected while parsing sni expression : %s", *err);
1553 return ERR_ALERT | ERR_FATAL;
1554 }
1555
1556 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1557 memprintf(err, "error detected while parsing sni expression : "
1558 " fetch method '%s' extracts information from '%s', "
1559 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001560 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001561 return ERR_ALERT | ERR_FATAL;
1562 }
1563
1564 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1565 release_sample_expr(newsrv->ssl_ctx.sni);
1566 newsrv->ssl_ctx.sni = expr;
1567
1568 return 0;
1569}
1570#endif
1571
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001572static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, int err_code, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001573{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001574 char *msg = "error encountered while processing ";
1575 char *quote = "'";
1576 char *token = args[cur_arg];
1577
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001578 if (err && *err) {
1579 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001580 msg = *err;
1581 quote = "";
1582 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001583 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001584
1585 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
1586 ha_warning("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1587 file, linenum, args[0], args[1],
1588 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001589 else
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001590 ha_alert("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1591 file, linenum, args[0], args[1],
1592 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001593}
1594
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001595static void srv_conn_src_sport_range_cpy(struct server *srv,
1596 struct server *src)
1597{
1598 int range_sz;
1599
1600 range_sz = src->conn_src.sport_range->size;
1601 if (range_sz > 0) {
1602 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1603 if (srv->conn_src.sport_range != NULL) {
1604 int i;
1605
1606 for (i = 0; i < range_sz; i++) {
1607 srv->conn_src.sport_range->ports[i] =
1608 src->conn_src.sport_range->ports[i];
1609 }
1610 }
1611 }
1612}
1613
1614/*
1615 * Copy <src> server connection source settings to <srv> server everything needed.
1616 */
1617static void srv_conn_src_cpy(struct server *srv, struct server *src)
1618{
1619 srv->conn_src.opts = src->conn_src.opts;
1620 srv->conn_src.source_addr = src->conn_src.source_addr;
1621
1622 /* Source port range copy. */
1623 if (src->conn_src.sport_range != NULL)
1624 srv_conn_src_sport_range_cpy(srv, src);
1625
1626#ifdef CONFIG_HAP_TRANSPARENT
1627 if (src->conn_src.bind_hdr_name != NULL) {
1628 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1629 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1630 }
1631 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1632 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1633#endif
1634 if (src->conn_src.iface_name != NULL)
1635 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1636}
1637
1638/*
1639 * Copy <src> server SSL settings to <srv> server allocating
1640 * everything needed.
1641 */
1642#if defined(USE_OPENSSL)
1643static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1644{
1645 if (src->ssl_ctx.ca_file != NULL)
1646 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1647 if (src->ssl_ctx.crl_file != NULL)
1648 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1649 if (src->ssl_ctx.client_crt != NULL)
1650 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1651
1652 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1653
1654 if (src->ssl_ctx.verify_host != NULL)
1655 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1656 if (src->ssl_ctx.ciphers != NULL)
1657 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001658 if (src->ssl_ctx.options)
1659 srv->ssl_ctx.options = src->ssl_ctx.options;
1660 if (src->ssl_ctx.methods.flags)
1661 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1662 if (src->ssl_ctx.methods.min)
1663 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1664 if (src->ssl_ctx.methods.max)
1665 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1666
Willy Tarreau5db847a2019-05-09 14:13:35 +02001667#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001668 if (src->ssl_ctx.ciphersuites != NULL)
1669 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1670#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001671 if (src->sni_expr != NULL)
1672 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001673
1674#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1675 if (src->ssl_ctx.alpn_str) {
1676 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1677 if (srv->ssl_ctx.alpn_str) {
1678 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1679 src->ssl_ctx.alpn_len);
1680 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1681 }
1682 }
1683#endif
1684#ifdef OPENSSL_NPN_NEGOTIATED
1685 if (src->ssl_ctx.npn_str) {
1686 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1687 if (srv->ssl_ctx.npn_str) {
1688 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1689 src->ssl_ctx.npn_len);
1690 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1691 }
1692 }
1693#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001694}
1695#endif
1696
1697/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001698 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001699 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001700 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001701 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001702static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001703{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001704 char *hostname_dn;
1705 int hostname_len, hostname_dn_len;
1706
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001707 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001708 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001709
Christopher Faulet67957bd2017-09-27 11:00:59 +02001710 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001711 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001712 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1713 hostname_dn, trash.size);
1714 if (hostname_dn_len == -1)
1715 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001716
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001717
Christopher Faulet67957bd2017-09-27 11:00:59 +02001718 free(srv->hostname);
1719 free(srv->hostname_dn);
1720 srv->hostname = strdup(hostname);
1721 srv->hostname_dn = strdup(hostname_dn);
1722 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001723 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001724 goto err;
1725
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001726 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001727
1728 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001729 free(srv->hostname); srv->hostname = NULL;
1730 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001731 return -1;
1732}
1733
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001734/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001735 * Copy <src> server settings to <srv> server allocating
1736 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001737 * This function is not supposed to be called at any time, but only
1738 * during server settings parsing or during server allocations from
1739 * a server template, and just after having calloc()'ed a new server.
1740 * So, <src> may only be a default server (when parsing server settings)
1741 * or a server template (during server allocations from a server template).
1742 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1743 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001744 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001745static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001746{
1747 /* Connection source settings copy */
1748 srv_conn_src_cpy(srv, src);
1749
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001750 if (srv_tmpl) {
1751 srv->addr = src->addr;
1752 srv->svc_port = src->svc_port;
1753 }
1754
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001755 srv->pp_opts = src->pp_opts;
1756 if (src->rdr_pfx != NULL) {
1757 srv->rdr_pfx = strdup(src->rdr_pfx);
1758 srv->rdr_len = src->rdr_len;
1759 }
1760 if (src->cookie != NULL) {
1761 srv->cookie = strdup(src->cookie);
1762 srv->cklen = src->cklen;
1763 }
1764 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01001765 srv->check.addr = src->check.addr;
1766 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001767 srv->check.use_ssl = src->check.use_ssl;
1768 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001769 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001770 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001771 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001772 /* Note: 'flags' field has potentially been already initialized. */
1773 srv->flags |= src->flags;
1774 srv->do_check = src->do_check;
1775 srv->do_agent = src->do_agent;
1776 if (srv->check.port)
1777 srv->flags |= SRV_F_CHECKPORT;
1778 srv->check.inter = src->check.inter;
1779 srv->check.fastinter = src->check.fastinter;
1780 srv->check.downinter = src->check.downinter;
1781 srv->agent.use_ssl = src->agent.use_ssl;
1782 srv->agent.port = src->agent.port;
1783 if (src->agent.send_string != NULL)
1784 srv->agent.send_string = strdup(src->agent.send_string);
1785 srv->agent.send_string_len = src->agent.send_string_len;
1786 srv->agent.inter = src->agent.inter;
1787 srv->agent.fastinter = src->agent.fastinter;
1788 srv->agent.downinter = src->agent.downinter;
1789 srv->maxqueue = src->maxqueue;
1790 srv->minconn = src->minconn;
1791 srv->maxconn = src->maxconn;
1792 srv->slowstart = src->slowstart;
1793 srv->observe = src->observe;
1794 srv->onerror = src->onerror;
1795 srv->onmarkeddown = src->onmarkeddown;
1796 srv->onmarkedup = src->onmarkedup;
1797 if (src->trackit != NULL)
1798 srv->trackit = strdup(src->trackit);
1799 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1800 srv->uweight = srv->iweight = src->iweight;
1801
1802 srv->check.send_proxy = src->check.send_proxy;
1803 /* health: up, but will fall down at first failure */
1804 srv->check.rise = srv->check.health = src->check.rise;
1805 srv->check.fall = src->check.fall;
1806
1807 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001808 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1809 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1810 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001811 srv->check.state |= CHK_ST_PAUSED;
1812 srv->check.health = 0;
1813 }
1814
1815 /* health: up but will fall down at first failure */
1816 srv->agent.rise = srv->agent.health = src->agent.rise;
1817 srv->agent.fall = src->agent.fall;
1818
1819 if (src->resolvers_id != NULL)
1820 srv->resolvers_id = strdup(src->resolvers_id);
1821 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001822 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Daniel Corbettf8716912019-11-17 09:48:56 -05001823 srv->dns_opts.ignore_weight = src->dns_opts.ignore_weight;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001824 if (srv->dns_opts.family_prio == AF_UNSPEC)
1825 srv->dns_opts.family_prio = AF_INET6;
1826 memcpy(srv->dns_opts.pref_net,
1827 src->dns_opts.pref_net,
1828 sizeof srv->dns_opts.pref_net);
1829 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1830
1831 srv->init_addr_methods = src->init_addr_methods;
1832 srv->init_addr = src->init_addr;
1833#if defined(USE_OPENSSL)
1834 srv_ssl_settings_cpy(srv, src);
1835#endif
1836#ifdef TCP_USER_TIMEOUT
1837 srv->tcp_ut = src->tcp_ut;
1838#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001839 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001840 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001841 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001842 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001843
Olivier Houchard8da5f982017-08-04 18:35:36 +02001844 if (srv_tmpl)
1845 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001846
1847 srv->check.via_socks4 = src->check.via_socks4;
1848 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001849}
1850
William Lallemand313bfd12018-10-26 14:47:32 +02001851struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001852{
1853 struct server *srv;
1854
1855 srv = calloc(1, sizeof *srv);
1856 if (!srv)
1857 return NULL;
1858
1859 srv->obj_type = OBJ_TYPE_SERVER;
1860 srv->proxy = proxy;
1861 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001862 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001863
Emeric Brun52a91d32017-08-31 14:41:55 +02001864 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001865 srv->last_change = now.tv_sec;
1866
1867 srv->check.status = HCHK_STATUS_INI;
1868 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001869 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001870 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1871
1872 srv->agent.status = HCHK_STATUS_INI;
1873 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001874 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001875 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1876
Willy Tarreau975b1552019-06-06 16:25:55 +02001877 /* please don't put default server settings here, they are set in
1878 * init_default_instance().
1879 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001880 return srv;
1881}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001882
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001883#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1884static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1885 struct server *srv, struct proxy *proxy)
1886{
1887 int ret;
1888 char *err = NULL;
1889
1890 if (!srv->sni_expr)
1891 return 0;
1892
1893 ret = server_parse_sni_expr(srv, proxy, &err);
1894 if (!ret)
1895 return 0;
1896
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001897 display_parser_err(file, linenum, args, cur_arg, ret, &err);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001898 free(err);
1899
1900 return ret;
1901}
1902#endif
1903
1904/*
1905 * Server initializations finalization.
1906 * Initialize health check, agent check and SNI expression if enabled.
1907 * Must not be called for a default server instance.
1908 */
1909static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1910 struct server *srv, struct proxy *px)
1911{
1912 int ret;
1913
Christopher Faulet8892e5d2020-03-26 19:48:20 +01001914 if (srv->do_check && srv->trackit) {
1915 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1916 file, linenum);
1917 return ERR_ALERT | ERR_FATAL;
1918 }
1919
1920 if (srv->do_agent && !srv->agent.port) {
1921 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1922 file, linenum, srv->id);
1923 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001924 }
1925
1926#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1927 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1928 return ret;
1929#endif
1930
1931 if (srv->flags & SRV_F_BACKUP)
1932 px->srv_bck++;
1933 else
1934 px->srv_act++;
1935 srv_lb_commit_status(srv);
1936
1937 return 0;
1938}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001939
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001940/*
1941 * Parse as much as possible such a range string argument: low[-high]
1942 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1943 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1944 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1945 * Returns 0 if succeeded, -1 if not.
1946 */
1947static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1948{
1949 char *nb_high_arg;
1950
1951 *nb_high = 0;
1952 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001953 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001954
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001955 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001956 *nb_high_arg++ = '\0';
1957 *nb_high = atoi(nb_high_arg);
1958 }
1959 else {
1960 *nb_high += *nb_low;
1961 *nb_low = 1;
1962 }
1963
1964 if (*nb_low < 0 || *nb_high < *nb_low)
1965 return -1;
1966
1967 return 0;
1968}
1969
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001970static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1971{
1972 chunk_printf(&trash, "%s%d", prefix, nb);
1973 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001974 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001975}
1976
1977/*
1978 * Initialize as much as possible servers from <srv> server template.
1979 * Note that a server template is a special server with
1980 * a few different parameters than a server which has
1981 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001982 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001983 * 'srv' template included.
1984 */
1985static int server_template_init(struct server *srv, struct proxy *px)
1986{
1987 int i;
1988 struct server *newsrv;
1989
1990 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001991 newsrv = new_server(px);
1992 if (!newsrv)
1993 goto err;
1994
1995 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001996 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001997#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1998 if (newsrv->sni_expr) {
1999 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2000 if (!newsrv->ssl_ctx.sni)
2001 goto err;
2002 }
2003#endif
2004 /* Set this new server ID. */
2005 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2006
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002007 /* Linked backwards first. This will be restablished after parsing. */
2008 newsrv->next = px->srv;
2009 px->srv = newsrv;
2010 }
2011 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2012
2013 return i - srv->tmpl_info.nb_low;
2014
2015 err:
2016 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2017 if (newsrv) {
2018#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2019 release_sample_expr(newsrv->ssl_ctx.sni);
2020#endif
2021 free_check(&newsrv->agent);
2022 free_check(&newsrv->check);
2023 }
2024 free(newsrv);
2025 return i - srv->tmpl_info.nb_low;
2026}
2027
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02002028int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy, int parse_addr, int in_peers_section)
Willy Tarreau272adea2014-03-31 10:39:59 +02002029{
2030 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002031 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002032 char *errmsg = NULL;
2033 int err_code = 0;
2034 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002035 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002036
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002037 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002038 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002039 !strcmp(args[0], "default-server") ||
2040 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002041 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002042 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002043 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002044 int srv_tmpl = !defsrv && !srv;
2045 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002046
2047 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002048 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002049 err_code |= ERR_ALERT | ERR_FATAL;
2050 goto out;
2051 }
2052 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002053 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002054
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002055 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002056 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002057 if (!*args[2]) {
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02002058 if (in_peers_section) {
2059 return 0;
2060 }
2061 else {
2062 /* 'server' line number of argument check. */
2063 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
2064 file, linenum, args[0]);
2065 err_code |= ERR_ALERT | ERR_FATAL;
2066 goto out;
2067 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002068 }
2069
2070 err = invalid_char(args[1]);
2071 }
2072 else if (srv_tmpl) {
2073 if (!*args[3]) {
2074 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002075 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 +02002076 file, linenum, args[0]);
2077 err_code |= ERR_ALERT | ERR_FATAL;
2078 goto out;
2079 }
2080
2081 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002082 }
2083
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002084 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002085 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 +02002086 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002087 err_code |= ERR_ALERT | ERR_FATAL;
2088 goto out;
2089 }
2090
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002091 cur_arg = 2;
2092 if (srv_tmpl) {
2093 /* Parse server-template <nb | range> arg. */
2094 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002095 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002096 file, linenum, args[0], args[cur_arg]);
2097 err_code |= ERR_ALERT | ERR_FATAL;
2098 goto out;
2099 }
2100 cur_arg++;
2101 }
2102
Willy Tarreau272adea2014-03-31 10:39:59 +02002103 if (!defsrv) {
2104 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002105 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002106 struct protocol *proto;
2107
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002108 newsrv = new_server(curproxy);
2109 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002110 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002111 err_code |= ERR_ALERT | ERR_ABORT;
2112 goto out;
2113 }
2114
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002115 if (srv_tmpl) {
2116 newsrv->tmpl_info.nb_low = tmpl_range_low;
2117 newsrv->tmpl_info.nb_high = tmpl_range_high;
2118 }
2119
Willy Tarreau272adea2014-03-31 10:39:59 +02002120 /* the servers are linked backwards first */
2121 newsrv->next = curproxy->srv;
2122 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002123 newsrv->conf.file = strdup(file);
2124 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002125 /* Note: for a server template, its id is its prefix.
2126 * This is a temporary id which will be used for server allocations to come
2127 * after parsing.
2128 */
2129 if (srv)
2130 newsrv->id = strdup(args[1]);
2131 else
2132 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002133
2134 /* several ways to check the port component :
2135 * - IP => port=+0, relative (IPv4 only)
2136 * - IP: => port=+0, relative
2137 * - IP:N => port=N, absolute
2138 * - IP:+N => port=+N, relative
2139 * - IP:-N => port=-N, relative
2140 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002141 if (!parse_addr)
2142 goto skip_addr;
2143
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002144 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002145 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002146 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002147 err_code |= ERR_ALERT | ERR_FATAL;
2148 goto out;
2149 }
2150
2151 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002152 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002153 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002154 file, linenum, args[0], args[1]);
2155 err_code |= ERR_ALERT | ERR_FATAL;
2156 goto out;
2157 }
2158
2159 if (!port1 || !port2) {
2160 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002161 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002162 }
2163 else if (port1 != port2) {
2164 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002165 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002166 file, linenum, args[0], args[1], args[2]);
2167 err_code |= ERR_ALERT | ERR_FATAL;
2168 goto out;
2169 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002170
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002171 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002172 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002173 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002174 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002175 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2176 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2177 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002178 err_code |= ERR_ALERT | ERR_FATAL;
2179 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002180 }
2181 }
2182 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002183 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002184 file, linenum, newsrv->id);
2185 err_code |= ERR_ALERT | ERR_FATAL;
2186 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002187 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002188 }
2189
Willy Tarreau272adea2014-03-31 10:39:59 +02002190 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002191 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002192
Olivier Houchard8da5f982017-08-04 18:35:36 +02002193 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002194 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002195 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002196 err_code |= ERR_ALERT | ERR_FATAL;
2197 goto out;
2198 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002199
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002200 cur_arg++;
2201 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002202 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002203 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002204 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002205 } else {
2206 newsrv = &curproxy->defsrv;
2207 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002208 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002209 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002210 }
2211
2212 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002213 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002214 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002215
2216 if (err == PARSE_TIME_OVER) {
2217 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2218 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2219 err_code |= ERR_ALERT | ERR_FATAL;
2220 goto out;
2221 }
2222 else if (err == PARSE_TIME_UNDER) {
2223 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2224 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2225 err_code |= ERR_ALERT | ERR_FATAL;
2226 goto out;
2227 }
2228 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002229 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002230 file, linenum, *err, newsrv->id);
2231 err_code |= ERR_ALERT | ERR_FATAL;
2232 goto out;
2233 }
2234 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002235 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002236 file, linenum, val, args[cur_arg], newsrv->id);
2237 err_code |= ERR_ALERT | ERR_FATAL;
2238 goto out;
2239 }
2240 newsrv->agent.inter = val;
2241 cur_arg += 2;
2242 }
Misiekea849332017-01-09 09:39:51 +01002243 else if (!strcmp(args[cur_arg], "agent-addr")) {
2244 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002245 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002246 goto out;
2247 }
2248
2249 cur_arg += 2;
2250 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002251 else if (!strcmp(args[cur_arg], "agent-port")) {
2252 global.maxsock++;
2253 newsrv->agent.port = atol(args[cur_arg + 1]);
2254 cur_arg += 2;
2255 }
James Brown55f9ff12015-10-21 18:19:05 -07002256 else if (!strcmp(args[cur_arg], "agent-send")) {
2257 global.maxsock++;
2258 free(newsrv->agent.send_string);
2259 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2260 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2261 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2262 cur_arg += 2;
2263 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002264 else if (!strcmp(args[cur_arg], "init-addr")) {
2265 char *p, *end;
2266 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002267 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002268
2269 newsrv->init_addr_methods = 0;
2270 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2271
2272 for (p = args[cur_arg + 1]; *p; p = end) {
2273 /* cut on next comma */
2274 for (end = p; *end && *end != ','; end++);
2275 if (*end)
2276 *(end++) = 0;
2277
Willy Tarreau4310d362016-11-02 15:05:56 +01002278 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002279 if (!strcmp(p, "libc")) {
2280 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2281 }
2282 else if (!strcmp(p, "last")) {
2283 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2284 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002285 else if (!strcmp(p, "none")) {
2286 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2287 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002288 else if (str2ip2(p, &sa, 0)) {
2289 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002290 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002291 file, linenum, args[cur_arg], p);
2292 err_code |= ERR_ALERT | ERR_FATAL;
2293 goto out;
2294 }
2295 newsrv->init_addr = sa;
2296 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2297 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002298 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002299 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 +02002300 file, linenum, args[cur_arg], p);
2301 err_code |= ERR_ALERT | ERR_FATAL;
2302 goto out;
2303 }
2304 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002305 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002306 file, linenum, args[cur_arg], p);
2307 err_code |= ERR_ALERT | ERR_FATAL;
2308 goto out;
2309 }
2310 }
2311 cur_arg += 2;
2312 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002313 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002314 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002315 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2316 cur_arg += 2;
2317 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002318 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2319 char *p, *end;
2320
2321 for (p = args[cur_arg + 1]; *p; p = end) {
2322 /* cut on next comma */
2323 for (end = p; *end && *end != ','; end++);
2324 if (*end)
2325 *(end++) = 0;
2326
2327 if (!strcmp(p, "allow-dup-ip")) {
2328 newsrv->dns_opts.accept_duplicate_ip = 1;
2329 }
Daniel Corbettf8716912019-11-17 09:48:56 -05002330 else if (!strcmp(p, "ignore-weight")) {
2331 newsrv->dns_opts.ignore_weight = 1;
2332 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002333 else if (!strcmp(p, "prevent-dup-ip")) {
2334 newsrv->dns_opts.accept_duplicate_ip = 0;
2335 }
2336 else {
Daniel Corbettf8716912019-11-17 09:48:56 -05002337 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.\n",
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002338 file, linenum, args[cur_arg], p);
2339 err_code |= ERR_ALERT | ERR_FATAL;
2340 goto out;
2341 }
2342 }
2343
2344 cur_arg += 2;
2345 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002346 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2347 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002348 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002349 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002350 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002351 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002352 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002353 file, linenum, args[cur_arg]);
2354 err_code |= ERR_ALERT | ERR_FATAL;
2355 goto out;
2356 }
2357 cur_arg += 2;
2358 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002359 else if (!strcmp(args[cur_arg], "resolve-net")) {
2360 char *p, *e;
2361 unsigned char mask;
2362 struct dns_options *opt;
2363
2364 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002365 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002366 file, linenum, args[cur_arg]);
2367 err_code |= ERR_ALERT | ERR_FATAL;
2368 goto out;
2369 }
2370
2371 opt = &newsrv->dns_opts;
2372
2373 /* Split arguments by comma, and convert it from ipv4 or ipv6
2374 * string network in in_addr or in6_addr.
2375 */
2376 p = args[cur_arg + 1];
2377 e = p;
2378 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002379 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002380 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002381 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002382 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2383 err_code |= ERR_ALERT | ERR_FATAL;
2384 goto out;
2385 }
2386 /* look for end or comma. */
2387 while (*e != ',' && *e != '\0')
2388 e++;
2389 if (*e == ',') {
2390 *e = '\0';
2391 e++;
2392 }
2393 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2394 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2395 /* Try to convert input string from ipv4 or ipv6 network. */
2396 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2397 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2398 &mask)) {
2399 /* Try to convert input string from ipv6 network. */
2400 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2401 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2402 } else {
2403 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002404 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002405 file, linenum, args[cur_arg], p);
2406 err_code |= ERR_ALERT | ERR_FATAL;
2407 goto out;
2408 }
2409 opt->pref_net_nb++;
2410 p = e;
2411 }
2412
2413 cur_arg += 2;
2414 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002415 else if (!strcmp(args[cur_arg], "rise")) {
2416 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002417 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002418 file, linenum, args[cur_arg]);
2419 err_code |= ERR_ALERT | ERR_FATAL;
2420 goto out;
2421 }
2422
2423 newsrv->check.rise = atol(args[cur_arg + 1]);
2424 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002425 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002426 file, linenum, args[cur_arg]);
2427 err_code |= ERR_ALERT | ERR_FATAL;
2428 goto out;
2429 }
2430
2431 if (newsrv->check.health)
2432 newsrv->check.health = newsrv->check.rise;
2433 cur_arg += 2;
2434 }
2435 else if (!strcmp(args[cur_arg], "fall")) {
2436 newsrv->check.fall = atol(args[cur_arg + 1]);
2437
2438 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002439 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002440 file, linenum, args[cur_arg]);
2441 err_code |= ERR_ALERT | ERR_FATAL;
2442 goto out;
2443 }
2444
2445 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002446 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002447 file, linenum, args[cur_arg]);
2448 err_code |= ERR_ALERT | ERR_FATAL;
2449 goto out;
2450 }
2451
2452 cur_arg += 2;
2453 }
2454 else if (!strcmp(args[cur_arg], "inter")) {
2455 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002456
2457 if (err == PARSE_TIME_OVER) {
2458 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2459 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2460 err_code |= ERR_ALERT | ERR_FATAL;
2461 goto out;
2462 }
2463 else if (err == PARSE_TIME_UNDER) {
2464 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2465 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2466 err_code |= ERR_ALERT | ERR_FATAL;
2467 goto out;
2468 }
2469 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002470 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002471 file, linenum, *err, newsrv->id);
2472 err_code |= ERR_ALERT | ERR_FATAL;
2473 goto out;
2474 }
2475 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002476 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002477 file, linenum, val, args[cur_arg], newsrv->id);
2478 err_code |= ERR_ALERT | ERR_FATAL;
2479 goto out;
2480 }
2481 newsrv->check.inter = val;
2482 cur_arg += 2;
2483 }
2484 else if (!strcmp(args[cur_arg], "fastinter")) {
2485 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002486
2487 if (err == PARSE_TIME_OVER) {
2488 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2489 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2490 err_code |= ERR_ALERT | ERR_FATAL;
2491 goto out;
2492 }
2493 else if (err == PARSE_TIME_UNDER) {
2494 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2495 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2496 err_code |= ERR_ALERT | ERR_FATAL;
2497 goto out;
2498 }
2499 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002500 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002501 file, linenum, *err, newsrv->id);
2502 err_code |= ERR_ALERT | ERR_FATAL;
2503 goto out;
2504 }
2505 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002506 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002507 file, linenum, val, args[cur_arg], newsrv->id);
2508 err_code |= ERR_ALERT | ERR_FATAL;
2509 goto out;
2510 }
2511 newsrv->check.fastinter = val;
2512 cur_arg += 2;
2513 }
2514 else if (!strcmp(args[cur_arg], "downinter")) {
2515 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002516
2517 if (err == PARSE_TIME_OVER) {
2518 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2519 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2520 err_code |= ERR_ALERT | ERR_FATAL;
2521 goto out;
2522 }
2523 else if (err == PARSE_TIME_UNDER) {
2524 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2525 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2526 err_code |= ERR_ALERT | ERR_FATAL;
2527 goto out;
2528 }
2529 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002530 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002531 file, linenum, *err, newsrv->id);
2532 err_code |= ERR_ALERT | ERR_FATAL;
2533 goto out;
2534 }
2535 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002536 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002537 file, linenum, val, args[cur_arg], newsrv->id);
2538 err_code |= ERR_ALERT | ERR_FATAL;
2539 goto out;
2540 }
2541 newsrv->check.downinter = val;
2542 cur_arg += 2;
2543 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002544 else if (!strcmp(args[cur_arg], "port")) {
2545 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002546 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002547 cur_arg += 2;
2548 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002549 else if (!strcmp(args[cur_arg], "weight")) {
2550 int w;
2551 w = atol(args[cur_arg + 1]);
2552 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002553 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002554 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2555 err_code |= ERR_ALERT | ERR_FATAL;
2556 goto out;
2557 }
2558 newsrv->uweight = newsrv->iweight = w;
2559 cur_arg += 2;
2560 }
2561 else if (!strcmp(args[cur_arg], "minconn")) {
2562 newsrv->minconn = atol(args[cur_arg + 1]);
2563 cur_arg += 2;
2564 }
2565 else if (!strcmp(args[cur_arg], "maxconn")) {
2566 newsrv->maxconn = atol(args[cur_arg + 1]);
2567 cur_arg += 2;
2568 }
2569 else if (!strcmp(args[cur_arg], "maxqueue")) {
2570 newsrv->maxqueue = atol(args[cur_arg + 1]);
2571 cur_arg += 2;
2572 }
2573 else if (!strcmp(args[cur_arg], "slowstart")) {
2574 /* slowstart is stored in seconds */
2575 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002576
2577 if (err == PARSE_TIME_OVER) {
2578 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2579 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2580 err_code |= ERR_ALERT | ERR_FATAL;
2581 goto out;
2582 }
2583 else if (err == PARSE_TIME_UNDER) {
2584 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2585 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2586 err_code |= ERR_ALERT | ERR_FATAL;
2587 goto out;
2588 }
2589 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002590 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002591 file, linenum, *err, newsrv->id);
2592 err_code |= ERR_ALERT | ERR_FATAL;
2593 goto out;
2594 }
2595 newsrv->slowstart = (val + 999) / 1000;
2596 cur_arg += 2;
2597 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002598 else if (!strcmp(args[cur_arg], "on-error")) {
2599 if (!strcmp(args[cur_arg + 1], "fastinter"))
2600 newsrv->onerror = HANA_ONERR_FASTINTER;
2601 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2602 newsrv->onerror = HANA_ONERR_FAILCHK;
2603 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2604 newsrv->onerror = HANA_ONERR_SUDDTH;
2605 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2606 newsrv->onerror = HANA_ONERR_MARKDWN;
2607 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002608 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002609 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2610 file, linenum, args[cur_arg], args[cur_arg + 1]);
2611 err_code |= ERR_ALERT | ERR_FATAL;
2612 goto out;
2613 }
2614
2615 cur_arg += 2;
2616 }
2617 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2618 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2619 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2620 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002621 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002622 file, linenum, args[cur_arg], args[cur_arg + 1]);
2623 err_code |= ERR_ALERT | ERR_FATAL;
2624 goto out;
2625 }
2626
2627 cur_arg += 2;
2628 }
2629 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2630 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2631 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2632 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002633 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002634 file, linenum, args[cur_arg], args[cur_arg + 1]);
2635 err_code |= ERR_ALERT | ERR_FATAL;
2636 goto out;
2637 }
2638
2639 cur_arg += 2;
2640 }
2641 else if (!strcmp(args[cur_arg], "error-limit")) {
2642 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002643 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002644 file, linenum, args[cur_arg]);
2645 err_code |= ERR_ALERT | ERR_FATAL;
2646 goto out;
2647 }
2648
2649 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2650
2651 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002652 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002653 file, linenum, args[cur_arg]);
2654 err_code |= ERR_ALERT | ERR_FATAL;
2655 goto out;
2656 }
2657 cur_arg += 2;
2658 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002659 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002660 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002661 file, linenum, "usesrc", "source");
2662 err_code |= ERR_ALERT | ERR_FATAL;
2663 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002664 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002665 else {
2666 static int srv_dumped;
2667 struct srv_kw *kw;
2668 char *err;
2669
2670 kw = srv_find_kw(args[cur_arg]);
2671 if (kw) {
2672 char *err = NULL;
2673 int code;
2674
2675 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002676 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 +02002677 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002678 if (kw->skip != -1)
2679 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002680 err_code |= ERR_ALERT | ERR_FATAL;
2681 goto out;
2682 }
2683
2684 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002685 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002686 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002687 if (kw->skip != -1)
2688 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002689 err_code |= ERR_ALERT;
2690 continue;
2691 }
2692
2693 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2694 err_code |= code;
2695
2696 if (code) {
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002697 display_parser_err(file, linenum, args, cur_arg, code, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002698 if (code & ERR_FATAL) {
2699 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002700 if (kw->skip != -1)
2701 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002702 goto out;
2703 }
2704 }
2705 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002706 if (kw->skip != -1)
2707 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002708 continue;
2709 }
2710
2711 err = NULL;
2712 if (!srv_dumped) {
2713 srv_dump_kws(&err);
2714 indent_msg(&err, 4);
2715 srv_dumped = 1;
2716 }
2717
Christopher Faulet767a84b2017-11-24 16:50:31 +01002718 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002719 file, linenum, args[0], args[1], args[cur_arg],
2720 err ? " Registered keywords :" : "", err ? err : "");
2721 free(err);
2722
2723 err_code |= ERR_ALERT | ERR_FATAL;
2724 goto out;
2725 }
2726 }
2727
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002728 if (!defsrv)
2729 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2730 if (err_code & ERR_FATAL)
2731 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002732 if (srv_tmpl)
2733 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002734 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002735 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002736 return 0;
2737
2738 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002739 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002740 free(errmsg);
2741 return err_code;
2742}
2743
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002744/* Returns a pointer to the first server matching either id <id>.
2745 * NULL is returned if no match is found.
2746 * the lookup is performed in the backend <bk>
2747 */
2748struct server *server_find_by_id(struct proxy *bk, int id)
2749{
2750 struct eb32_node *eb32;
2751 struct server *curserver;
2752
2753 if (!bk || (id ==0))
2754 return NULL;
2755
2756 /* <bk> has no backend capabilities, so it can't have a server */
2757 if (!(bk->cap & PR_CAP_BE))
2758 return NULL;
2759
2760 curserver = NULL;
2761
2762 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2763 if (eb32)
2764 curserver = container_of(eb32, struct server, conf.id);
2765
2766 return curserver;
2767}
2768
2769/* Returns a pointer to the first server matching either name <name>, or id
2770 * if <name> starts with a '#'. NULL is returned if no match is found.
2771 * the lookup is performed in the backend <bk>
2772 */
2773struct server *server_find_by_name(struct proxy *bk, const char *name)
2774{
2775 struct server *curserver;
2776
2777 if (!bk || !name)
2778 return NULL;
2779
2780 /* <bk> has no backend capabilities, so it can't have a server */
2781 if (!(bk->cap & PR_CAP_BE))
2782 return NULL;
2783
2784 curserver = NULL;
2785 if (*name == '#') {
2786 curserver = server_find_by_id(bk, atoi(name + 1));
2787 if (curserver)
2788 return curserver;
2789 }
2790 else {
2791 curserver = bk->srv;
2792
2793 while (curserver && (strcmp(curserver->id, name) != 0))
2794 curserver = curserver->next;
2795
2796 if (curserver)
2797 return curserver;
2798 }
2799
2800 return NULL;
2801}
2802
2803struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2804{
2805 struct server *byname;
2806 struct server *byid;
2807
2808 if (!name && !id)
2809 return NULL;
2810
2811 if (diff)
2812 *diff = 0;
2813
2814 byname = byid = NULL;
2815
2816 if (name) {
2817 byname = server_find_by_name(bk, name);
2818 if (byname && (!id || byname->puid == id))
2819 return byname;
2820 }
2821
2822 /* remaining possibilities :
2823 * - name not set
2824 * - name set but not found
2825 * - name found but ID doesn't match
2826 */
2827 if (id) {
2828 byid = server_find_by_id(bk, id);
2829 if (byid) {
2830 if (byname) {
2831 /* use id only if forced by configuration */
2832 if (byid->flags & SRV_F_FORCED_ID) {
2833 if (diff)
2834 *diff |= 2;
2835 return byid;
2836 }
2837 else {
2838 if (diff)
2839 *diff |= 1;
2840 return byname;
2841 }
2842 }
2843
2844 /* remaining possibilities:
2845 * - name not set
2846 * - name set but not found
2847 */
2848 if (name && diff)
2849 *diff |= 2;
2850 return byid;
2851 }
2852
2853 /* id bot found */
2854 if (byname) {
2855 if (diff)
2856 *diff |= 1;
2857 return byname;
2858 }
2859 }
2860
2861 return NULL;
2862}
2863
Willy Tarreau46b7f532018-08-21 11:54:26 +02002864/* Update a server state using the parameters available in the params list.
2865 *
2866 * Grabs the server lock during operation.
2867 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002868static void srv_update_state(struct server *srv, int version, char **params)
2869{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002870 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002871 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002872
2873 /* fields since version 1
2874 * and common to all other upcoming versions
2875 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002876 enum srv_state srv_op_state;
2877 enum srv_admin srv_admin_state;
2878 unsigned srv_uweight, srv_iweight;
2879 unsigned long srv_last_time_change;
2880 short srv_check_status;
2881 enum chk_result srv_check_result;
2882 int srv_check_health;
2883 int srv_check_state, srv_agent_state;
2884 int bk_f_forced_id;
2885 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002886 int fqdn_set_by_cli;
2887 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002888 const char *port_str;
2889 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002890 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002891
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002892 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002893 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002894 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002895 switch (version) {
2896 case 1:
2897 /*
2898 * now we can proceed with server's state update:
2899 * srv_addr: params[0]
2900 * srv_op_state: params[1]
2901 * srv_admin_state: params[2]
2902 * srv_uweight: params[3]
2903 * srv_iweight: params[4]
2904 * srv_last_time_change: params[5]
2905 * srv_check_status: params[6]
2906 * srv_check_result: params[7]
2907 * srv_check_health: params[8]
2908 * srv_check_state: params[9]
2909 * srv_agent_state: params[10]
2910 * bk_f_forced_id: params[11]
2911 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002912 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002913 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002914 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002915 */
2916
2917 /* validating srv_op_state */
2918 p = NULL;
2919 errno = 0;
2920 srv_op_state = strtol(params[1], &p, 10);
2921 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2922 (srv_op_state != SRV_ST_STOPPED &&
2923 srv_op_state != SRV_ST_STARTING &&
2924 srv_op_state != SRV_ST_RUNNING &&
2925 srv_op_state != SRV_ST_STOPPING)) {
2926 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2927 }
2928
2929 /* validating srv_admin_state */
2930 p = NULL;
2931 errno = 0;
2932 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002933 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002934
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002935 /* inherited statuses will be recomputed later.
2936 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2937 */
2938 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002939
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002940 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2941 (srv_admin_state != 0 &&
2942 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002943 srv_admin_state != SRV_ADMF_CMAINT &&
2944 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002945 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002946 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002947 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2948 }
2949
2950 /* validating srv_uweight */
2951 p = NULL;
2952 errno = 0;
2953 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002954 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002955 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2956
2957 /* validating srv_iweight */
2958 p = NULL;
2959 errno = 0;
2960 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002961 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002962 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2963
2964 /* validating srv_last_time_change */
2965 p = NULL;
2966 errno = 0;
2967 srv_last_time_change = strtol(params[5], &p, 10);
2968 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2969 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2970
2971 /* validating srv_check_status */
2972 p = NULL;
2973 errno = 0;
2974 srv_check_status = strtol(params[6], &p, 10);
2975 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2976 (srv_check_status >= HCHK_STATUS_SIZE))
2977 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2978
2979 /* validating srv_check_result */
2980 p = NULL;
2981 errno = 0;
2982 srv_check_result = strtol(params[7], &p, 10);
2983 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2984 (srv_check_result != CHK_RES_UNKNOWN &&
2985 srv_check_result != CHK_RES_NEUTRAL &&
2986 srv_check_result != CHK_RES_FAILED &&
2987 srv_check_result != CHK_RES_PASSED &&
2988 srv_check_result != CHK_RES_CONDPASS)) {
2989 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2990 }
2991
2992 /* validating srv_check_health */
2993 p = NULL;
2994 errno = 0;
2995 srv_check_health = strtol(params[8], &p, 10);
2996 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2997 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2998
2999 /* validating srv_check_state */
3000 p = NULL;
3001 errno = 0;
3002 srv_check_state = strtol(params[9], &p, 10);
3003 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3004 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3005 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3006
3007 /* validating srv_agent_state */
3008 p = NULL;
3009 errno = 0;
3010 srv_agent_state = strtol(params[10], &p, 10);
3011 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3012 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3013 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3014
3015 /* validating bk_f_forced_id */
3016 p = NULL;
3017 errno = 0;
3018 bk_f_forced_id = strtol(params[11], &p, 10);
3019 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3020 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3021
3022 /* validating srv_f_forced_id */
3023 p = NULL;
3024 errno = 0;
3025 srv_f_forced_id = strtol(params[12], &p, 10);
3026 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3027 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3028
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003029 /* validating srv_fqdn */
3030 fqdn = params[13];
3031 if (fqdn && *fqdn == '-')
3032 fqdn = NULL;
3033 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3034 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3035 fqdn = NULL;
3036 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003037
Frédéric Lécaille31694712017-08-01 08:47:19 +02003038 port_str = params[14];
3039 if (port_str) {
3040 port = strl2uic(port_str, strlen(port_str));
3041 if (port > USHRT_MAX) {
3042 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3043 port_str = NULL;
3044 }
3045 }
3046
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003047 /* SRV record
3048 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3049 */
3050 srvrecord = params[15];
3051 if (srvrecord && *srvrecord != '_')
3052 srvrecord = NULL;
3053
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003054 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003055 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003056 goto out;
3057
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003058 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003059 /* recover operational state and apply it to this server
3060 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003061 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003062 switch (srv_op_state) {
3063 case SRV_ST_STOPPED:
3064 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003065 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003066 break;
3067 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003068 /* If rise == 1 there is no STARTING state, let's switch to
3069 * RUNNING
3070 */
3071 if (srv->check.rise == 1) {
3072 srv->check.health = srv->check.rise + srv->check.fall - 1;
3073 srv_set_running(srv, "", NULL);
3074 break;
3075 }
3076 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3077 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003078 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003079 break;
3080 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003081 /* If fall == 1 there is no STOPPING state, let's switch to
3082 * STOPPED
3083 */
3084 if (srv->check.fall == 1) {
3085 srv->check.health = 0;
3086 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3087 break;
3088 }
3089 if (srv->check.health < srv->check.rise ||
3090 srv->check.health > srv->check.rise + srv->check.fall - 2)
3091 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003092 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003093 break;
3094 case SRV_ST_RUNNING:
3095 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003096 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003097 break;
3098 }
3099
3100 /* When applying server state, the following rules apply:
3101 * - in case of a configuration change, we apply the setting from the new
3102 * configuration, regardless of old running state
3103 * - if no configuration change, we apply old running state only if old running
3104 * state is different from new configuration state
3105 */
3106 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003107 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3108 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003109 srv_adm_set_maint(srv);
3110 else
3111 srv_adm_set_ready(srv);
3112 }
3113 /* configuration is the same, let's compate old running state and new conf state */
3114 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003115 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003116 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003117 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003118 srv_adm_set_ready(srv);
3119 }
3120 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003121 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003122 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003123 * (srv->iweight is the weight set up in configuration).
3124 * There are two possible reasons for FDRAIN to have been present :
3125 * - previous config weight was zero
3126 * - "set server b/s drain" was sent to the CLI
3127 *
3128 * In the first case, we simply want to drop this drain state
3129 * if the new weight is not zero anymore, meaning the administrator
3130 * has intentionally turned the weight back to a positive value to
3131 * enable the server again after an operation. In the second case,
3132 * the drain state was forced on the CLI regardless of the config's
3133 * weight so we don't want a change to the config weight to lose this
3134 * status. What this means is :
3135 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3136 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003137 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003138 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003139 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003140 }
3141
3142 srv->last_change = date.tv_sec - srv_last_time_change;
3143 srv->check.status = srv_check_status;
3144 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003145
3146 /* Only case we want to apply is removing ENABLED flag which could have been
3147 * done by the "disable health" command over the stats socket
3148 */
3149 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3150 (srv_check_state & CHK_ST_CONFIGURED) &&
3151 !(srv_check_state & CHK_ST_ENABLED))
3152 srv->check.state &= ~CHK_ST_ENABLED;
3153
3154 /* Only case we want to apply is removing ENABLED flag which could have been
3155 * done by the "disable agent" command over the stats socket
3156 */
3157 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3158 (srv_agent_state & CHK_ST_CONFIGURED) &&
3159 !(srv_agent_state & CHK_ST_ENABLED))
3160 srv->agent.state &= ~CHK_ST_ENABLED;
3161
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003162 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3163 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003164 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003165 * It means that a configuration file change has precedence over a unix socket change
3166 * for server's weight
3167 *
3168 * by default, HAProxy applies the following weight when parsing the configuration
3169 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003170 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003171 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003172 srv->uweight = srv_uweight;
3173 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003174 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003175
Willy Tarreaue5a60682016-11-09 14:54:53 +01003176 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003177 if (strcmp(params[0], "-"))
3178 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003179
3180 if (fqdn && srv->hostname) {
3181 if (!strcmp(srv->hostname, fqdn)) {
3182 /* Here we reset the 'set from stats socket FQDN' flag
3183 * to support such transitions:
3184 * Let's say initial FQDN value is foo1 (in configuration file).
3185 * - FQDN changed from stats socket, from foo1 to foo2 value,
3186 * - FQDN changed again from file configuration (with the same previous value
3187 set from stats socket, from foo1 to foo2 value),
3188 * - reload for any other reason than a FQDN modification,
3189 * the configuration file FQDN matches the fqdn server state file value.
3190 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003191 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003192 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003193 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003194 }
3195 else {
3196 /* If the FDQN has been changed from stats socket,
3197 * apply fqdn state file value (which is the value set
3198 * from stats socket).
Baptiste Assmann13a92322019-06-07 09:40:55 +02003199 * Also ensure the runtime resolver will process this resolution.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003200 */
3201 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003202 srv_set_fqdn(srv, fqdn, 0);
Baptiste Assmann13a92322019-06-07 09:40:55 +02003203 srv->flags &= ~SRV_F_NO_RESOLUTION;
Emeric Brun52a91d32017-08-31 14:41:55 +02003204 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003205 }
3206 }
3207 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003208 /* If all the conditions below are validated, this means
3209 * we're evaluating a server managed by SRV resolution
3210 */
3211 else if (fqdn && !srv->hostname && srvrecord) {
3212 int res;
3213
3214 /* we can't apply previous state if SRV record has changed */
3215 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3216 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);
3217 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3218 goto out;
3219 }
3220
3221 /* create or find a SRV resolution for this srv record */
3222 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3223 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3224 if (srv->srvrq == NULL) {
3225 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3226 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3227 goto out;
3228 }
3229
3230 /* prepare DNS resolution for this server */
3231 res = srv_prepare_for_resolution(srv, fqdn);
3232 if (res == -1) {
3233 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3234 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3235 goto out;
3236 }
3237
3238 /* configure check.port accordingly */
3239 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3240 !(srv->flags & SRV_F_CHECKPORT))
3241 srv->check.port = port;
3242
3243 /* Unset SRV_F_MAPPORTS for SRV records.
3244 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3245 * because no ports are provided in the configuration file.
3246 * This is because HAProxy will use the port found into the SRV record.
3247 */
3248 srv->flags &= ~SRV_F_MAPPORTS;
3249 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003250
Frédéric Lécaille31694712017-08-01 08:47:19 +02003251 if (port_str)
3252 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003253 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003254
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003255 break;
3256 default:
3257 chunk_appendf(msg, ", version '%d' not supported", version);
3258 }
3259
3260 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003261 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003262 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003263 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003264 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003265 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003266}
3267
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003268
3269/*
3270 * read next line from file <f> and return the server state version if one found.
3271 * If no version is found, then 0 is returned
3272 * Note that this should be the first read on <f>
3273 */
3274static int srv_state_get_version(FILE *f) {
3275 char buf[2];
3276 int ret;
3277
3278 /* first character of first line of the file must contain the version of the export */
3279 if (fgets(buf, 2, f) == NULL) {
3280 return 0;
3281 }
3282
3283 ret = atoi(buf);
3284 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
3285 (ret > SRV_STATE_FILE_VERSION_MAX))
3286 return 0;
3287
3288 return ret;
3289}
3290
3291
3292/*
3293 * parses server state line stored in <buf> and supposedly in version <version>.
3294 * Set <params> and <srv_params> accordingly.
3295 * In case of error, params[0] is set to NULL.
3296 */
3297static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
3298{
3299 int buflen, arg, srv_arg;
3300 char *cur, *end;
3301
3302 buflen = strlen(buf);
3303 cur = buf;
3304 end = cur + buflen;
3305
3306 /* we need at least one character */
3307 if (buflen == 0) {
3308 params[0] = NULL;
3309 return;
3310 }
3311
3312 /* ignore blank characters at the beginning of the line */
Willy Tarreau90807112020-02-25 08:16:33 +01003313 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003314 ++cur;
3315
3316 /* Ignore empty or commented lines */
3317 if (cur == end || *cur == '#') {
3318 params[0] = NULL;
3319 return;
3320 }
3321
3322 /* truncated lines */
3323 if (buf[buflen - 1] != '\n') {
3324 //ha_warning("server-state file '%s': truncated line\n", filepath);
3325 params[0] = NULL;
3326 return;
3327 }
3328
3329 /* Removes trailing '\n' */
3330 buf[buflen - 1] = '\0';
3331
3332 /* we're now ready to move the line into *srv_params[] */
3333 params[0] = cur;
3334 arg = 1;
3335 srv_arg = 0;
3336 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
Willy Tarreau90807112020-02-25 08:16:33 +01003337 if (isspace((unsigned char)*cur)) {
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003338 *cur = '\0';
3339 ++cur;
Willy Tarreau90807112020-02-25 08:16:33 +01003340 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003341 ++cur;
3342 switch (version) {
3343 case 1:
3344 /*
3345 * srv_addr: params[4] => srv_params[0]
3346 * srv_op_state: params[5] => srv_params[1]
3347 * srv_admin_state: params[6] => srv_params[2]
3348 * srv_uweight: params[7] => srv_params[3]
3349 * srv_iweight: params[8] => srv_params[4]
3350 * srv_last_time_change: params[9] => srv_params[5]
3351 * srv_check_status: params[10] => srv_params[6]
3352 * srv_check_result: params[11] => srv_params[7]
3353 * srv_check_health: params[12] => srv_params[8]
3354 * srv_check_state: params[13] => srv_params[9]
3355 * srv_agent_state: params[14] => srv_params[10]
3356 * bk_f_forced_id: params[15] => srv_params[11]
3357 * srv_f_forced_id: params[16] => srv_params[12]
3358 * srv_fqdn: params[17] => srv_params[13]
3359 * srv_port: params[18] => srv_params[14]
3360 * srvrecord: params[19] => srv_params[15]
3361 */
3362 if (arg >= 4) {
3363 srv_params[srv_arg] = cur;
3364 ++srv_arg;
3365 }
3366 break;
3367 }
3368
3369 params[arg] = cur;
3370 ++arg;
3371 }
3372 else {
3373 ++cur;
3374 }
3375 }
3376
3377 /* if line is incomplete line, then ignore it.
3378 * otherwise, update useful flags */
3379 switch (version) {
3380 case 1:
3381 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3382 params[0] = NULL;
3383 return;
3384 }
3385 break;
3386 }
3387
3388 return;
3389}
3390
3391
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003392/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3393 * For each proxy, it does the following:
3394 * - opens its server state file (either one or local one)
3395 * - read whole file, line by line
3396 * - analyse each line to check if it matches our current backend:
3397 * - backend name matches
3398 * - backend id matches if id is forced and name doesn't match
3399 * - if the server pointed by the line is found, then state is applied
3400 *
3401 * If the running backend uuid or id differs from the state file, then HAProxy reports
3402 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003403 *
3404 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003405 */
3406void apply_server_state(void)
3407{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003408 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003409 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3410 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003411 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003412 FILE *f;
3413 char *filepath;
3414 char globalfilepath[MAXPATHLEN + 1];
3415 char localfilepath[MAXPATHLEN + 1];
3416 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003417 struct proxy *curproxy, *bk;
3418 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003419 char *line;
3420 char *bkname, *srvname;
3421 struct state_line *st;
3422 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003423
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003424
3425 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003426 globalfilepathlen = 0;
3427 /* create the globalfilepath variable */
3428 if (global.server_state_file) {
3429 /* absolute path or no base directory provided */
3430 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3431 len = strlen(global.server_state_file);
3432 if (len > MAXPATHLEN) {
3433 globalfilepathlen = 0;
3434 goto globalfileerror;
3435 }
3436 memcpy(globalfilepath, global.server_state_file, len);
3437 globalfilepath[len] = '\0';
3438 globalfilepathlen = len;
3439 }
3440 else if (global.server_state_base) {
3441 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003442 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003443 globalfilepathlen = 0;
3444 goto globalfileerror;
3445 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003446 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003447 globalfilepath[len] = 0;
3448 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003449
3450 /* append a slash if needed */
3451 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3452 if (globalfilepathlen + 1 > MAXPATHLEN) {
3453 globalfilepathlen = 0;
3454 goto globalfileerror;
3455 }
3456 globalfilepath[globalfilepathlen++] = '/';
3457 }
3458
3459 len = strlen(global.server_state_file);
3460 if (globalfilepathlen + len > MAXPATHLEN) {
3461 globalfilepathlen = 0;
3462 goto globalfileerror;
3463 }
3464 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3465 globalfilepathlen += len;
3466 globalfilepath[globalfilepathlen++] = 0;
3467 }
3468 }
3469 globalfileerror:
3470 if (globalfilepathlen == 0)
3471 globalfilepath[0] = '\0';
3472
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003473 /* Load global server state in a tree */
3474 if (globalfilepathlen > 0) {
3475 errno = 0;
3476 f = fopen(globalfilepath, "r");
3477 if (errno)
3478 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
Vedran Furac5d486272019-10-16 14:49:38 +02003479 if (!f)
3480 goto out_load_server_state_in_tree;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003481
3482 global_file_version = srv_state_get_version(f);
3483 if (global_file_version == 0)
3484 goto out_load_server_state_in_tree;
3485
3486 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3487 line = NULL;
3488
3489 line = strdup(mybuf);
3490 if (line == NULL)
3491 continue;
3492
3493 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3494 if (params[0] == NULL)
Willy Tarreauca7a5af2019-12-20 17:26:27 +01003495 goto nextline;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003496
3497 /* bkname */
3498 bkname = params[1];
3499 /* srvname */
3500 srvname = params[3];
3501
3502 /* key */
3503 chunk_printf(&trash, "%s %s", bkname, srvname);
3504
3505 /* store line in tree */
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003506 st = calloc(1, sizeof(*st) + trash.data + 1);
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003507 if (st == NULL) {
3508 goto nextline;
3509 }
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003510 memcpy(st->name_name.key, trash.area, trash.data + 1);
Willy Tarreaufd1aa012019-12-20 17:23:40 +01003511 if (ebst_insert(&state_file, &st->name_name) != &st->name_name) {
3512 /* this is a duplicate key, probably a hand-crafted file,
3513 * drop it!
3514 */
3515 goto nextline;
3516 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003517
3518 /* save line */
3519 st->line = line;
3520
3521 continue;
3522
3523 nextline:
3524 /* free up memory in case of error during the processing of the line */
3525 free(line);
3526 }
3527 }
3528 out_load_server_state_in_tree:
3529
3530 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003531 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003532 /* servers are only in backends */
3533 if (!(curproxy->cap & PR_CAP_BE))
3534 continue;
3535 fileopenerr = 0;
3536 filepath = NULL;
3537
3538 /* search server state file path and name */
3539 switch (curproxy->load_server_state_from_file) {
3540 /* read servers state from global file */
3541 case PR_SRV_STATE_FILE_GLOBAL:
3542 /* there was an error while generating global server state file path */
3543 if (globalfilepathlen == 0)
3544 continue;
3545 filepath = globalfilepath;
3546 fileopenerr = 1;
3547 break;
3548 /* this backend has its own file */
3549 case PR_SRV_STATE_FILE_LOCAL:
3550 localfilepathlen = 0;
3551 localfilepath[0] = '\0';
3552 len = 0;
3553 /* create the localfilepath variable */
3554 /* absolute path or no base directory provided */
3555 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3556 len = strlen(curproxy->server_state_file_name);
3557 if (len > MAXPATHLEN) {
3558 localfilepathlen = 0;
3559 goto localfileerror;
3560 }
3561 memcpy(localfilepath, curproxy->server_state_file_name, len);
3562 localfilepath[len] = '\0';
3563 localfilepathlen = len;
3564 }
3565 else if (global.server_state_base) {
3566 len = strlen(global.server_state_base);
3567 localfilepathlen += len;
3568
3569 if (localfilepathlen > MAXPATHLEN) {
3570 localfilepathlen = 0;
3571 goto localfileerror;
3572 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003573 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003574 localfilepath[localfilepathlen] = 0;
3575
3576 /* append a slash if needed */
3577 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3578 if (localfilepathlen + 1 > MAXPATHLEN) {
3579 localfilepathlen = 0;
3580 goto localfileerror;
3581 }
3582 localfilepath[localfilepathlen++] = '/';
3583 }
3584
3585 len = strlen(curproxy->server_state_file_name);
3586 if (localfilepathlen + len > MAXPATHLEN) {
3587 localfilepathlen = 0;
3588 goto localfileerror;
3589 }
3590 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3591 localfilepathlen += len;
3592 localfilepath[localfilepathlen++] = 0;
3593 }
3594 filepath = localfilepath;
3595 localfileerror:
3596 if (localfilepathlen == 0)
3597 localfilepath[0] = '\0';
3598
3599 break;
3600 case PR_SRV_STATE_FILE_NONE:
3601 default:
3602 continue;
3603 }
3604
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003605 /* when global file is used, we get data from the tree
3606 * Note that in such case we don't check backend name neither uuid.
3607 * Backend name can't be wrong since it's used as a key to retrieve the server state
3608 * line from the tree.
3609 */
3610 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3611 struct server *srv = curproxy->srv;
3612 while (srv) {
3613 struct ebmb_node *node;
3614 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003615
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003616 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3617 node = ebst_lookup(&state_file, trash.area);
3618 if (!node)
3619 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003620
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003621 st = container_of(node, struct state_line, name_name);
3622 memcpy(mybuf, st->line, strlen(st->line));
3623 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003624
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003625 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3626 if (params[0] == NULL)
3627 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003628
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003629 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003630
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003631 next:
3632 srv = srv->next;
3633 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003634
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003635 continue; /* next proxy in list */
3636 }
3637 else {
3638 /* load 'local' state file */
3639 errno = 0;
3640 f = fopen(filepath, "r");
3641 if (errno && fileopenerr)
3642 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3643 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003644 continue;
3645
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003646 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003647
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003648 /* first character of first line of the file must contain the version of the export */
3649 version = srv_state_get_version(f);
3650 if (version == 0) {
3651 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3652 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003653 }
3654
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003655 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3656 int bk_f_forced_id = 0;
3657 int check_id = 0;
3658 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003659
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003660 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003661
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003662 if (params[0] == NULL) {
3663 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003664 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003665
3666 /* if line is incomplete line, then ignore it.
3667 * otherwise, update useful flags */
3668 switch (version) {
3669 case 1:
3670 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3671 check_id = (atoi(params[0]) == curproxy->uuid);
3672 check_name = (strcmp(curproxy->id, params[1]) == 0);
3673 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003674 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003675
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003676 bk = curproxy;
3677
3678 /* if backend can't be found, let's continue */
3679 if (!check_id && !check_name)
3680 continue;
3681 else if (!check_id && check_name) {
3682 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3683 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3684 }
3685 else if (check_id && !check_name) {
3686 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3687 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3688 /* if name doesn't match, we still want to update curproxy if the backend id
3689 * was forced in previous the previous configuration */
3690 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003691 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003692 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003693
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003694 /* look for the server by its name: param[3] */
3695 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003696
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003697 if (!srv) {
3698 /* if no server found, then warning and continue with next line */
3699 ha_warning("can't find server '%s' in backend '%s'\n",
3700 params[3], params[1]);
3701 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3702 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003703 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003704 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003705
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003706 /* now we can proceed with server's state update */
3707 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003708 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003709 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003710fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003711 fclose(f);
3712 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003713
3714 /* now free memory allocated for the tree */
3715 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3716 node;
3717 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3718 st = container_of(node, struct state_line, name_name);
3719 ebmb_delete(&st->name_name);
3720 free(st->line);
3721 free(st);
3722 }
3723
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003724}
3725
Simon Horman7d09b9a2013-02-12 10:45:51 +09003726/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003727 * update a server's current IP address.
3728 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3729 * ip is in network format.
3730 * updater is a string which contains an information about the requester of the update.
3731 * updater is used if not NULL.
3732 *
3733 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003734 *
3735 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003736 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003737int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003738{
3739 /* generates a log line and a warning on stderr */
3740 if (1) {
3741 /* book enough space for both IPv4 and IPv6 */
3742 char oldip[INET6_ADDRSTRLEN];
3743 char newip[INET6_ADDRSTRLEN];
3744
3745 memset(oldip, '\0', INET6_ADDRSTRLEN);
3746 memset(newip, '\0', INET6_ADDRSTRLEN);
3747
3748 /* copy old IP address in a string */
3749 switch (s->addr.ss_family) {
3750 case AF_INET:
3751 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3752 break;
3753 case AF_INET6:
3754 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3755 break;
3756 };
3757
3758 /* copy new IP address in a string */
3759 switch (ip_sin_family) {
3760 case AF_INET:
3761 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3762 break;
3763 case AF_INET6:
3764 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3765 break;
3766 };
3767
3768 /* save log line into a buffer */
3769 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3770 s->proxy->id, s->id, oldip, newip, updater);
3771
3772 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003773 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003774
3775 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003776 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003777 }
3778
3779 /* save the new IP family */
3780 s->addr.ss_family = ip_sin_family;
3781 /* save the new IP address */
3782 switch (ip_sin_family) {
3783 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003784 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003785 break;
3786 case AF_INET6:
3787 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3788 break;
3789 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003790 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003791
3792 return 0;
3793}
3794
3795/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003796 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3797 *
3798 * Caller can pass its name through <updater> to get it integrated in the response message
3799 * returned by the function.
3800 *
3801 * The function first does the following, in that order:
3802 * - validates the new addr and/or port
3803 * - checks if an update is required (new IP or port is different than current ones)
3804 * - checks the update is allowed:
3805 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3806 * - allow all changes if no CHECKS are configured
3807 * - if CHECK is configured:
3808 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3809 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3810 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003811 *
3812 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003813 */
3814const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3815{
3816 struct sockaddr_storage sa;
3817 int ret, port_change_required;
3818 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003819 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003820 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003821 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003822
3823 msg = get_trash_chunk();
3824 chunk_reset(msg);
3825
3826 if (addr) {
3827 memset(&sa, 0, sizeof(struct sockaddr_storage));
3828 if (str2ip2(addr, &sa, 0) == NULL) {
3829 chunk_printf(msg, "Invalid addr '%s'", addr);
3830 goto out;
3831 }
3832
3833 /* changes are allowed on AF_INET* families only */
3834 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3835 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3836 goto out;
3837 }
3838
3839 /* collecting data currently setup */
3840 memset(current_addr, '\0', sizeof(current_addr));
3841 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3842 /* changes are allowed on AF_INET* families only */
3843 if ((ret != AF_INET) && (ret != AF_INET6)) {
3844 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3845 goto out;
3846 }
3847
3848 /* applying ADDR changes if required and allowed
3849 * ipcmp returns 0 when both ADDR are the same
3850 */
3851 if (ipcmp(&s->addr, &sa) == 0) {
3852 chunk_appendf(msg, "no need to change the addr");
3853 goto port;
3854 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003855 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003856 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003857
3858 /* we also need to update check's ADDR only if it uses the server's one */
3859 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003860 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003861 }
3862
3863 /* we also need to update agent ADDR only if it use the server's one */
3864 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003865 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003866 }
3867
3868 /* update report for caller */
3869 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3870 }
3871
3872 port:
3873 if (port) {
3874 char sign = '\0';
3875 char *endptr;
3876
3877 if (addr)
3878 chunk_appendf(msg, ", ");
3879
3880 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003881 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003882
3883 /* check if PORT change is required */
3884 port_change_required = 0;
3885
3886 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003887 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003888 new_port = strtol(port, &endptr, 10);
3889 if ((errno != 0) || (port == endptr)) {
3890 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3891 goto out;
3892 }
3893
3894 /* check if caller triggers a port mapped or offset */
3895 if (sign == '-' || (sign == '+')) {
3896 /* check if server currently uses port map */
3897 if (!(s->flags & SRV_F_MAPPORTS)) {
3898 /* switch from fixed port to port map mandatorily triggers
3899 * a port change */
3900 port_change_required = 1;
3901 /* check is configured
3902 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3903 * prevent PORT change if check doesn't have it's dedicated port while switching
3904 * to port mapping */
3905 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3906 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.");
3907 goto out;
3908 }
3909 }
3910 /* we're already using port maps */
3911 else {
3912 port_change_required = current_port != new_port;
3913 }
3914 }
3915 /* fixed port */
3916 else {
3917 port_change_required = current_port != new_port;
3918 }
3919
3920 /* applying PORT changes if required and update response message */
3921 if (port_change_required) {
3922 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003923 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003924 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003925
3926 /* prepare message */
3927 chunk_appendf(msg, "port changed from '");
3928 if (s->flags & SRV_F_MAPPORTS)
3929 chunk_appendf(msg, "+");
3930 chunk_appendf(msg, "%d' to '", current_port);
3931
3932 if (sign == '-') {
3933 s->flags |= SRV_F_MAPPORTS;
3934 chunk_appendf(msg, "%c", sign);
3935 /* just use for result output */
3936 new_port = -new_port;
3937 }
3938 else if (sign == '+') {
3939 s->flags |= SRV_F_MAPPORTS;
3940 chunk_appendf(msg, "%c", sign);
3941 }
3942 else {
3943 s->flags &= ~SRV_F_MAPPORTS;
3944 }
3945
3946 chunk_appendf(msg, "%d'", new_port);
3947
3948 /* we also need to update health checks port only if it uses server's realport */
3949 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3950 s->check.port = new_port;
3951 }
3952 }
3953 else {
3954 chunk_appendf(msg, "no need to change the port");
3955 }
3956 }
3957
3958out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003959 if (changed)
3960 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003961 if (updater)
3962 chunk_appendf(msg, " by '%s'", updater);
3963 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003964 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003965}
3966
3967
3968/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003969 * update server status based on result of name resolution
3970 * returns:
3971 * 0 if server status is updated
3972 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003973 *
3974 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003975 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003976int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003977{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003978 struct dns_resolvers *resolvers = s->resolvers;
3979 struct dns_resolution *resolution = s->dns_requester->resolution;
3980 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003981
3982 switch (resolution->status) {
3983 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003984 /* status when HAProxy has just (re)started.
3985 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003986 break;
3987
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003988 case RSLV_STATUS_VALID:
3989 /*
3990 * resume health checks
3991 * server will be turned back on if health check is safe
3992 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003993 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003994 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003995 return 1;
3996 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3997 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003998 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003999 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02004000
Emeric Brun52a91d32017-08-31 14:41:55 +02004001 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004002 return 1;
4003 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
4004 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
4005 s->proxy->id, s->id);
4006
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004007 ha_warning("%s.\n", trash.area);
4008 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004009 return 0;
4010
4011 case RSLV_STATUS_NX:
4012 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004013 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
4014 if (!tick_is_expired(exp, now_ms))
4015 break;
4016
4017 if (s->next_admin & SRV_ADMF_RMAINT)
4018 return 1;
4019 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
4020 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004021
4022 case RSLV_STATUS_TIMEOUT:
4023 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004024 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
4025 if (!tick_is_expired(exp, now_ms))
4026 break;
4027
4028 if (s->next_admin & SRV_ADMF_RMAINT)
4029 return 1;
4030 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
4031 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004032
4033 case RSLV_STATUS_REFUSED:
4034 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004035 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
4036 if (!tick_is_expired(exp, now_ms))
4037 break;
4038
4039 if (s->next_admin & SRV_ADMF_RMAINT)
4040 return 1;
4041 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
4042 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004043
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004044 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004045 /* stop server if resolution failed for a long enough period */
4046 exp = tick_add(resolution->last_valid, resolvers->hold.other);
4047 if (!tick_is_expired(exp, now_ms))
4048 break;
4049
4050 if (s->next_admin & SRV_ADMF_RMAINT)
4051 return 1;
4052 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
4053 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004054 }
4055
4056 return 1;
4057}
4058
4059/*
4060 * Server Name Resolution valid response callback
4061 * It expects:
4062 * - <nameserver>: the name server which answered the valid response
4063 * - <response>: buffer containing a valid DNS response
4064 * - <response_len>: size of <response>
4065 * It performs the following actions:
4066 * - ignore response if current ip found and server family not met
4067 * - update with first new ip found if family is met and current IP is not found
4068 * returns:
4069 * 0 on error
4070 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01004071 *
4072 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004073 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004074int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004075{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004076 struct server *s = NULL;
4077 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004078 void *serverip, *firstip;
4079 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004080 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02004081 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004082 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004083
Christopher Faulet67957bd2017-09-27 11:00:59 +02004084 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004085 if (!s)
4086 return 1;
4087
Christopher Faulet67957bd2017-09-27 11:00:59 +02004088 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004089
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004090 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004091 firstip = NULL; /* pointer to the first valid response found */
4092 /* it will be used as the new IP if a change is required */
4093 firstip_sin_family = AF_UNSPEC;
4094 serverip = NULL; /* current server IP address */
4095
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004096 /* initializing server IP pointer */
4097 server_sin_family = s->addr.ss_family;
4098 switch (server_sin_family) {
4099 case AF_INET:
4100 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
4101 break;
4102
4103 case AF_INET6:
4104 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
4105 break;
4106
Willy Tarreau3acfcd12017-01-06 19:18:32 +01004107 case AF_UNSPEC:
4108 break;
4109
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004110 default:
4111 goto invalid;
4112 }
4113
Baptiste Assmann729c9012017-05-22 15:13:10 +02004114 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01004115 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004116 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004117
4118 switch (ret) {
4119 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004120 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004121
4122 case DNS_UPD_SRVIP_NOT_FOUND:
4123 goto save_ip;
4124
4125 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004126 goto invalid;
4127
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004128 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004129 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004130 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004131
Baptiste Assmannfad03182015-10-28 02:03:32 +01004132 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01004133 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004134 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004135 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01004136
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004137 default:
4138 goto invalid;
4139
4140 }
4141
4142 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004143 if (nameserver) {
4144 nameserver->counters.update++;
4145 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004146 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004147 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004148 else
4149 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004150 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004151
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004152 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004153 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004154 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004155
4156 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004157 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02004158 nameserver->counters.invalid++;
4159 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004160 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004161 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004162 return 0;
4163}
4164
4165/*
4166 * Server Name Resolution error management callback
4167 * returns:
4168 * 0 on error
4169 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004170 *
4171 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004172 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004173int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004174{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004175 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004176
Christopher Faulet67957bd2017-09-27 11:00:59 +02004177 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004178 if (!s)
4179 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004180 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004181 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004182 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004183 return 1;
4184}
4185
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004186/*
4187 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004188 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004189 * It returns a pointer to the first server found or NULL if <ip> is not yet
4190 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004191 *
4192 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004193 */
4194struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4195{
4196 struct server *tmpsrv;
4197 struct proxy *be;
4198
4199 if (!srv)
4200 return NULL;
4201
4202 be = srv->proxy;
4203 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004204 /* we found the current server is the same, ignore it */
4205 if (srv == tmpsrv)
4206 continue;
4207
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004208 /* We want to compare the IP in the record with the IP of the servers in the
4209 * same backend, only if:
4210 * * DNS resolution is enabled on the server
4211 * * the hostname used for the resolution by our server is the same than the
4212 * one used for the server found in the backend
4213 * * the server found in the backend is not our current server
4214 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004215 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004216 if ((tmpsrv->hostname_dn == NULL) ||
4217 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4218 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004219 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004220 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004221 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004222 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004223
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004224 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004225 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004226 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004227 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004228 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004229
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004230 /* At this point, we have 2 different servers using the same DNS hostname
4231 * for their respective resolution.
4232 */
4233 if (*ip_family == tmpsrv->addr.ss_family &&
4234 ((tmpsrv->addr.ss_family == AF_INET &&
4235 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4236 (tmpsrv->addr.ss_family == AF_INET6 &&
4237 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004238 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004239 return tmpsrv;
4240 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004241 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004242 }
4243
Emeric Brune9fd6b52017-11-02 17:20:39 +01004244
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004245 return NULL;
4246}
4247
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004248/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4249 * resolver. This is suited for initial address configuration. Returns 0 on
4250 * success otherwise a non-zero error code. In case of error, *err_code, if
4251 * not NULL, is filled up.
4252 */
4253int srv_set_addr_via_libc(struct server *srv, int *err_code)
4254{
4255 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004256 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004257 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004258 return 1;
4259 }
4260 return 0;
4261}
4262
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004263/* Set the server's FDQN (->hostname) from <hostname>.
4264 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004265 *
4266 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004267 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004268int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004269{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004270 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004271 char *hostname_dn;
4272 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004273
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004274 /* Note that the server lock is already held. */
4275 if (!srv->resolvers)
4276 return -1;
4277
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004278 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004279 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004280 /* run time DNS resolution was not active for this server
4281 * and we can't enable it at run time for now.
4282 */
4283 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004284 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004285
4286 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004287 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004288 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004289 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4290 hostname_dn, trash.size);
4291 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004292 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004293
Christopher Faulet67957bd2017-09-27 11:00:59 +02004294 resolution = srv->dns_requester->resolution;
4295 if (resolution &&
4296 resolution->hostname_dn &&
4297 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004298 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004299
Christopher Faulet67957bd2017-09-27 11:00:59 +02004300 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004301
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004302 free(srv->hostname);
4303 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004304 srv->hostname = strdup(hostname);
4305 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004306 srv->hostname_dn_len = hostname_dn_len;
4307 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004308 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004309
Baptiste Assmann13a92322019-06-07 09:40:55 +02004310 if (srv->flags & SRV_F_NO_RESOLUTION)
4311 goto end;
4312
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004313 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004314 goto err;
4315
4316 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004317 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004318 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004319 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004320
4321 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004322 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004323 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004324 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004325}
4326
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004327/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4328 * from the state file. This is suited for initial address configuration.
4329 * Returns 0 on success otherwise a non-zero error code. In case of error,
4330 * *err_code, if not NULL, is filled up.
4331 */
4332static int srv_apply_lastaddr(struct server *srv, int *err_code)
4333{
4334 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4335 if (err_code)
4336 *err_code |= ERR_WARN;
4337 return 1;
4338 }
4339 return 0;
4340}
4341
Willy Tarreau25e51522016-11-04 15:10:17 +01004342/* returns 0 if no error, otherwise a combination of ERR_* flags */
4343static int srv_iterate_initaddr(struct server *srv)
4344{
4345 int return_code = 0;
4346 int err_code;
4347 unsigned int methods;
4348
4349 methods = srv->init_addr_methods;
4350 if (!methods) { // default to "last,libc"
4351 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4352 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4353 }
4354
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004355 /* "-dr" : always append "none" so that server addresses resolution
4356 * failures are silently ignored, this is convenient to validate some
4357 * configs out of their environment.
4358 */
4359 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4360 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4361
Willy Tarreau25e51522016-11-04 15:10:17 +01004362 while (methods) {
4363 err_code = 0;
4364 switch (srv_get_next_initaddr(&methods)) {
4365 case SRV_IADDR_LAST:
4366 if (!srv->lastaddr)
4367 continue;
4368 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004369 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004370 return_code |= err_code;
4371 break;
4372
4373 case SRV_IADDR_LIBC:
4374 if (!srv->hostname)
4375 continue;
4376 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004377 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004378 return_code |= err_code;
4379 break;
4380
Willy Tarreau37ebe122016-11-04 15:17:58 +01004381 case SRV_IADDR_NONE:
4382 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004383 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004384 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4385 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004386 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004387 return return_code;
4388
Willy Tarreau4310d362016-11-02 15:05:56 +01004389 case SRV_IADDR_IP:
4390 ipcpy(&srv->init_addr, &srv->addr);
4391 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004392 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4393 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004394 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004395 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004396
Willy Tarreau25e51522016-11-04 15:10:17 +01004397 default: /* unhandled method */
4398 break;
4399 }
4400 }
4401
4402 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004403 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004404 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4405 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004406 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004407 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004408 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4409 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004410
4411 return_code |= ERR_ALERT | ERR_FATAL;
4412 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004413out:
4414 srv_set_dyncookie(srv);
4415 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004416}
4417
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004418/*
4419 * This function parses all backends and all servers within each backend
4420 * and performs servers' addr resolution based on information provided by:
4421 * - configuration file
4422 * - server-state file (states provided by an 'old' haproxy process)
4423 *
4424 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4425 */
4426int srv_init_addr(void)
4427{
4428 struct proxy *curproxy;
4429 int return_code = 0;
4430
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004431 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004432 while (curproxy) {
4433 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004434
4435 /* servers are in backend only */
4436 if (!(curproxy->cap & PR_CAP_BE))
4437 goto srv_init_addr_next;
4438
Willy Tarreau25e51522016-11-04 15:10:17 +01004439 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004440 if (srv->hostname)
4441 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004442
4443 srv_init_addr_next:
4444 curproxy = curproxy->next;
4445 }
4446
4447 return return_code;
4448}
4449
Willy Tarreau46b7f532018-08-21 11:54:26 +02004450/*
4451 * Must be called with the server lock held.
4452 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004453const 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 +02004454{
4455
Willy Tarreau83061a82018-07-13 11:56:34 +02004456 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004457
4458 msg = get_trash_chunk();
4459 chunk_reset(msg);
4460
Olivier Houchard8da5f982017-08-04 18:35:36 +02004461 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004462 chunk_appendf(msg, "no need to change the FDQN");
4463 goto out;
4464 }
4465
4466 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4467 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4468 goto out;
4469 }
4470
4471 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4472 server->proxy->id, server->id, server->hostname, fqdn);
4473
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004474 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004475 chunk_reset(msg);
4476 chunk_appendf(msg, "could not update %s/%s FQDN",
4477 server->proxy->id, server->id);
4478 goto out;
4479 }
4480
4481 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004482 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004483
4484 out:
4485 if (updater)
4486 chunk_appendf(msg, " by '%s'", updater);
4487 chunk_appendf(msg, "\n");
4488
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004489 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004490}
4491
4492
Willy Tarreau21b069d2016-11-23 17:15:08 +01004493/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4494 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004495 * 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 +01004496 * used for CLI commands requiring a server name.
4497 * Important: the <arg> is modified to remove the '/'.
4498 */
4499struct server *cli_find_server(struct appctx *appctx, char *arg)
4500{
4501 struct proxy *px;
4502 struct server *sv;
4503 char *line;
4504
4505 /* split "backend/server" and make <line> point to server */
4506 for (line = arg; *line; line++)
4507 if (*line == '/') {
4508 *line++ = '\0';
4509 break;
4510 }
4511
4512 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004513 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004514 return NULL;
4515 }
4516
4517 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004518 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004519 return NULL;
4520 }
4521
4522 if (px->state == PR_STSTOPPED) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004523 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004524 return NULL;
4525 }
4526
4527 return sv;
4528}
4529
William Lallemand222baf22016-11-19 02:00:33 +01004530
Willy Tarreau46b7f532018-08-21 11:54:26 +02004531/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004532static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004533{
4534 struct server *sv;
4535 const char *warning;
4536
4537 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4538 return 1;
4539
4540 sv = cli_find_server(appctx, args[2]);
4541 if (!sv)
4542 return 1;
4543
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004544 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004545
William Lallemand222baf22016-11-19 02:00:33 +01004546 if (strcmp(args[3], "weight") == 0) {
4547 warning = server_parse_weight_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004548 if (warning)
4549 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004550 }
4551 else if (strcmp(args[3], "state") == 0) {
4552 if (strcmp(args[4], "ready") == 0)
4553 srv_adm_set_ready(sv);
4554 else if (strcmp(args[4], "drain") == 0)
4555 srv_adm_set_drain(sv);
4556 else if (strcmp(args[4], "maint") == 0)
4557 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004558 else
4559 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004560 }
4561 else if (strcmp(args[3], "health") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004562 if (sv->track)
4563 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004564 else if (strcmp(args[4], "up") == 0) {
4565 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004566 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004567 }
4568 else if (strcmp(args[4], "stopping") == 0) {
4569 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004570 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004571 }
4572 else if (strcmp(args[4], "down") == 0) {
4573 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004574 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004575 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004576 else
4577 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004578 }
4579 else if (strcmp(args[3], "agent") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004580 if (!(sv->agent.state & CHK_ST_ENABLED))
4581 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004582 else if (strcmp(args[4], "up") == 0) {
4583 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004584 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004585 }
4586 else if (strcmp(args[4], "down") == 0) {
4587 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004588 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004589 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004590 else
4591 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004592 }
Misiek2da082d2017-01-09 09:40:42 +01004593 else if (strcmp(args[3], "agent-addr") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004594 if (!(sv->agent.state & CHK_ST_ENABLED))
4595 cli_err(appctx, "agent checks are not enabled on this server.\n");
4596 else if (str2ip(args[4], &sv->agent.addr) == NULL)
4597 cli_err(appctx, "incorrect addr address given for agent.\n");
Misiek2da082d2017-01-09 09:40:42 +01004598 }
4599 else if (strcmp(args[3], "agent-send") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004600 if (!(sv->agent.state & CHK_ST_ENABLED))
4601 cli_err(appctx, "agent checks are not enabled on this server.\n");
4602 else {
Misiek2da082d2017-01-09 09:40:42 +01004603 char *nss = strdup(args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004604 if (!nss)
4605 cli_err(appctx, "cannot allocate memory for new string.\n");
4606 else {
Misiek2da082d2017-01-09 09:40:42 +01004607 free(sv->agent.send_string);
4608 sv->agent.send_string = nss;
4609 sv->agent.send_string_len = strlen(args[4]);
4610 }
4611 }
4612 }
William Lallemand222baf22016-11-19 02:00:33 +01004613 else if (strcmp(args[3], "check-port") == 0) {
4614 int i = 0;
4615 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004616 cli_err(appctx, "'set server <srv> check-port' expects an integer as argument.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004617 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004618 }
4619 if ((i < 0) || (i > 65535)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004620 cli_err(appctx, "provided port is not valid.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004621 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004622 }
4623 /* prevent the update of port to 0 if MAPPORTS are in use */
4624 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004625 cli_err(appctx, "can't unset 'port' since MAPPORTS is in use.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004626 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004627 }
4628 sv->check.port = i;
Willy Tarreau9d008692019-08-09 11:21:01 +02004629 cli_msg(appctx, LOG_NOTICE, "health check port updated.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004630 }
4631 else if (strcmp(args[3], "addr") == 0) {
4632 char *addr = NULL;
4633 char *port = NULL;
4634 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004635 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004636 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004637 }
4638 else {
4639 addr = args[4];
4640 }
4641 if (strcmp(args[5], "port") == 0) {
4642 port = args[6];
4643 }
4644 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004645 if (warning)
4646 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004647 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4648 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004649 else if (strcmp(args[3], "fqdn") == 0) {
4650 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004651 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004652 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004653 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02004654 /* ensure runtime resolver will process this new fqdn */
4655 if (sv->flags & SRV_F_NO_RESOLUTION) {
4656 sv->flags &= ~SRV_F_NO_RESOLUTION;
4657 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004658 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Willy Tarreau9d008692019-08-09 11:21:01 +02004659 if (warning)
4660 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004661 }
William Lallemand222baf22016-11-19 02:00:33 +01004662 else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004663 cli_err(appctx,
4664 "'set server <srv>' only supports 'agent', 'health', 'state',"
4665 " 'weight', 'addr', 'fqdn' and 'check-port'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004666 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004667 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004668 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004669 return 1;
4670}
4671
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004672static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004673{
4674 struct stream_interface *si = appctx->owner;
4675 struct proxy *px;
4676 struct server *sv;
4677 char *line;
4678
4679
4680 /* split "backend/server" and make <line> point to server */
4681 for (line = args[2]; *line; line++)
4682 if (*line == '/') {
4683 *line++ = '\0';
4684 break;
4685 }
4686
Willy Tarreau9d008692019-08-09 11:21:01 +02004687 if (!*line)
4688 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004689
Willy Tarreau9d008692019-08-09 11:21:01 +02004690 if (!get_backend_server(args[2], line, &px, &sv))
4691 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004692
4693 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004694 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4695 sv->iweight);
4696 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004697 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004698 return 0;
4699 }
William Lallemand6b160942016-11-22 12:34:35 +01004700 return 1;
4701}
4702
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004703/* Parse a "set weight" command.
4704 *
4705 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004706 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004707static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004708{
4709 struct server *sv;
4710 const char *warning;
4711
4712 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4713 return 1;
4714
4715 sv = cli_find_server(appctx, args[2]);
4716 if (!sv)
4717 return 1;
4718
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004719 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4720
William Lallemand6b160942016-11-22 12:34:35 +01004721 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004722 if (warning)
4723 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004724
4725 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4726
William Lallemand6b160942016-11-22 12:34:35 +01004727 return 1;
4728}
4729
Willy Tarreau46b7f532018-08-21 11:54:26 +02004730/* parse a "set maxconn server" command. It always returns 1.
4731 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004732 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004733 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004734static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004735{
4736 struct server *sv;
4737 const char *warning;
4738
4739 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4740 return 1;
4741
4742 sv = cli_find_server(appctx, args[3]);
4743 if (!sv)
4744 return 1;
4745
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004746 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4747
Willy Tarreaub8026272016-11-23 11:26:56 +01004748 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004749 if (warning)
4750 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004751
4752 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4753
Willy Tarreaub8026272016-11-23 11:26:56 +01004754 return 1;
4755}
William Lallemand6b160942016-11-22 12:34:35 +01004756
Willy Tarreau46b7f532018-08-21 11:54:26 +02004757/* parse a "disable agent" command. It always returns 1.
4758 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004759 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004760 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004761static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004762{
4763 struct server *sv;
4764
4765 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4766 return 1;
4767
4768 sv = cli_find_server(appctx, args[2]);
4769 if (!sv)
4770 return 1;
4771
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004772 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004773 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004774 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004775 return 1;
4776}
4777
Willy Tarreau46b7f532018-08-21 11:54:26 +02004778/* parse a "disable health" command. It always returns 1.
4779 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004780 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004781 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004782static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004783{
4784 struct server *sv;
4785
4786 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4787 return 1;
4788
4789 sv = cli_find_server(appctx, args[2]);
4790 if (!sv)
4791 return 1;
4792
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004793 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004794 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004795 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004796 return 1;
4797}
4798
Willy Tarreau46b7f532018-08-21 11:54:26 +02004799/* parse a "disable server" command. It always returns 1.
4800 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004801 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004802 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004803static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004804{
4805 struct server *sv;
4806
4807 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4808 return 1;
4809
4810 sv = cli_find_server(appctx, args[2]);
4811 if (!sv)
4812 return 1;
4813
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004814 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004815 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004816 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004817 return 1;
4818}
4819
Willy Tarreau46b7f532018-08-21 11:54:26 +02004820/* parse a "enable agent" command. It always returns 1.
4821 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004822 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004823 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004824static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004825{
4826 struct server *sv;
4827
4828 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4829 return 1;
4830
4831 sv = cli_find_server(appctx, args[2]);
4832 if (!sv)
4833 return 1;
4834
Willy Tarreau9d008692019-08-09 11:21:01 +02004835 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4836 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004837
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004838 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004839 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004840 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004841 return 1;
4842}
4843
Willy Tarreau46b7f532018-08-21 11:54:26 +02004844/* parse a "enable health" command. It always returns 1.
4845 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004846 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004847 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004848static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004849{
4850 struct server *sv;
4851
4852 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4853 return 1;
4854
4855 sv = cli_find_server(appctx, args[2]);
4856 if (!sv)
4857 return 1;
4858
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004859 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004860 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004861 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004862 return 1;
4863}
4864
Willy Tarreau46b7f532018-08-21 11:54:26 +02004865/* parse a "enable server" command. It always returns 1.
4866 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004867 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004868 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004869static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004870{
4871 struct server *sv;
4872
4873 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4874 return 1;
4875
4876 sv = cli_find_server(appctx, args[2]);
4877 if (!sv)
4878 return 1;
4879
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004880 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004881 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004882 if (!(sv->flags & SRV_F_COOKIESET)
4883 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4884 sv->cookie)
4885 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004886 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004887 return 1;
4888}
4889
William Lallemand222baf22016-11-19 02:00:33 +01004890/* register cli keywords */
4891static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004892 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004893 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004894 { { "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 +01004895 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004896 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004897 { { "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 +01004898 { { "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 +01004899 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004900 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4901 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4902
William Lallemand222baf22016-11-19 02:00:33 +01004903 {{},}
4904}};
4905
Willy Tarreau0108d902018-11-25 19:14:37 +01004906INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004907
Emeric Brun64cc49c2017-10-03 14:46:45 +02004908/*
4909 * This function applies server's status changes, it is
4910 * is designed to be called asynchronously.
4911 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004912 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004913 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004914static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004915{
4916 struct check *check = &s->check;
4917 int xferred;
4918 struct proxy *px = s->proxy;
4919 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4920 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4921 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004922 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004923
Emeric Brun64cc49c2017-10-03 14:46:45 +02004924 /* If currently main is not set we try to apply pending state changes */
4925 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4926 int next_admin;
4927
4928 /* Backup next admin */
4929 next_admin = s->next_admin;
4930
4931 /* restore current admin state */
4932 s->next_admin = s->cur_admin;
4933
4934 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4935 s->last_change = now.tv_sec;
4936 if (s->proxy->lbprm.set_server_status_down)
4937 s->proxy->lbprm.set_server_status_down(s);
4938
4939 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4940 srv_shutdown_streams(s, SF_ERR_DOWN);
4941
4942 /* we might have streams queued on this server and waiting for
4943 * a connection. Those which are redispatchable will be queued
4944 * to another server or to the proxy itself.
4945 */
4946 xferred = pendconn_redistribute(s);
4947
4948 tmptrash = alloc_trash_chunk();
4949 if (tmptrash) {
4950 chunk_printf(tmptrash,
4951 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4952 s->proxy->id, s->id);
4953
Emeric Brun5a133512017-10-19 14:42:30 +02004954 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004955 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004956
4957 /* we don't send an alert if the server was previously paused */
4958 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004959 send_log(s->proxy, log_level, "%s.\n",
4960 tmptrash->area);
4961 send_email_alert(s, log_level, "%s",
4962 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004963 free_trash_chunk(tmptrash);
4964 tmptrash = NULL;
4965 }
4966 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4967 set_backend_down(s->proxy);
4968
4969 s->counters.down_trans++;
4970 }
4971 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4972 s->last_change = now.tv_sec;
4973 if (s->proxy->lbprm.set_server_status_down)
4974 s->proxy->lbprm.set_server_status_down(s);
4975
4976 /* we might have streams queued on this server and waiting for
4977 * a connection. Those which are redispatchable will be queued
4978 * to another server or to the proxy itself.
4979 */
4980 xferred = pendconn_redistribute(s);
4981
4982 tmptrash = alloc_trash_chunk();
4983 if (tmptrash) {
4984 chunk_printf(tmptrash,
4985 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4986 s->proxy->id, s->id);
4987
Emeric Brun5a133512017-10-19 14:42:30 +02004988 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004989
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004990 ha_warning("%s.\n", tmptrash->area);
4991 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4992 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004993 free_trash_chunk(tmptrash);
4994 tmptrash = NULL;
4995 }
4996
4997 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4998 set_backend_down(s->proxy);
4999 }
5000 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5001 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
5002 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5003 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5004 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5005 s->proxy->last_change = now.tv_sec;
5006 }
5007
5008 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
5009 s->down_time += now.tv_sec - s->last_change;
5010
5011 s->last_change = now.tv_sec;
5012 if (s->next_state == SRV_ST_STARTING)
5013 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5014
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005015 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005016 /* now propagate the status change to any LB algorithms */
5017 if (px->lbprm.update_server_eweight)
5018 px->lbprm.update_server_eweight(s);
5019 else if (srv_willbe_usable(s)) {
5020 if (px->lbprm.set_server_status_up)
5021 px->lbprm.set_server_status_up(s);
5022 }
5023 else {
5024 if (px->lbprm.set_server_status_down)
5025 px->lbprm.set_server_status_down(s);
5026 }
5027
5028 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5029 * and it's not a backup server and its effective weight is > 0,
5030 * then it can accept new connections, so we shut down all streams
5031 * on all backup servers.
5032 */
5033 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5034 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5035 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5036
5037 /* check if we can handle some connections queued at the proxy. We
5038 * will take as many as we can handle.
5039 */
5040 xferred = pendconn_grab_from_px(s);
5041
5042 tmptrash = alloc_trash_chunk();
5043 if (tmptrash) {
5044 chunk_printf(tmptrash,
5045 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5046 s->proxy->id, s->id);
5047
Emeric Brun5a133512017-10-19 14:42:30 +02005048 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005049 ha_warning("%s.\n", tmptrash->area);
5050 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5051 tmptrash->area);
5052 send_email_alert(s, LOG_NOTICE, "%s",
5053 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005054 free_trash_chunk(tmptrash);
5055 tmptrash = NULL;
5056 }
5057
5058 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5059 set_backend_down(s->proxy);
5060 }
5061 else if (s->cur_eweight != s->next_eweight) {
5062 /* now propagate the status change to any LB algorithms */
5063 if (px->lbprm.update_server_eweight)
5064 px->lbprm.update_server_eweight(s);
5065 else if (srv_willbe_usable(s)) {
5066 if (px->lbprm.set_server_status_up)
5067 px->lbprm.set_server_status_up(s);
5068 }
5069 else {
5070 if (px->lbprm.set_server_status_down)
5071 px->lbprm.set_server_status_down(s);
5072 }
5073
5074 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5075 set_backend_down(s->proxy);
5076 }
5077
5078 s->next_admin = next_admin;
5079 }
5080
Emeric Brun5a133512017-10-19 14:42:30 +02005081 /* reset operational state change */
5082 *s->op_st_chg.reason = 0;
5083 s->op_st_chg.status = s->op_st_chg.code = -1;
5084 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005085
5086 /* Now we try to apply pending admin changes */
5087
5088 /* Maintenance must also disable health checks */
5089 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5090 if (s->check.state & CHK_ST_ENABLED) {
5091 s->check.state |= CHK_ST_PAUSED;
5092 check->health = 0;
5093 }
5094
5095 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005096 tmptrash = alloc_trash_chunk();
5097 if (tmptrash) {
5098 chunk_printf(tmptrash,
5099 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5100 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5101 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005102
Olivier Houchard796a2b32017-10-24 17:42:47 +02005103 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005104
Olivier Houchard796a2b32017-10-24 17:42:47 +02005105 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005106 ha_warning("%s.\n", tmptrash->area);
5107 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5108 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005109 }
5110 free_trash_chunk(tmptrash);
5111 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005112 }
Emeric Brun8f298292017-12-06 16:47:17 +01005113 /* commit new admin status */
5114
5115 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005116 }
5117 else { /* server was still running */
5118 check->health = 0; /* failure */
5119 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005120
5121 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005122 if (s->proxy->lbprm.set_server_status_down)
5123 s->proxy->lbprm.set_server_status_down(s);
5124
Emeric Brun64cc49c2017-10-03 14:46:45 +02005125 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5126 srv_shutdown_streams(s, SF_ERR_DOWN);
5127
5128 /* we might have streams queued on this server and waiting for
5129 * a connection. Those which are redispatchable will be queued
5130 * to another server or to the proxy itself.
5131 */
5132 xferred = pendconn_redistribute(s);
5133
5134 tmptrash = alloc_trash_chunk();
5135 if (tmptrash) {
5136 chunk_printf(tmptrash,
5137 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5138 s->flags & SRV_F_BACKUP ? "Backup " : "",
5139 s->proxy->id, s->id,
5140 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5141
5142 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5143
5144 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005145 ha_warning("%s.\n", tmptrash->area);
5146 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5147 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005148 }
5149 free_trash_chunk(tmptrash);
5150 tmptrash = NULL;
5151 }
5152 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5153 set_backend_down(s->proxy);
5154
5155 s->counters.down_trans++;
5156 }
5157 }
5158 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5159 /* OK here we're leaving maintenance, we have many things to check,
5160 * because the server might possibly be coming back up depending on
5161 * its state. In practice, leaving maintenance means that we should
5162 * immediately turn to UP (more or less the slowstart) under the
5163 * following conditions :
5164 * - server is neither checked nor tracked
5165 * - server tracks another server which is not checked
5166 * - server tracks another server which is already up
5167 * Which sums up as something simpler :
5168 * "either the tracking server is up or the server's checks are disabled
5169 * or up". Otherwise we only re-enable health checks. There's a special
5170 * case associated to the stopping state which can be inherited. Note
5171 * that the server might still be in drain mode, which is naturally dealt
5172 * with by the lower level functions.
5173 */
5174
5175 if (s->check.state & CHK_ST_ENABLED) {
5176 s->check.state &= ~CHK_ST_PAUSED;
5177 check->health = check->rise; /* start OK but check immediately */
5178 }
5179
5180 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5181 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5182 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5183 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5184 s->next_state = SRV_ST_STOPPING;
5185 }
5186 else {
5187 s->next_state = SRV_ST_STARTING;
5188 if (s->slowstart > 0)
5189 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5190 else
5191 s->next_state = SRV_ST_RUNNING;
5192 }
5193
5194 }
5195
5196 tmptrash = alloc_trash_chunk();
5197 if (tmptrash) {
5198 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5199 chunk_printf(tmptrash,
5200 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5201 s->flags & SRV_F_BACKUP ? "Backup " : "",
5202 s->proxy->id, s->id,
5203 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5204 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5205 }
5206 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5207 chunk_printf(tmptrash,
5208 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5209 s->flags & SRV_F_BACKUP ? "Backup " : "",
5210 s->proxy->id, s->id, s->hostname,
5211 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5212 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5213 }
5214 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5215 chunk_printf(tmptrash,
5216 "%sServer %s/%s is %s/%s (leaving maintenance)",
5217 s->flags & SRV_F_BACKUP ? "Backup " : "",
5218 s->proxy->id, s->id,
5219 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5220 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5221 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005222 ha_warning("%s.\n", tmptrash->area);
5223 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5224 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005225 free_trash_chunk(tmptrash);
5226 tmptrash = NULL;
5227 }
5228
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005229 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005230 /* now propagate the status change to any LB algorithms */
5231 if (px->lbprm.update_server_eweight)
5232 px->lbprm.update_server_eweight(s);
5233 else if (srv_willbe_usable(s)) {
5234 if (px->lbprm.set_server_status_up)
5235 px->lbprm.set_server_status_up(s);
5236 }
5237 else {
5238 if (px->lbprm.set_server_status_down)
5239 px->lbprm.set_server_status_down(s);
5240 }
5241
5242 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5243 set_backend_down(s->proxy);
5244
Willy Tarreau6a78e612018-08-07 10:14:53 +02005245 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5246 * and it's not a backup server and its effective weight is > 0,
5247 * then it can accept new connections, so we shut down all streams
5248 * on all backup servers.
5249 */
5250 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5251 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5252 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5253
5254 /* check if we can handle some connections queued at the proxy. We
5255 * will take as many as we can handle.
5256 */
5257 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005258 }
5259 else if (s->next_admin & SRV_ADMF_MAINT) {
5260 /* remaining in maintenance mode, let's inform precisely about the
5261 * situation.
5262 */
5263 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5264 tmptrash = alloc_trash_chunk();
5265 if (tmptrash) {
5266 chunk_printf(tmptrash,
5267 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5268 s->flags & SRV_F_BACKUP ? "Backup " : "",
5269 s->proxy->id, s->id);
5270
5271 if (s->track) /* normally it's mandatory here */
5272 chunk_appendf(tmptrash, " via %s/%s",
5273 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005274 ha_warning("%s.\n", tmptrash->area);
5275 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5276 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005277 free_trash_chunk(tmptrash);
5278 tmptrash = NULL;
5279 }
5280 }
5281 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5282 tmptrash = alloc_trash_chunk();
5283 if (tmptrash) {
5284 chunk_printf(tmptrash,
5285 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5286 s->flags & SRV_F_BACKUP ? "Backup " : "",
5287 s->proxy->id, s->id, s->hostname);
5288
5289 if (s->track) /* normally it's mandatory here */
5290 chunk_appendf(tmptrash, " via %s/%s",
5291 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005292 ha_warning("%s.\n", tmptrash->area);
5293 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5294 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005295 free_trash_chunk(tmptrash);
5296 tmptrash = NULL;
5297 }
5298 }
5299 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5300 tmptrash = alloc_trash_chunk();
5301 if (tmptrash) {
5302 chunk_printf(tmptrash,
5303 "%sServer %s/%s remains in forced maintenance",
5304 s->flags & SRV_F_BACKUP ? "Backup " : "",
5305 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005306 ha_warning("%s.\n", tmptrash->area);
5307 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5308 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005309 free_trash_chunk(tmptrash);
5310 tmptrash = NULL;
5311 }
5312 }
5313 /* don't report anything when leaving drain mode and remaining in maintenance */
5314
5315 s->cur_admin = s->next_admin;
5316 }
5317
5318 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5319 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5320 /* drain state is applied only if not yet in maint */
5321
5322 s->last_change = now.tv_sec;
5323 if (px->lbprm.set_server_status_down)
5324 px->lbprm.set_server_status_down(s);
5325
5326 /* we might have streams queued on this server and waiting for
5327 * a connection. Those which are redispatchable will be queued
5328 * to another server or to the proxy itself.
5329 */
5330 xferred = pendconn_redistribute(s);
5331
5332 tmptrash = alloc_trash_chunk();
5333 if (tmptrash) {
5334 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5335 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5336 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5337
5338 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5339
5340 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005341 ha_warning("%s.\n", tmptrash->area);
5342 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5343 tmptrash->area);
5344 send_email_alert(s, LOG_NOTICE, "%s",
5345 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005346 }
5347 free_trash_chunk(tmptrash);
5348 tmptrash = NULL;
5349 }
5350
5351 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5352 set_backend_down(s->proxy);
5353 }
5354 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5355 /* OK completely leaving drain mode */
5356 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5357 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5358 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5359 s->proxy->last_change = now.tv_sec;
5360 }
5361
5362 if (s->last_change < now.tv_sec) // ignore negative times
5363 s->down_time += now.tv_sec - s->last_change;
5364 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005365 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005366
5367 tmptrash = alloc_trash_chunk();
5368 if (tmptrash) {
5369 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5370 chunk_printf(tmptrash,
5371 "%sServer %s/%s is %s (leaving forced drain)",
5372 s->flags & SRV_F_BACKUP ? "Backup " : "",
5373 s->proxy->id, s->id,
5374 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5375 }
5376 else {
5377 chunk_printf(tmptrash,
5378 "%sServer %s/%s is %s (leaving drain)",
5379 s->flags & SRV_F_BACKUP ? "Backup " : "",
5380 s->proxy->id, s->id,
5381 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5382 if (s->track) /* normally it's mandatory here */
5383 chunk_appendf(tmptrash, " via %s/%s",
5384 s->track->proxy->id, s->track->id);
5385 }
5386
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005387 ha_warning("%s.\n", tmptrash->area);
5388 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5389 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005390 free_trash_chunk(tmptrash);
5391 tmptrash = NULL;
5392 }
5393
5394 /* now propagate the status change to any LB algorithms */
5395 if (px->lbprm.update_server_eweight)
5396 px->lbprm.update_server_eweight(s);
5397 else if (srv_willbe_usable(s)) {
5398 if (px->lbprm.set_server_status_up)
5399 px->lbprm.set_server_status_up(s);
5400 }
5401 else {
5402 if (px->lbprm.set_server_status_down)
5403 px->lbprm.set_server_status_down(s);
5404 }
5405 }
5406 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5407 /* remaining in drain mode after removing one of its flags */
5408
5409 tmptrash = alloc_trash_chunk();
5410 if (tmptrash) {
5411 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5412 chunk_printf(tmptrash,
5413 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5414 s->flags & SRV_F_BACKUP ? "Backup " : "",
5415 s->proxy->id, s->id);
5416
5417 if (s->track) /* normally it's mandatory here */
5418 chunk_appendf(tmptrash, " via %s/%s",
5419 s->track->proxy->id, s->track->id);
5420 }
5421 else {
5422 chunk_printf(tmptrash,
5423 "%sServer %s/%s remains in forced drain mode",
5424 s->flags & SRV_F_BACKUP ? "Backup " : "",
5425 s->proxy->id, s->id);
5426 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005427 ha_warning("%s.\n", tmptrash->area);
5428 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5429 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005430 free_trash_chunk(tmptrash);
5431 tmptrash = NULL;
5432 }
5433
5434 /* commit new admin status */
5435
5436 s->cur_admin = s->next_admin;
5437 }
5438 }
5439
5440 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005441 *s->adm_st_chg_cause = 0;
5442}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005443
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005444struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5445{
5446 struct connection *conn;
5447
Olivier Houchard859dc802019-08-08 15:47:21 +02005448 while ((conn = MT_LIST_POP(&toremove_connections[tid],
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005449 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005450 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005451 }
5452
5453 return task;
5454}
5455
Willy Tarreau980855b2019-02-07 14:59:29 +01005456struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005457{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005458 struct server *srv;
5459 struct eb32_node *eb;
5460 int i;
5461 unsigned int next_wakeup;
5462 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005463
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005464 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5465 while (1) {
5466 int srv_is_empty = 1;
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005467 int exceed_conns;
5468 int to_kill;
5469 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005470
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005471 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5472 if (!eb) {
5473 /* we might have reached the end of the tree, typically because
5474 * <now_ms> is in the first half and we're first scanning the last
5475 * half. Let's loop back to the beginning of the tree now.
5476 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005477
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005478 eb = eb32_first(&idle_conn_srv);
5479 if (likely(!eb))
5480 break;
5481 }
5482 if (tick_is_lt(now_ms, eb->key)) {
5483 /* timer not expired yet, revisit it later */
5484 next_wakeup = eb->key;
5485 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005486 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005487 }
5488 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005489
5490 /* Calculate how many idle connections we want to kill :
5491 * we want to remove half the difference between the total
5492 * of established connections (used or idle) and the max
5493 * number of used connections.
5494 */
5495 curr_idle = srv->curr_idle_conns;
5496 if (curr_idle == 0)
5497 goto remove;
5498 exceed_conns = srv->curr_used_conns + curr_idle -
5499 srv->max_used_conns;
5500 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
5501 srv->max_used_conns = srv->curr_used_conns;
5502
5503 for (i = 0; i < global.nbthread && to_kill > 0; i++) {
5504 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005505 int j;
5506 int did_remove = 0;
5507
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005508 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
5509 curr_idle + 1;
Olivier Houchard4be71902019-07-11 15:49:00 +02005510 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005511 for (j = 0; j < max_conn; j++) {
Olivier Houcharddc2f2752020-02-13 19:12:07 +01005512 struct connection *conn = MT_LIST_POP(&srv->idle_conns[i], struct connection *, list);
5513 if (!conn)
5514 conn = MT_LIST_POP(&srv->safe_conns[i],
5515 struct connection *, list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005516 if (!conn)
5517 break;
5518 did_remove = 1;
Olivier Houchard859dc802019-08-08 15:47:21 +02005519 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005520 }
Olivier Houchard4be71902019-07-11 15:49:00 +02005521 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005522 if (did_remove && max_conn < srv->curr_idle_thr[i])
5523 srv_is_empty = 0;
5524 if (did_remove)
5525 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5526 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005527remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005528 eb32_delete(&srv->idle_node);
5529 if (!srv_is_empty) {
5530 /* There are still more idle connections, add the
5531 * server back in the tree.
5532 */
5533 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5534 now_ms);
5535 eb32_insert(&idle_conn_srv, &srv->idle_node);
5536 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005537 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005538 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5539
5540 if (need_wakeup)
5541 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005542 else
5543 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005544
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005545 return task;
5546}
Olivier Houchard88698d92019-04-16 19:07:22 +02005547
5548/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5549static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5550 struct proxy *defpx, const char *file, int line,
5551 char **err)
5552{
5553 int arg = -1;
5554
5555 if (too_many_args(1, args, err, NULL))
5556 return -1;
5557
5558 if (*(args[1]) != 0)
5559 arg = atoi(args[1]);
5560
5561 if (arg < 0 || arg > 100) {
5562 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5563 return -1;
5564 }
5565
5566 if (args[0][10] == 'h')
5567 global.tune.pool_high_ratio = arg;
5568 else
5569 global.tune.pool_low_ratio = arg;
5570 return 0;
5571}
5572
5573/* config keyword parsers */
5574static struct cfg_kw_list cfg_kws = {ILH, {
5575 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5576 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5577 { 0, NULL, NULL }
5578}};
5579
5580INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5581
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005582/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005583 * Local variables:
5584 * c-indent-level: 8
5585 * c-basic-offset: 8
5586 * End:
5587 */