blob: 718ca1c4003fc571e07061d6894ab0eb40c87058 [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 */
78struct eb_root state_file = EB_ROOT;
79
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;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100659 } else
660 goto fail;
661 }
662 return 0;
663 fail:
664 if (err)
665 memprintf(err, "'%s' : proxy v2 option not implemented", p);
666 return ERR_ALERT | ERR_FATAL;
667}
668
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100669/* Parse the "observe" server keyword */
670static int srv_parse_observe(char **args, int *cur_arg,
671 struct proxy *curproxy, struct server *newsrv, char **err)
672{
673 char *arg;
674
675 arg = args[*cur_arg + 1];
676 if (!*arg) {
677 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
678 return ERR_ALERT | ERR_FATAL;
679 }
680
681 if (!strcmp(arg, "none")) {
682 newsrv->observe = HANA_OBS_NONE;
683 }
684 else if (!strcmp(arg, "layer4")) {
685 newsrv->observe = HANA_OBS_LAYER4;
686 }
687 else if (!strcmp(arg, "layer7")) {
688 if (curproxy->mode != PR_MODE_HTTP) {
689 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
690 return ERR_ALERT;
691 }
692 newsrv->observe = HANA_OBS_LAYER7;
693 }
694 else {
695 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
696 "but got '%s'\n", args[*cur_arg], arg);
697 return ERR_ALERT | ERR_FATAL;
698 }
699
700 return 0;
701}
702
Frédéric Lécaille16186232017-03-14 16:42:49 +0100703/* Parse the "redir" server keyword */
704static int srv_parse_redir(char **args, int *cur_arg,
705 struct proxy *curproxy, struct server *newsrv, char **err)
706{
707 char *arg;
708
709 arg = args[*cur_arg + 1];
710 if (!*arg) {
711 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
712 return ERR_ALERT | ERR_FATAL;
713 }
714
715 free(newsrv->rdr_pfx);
716 newsrv->rdr_pfx = strdup(arg);
717 newsrv->rdr_len = strlen(arg);
718
719 return 0;
720}
721
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100722/* Parse the "send-proxy" server keyword */
723static int srv_parse_send_proxy(char **args, int *cur_arg,
724 struct proxy *curproxy, struct server *newsrv, char **err)
725{
726 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
727}
728
729/* Parse the "send-proxy-v2" server keyword */
730static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
731 struct proxy *curproxy, struct server *newsrv, char **err)
732{
733 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
734}
735
Frédéric Lécailledba97072017-03-17 15:33:50 +0100736
737/* Parse the "source" server keyword */
738static int srv_parse_source(char **args, int *cur_arg,
739 struct proxy *curproxy, struct server *newsrv, char **err)
740{
741 char *errmsg;
742 int port_low, port_high;
743 struct sockaddr_storage *sk;
744 struct protocol *proto;
745
746 errmsg = NULL;
747
748 if (!*args[*cur_arg + 1]) {
749 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
750 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
751 goto err;
752 }
753
754 /* 'sk' is statically allocated (no need to be freed). */
755 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
756 if (!sk) {
757 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
758 goto err;
759 }
760
761 proto = protocol_by_family(sk->ss_family);
762 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100763 ha_alert("'%s %s' : connect() not supported for this address family.\n",
764 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100765 goto err;
766 }
767
768 newsrv->conn_src.opts |= CO_SRC_BIND;
769 newsrv->conn_src.source_addr = *sk;
770
771 if (port_low != port_high) {
772 int i;
773
774 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100775 ha_alert("'%s' does not support port offsets (found '%s').\n",
776 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100777 goto err;
778 }
779
780 if (port_low <= 0 || port_low > 65535 ||
781 port_high <= 0 || port_high > 65535 ||
782 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100783 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 +0100784 goto err;
785 }
786 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
787 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
788 newsrv->conn_src.sport_range->ports[i] = port_low + i;
789 }
790
791 *cur_arg += 2;
792 while (*(args[*cur_arg])) {
793 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
794#if defined(CONFIG_HAP_TRANSPARENT)
795 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100796 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
797 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100798 goto err;
799 }
800 if (!strcmp(args[*cur_arg + 1], "client")) {
801 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
802 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
803 }
804 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
805 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
806 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
807 }
808 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
809 char *name, *end;
810
811 name = args[*cur_arg + 1] + 7;
812 while (isspace(*name))
813 name++;
814
815 end = name;
816 while (*end && !isspace(*end) && *end != ',' && *end != ')')
817 end++;
818
819 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
820 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
821 free(newsrv->conn_src.bind_hdr_name);
822 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
823 newsrv->conn_src.bind_hdr_len = end - name;
824 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
825 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
826 newsrv->conn_src.bind_hdr_occ = -1;
827
828 /* now look for an occurrence number */
829 while (isspace(*end))
830 end++;
831 if (*end == ',') {
832 end++;
833 name = end;
834 if (*end == '-')
835 end++;
836 while (isdigit((int)*end))
837 end++;
838 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
839 }
840
841 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100842 ha_alert("usesrc hdr_ip(name,num) does not support negative"
843 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100844 goto err;
845 }
846 }
847 else {
848 struct sockaddr_storage *sk;
849 int port1, port2;
850
851 /* 'sk' is statically allocated (no need to be freed). */
852 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
853 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100854 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100855 goto err;
856 }
857
858 proto = protocol_by_family(sk->ss_family);
859 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100860 ha_alert("'%s %s' : connect() not supported for this address family.\n",
861 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100862 goto err;
863 }
864
865 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100866 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
867 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100868 goto err;
869 }
870 newsrv->conn_src.tproxy_addr = *sk;
871 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
872 }
873 global.last_checks |= LSTCHK_NETADM;
874 *cur_arg += 2;
875 continue;
876#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100877 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 +0100878 goto err;
879#endif /* defined(CONFIG_HAP_TRANSPARENT) */
880 } /* "usesrc" */
881
882 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
883#ifdef SO_BINDTODEVICE
884 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100885 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100886 goto err;
887 }
888 free(newsrv->conn_src.iface_name);
889 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
890 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
891 global.last_checks |= LSTCHK_NETADM;
892#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100893 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100894 goto err;
895#endif
896 *cur_arg += 2;
897 continue;
898 }
899 /* this keyword in not an option of "source" */
900 break;
901 } /* while */
902
903 return 0;
904
905 err:
906 free(errmsg);
907 return ERR_ALERT | ERR_FATAL;
908}
909
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100910/* Parse the "stick" server keyword */
911static int srv_parse_stick(char **args, int *cur_arg,
912 struct proxy *curproxy, struct server *newsrv, char **err)
913{
914 newsrv->flags &= ~SRV_F_NON_STICK;
915 return 0;
916}
917
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100918/* Parse the "track" server keyword */
919static int srv_parse_track(char **args, int *cur_arg,
920 struct proxy *curproxy, struct server *newsrv, char **err)
921{
922 char *arg;
923
924 arg = args[*cur_arg + 1];
925 if (!*arg) {
926 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
927 return ERR_ALERT | ERR_FATAL;
928 }
929
930 free(newsrv->trackit);
931 newsrv->trackit = strdup(arg);
932
933 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800934}
935
936/* Parse the "socks4" server keyword */
937static int srv_parse_socks4(char **args, int *cur_arg,
938 struct proxy *curproxy, struct server *newsrv, char **err)
939{
940 char *errmsg;
941 int port_low, port_high;
942 struct sockaddr_storage *sk;
943 struct protocol *proto;
944
945 errmsg = NULL;
946
947 if (!*args[*cur_arg + 1]) {
948 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
949 goto err;
950 }
951
952 /* 'sk' is statically allocated (no need to be freed). */
953 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
954 if (!sk) {
955 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
956 goto err;
957 }
958
959 proto = protocol_by_family(sk->ss_family);
960 if (!proto || !proto->connect) {
961 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
962 goto err;
963 }
964
965 newsrv->flags |= SRV_F_SOCKS4_PROXY;
966 newsrv->socks4_addr = *sk;
967
968 if (port_low != port_high) {
969 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
970 goto err;
971 }
972
973 if (!port_low) {
974 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
975 goto err;
976 }
977
978 return 0;
979
980 err:
981 free(errmsg);
982 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100983}
984
Frédéric Lécailledba97072017-03-17 15:33:50 +0100985
Willy Tarreau034c88c2017-01-23 23:36:45 +0100986/* parse the "tfo" server keyword */
987static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
988{
989 newsrv->flags |= SRV_F_FASTOPEN;
990 return 0;
991}
992
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200993/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200994 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200995 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200996 *
997 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200998 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200999void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001000{
Willy Tarreau87b09662015-04-03 00:22:06 +02001001 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001002
Willy Tarreau87b09662015-04-03 00:22:06 +02001003 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
1004 if (stream->srv_conn == srv)
1005 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001006}
1007
1008/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001009 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001010 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001011 *
1012 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001013 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001014void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001015{
1016 struct server *srv;
1017
1018 for (srv = px->srv; srv != NULL; srv = srv->next)
1019 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001020 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001021}
1022
Willy Tarreaubda92272014-05-20 21:55:30 +02001023/* Appends some information to a message string related to a server going UP or
1024 * DOWN. If both <forced> and <reason> are null and the server tracks another
1025 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001026 * If <check> is non-null, an entire string describing the check result will be
1027 * appended after a comma and a space (eg: to report some information from the
1028 * check that changed the state). In the other case, the string will be built
1029 * using the check results stored into the struct server if present.
1030 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001031 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001032 *
1033 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001034 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001035void srv_append_status(struct buffer *msg, struct server *s,
1036 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001037{
Emeric Brun5a133512017-10-19 14:42:30 +02001038 short status = s->op_st_chg.status;
1039 short code = s->op_st_chg.code;
1040 long duration = s->op_st_chg.duration;
1041 char *desc = s->op_st_chg.reason;
1042
1043 if (check) {
1044 status = check->status;
1045 code = check->code;
1046 duration = check->duration;
1047 desc = check->desc;
1048 }
1049
1050 if (status != -1) {
1051 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1052
1053 if (status >= HCHK_STATUS_L57DATA)
1054 chunk_appendf(msg, ", code: %d", code);
1055
1056 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001057 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001058
1059 chunk_appendf(msg, ", info: \"");
1060
1061 chunk_initlen(&src, desc, 0, strlen(desc));
1062 chunk_asciiencode(msg, &src, '"');
1063
1064 chunk_appendf(msg, "\"");
1065 }
1066
1067 if (duration >= 0)
1068 chunk_appendf(msg, ", check duration: %ldms", duration);
1069 }
1070 else if (desc && *desc) {
1071 chunk_appendf(msg, ", %s", desc);
1072 }
1073 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001074 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001075 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001076
1077 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001078 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001079 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1080 " %d sessions active, %d requeued, %d remaining in queue",
1081 s->proxy->srv_act, s->proxy->srv_bck,
1082 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1083 s->cur_sess, xferred, s->nbpend);
1084 else
1085 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1086 " %d sessions requeued, %d total in queue",
1087 s->proxy->srv_act, s->proxy->srv_bck,
1088 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1089 xferred, s->nbpend);
1090 }
1091}
1092
Emeric Brun5a133512017-10-19 14:42:30 +02001093/* Marks server <s> down, regardless of its checks' statuses. The server is
1094 * registered in a list to postpone the counting of the remaining servers on
1095 * the proxy and transfers queued streams whenever possible to other servers at
1096 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1097 * non-null as the reason for going down or the available data from the check
1098 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001099 *
1100 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001101 */
Emeric Brun5a133512017-10-19 14:42:30 +02001102void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001103{
1104 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001105
Emeric Brun64cc49c2017-10-03 14:46:45 +02001106 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001107 return;
1108
Emeric Brun52a91d32017-08-31 14:41:55 +02001109 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001110 *s->op_st_chg.reason = 0;
1111 s->op_st_chg.status = -1;
1112 if (reason) {
1113 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1114 }
1115 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001116 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001117 s->op_st_chg.code = check->code;
1118 s->op_st_chg.status = check->status;
1119 s->op_st_chg.duration = check->duration;
1120 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001121
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001122 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001123 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001124
Emeric Brun9f0b4582017-10-23 14:39:51 +02001125 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001126 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001127 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001128 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001129 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001130}
1131
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001132/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001133 * in maintenance. The server is registered in a list to postpone the counting
1134 * of the remaining servers on the proxy and tries to grab requests from the
1135 * proxy at a sync point. Maintenance servers are ignored. It stores the
1136 * <reason> if non-null as the reason for going down or the available data
1137 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001138 *
1139 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001140 */
Emeric Brun5a133512017-10-19 14:42:30 +02001141void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001142{
1143 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001144
Emeric Brun64cc49c2017-10-03 14:46:45 +02001145 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001146 return;
1147
Emeric Brun52a91d32017-08-31 14:41:55 +02001148 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001149 return;
1150
Emeric Brun52a91d32017-08-31 14:41:55 +02001151 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001152 *s->op_st_chg.reason = 0;
1153 s->op_st_chg.status = -1;
1154 if (reason) {
1155 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1156 }
1157 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001158 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001159 s->op_st_chg.code = check->code;
1160 s->op_st_chg.status = check->status;
1161 s->op_st_chg.duration = check->duration;
1162 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001163
Emeric Brun64cc49c2017-10-03 14:46:45 +02001164 if (s->slowstart <= 0)
1165 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001166
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001167 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001168 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001169
Emeric Brun9f0b4582017-10-23 14:39:51 +02001170 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001171 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001172 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001173 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001174 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001175}
1176
Willy Tarreau8eb77842014-05-21 13:54:57 +02001177/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001178 * isn't in maintenance. The server is registered in a list to postpone the
1179 * counting of the remaining servers on the proxy and tries to grab requests
1180 * from the proxy. Maintenance servers are ignored. It stores the
1181 * <reason> if non-null as the reason for going down or the available data
1182 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001183 * up. Note that it makes use of the trash to build the log strings, so <reason>
1184 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001185 *
1186 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001187 */
Emeric Brun5a133512017-10-19 14:42:30 +02001188void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001189{
1190 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001191
Emeric Brun64cc49c2017-10-03 14:46:45 +02001192 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001193 return;
1194
Emeric Brun52a91d32017-08-31 14:41:55 +02001195 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001196 return;
1197
Emeric Brun52a91d32017-08-31 14:41:55 +02001198 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001199 *s->op_st_chg.reason = 0;
1200 s->op_st_chg.status = -1;
1201 if (reason) {
1202 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1203 }
1204 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001205 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001206 s->op_st_chg.code = check->code;
1207 s->op_st_chg.status = check->status;
1208 s->op_st_chg.duration = check->duration;
1209 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001210
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001211 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001212 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001213
Emeric Brun9f0b4582017-10-23 14:39:51 +02001214 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001215 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001216 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001217 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001218 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001219}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001220
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001221/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1222 * enforce either maint mode or drain mode. It is not allowed to set more than
1223 * one flag at once. The equivalent "inherited" flag is propagated to all
1224 * tracking servers. Maintenance mode disables health checks (but not agent
1225 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001226 * is done. If <cause> is non-null, it will be displayed at the end of the log
1227 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001228 *
1229 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001230 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001231void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001232{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001233 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001234
1235 if (!mode)
1236 return;
1237
1238 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001239 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001240 return;
1241
Emeric Brun52a91d32017-08-31 14:41:55 +02001242 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001243 if (cause)
1244 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1245
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001246 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001247 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001248
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001249 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001250 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1251 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001252 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001253
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001254 /* compute the inherited flag to propagate */
1255 if (mode & SRV_ADMF_MAINT)
1256 mode = SRV_ADMF_IMAINT;
1257 else if (mode & SRV_ADMF_DRAIN)
1258 mode = SRV_ADMF_IDRAIN;
1259
Emeric Brun9f0b4582017-10-23 14:39:51 +02001260 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001261 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001262 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001263 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001264 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001265}
1266
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001267/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1268 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1269 * than one flag at once. The equivalent "inherited" flag is propagated to all
1270 * tracking servers. Leaving maintenance mode re-enables health checks. When
1271 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001272 *
1273 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001274 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001275void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001276{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001277 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001278
1279 if (!mode)
1280 return;
1281
1282 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001283 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001284 return;
1285
Emeric Brun52a91d32017-08-31 14:41:55 +02001286 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001287
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001288 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001289 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001290
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001291 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001292 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1293 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001294 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001295
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001296 if (mode & SRV_ADMF_MAINT)
1297 mode = SRV_ADMF_IMAINT;
1298 else if (mode & SRV_ADMF_DRAIN)
1299 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001300
Emeric Brun9f0b4582017-10-23 14:39:51 +02001301 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001302 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001303 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001304 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001305 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001306}
1307
Willy Tarreau757478e2016-11-03 19:22:19 +01001308/* principle: propagate maint and drain to tracking servers. This is useful
1309 * upon startup so that inherited states are correct.
1310 */
1311static void srv_propagate_admin_state(struct server *srv)
1312{
1313 struct server *srv2;
1314
1315 if (!srv->trackers)
1316 return;
1317
1318 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001319 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001320 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001321 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001322
Emeric Brun52a91d32017-08-31 14:41:55 +02001323 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001324 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001325 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001326 }
1327}
1328
1329/* Compute and propagate the admin states for all servers in proxy <px>.
1330 * Only servers *not* tracking another one are considered, because other
1331 * ones will be handled when the server they track is visited.
1332 */
1333void srv_compute_all_admin_states(struct proxy *px)
1334{
1335 struct server *srv;
1336
1337 for (srv = px->srv; srv; srv = srv->next) {
1338 if (srv->track)
1339 continue;
1340 srv_propagate_admin_state(srv);
1341 }
1342}
1343
Willy Tarreaudff55432012-10-10 17:51:05 +02001344/* Note: must not be declared <const> as its list will be overwritten.
1345 * Please take care of keeping this list alphabetically sorted, doing so helps
1346 * all code contributors.
1347 * Optional keywords are also declared with a NULL ->parse() function so that
1348 * the config parser can report an appropriate error when a known keyword was
1349 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001350 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001351 */
1352static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001353 { "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 +01001354 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001355 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001356 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001357 { "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 +01001358 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001359 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1360 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001361 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001362 { "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 +01001363 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001364 { "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 +01001365 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001366 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001367 { "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 +01001368 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1369 { "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 +02001370 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001371 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001372 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001373 { "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 +01001374 { "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 +02001375 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001376 { "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 +01001377 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001378 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1379 { "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 +02001380 { "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 +01001381 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001382 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001383 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001384 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
1385 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* enable socks4 proxy for health checks */
Willy Tarreaudff55432012-10-10 17:51:05 +02001386 { NULL, NULL, 0 },
1387}};
1388
Willy Tarreau0108d902018-11-25 19:14:37 +01001389INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001390
Willy Tarreau004e0452013-11-21 11:22:01 +01001391/* Recomputes the server's eweight based on its state, uweight, the current time,
1392 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001393 * state is automatically disabled if the time is elapsed. If <must_update> is
1394 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001395 *
1396 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001397 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001398void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001399{
1400 struct proxy *px = sv->proxy;
1401 unsigned w;
1402
1403 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1404 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001405 if (sv->next_state == SRV_ST_STARTING)
1406 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001407 }
1408
1409 /* We must take care of not pushing the server to full throttle during slow starts.
1410 * It must also start immediately, at least at the minimal step when leaving maintenance.
1411 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001412 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001413 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1414 else
1415 w = px->lbprm.wdiv;
1416
Emeric Brun52a91d32017-08-31 14:41:55 +02001417 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001418
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001419 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001420 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001421 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001422}
1423
Willy Tarreaubaaee002006-06-26 02:48:02 +02001424/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001425 * Parses weight_str and configures sv accordingly.
1426 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001427 *
1428 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001429 */
1430const char *server_parse_weight_change_request(struct server *sv,
1431 const char *weight_str)
1432{
1433 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001434 long int w;
1435 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001436
1437 px = sv->proxy;
1438
1439 /* if the weight is terminated with '%', it is set relative to
1440 * the initial weight, otherwise it is absolute.
1441 */
1442 if (!*weight_str)
1443 return "Require <weight> or <weight%>.\n";
1444
Simon Hormanb796afa2013-02-12 10:45:53 +09001445 w = strtol(weight_str, &end, 10);
1446 if (end == weight_str)
1447 return "Empty weight string empty or preceded by garbage";
1448 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001449 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001450 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001451 /* Avoid integer overflow */
1452 if (w > 25600)
1453 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001454 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001455 if (w > 256)
1456 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001457 }
1458 else if (w < 0 || w > 256)
1459 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001460 else if (end[0] != '\0')
1461 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001462
1463 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1464 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1465
1466 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001467 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001468
1469 return NULL;
1470}
1471
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001472/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001473 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1474 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001475 * Returns:
1476 * - error string on error
1477 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001478 *
1479 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001480 */
1481const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001482 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001483{
1484 unsigned char ip[INET6_ADDRSTRLEN];
1485
1486 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001487 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001488 return NULL;
1489 }
1490 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001491 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001492 return NULL;
1493 }
1494
1495 return "Could not understand IP address format.\n";
1496}
1497
Willy Tarreau46b7f532018-08-21 11:54:26 +02001498/*
1499 * Must be called with the server lock held.
1500 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001501const char *server_parse_maxconn_change_request(struct server *sv,
1502 const char *maxconn_str)
1503{
1504 long int v;
1505 char *end;
1506
1507 if (!*maxconn_str)
1508 return "Require <maxconn>.\n";
1509
1510 v = strtol(maxconn_str, &end, 10);
1511 if (end == maxconn_str)
1512 return "maxconn string empty or preceded by garbage";
1513 else if (end[0] != '\0')
1514 return "Trailing garbage in maxconn string";
1515
1516 if (sv->maxconn == sv->minconn) { // static maxconn
1517 sv->maxconn = sv->minconn = v;
1518 } else { // dynamic maxconn
1519 sv->maxconn = v;
1520 }
1521
1522 if (may_dequeue_tasks(sv, sv->proxy))
1523 process_srv_queue(sv);
1524
1525 return NULL;
1526}
1527
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001528#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001529static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1530 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001531{
1532 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001533 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001534 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001535 NULL,
1536 };
1537
1538 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001539 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001540
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001541 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1542}
1543
1544static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1545{
1546 struct sample_expr *expr;
1547
1548 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 +01001549 if (!expr) {
1550 memprintf(err, "error detected while parsing sni expression : %s", *err);
1551 return ERR_ALERT | ERR_FATAL;
1552 }
1553
1554 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1555 memprintf(err, "error detected while parsing sni expression : "
1556 " fetch method '%s' extracts information from '%s', "
1557 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001558 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001559 return ERR_ALERT | ERR_FATAL;
1560 }
1561
1562 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1563 release_sample_expr(newsrv->ssl_ctx.sni);
1564 newsrv->ssl_ctx.sni = expr;
1565
1566 return 0;
1567}
1568#endif
1569
1570static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1571{
1572 if (err && *err) {
1573 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001574 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001575 }
1576 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001577 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1578 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001579}
1580
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001581static void srv_conn_src_sport_range_cpy(struct server *srv,
1582 struct server *src)
1583{
1584 int range_sz;
1585
1586 range_sz = src->conn_src.sport_range->size;
1587 if (range_sz > 0) {
1588 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1589 if (srv->conn_src.sport_range != NULL) {
1590 int i;
1591
1592 for (i = 0; i < range_sz; i++) {
1593 srv->conn_src.sport_range->ports[i] =
1594 src->conn_src.sport_range->ports[i];
1595 }
1596 }
1597 }
1598}
1599
1600/*
1601 * Copy <src> server connection source settings to <srv> server everything needed.
1602 */
1603static void srv_conn_src_cpy(struct server *srv, struct server *src)
1604{
1605 srv->conn_src.opts = src->conn_src.opts;
1606 srv->conn_src.source_addr = src->conn_src.source_addr;
1607
1608 /* Source port range copy. */
1609 if (src->conn_src.sport_range != NULL)
1610 srv_conn_src_sport_range_cpy(srv, src);
1611
1612#ifdef CONFIG_HAP_TRANSPARENT
1613 if (src->conn_src.bind_hdr_name != NULL) {
1614 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1615 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1616 }
1617 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1618 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1619#endif
1620 if (src->conn_src.iface_name != NULL)
1621 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1622}
1623
1624/*
1625 * Copy <src> server SSL settings to <srv> server allocating
1626 * everything needed.
1627 */
1628#if defined(USE_OPENSSL)
1629static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1630{
1631 if (src->ssl_ctx.ca_file != NULL)
1632 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1633 if (src->ssl_ctx.crl_file != NULL)
1634 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1635 if (src->ssl_ctx.client_crt != NULL)
1636 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1637
1638 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1639
1640 if (src->ssl_ctx.verify_host != NULL)
1641 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1642 if (src->ssl_ctx.ciphers != NULL)
1643 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Willy Tarreau5db847a2019-05-09 14:13:35 +02001644#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001645 if (src->ssl_ctx.ciphersuites != NULL)
1646 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1647#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001648 if (src->sni_expr != NULL)
1649 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001650
1651#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1652 if (src->ssl_ctx.alpn_str) {
1653 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1654 if (srv->ssl_ctx.alpn_str) {
1655 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1656 src->ssl_ctx.alpn_len);
1657 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1658 }
1659 }
1660#endif
1661#ifdef OPENSSL_NPN_NEGOTIATED
1662 if (src->ssl_ctx.npn_str) {
1663 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1664 if (srv->ssl_ctx.npn_str) {
1665 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1666 src->ssl_ctx.npn_len);
1667 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1668 }
1669 }
1670#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001671}
1672#endif
1673
1674/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001675 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001676 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001677 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001678 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001679static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001680{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001681 char *hostname_dn;
1682 int hostname_len, hostname_dn_len;
1683
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001684 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001685 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001686
Christopher Faulet67957bd2017-09-27 11:00:59 +02001687 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001688 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001689 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1690 hostname_dn, trash.size);
1691 if (hostname_dn_len == -1)
1692 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001693
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001694
Christopher Faulet67957bd2017-09-27 11:00:59 +02001695 free(srv->hostname);
1696 free(srv->hostname_dn);
1697 srv->hostname = strdup(hostname);
1698 srv->hostname_dn = strdup(hostname_dn);
1699 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001700 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001701 goto err;
1702
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001703 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001704
1705 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001706 free(srv->hostname); srv->hostname = NULL;
1707 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001708 return -1;
1709}
1710
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001711/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001712 * Copy <src> server settings to <srv> server allocating
1713 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001714 * This function is not supposed to be called at any time, but only
1715 * during server settings parsing or during server allocations from
1716 * a server template, and just after having calloc()'ed a new server.
1717 * So, <src> may only be a default server (when parsing server settings)
1718 * or a server template (during server allocations from a server template).
1719 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1720 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001721 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001722static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001723{
1724 /* Connection source settings copy */
1725 srv_conn_src_cpy(srv, src);
1726
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001727 if (srv_tmpl) {
1728 srv->addr = src->addr;
1729 srv->svc_port = src->svc_port;
1730 }
1731
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001732 srv->pp_opts = src->pp_opts;
1733 if (src->rdr_pfx != NULL) {
1734 srv->rdr_pfx = strdup(src->rdr_pfx);
1735 srv->rdr_len = src->rdr_len;
1736 }
1737 if (src->cookie != NULL) {
1738 srv->cookie = strdup(src->cookie);
1739 srv->cklen = src->cklen;
1740 }
1741 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01001742 srv->check.addr = src->check.addr;
1743 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001744 srv->check.use_ssl = src->check.use_ssl;
1745 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001746 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001747 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001748 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001749 /* Note: 'flags' field has potentially been already initialized. */
1750 srv->flags |= src->flags;
1751 srv->do_check = src->do_check;
1752 srv->do_agent = src->do_agent;
1753 if (srv->check.port)
1754 srv->flags |= SRV_F_CHECKPORT;
1755 srv->check.inter = src->check.inter;
1756 srv->check.fastinter = src->check.fastinter;
1757 srv->check.downinter = src->check.downinter;
1758 srv->agent.use_ssl = src->agent.use_ssl;
1759 srv->agent.port = src->agent.port;
1760 if (src->agent.send_string != NULL)
1761 srv->agent.send_string = strdup(src->agent.send_string);
1762 srv->agent.send_string_len = src->agent.send_string_len;
1763 srv->agent.inter = src->agent.inter;
1764 srv->agent.fastinter = src->agent.fastinter;
1765 srv->agent.downinter = src->agent.downinter;
1766 srv->maxqueue = src->maxqueue;
1767 srv->minconn = src->minconn;
1768 srv->maxconn = src->maxconn;
1769 srv->slowstart = src->slowstart;
1770 srv->observe = src->observe;
1771 srv->onerror = src->onerror;
1772 srv->onmarkeddown = src->onmarkeddown;
1773 srv->onmarkedup = src->onmarkedup;
1774 if (src->trackit != NULL)
1775 srv->trackit = strdup(src->trackit);
1776 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1777 srv->uweight = srv->iweight = src->iweight;
1778
1779 srv->check.send_proxy = src->check.send_proxy;
1780 /* health: up, but will fall down at first failure */
1781 srv->check.rise = srv->check.health = src->check.rise;
1782 srv->check.fall = src->check.fall;
1783
1784 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001785 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1786 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1787 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001788 srv->check.state |= CHK_ST_PAUSED;
1789 srv->check.health = 0;
1790 }
1791
1792 /* health: up but will fall down at first failure */
1793 srv->agent.rise = srv->agent.health = src->agent.rise;
1794 srv->agent.fall = src->agent.fall;
1795
1796 if (src->resolvers_id != NULL)
1797 srv->resolvers_id = strdup(src->resolvers_id);
1798 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001799 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Daniel Corbettf8716912019-11-17 09:48:56 -05001800 srv->dns_opts.ignore_weight = src->dns_opts.ignore_weight;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001801 if (srv->dns_opts.family_prio == AF_UNSPEC)
1802 srv->dns_opts.family_prio = AF_INET6;
1803 memcpy(srv->dns_opts.pref_net,
1804 src->dns_opts.pref_net,
1805 sizeof srv->dns_opts.pref_net);
1806 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1807
1808 srv->init_addr_methods = src->init_addr_methods;
1809 srv->init_addr = src->init_addr;
1810#if defined(USE_OPENSSL)
1811 srv_ssl_settings_cpy(srv, src);
1812#endif
1813#ifdef TCP_USER_TIMEOUT
1814 srv->tcp_ut = src->tcp_ut;
1815#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001816 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001817 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001818 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001819 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001820
Olivier Houchard8da5f982017-08-04 18:35:36 +02001821 if (srv_tmpl)
1822 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001823
1824 srv->check.via_socks4 = src->check.via_socks4;
1825 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001826}
1827
William Lallemand313bfd12018-10-26 14:47:32 +02001828struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001829{
1830 struct server *srv;
1831
1832 srv = calloc(1, sizeof *srv);
1833 if (!srv)
1834 return NULL;
1835
1836 srv->obj_type = OBJ_TYPE_SERVER;
1837 srv->proxy = proxy;
1838 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001839 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001840
Emeric Brun52a91d32017-08-31 14:41:55 +02001841 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001842 srv->last_change = now.tv_sec;
1843
1844 srv->check.status = HCHK_STATUS_INI;
1845 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001846 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001847 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1848
1849 srv->agent.status = HCHK_STATUS_INI;
1850 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001851 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001852 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1853
Willy Tarreau975b1552019-06-06 16:25:55 +02001854 /* please don't put default server settings here, they are set in
1855 * init_default_instance().
1856 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001857 return srv;
1858}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001859
1860/*
1861 * Validate <srv> server health-check settings.
1862 * Returns 0 if everything is OK, -1 if not.
1863 */
1864static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1865{
1866 struct tcpcheck_rule *r = NULL;
1867 struct list *l;
1868
1869 /*
1870 * We need at least a service port, a check port or the first tcp-check rule must
1871 * be a 'connect' one when checking an IPv4/IPv6 server.
1872 */
1873 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1874 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1875 return 0;
1876
1877 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1878 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001879 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1880 "Check has been disabled.\n",
1881 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001882 return -1;
1883 }
1884
1885 /* search the first action (connect / send / expect) in the list */
1886 l = &srv->proxy->tcpcheck_rules;
1887 list_for_each_entry(r, l, list) {
1888 if (r->action != TCPCHK_ACT_COMMENT)
1889 break;
1890 }
1891
1892 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001893 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1894 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1895 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001896 return -1;
1897 }
1898
1899 /* scan the tcp-check ruleset to ensure a port has been configured */
1900 l = &srv->proxy->tcpcheck_rules;
1901 list_for_each_entry(r, l, list) {
1902 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001903 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1904 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1905 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001906 return -1;
1907 }
1908 }
1909
1910 return 0;
1911}
1912
1913/*
1914 * Initialize <srv> health-check structure.
1915 * Returns the error string in case of memory allocation failure, NULL if not.
1916 */
1917static const char *do_health_check_init(struct server *srv, int check_type, int state)
1918{
1919 const char *ret;
1920
1921 if (!srv->do_check)
1922 return NULL;
1923
1924 ret = init_check(&srv->check, check_type);
1925 if (ret)
1926 return ret;
1927
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001928 srv->check.state |= state;
1929 global.maxsock++;
1930
1931 return NULL;
1932}
1933
1934static int server_health_check_init(const char *file, int linenum,
1935 struct server *srv, struct proxy *curproxy)
1936{
1937 const char *ret;
1938
1939 if (!srv->do_check)
1940 return 0;
1941
1942 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001943 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1944 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001945 return ERR_ALERT | ERR_FATAL;
1946 }
1947
1948 if (server_healthcheck_validate(file, linenum, srv) < 0)
1949 return ERR_ALERT | ERR_ABORT;
1950
1951 /* note: check type will be set during the config review phase */
1952 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1953 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001954 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001955 return ERR_ALERT | ERR_ABORT;
1956 }
1957
1958 return 0;
1959}
1960
1961/*
1962 * Initialize <srv> agent check structure.
1963 * Returns the error string in case of memory allocation failure, NULL if not.
1964 */
1965static const char *do_server_agent_check_init(struct server *srv, int state)
1966{
1967 const char *ret;
1968
1969 if (!srv->do_agent)
1970 return NULL;
1971
1972 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1973 if (ret)
1974 return ret;
1975
1976 if (!srv->agent.inter)
1977 srv->agent.inter = srv->check.inter;
1978
1979 srv->agent.state |= state;
1980 global.maxsock++;
1981
1982 return NULL;
1983}
1984
1985static int server_agent_check_init(const char *file, int linenum,
1986 struct server *srv, struct proxy *curproxy)
1987{
1988 const char *ret;
1989
1990 if (!srv->do_agent)
1991 return 0;
1992
1993 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001994 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001995 file, linenum, srv->id);
1996 return ERR_ALERT | ERR_FATAL;
1997 }
1998
1999 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
2000 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002001 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002002 return ERR_ALERT | ERR_ABORT;
2003 }
2004
2005 return 0;
2006}
2007
2008#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2009static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
2010 struct server *srv, struct proxy *proxy)
2011{
2012 int ret;
2013 char *err = NULL;
2014
2015 if (!srv->sni_expr)
2016 return 0;
2017
2018 ret = server_parse_sni_expr(srv, proxy, &err);
2019 if (!ret)
2020 return 0;
2021
2022 display_parser_err(file, linenum, args, cur_arg, &err);
2023 free(err);
2024
2025 return ret;
2026}
2027#endif
2028
2029/*
2030 * Server initializations finalization.
2031 * Initialize health check, agent check and SNI expression if enabled.
2032 * Must not be called for a default server instance.
2033 */
2034static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2035 struct server *srv, struct proxy *px)
2036{
2037 int ret;
2038
2039 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2040 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2041 return ret;
2042 }
2043
2044#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2045 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2046 return ret;
2047#endif
2048
2049 if (srv->flags & SRV_F_BACKUP)
2050 px->srv_bck++;
2051 else
2052 px->srv_act++;
2053 srv_lb_commit_status(srv);
2054
2055 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01002056err:
2057 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002058}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002059
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002060/*
2061 * Parse as much as possible such a range string argument: low[-high]
2062 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2063 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2064 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2065 * Returns 0 if succeeded, -1 if not.
2066 */
2067static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2068{
2069 char *nb_high_arg;
2070
2071 *nb_high = 0;
2072 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002073 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002074
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002075 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002076 *nb_high_arg++ = '\0';
2077 *nb_high = atoi(nb_high_arg);
2078 }
2079 else {
2080 *nb_high += *nb_low;
2081 *nb_low = 1;
2082 }
2083
2084 if (*nb_low < 0 || *nb_high < *nb_low)
2085 return -1;
2086
2087 return 0;
2088}
2089
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002090static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2091{
2092 chunk_printf(&trash, "%s%d", prefix, nb);
2093 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002094 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002095}
2096
2097/*
2098 * Initialize as much as possible servers from <srv> server template.
2099 * Note that a server template is a special server with
2100 * a few different parameters than a server which has
2101 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002102 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002103 * 'srv' template included.
2104 */
2105static int server_template_init(struct server *srv, struct proxy *px)
2106{
2107 int i;
2108 struct server *newsrv;
2109
2110 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2111 int check_init_state;
2112 int agent_init_state;
2113
2114 newsrv = new_server(px);
2115 if (!newsrv)
2116 goto err;
2117
2118 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002119 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002120#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2121 if (newsrv->sni_expr) {
2122 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2123 if (!newsrv->ssl_ctx.sni)
2124 goto err;
2125 }
2126#endif
2127 /* Set this new server ID. */
2128 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2129
2130 /* Initial checks states. */
2131 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2132 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2133
2134 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2135 do_server_agent_check_init(newsrv, agent_init_state))
2136 goto err;
2137
2138 /* Linked backwards first. This will be restablished after parsing. */
2139 newsrv->next = px->srv;
2140 px->srv = newsrv;
2141 }
2142 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2143
2144 return i - srv->tmpl_info.nb_low;
2145
2146 err:
2147 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2148 if (newsrv) {
2149#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2150 release_sample_expr(newsrv->ssl_ctx.sni);
2151#endif
2152 free_check(&newsrv->agent);
2153 free_check(&newsrv->check);
2154 }
2155 free(newsrv);
2156 return i - srv->tmpl_info.nb_low;
2157}
2158
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002159int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy, int parse_addr)
Willy Tarreau272adea2014-03-31 10:39:59 +02002160{
2161 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002162 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002163 char *errmsg = NULL;
2164 int err_code = 0;
2165 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002166 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002167
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002168 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002169 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002170 !strcmp(args[0], "default-server") ||
2171 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002172 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002173 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002174 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002175 int srv_tmpl = !defsrv && !srv;
2176 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002177
2178 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002179 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002180 err_code |= ERR_ALERT | ERR_FATAL;
2181 goto out;
2182 }
2183 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002184 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002185
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002186 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002187 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002188 if (!*args[2]) {
2189 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002190 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002191 file, linenum, args[0]);
2192 err_code |= ERR_ALERT | ERR_FATAL;
2193 goto out;
2194 }
2195
2196 err = invalid_char(args[1]);
2197 }
2198 else if (srv_tmpl) {
2199 if (!*args[3]) {
2200 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002201 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 +02002202 file, linenum, args[0]);
2203 err_code |= ERR_ALERT | ERR_FATAL;
2204 goto out;
2205 }
2206
2207 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002208 }
2209
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002210 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002211 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 +02002212 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002213 err_code |= ERR_ALERT | ERR_FATAL;
2214 goto out;
2215 }
2216
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002217 cur_arg = 2;
2218 if (srv_tmpl) {
2219 /* Parse server-template <nb | range> arg. */
2220 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002221 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002222 file, linenum, args[0], args[cur_arg]);
2223 err_code |= ERR_ALERT | ERR_FATAL;
2224 goto out;
2225 }
2226 cur_arg++;
2227 }
2228
Willy Tarreau272adea2014-03-31 10:39:59 +02002229 if (!defsrv) {
2230 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002231 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002232 struct protocol *proto;
2233
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002234 newsrv = new_server(curproxy);
2235 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002236 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002237 err_code |= ERR_ALERT | ERR_ABORT;
2238 goto out;
2239 }
2240
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002241 if (srv_tmpl) {
2242 newsrv->tmpl_info.nb_low = tmpl_range_low;
2243 newsrv->tmpl_info.nb_high = tmpl_range_high;
2244 }
2245
Willy Tarreau272adea2014-03-31 10:39:59 +02002246 /* the servers are linked backwards first */
2247 newsrv->next = curproxy->srv;
2248 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002249 newsrv->conf.file = strdup(file);
2250 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002251 /* Note: for a server template, its id is its prefix.
2252 * This is a temporary id which will be used for server allocations to come
2253 * after parsing.
2254 */
2255 if (srv)
2256 newsrv->id = strdup(args[1]);
2257 else
2258 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002259
2260 /* several ways to check the port component :
2261 * - IP => port=+0, relative (IPv4 only)
2262 * - IP: => port=+0, relative
2263 * - IP:N => port=N, absolute
2264 * - IP:+N => port=+N, relative
2265 * - IP:-N => port=-N, relative
2266 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002267 if (!parse_addr)
2268 goto skip_addr;
2269
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002270 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002271 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002272 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002273 err_code |= ERR_ALERT | ERR_FATAL;
2274 goto out;
2275 }
2276
2277 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002278 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002279 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002280 file, linenum, args[0], args[1]);
2281 err_code |= ERR_ALERT | ERR_FATAL;
2282 goto out;
2283 }
2284
2285 if (!port1 || !port2) {
2286 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002287 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002288 }
2289 else if (port1 != port2) {
2290 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002291 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002292 file, linenum, args[0], args[1], args[2]);
2293 err_code |= ERR_ALERT | ERR_FATAL;
2294 goto out;
2295 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002296
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002297 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002298 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002299 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002300 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002301 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2302 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2303 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002304 err_code |= ERR_ALERT | ERR_FATAL;
2305 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002306 }
2307 }
2308 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002309 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002310 file, linenum, newsrv->id);
2311 err_code |= ERR_ALERT | ERR_FATAL;
2312 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002313 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002314 }
2315
Willy Tarreau272adea2014-03-31 10:39:59 +02002316 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002317 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002318
Olivier Houchard8da5f982017-08-04 18:35:36 +02002319 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002320 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002321 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002322 err_code |= ERR_ALERT | ERR_FATAL;
2323 goto out;
2324 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002325
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002326 cur_arg++;
2327 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002328 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002329 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002330 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002331 } else {
2332 newsrv = &curproxy->defsrv;
2333 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002334 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002335 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002336 }
2337
2338 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002339 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002340 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002341
2342 if (err == PARSE_TIME_OVER) {
2343 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2344 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2345 err_code |= ERR_ALERT | ERR_FATAL;
2346 goto out;
2347 }
2348 else if (err == PARSE_TIME_UNDER) {
2349 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2350 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2351 err_code |= ERR_ALERT | ERR_FATAL;
2352 goto out;
2353 }
2354 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002355 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002356 file, linenum, *err, newsrv->id);
2357 err_code |= ERR_ALERT | ERR_FATAL;
2358 goto out;
2359 }
2360 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002361 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002362 file, linenum, val, args[cur_arg], newsrv->id);
2363 err_code |= ERR_ALERT | ERR_FATAL;
2364 goto out;
2365 }
2366 newsrv->agent.inter = val;
2367 cur_arg += 2;
2368 }
Misiekea849332017-01-09 09:39:51 +01002369 else if (!strcmp(args[cur_arg], "agent-addr")) {
2370 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002371 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002372 goto out;
2373 }
2374
2375 cur_arg += 2;
2376 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002377 else if (!strcmp(args[cur_arg], "agent-port")) {
2378 global.maxsock++;
2379 newsrv->agent.port = atol(args[cur_arg + 1]);
2380 cur_arg += 2;
2381 }
James Brown55f9ff12015-10-21 18:19:05 -07002382 else if (!strcmp(args[cur_arg], "agent-send")) {
2383 global.maxsock++;
2384 free(newsrv->agent.send_string);
2385 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2386 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2387 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2388 cur_arg += 2;
2389 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002390 else if (!strcmp(args[cur_arg], "init-addr")) {
2391 char *p, *end;
2392 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002393 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002394
2395 newsrv->init_addr_methods = 0;
2396 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2397
2398 for (p = args[cur_arg + 1]; *p; p = end) {
2399 /* cut on next comma */
2400 for (end = p; *end && *end != ','; end++);
2401 if (*end)
2402 *(end++) = 0;
2403
Willy Tarreau4310d362016-11-02 15:05:56 +01002404 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002405 if (!strcmp(p, "libc")) {
2406 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2407 }
2408 else if (!strcmp(p, "last")) {
2409 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2410 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002411 else if (!strcmp(p, "none")) {
2412 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2413 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002414 else if (str2ip2(p, &sa, 0)) {
2415 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002416 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002417 file, linenum, args[cur_arg], p);
2418 err_code |= ERR_ALERT | ERR_FATAL;
2419 goto out;
2420 }
2421 newsrv->init_addr = sa;
2422 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2423 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002424 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002425 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 +02002426 file, linenum, args[cur_arg], p);
2427 err_code |= ERR_ALERT | ERR_FATAL;
2428 goto out;
2429 }
2430 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002431 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002432 file, linenum, args[cur_arg], p);
2433 err_code |= ERR_ALERT | ERR_FATAL;
2434 goto out;
2435 }
2436 }
2437 cur_arg += 2;
2438 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002439 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002440 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002441 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2442 cur_arg += 2;
2443 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002444 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2445 char *p, *end;
2446
2447 for (p = args[cur_arg + 1]; *p; p = end) {
2448 /* cut on next comma */
2449 for (end = p; *end && *end != ','; end++);
2450 if (*end)
2451 *(end++) = 0;
2452
2453 if (!strcmp(p, "allow-dup-ip")) {
2454 newsrv->dns_opts.accept_duplicate_ip = 1;
2455 }
Daniel Corbettf8716912019-11-17 09:48:56 -05002456 else if (!strcmp(p, "ignore-weight")) {
2457 newsrv->dns_opts.ignore_weight = 1;
2458 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002459 else if (!strcmp(p, "prevent-dup-ip")) {
2460 newsrv->dns_opts.accept_duplicate_ip = 0;
2461 }
2462 else {
Daniel Corbettf8716912019-11-17 09:48:56 -05002463 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 +02002464 file, linenum, args[cur_arg], p);
2465 err_code |= ERR_ALERT | ERR_FATAL;
2466 goto out;
2467 }
2468 }
2469
2470 cur_arg += 2;
2471 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002472 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2473 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002474 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002475 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002476 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002477 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002478 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002479 file, linenum, args[cur_arg]);
2480 err_code |= ERR_ALERT | ERR_FATAL;
2481 goto out;
2482 }
2483 cur_arg += 2;
2484 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002485 else if (!strcmp(args[cur_arg], "resolve-net")) {
2486 char *p, *e;
2487 unsigned char mask;
2488 struct dns_options *opt;
2489
2490 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002491 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002492 file, linenum, args[cur_arg]);
2493 err_code |= ERR_ALERT | ERR_FATAL;
2494 goto out;
2495 }
2496
2497 opt = &newsrv->dns_opts;
2498
2499 /* Split arguments by comma, and convert it from ipv4 or ipv6
2500 * string network in in_addr or in6_addr.
2501 */
2502 p = args[cur_arg + 1];
2503 e = p;
2504 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002505 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002506 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002507 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002508 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2509 err_code |= ERR_ALERT | ERR_FATAL;
2510 goto out;
2511 }
2512 /* look for end or comma. */
2513 while (*e != ',' && *e != '\0')
2514 e++;
2515 if (*e == ',') {
2516 *e = '\0';
2517 e++;
2518 }
2519 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2520 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2521 /* Try to convert input string from ipv4 or ipv6 network. */
2522 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2523 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2524 &mask)) {
2525 /* Try to convert input string from ipv6 network. */
2526 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2527 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2528 } else {
2529 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002530 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002531 file, linenum, args[cur_arg], p);
2532 err_code |= ERR_ALERT | ERR_FATAL;
2533 goto out;
2534 }
2535 opt->pref_net_nb++;
2536 p = e;
2537 }
2538
2539 cur_arg += 2;
2540 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002541 else if (!strcmp(args[cur_arg], "rise")) {
2542 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002543 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002544 file, linenum, args[cur_arg]);
2545 err_code |= ERR_ALERT | ERR_FATAL;
2546 goto out;
2547 }
2548
2549 newsrv->check.rise = atol(args[cur_arg + 1]);
2550 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002551 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002552 file, linenum, args[cur_arg]);
2553 err_code |= ERR_ALERT | ERR_FATAL;
2554 goto out;
2555 }
2556
2557 if (newsrv->check.health)
2558 newsrv->check.health = newsrv->check.rise;
2559 cur_arg += 2;
2560 }
2561 else if (!strcmp(args[cur_arg], "fall")) {
2562 newsrv->check.fall = atol(args[cur_arg + 1]);
2563
2564 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002565 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002566 file, linenum, args[cur_arg]);
2567 err_code |= ERR_ALERT | ERR_FATAL;
2568 goto out;
2569 }
2570
2571 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002572 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002573 file, linenum, args[cur_arg]);
2574 err_code |= ERR_ALERT | ERR_FATAL;
2575 goto out;
2576 }
2577
2578 cur_arg += 2;
2579 }
2580 else if (!strcmp(args[cur_arg], "inter")) {
2581 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002582
2583 if (err == PARSE_TIME_OVER) {
2584 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\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 == PARSE_TIME_UNDER) {
2590 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2591 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2592 err_code |= ERR_ALERT | ERR_FATAL;
2593 goto out;
2594 }
2595 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002596 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002597 file, linenum, *err, newsrv->id);
2598 err_code |= ERR_ALERT | ERR_FATAL;
2599 goto out;
2600 }
2601 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002602 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002603 file, linenum, val, args[cur_arg], newsrv->id);
2604 err_code |= ERR_ALERT | ERR_FATAL;
2605 goto out;
2606 }
2607 newsrv->check.inter = val;
2608 cur_arg += 2;
2609 }
2610 else if (!strcmp(args[cur_arg], "fastinter")) {
2611 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002612
2613 if (err == PARSE_TIME_OVER) {
2614 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2615 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2616 err_code |= ERR_ALERT | ERR_FATAL;
2617 goto out;
2618 }
2619 else if (err == PARSE_TIME_UNDER) {
2620 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2621 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2622 err_code |= ERR_ALERT | ERR_FATAL;
2623 goto out;
2624 }
2625 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002626 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002627 file, linenum, *err, newsrv->id);
2628 err_code |= ERR_ALERT | ERR_FATAL;
2629 goto out;
2630 }
2631 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002632 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002633 file, linenum, val, args[cur_arg], newsrv->id);
2634 err_code |= ERR_ALERT | ERR_FATAL;
2635 goto out;
2636 }
2637 newsrv->check.fastinter = val;
2638 cur_arg += 2;
2639 }
2640 else if (!strcmp(args[cur_arg], "downinter")) {
2641 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002642
2643 if (err == PARSE_TIME_OVER) {
2644 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2645 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2646 err_code |= ERR_ALERT | ERR_FATAL;
2647 goto out;
2648 }
2649 else if (err == PARSE_TIME_UNDER) {
2650 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2651 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2652 err_code |= ERR_ALERT | ERR_FATAL;
2653 goto out;
2654 }
2655 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002656 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002657 file, linenum, *err, newsrv->id);
2658 err_code |= ERR_ALERT | ERR_FATAL;
2659 goto out;
2660 }
2661 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002662 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002663 file, linenum, val, args[cur_arg], newsrv->id);
2664 err_code |= ERR_ALERT | ERR_FATAL;
2665 goto out;
2666 }
2667 newsrv->check.downinter = val;
2668 cur_arg += 2;
2669 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002670 else if (!strcmp(args[cur_arg], "port")) {
2671 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002672 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002673 cur_arg += 2;
2674 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002675 else if (!strcmp(args[cur_arg], "weight")) {
2676 int w;
2677 w = atol(args[cur_arg + 1]);
2678 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002679 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002680 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2681 err_code |= ERR_ALERT | ERR_FATAL;
2682 goto out;
2683 }
2684 newsrv->uweight = newsrv->iweight = w;
2685 cur_arg += 2;
2686 }
2687 else if (!strcmp(args[cur_arg], "minconn")) {
2688 newsrv->minconn = atol(args[cur_arg + 1]);
2689 cur_arg += 2;
2690 }
2691 else if (!strcmp(args[cur_arg], "maxconn")) {
2692 newsrv->maxconn = atol(args[cur_arg + 1]);
2693 cur_arg += 2;
2694 }
2695 else if (!strcmp(args[cur_arg], "maxqueue")) {
2696 newsrv->maxqueue = atol(args[cur_arg + 1]);
2697 cur_arg += 2;
2698 }
2699 else if (!strcmp(args[cur_arg], "slowstart")) {
2700 /* slowstart is stored in seconds */
2701 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002702
2703 if (err == PARSE_TIME_OVER) {
2704 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2705 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2706 err_code |= ERR_ALERT | ERR_FATAL;
2707 goto out;
2708 }
2709 else if (err == PARSE_TIME_UNDER) {
2710 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2711 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2712 err_code |= ERR_ALERT | ERR_FATAL;
2713 goto out;
2714 }
2715 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002716 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002717 file, linenum, *err, newsrv->id);
2718 err_code |= ERR_ALERT | ERR_FATAL;
2719 goto out;
2720 }
2721 newsrv->slowstart = (val + 999) / 1000;
2722 cur_arg += 2;
2723 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002724 else if (!strcmp(args[cur_arg], "on-error")) {
2725 if (!strcmp(args[cur_arg + 1], "fastinter"))
2726 newsrv->onerror = HANA_ONERR_FASTINTER;
2727 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2728 newsrv->onerror = HANA_ONERR_FAILCHK;
2729 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2730 newsrv->onerror = HANA_ONERR_SUDDTH;
2731 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2732 newsrv->onerror = HANA_ONERR_MARKDWN;
2733 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002734 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002735 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2736 file, linenum, args[cur_arg], args[cur_arg + 1]);
2737 err_code |= ERR_ALERT | ERR_FATAL;
2738 goto out;
2739 }
2740
2741 cur_arg += 2;
2742 }
2743 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2744 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2745 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2746 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002747 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002748 file, linenum, args[cur_arg], args[cur_arg + 1]);
2749 err_code |= ERR_ALERT | ERR_FATAL;
2750 goto out;
2751 }
2752
2753 cur_arg += 2;
2754 }
2755 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2756 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2757 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2758 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002759 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002760 file, linenum, args[cur_arg], args[cur_arg + 1]);
2761 err_code |= ERR_ALERT | ERR_FATAL;
2762 goto out;
2763 }
2764
2765 cur_arg += 2;
2766 }
2767 else if (!strcmp(args[cur_arg], "error-limit")) {
2768 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002769 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002770 file, linenum, args[cur_arg]);
2771 err_code |= ERR_ALERT | ERR_FATAL;
2772 goto out;
2773 }
2774
2775 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2776
2777 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002778 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002779 file, linenum, args[cur_arg]);
2780 err_code |= ERR_ALERT | ERR_FATAL;
2781 goto out;
2782 }
2783 cur_arg += 2;
2784 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002785 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002786 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002787 file, linenum, "usesrc", "source");
2788 err_code |= ERR_ALERT | ERR_FATAL;
2789 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002790 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002791 else {
2792 static int srv_dumped;
2793 struct srv_kw *kw;
2794 char *err;
2795
2796 kw = srv_find_kw(args[cur_arg]);
2797 if (kw) {
2798 char *err = NULL;
2799 int code;
2800
2801 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002802 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 +02002803 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002804 if (kw->skip != -1)
2805 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002806 err_code |= ERR_ALERT | ERR_FATAL;
2807 goto out;
2808 }
2809
2810 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002811 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002812 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002813 if (kw->skip != -1)
2814 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002815 err_code |= ERR_ALERT;
2816 continue;
2817 }
2818
2819 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2820 err_code |= code;
2821
2822 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002823 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002824 if (code & ERR_FATAL) {
2825 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002826 if (kw->skip != -1)
2827 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002828 goto out;
2829 }
2830 }
2831 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002832 if (kw->skip != -1)
2833 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002834 continue;
2835 }
2836
2837 err = NULL;
2838 if (!srv_dumped) {
2839 srv_dump_kws(&err);
2840 indent_msg(&err, 4);
2841 srv_dumped = 1;
2842 }
2843
Christopher Faulet767a84b2017-11-24 16:50:31 +01002844 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002845 file, linenum, args[0], args[1], args[cur_arg],
2846 err ? " Registered keywords :" : "", err ? err : "");
2847 free(err);
2848
2849 err_code |= ERR_ALERT | ERR_FATAL;
2850 goto out;
2851 }
2852 }
2853
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002854 if (!defsrv)
2855 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2856 if (err_code & ERR_FATAL)
2857 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002858 if (srv_tmpl)
2859 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002860 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002861 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002862 return 0;
2863
2864 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002865 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002866 free(errmsg);
2867 return err_code;
2868}
2869
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002870/* Returns a pointer to the first server matching either id <id>.
2871 * NULL is returned if no match is found.
2872 * the lookup is performed in the backend <bk>
2873 */
2874struct server *server_find_by_id(struct proxy *bk, int id)
2875{
2876 struct eb32_node *eb32;
2877 struct server *curserver;
2878
2879 if (!bk || (id ==0))
2880 return NULL;
2881
2882 /* <bk> has no backend capabilities, so it can't have a server */
2883 if (!(bk->cap & PR_CAP_BE))
2884 return NULL;
2885
2886 curserver = NULL;
2887
2888 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2889 if (eb32)
2890 curserver = container_of(eb32, struct server, conf.id);
2891
2892 return curserver;
2893}
2894
2895/* Returns a pointer to the first server matching either name <name>, or id
2896 * if <name> starts with a '#'. NULL is returned if no match is found.
2897 * the lookup is performed in the backend <bk>
2898 */
2899struct server *server_find_by_name(struct proxy *bk, const char *name)
2900{
2901 struct server *curserver;
2902
2903 if (!bk || !name)
2904 return NULL;
2905
2906 /* <bk> has no backend capabilities, so it can't have a server */
2907 if (!(bk->cap & PR_CAP_BE))
2908 return NULL;
2909
2910 curserver = NULL;
2911 if (*name == '#') {
2912 curserver = server_find_by_id(bk, atoi(name + 1));
2913 if (curserver)
2914 return curserver;
2915 }
2916 else {
2917 curserver = bk->srv;
2918
2919 while (curserver && (strcmp(curserver->id, name) != 0))
2920 curserver = curserver->next;
2921
2922 if (curserver)
2923 return curserver;
2924 }
2925
2926 return NULL;
2927}
2928
2929struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2930{
2931 struct server *byname;
2932 struct server *byid;
2933
2934 if (!name && !id)
2935 return NULL;
2936
2937 if (diff)
2938 *diff = 0;
2939
2940 byname = byid = NULL;
2941
2942 if (name) {
2943 byname = server_find_by_name(bk, name);
2944 if (byname && (!id || byname->puid == id))
2945 return byname;
2946 }
2947
2948 /* remaining possibilities :
2949 * - name not set
2950 * - name set but not found
2951 * - name found but ID doesn't match
2952 */
2953 if (id) {
2954 byid = server_find_by_id(bk, id);
2955 if (byid) {
2956 if (byname) {
2957 /* use id only if forced by configuration */
2958 if (byid->flags & SRV_F_FORCED_ID) {
2959 if (diff)
2960 *diff |= 2;
2961 return byid;
2962 }
2963 else {
2964 if (diff)
2965 *diff |= 1;
2966 return byname;
2967 }
2968 }
2969
2970 /* remaining possibilities:
2971 * - name not set
2972 * - name set but not found
2973 */
2974 if (name && diff)
2975 *diff |= 2;
2976 return byid;
2977 }
2978
2979 /* id bot found */
2980 if (byname) {
2981 if (diff)
2982 *diff |= 1;
2983 return byname;
2984 }
2985 }
2986
2987 return NULL;
2988}
2989
Willy Tarreau46b7f532018-08-21 11:54:26 +02002990/* Update a server state using the parameters available in the params list.
2991 *
2992 * Grabs the server lock during operation.
2993 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002994static void srv_update_state(struct server *srv, int version, char **params)
2995{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002996 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002997 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002998
2999 /* fields since version 1
3000 * and common to all other upcoming versions
3001 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003002 enum srv_state srv_op_state;
3003 enum srv_admin srv_admin_state;
3004 unsigned srv_uweight, srv_iweight;
3005 unsigned long srv_last_time_change;
3006 short srv_check_status;
3007 enum chk_result srv_check_result;
3008 int srv_check_health;
3009 int srv_check_state, srv_agent_state;
3010 int bk_f_forced_id;
3011 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003012 int fqdn_set_by_cli;
3013 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02003014 const char *port_str;
3015 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003016 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003017
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003018 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02003019 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02003020 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003021 switch (version) {
3022 case 1:
3023 /*
3024 * now we can proceed with server's state update:
3025 * srv_addr: params[0]
3026 * srv_op_state: params[1]
3027 * srv_admin_state: params[2]
3028 * srv_uweight: params[3]
3029 * srv_iweight: params[4]
3030 * srv_last_time_change: params[5]
3031 * srv_check_status: params[6]
3032 * srv_check_result: params[7]
3033 * srv_check_health: params[8]
3034 * srv_check_state: params[9]
3035 * srv_agent_state: params[10]
3036 * bk_f_forced_id: params[11]
3037 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003038 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003039 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003040 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003041 */
3042
3043 /* validating srv_op_state */
3044 p = NULL;
3045 errno = 0;
3046 srv_op_state = strtol(params[1], &p, 10);
3047 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
3048 (srv_op_state != SRV_ST_STOPPED &&
3049 srv_op_state != SRV_ST_STARTING &&
3050 srv_op_state != SRV_ST_RUNNING &&
3051 srv_op_state != SRV_ST_STOPPING)) {
3052 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
3053 }
3054
3055 /* validating srv_admin_state */
3056 p = NULL;
3057 errno = 0;
3058 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003059 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01003060
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003061 /* inherited statuses will be recomputed later.
3062 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
3063 */
3064 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01003065
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003066 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
3067 (srv_admin_state != 0 &&
3068 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003069 srv_admin_state != SRV_ADMF_CMAINT &&
3070 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01003071 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01003072 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003073 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
3074 }
3075
3076 /* validating srv_uweight */
3077 p = NULL;
3078 errno = 0;
3079 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003080 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003081 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
3082
3083 /* validating srv_iweight */
3084 p = NULL;
3085 errno = 0;
3086 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003087 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003088 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
3089
3090 /* validating srv_last_time_change */
3091 p = NULL;
3092 errno = 0;
3093 srv_last_time_change = strtol(params[5], &p, 10);
3094 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
3095 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
3096
3097 /* validating srv_check_status */
3098 p = NULL;
3099 errno = 0;
3100 srv_check_status = strtol(params[6], &p, 10);
3101 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3102 (srv_check_status >= HCHK_STATUS_SIZE))
3103 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3104
3105 /* validating srv_check_result */
3106 p = NULL;
3107 errno = 0;
3108 srv_check_result = strtol(params[7], &p, 10);
3109 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3110 (srv_check_result != CHK_RES_UNKNOWN &&
3111 srv_check_result != CHK_RES_NEUTRAL &&
3112 srv_check_result != CHK_RES_FAILED &&
3113 srv_check_result != CHK_RES_PASSED &&
3114 srv_check_result != CHK_RES_CONDPASS)) {
3115 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3116 }
3117
3118 /* validating srv_check_health */
3119 p = NULL;
3120 errno = 0;
3121 srv_check_health = strtol(params[8], &p, 10);
3122 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3123 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3124
3125 /* validating srv_check_state */
3126 p = NULL;
3127 errno = 0;
3128 srv_check_state = strtol(params[9], &p, 10);
3129 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3130 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3131 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3132
3133 /* validating srv_agent_state */
3134 p = NULL;
3135 errno = 0;
3136 srv_agent_state = strtol(params[10], &p, 10);
3137 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3138 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3139 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3140
3141 /* validating bk_f_forced_id */
3142 p = NULL;
3143 errno = 0;
3144 bk_f_forced_id = strtol(params[11], &p, 10);
3145 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3146 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3147
3148 /* validating srv_f_forced_id */
3149 p = NULL;
3150 errno = 0;
3151 srv_f_forced_id = strtol(params[12], &p, 10);
3152 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3153 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3154
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003155 /* validating srv_fqdn */
3156 fqdn = params[13];
3157 if (fqdn && *fqdn == '-')
3158 fqdn = NULL;
3159 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3160 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3161 fqdn = NULL;
3162 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003163
Frédéric Lécaille31694712017-08-01 08:47:19 +02003164 port_str = params[14];
3165 if (port_str) {
3166 port = strl2uic(port_str, strlen(port_str));
3167 if (port > USHRT_MAX) {
3168 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3169 port_str = NULL;
3170 }
3171 }
3172
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003173 /* SRV record
3174 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3175 */
3176 srvrecord = params[15];
3177 if (srvrecord && *srvrecord != '_')
3178 srvrecord = NULL;
3179
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003180 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003181 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003182 goto out;
3183
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003184 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003185 /* recover operational state and apply it to this server
3186 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003187 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003188 switch (srv_op_state) {
3189 case SRV_ST_STOPPED:
3190 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003191 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003192 break;
3193 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003194 /* If rise == 1 there is no STARTING state, let's switch to
3195 * RUNNING
3196 */
3197 if (srv->check.rise == 1) {
3198 srv->check.health = srv->check.rise + srv->check.fall - 1;
3199 srv_set_running(srv, "", NULL);
3200 break;
3201 }
3202 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3203 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003204 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003205 break;
3206 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003207 /* If fall == 1 there is no STOPPING state, let's switch to
3208 * STOPPED
3209 */
3210 if (srv->check.fall == 1) {
3211 srv->check.health = 0;
3212 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3213 break;
3214 }
3215 if (srv->check.health < srv->check.rise ||
3216 srv->check.health > srv->check.rise + srv->check.fall - 2)
3217 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003218 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003219 break;
3220 case SRV_ST_RUNNING:
3221 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003222 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003223 break;
3224 }
3225
3226 /* When applying server state, the following rules apply:
3227 * - in case of a configuration change, we apply the setting from the new
3228 * configuration, regardless of old running state
3229 * - if no configuration change, we apply old running state only if old running
3230 * state is different from new configuration state
3231 */
3232 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003233 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3234 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003235 srv_adm_set_maint(srv);
3236 else
3237 srv_adm_set_ready(srv);
3238 }
3239 /* configuration is the same, let's compate old running state and new conf state */
3240 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003241 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003242 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003243 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003244 srv_adm_set_ready(srv);
3245 }
3246 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003247 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003248 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003249 * (srv->iweight is the weight set up in configuration).
3250 * There are two possible reasons for FDRAIN to have been present :
3251 * - previous config weight was zero
3252 * - "set server b/s drain" was sent to the CLI
3253 *
3254 * In the first case, we simply want to drop this drain state
3255 * if the new weight is not zero anymore, meaning the administrator
3256 * has intentionally turned the weight back to a positive value to
3257 * enable the server again after an operation. In the second case,
3258 * the drain state was forced on the CLI regardless of the config's
3259 * weight so we don't want a change to the config weight to lose this
3260 * status. What this means is :
3261 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3262 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003263 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003264 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003265 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003266 }
3267
3268 srv->last_change = date.tv_sec - srv_last_time_change;
3269 srv->check.status = srv_check_status;
3270 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003271
3272 /* Only case we want to apply is removing ENABLED flag which could have been
3273 * done by the "disable health" command over the stats socket
3274 */
3275 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3276 (srv_check_state & CHK_ST_CONFIGURED) &&
3277 !(srv_check_state & CHK_ST_ENABLED))
3278 srv->check.state &= ~CHK_ST_ENABLED;
3279
3280 /* Only case we want to apply is removing ENABLED flag which could have been
3281 * done by the "disable agent" command over the stats socket
3282 */
3283 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3284 (srv_agent_state & CHK_ST_CONFIGURED) &&
3285 !(srv_agent_state & CHK_ST_ENABLED))
3286 srv->agent.state &= ~CHK_ST_ENABLED;
3287
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003288 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3289 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003290 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003291 * It means that a configuration file change has precedence over a unix socket change
3292 * for server's weight
3293 *
3294 * by default, HAProxy applies the following weight when parsing the configuration
3295 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003296 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003297 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003298 srv->uweight = srv_uweight;
3299 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003300 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003301
Willy Tarreaue5a60682016-11-09 14:54:53 +01003302 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003303 if (strcmp(params[0], "-"))
3304 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003305
3306 if (fqdn && srv->hostname) {
3307 if (!strcmp(srv->hostname, fqdn)) {
3308 /* Here we reset the 'set from stats socket FQDN' flag
3309 * to support such transitions:
3310 * Let's say initial FQDN value is foo1 (in configuration file).
3311 * - FQDN changed from stats socket, from foo1 to foo2 value,
3312 * - FQDN changed again from file configuration (with the same previous value
3313 set from stats socket, from foo1 to foo2 value),
3314 * - reload for any other reason than a FQDN modification,
3315 * the configuration file FQDN matches the fqdn server state file value.
3316 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003317 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003318 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003319 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003320 }
3321 else {
3322 /* If the FDQN has been changed from stats socket,
3323 * apply fqdn state file value (which is the value set
3324 * from stats socket).
3325 */
3326 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003327 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003328 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003329 }
3330 }
3331 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003332 /* If all the conditions below are validated, this means
3333 * we're evaluating a server managed by SRV resolution
3334 */
3335 else if (fqdn && !srv->hostname && srvrecord) {
3336 int res;
3337
3338 /* we can't apply previous state if SRV record has changed */
3339 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3340 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);
3341 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3342 goto out;
3343 }
3344
3345 /* create or find a SRV resolution for this srv record */
3346 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3347 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3348 if (srv->srvrq == NULL) {
3349 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3350 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3351 goto out;
3352 }
3353
3354 /* prepare DNS resolution for this server */
3355 res = srv_prepare_for_resolution(srv, fqdn);
3356 if (res == -1) {
3357 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3358 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3359 goto out;
3360 }
3361
3362 /* configure check.port accordingly */
3363 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3364 !(srv->flags & SRV_F_CHECKPORT))
3365 srv->check.port = port;
3366
3367 /* Unset SRV_F_MAPPORTS for SRV records.
3368 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3369 * because no ports are provided in the configuration file.
3370 * This is because HAProxy will use the port found into the SRV record.
3371 */
3372 srv->flags &= ~SRV_F_MAPPORTS;
3373 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003374
Frédéric Lécaille31694712017-08-01 08:47:19 +02003375 if (port_str)
3376 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003377 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003378
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003379 break;
3380 default:
3381 chunk_appendf(msg, ", version '%d' not supported", version);
3382 }
3383
3384 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003385 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003386 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003387 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003388 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003389 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003390}
3391
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003392
3393/*
3394 * read next line from file <f> and return the server state version if one found.
3395 * If no version is found, then 0 is returned
3396 * Note that this should be the first read on <f>
3397 */
3398static int srv_state_get_version(FILE *f) {
3399 char buf[2];
3400 int ret;
3401
3402 /* first character of first line of the file must contain the version of the export */
3403 if (fgets(buf, 2, f) == NULL) {
3404 return 0;
3405 }
3406
3407 ret = atoi(buf);
3408 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
3409 (ret > SRV_STATE_FILE_VERSION_MAX))
3410 return 0;
3411
3412 return ret;
3413}
3414
3415
3416/*
3417 * parses server state line stored in <buf> and supposedly in version <version>.
3418 * Set <params> and <srv_params> accordingly.
3419 * In case of error, params[0] is set to NULL.
3420 */
3421static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
3422{
3423 int buflen, arg, srv_arg;
3424 char *cur, *end;
3425
3426 buflen = strlen(buf);
3427 cur = buf;
3428 end = cur + buflen;
3429
3430 /* we need at least one character */
3431 if (buflen == 0) {
3432 params[0] = NULL;
3433 return;
3434 }
3435
3436 /* ignore blank characters at the beginning of the line */
3437 while (isspace(*cur))
3438 ++cur;
3439
3440 /* Ignore empty or commented lines */
3441 if (cur == end || *cur == '#') {
3442 params[0] = NULL;
3443 return;
3444 }
3445
3446 /* truncated lines */
3447 if (buf[buflen - 1] != '\n') {
3448 //ha_warning("server-state file '%s': truncated line\n", filepath);
3449 params[0] = NULL;
3450 return;
3451 }
3452
3453 /* Removes trailing '\n' */
3454 buf[buflen - 1] = '\0';
3455
3456 /* we're now ready to move the line into *srv_params[] */
3457 params[0] = cur;
3458 arg = 1;
3459 srv_arg = 0;
3460 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3461 if (isspace(*cur)) {
3462 *cur = '\0';
3463 ++cur;
3464 while (isspace(*cur))
3465 ++cur;
3466 switch (version) {
3467 case 1:
3468 /*
3469 * srv_addr: params[4] => srv_params[0]
3470 * srv_op_state: params[5] => srv_params[1]
3471 * srv_admin_state: params[6] => srv_params[2]
3472 * srv_uweight: params[7] => srv_params[3]
3473 * srv_iweight: params[8] => srv_params[4]
3474 * srv_last_time_change: params[9] => srv_params[5]
3475 * srv_check_status: params[10] => srv_params[6]
3476 * srv_check_result: params[11] => srv_params[7]
3477 * srv_check_health: params[12] => srv_params[8]
3478 * srv_check_state: params[13] => srv_params[9]
3479 * srv_agent_state: params[14] => srv_params[10]
3480 * bk_f_forced_id: params[15] => srv_params[11]
3481 * srv_f_forced_id: params[16] => srv_params[12]
3482 * srv_fqdn: params[17] => srv_params[13]
3483 * srv_port: params[18] => srv_params[14]
3484 * srvrecord: params[19] => srv_params[15]
3485 */
3486 if (arg >= 4) {
3487 srv_params[srv_arg] = cur;
3488 ++srv_arg;
3489 }
3490 break;
3491 }
3492
3493 params[arg] = cur;
3494 ++arg;
3495 }
3496 else {
3497 ++cur;
3498 }
3499 }
3500
3501 /* if line is incomplete line, then ignore it.
3502 * otherwise, update useful flags */
3503 switch (version) {
3504 case 1:
3505 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3506 params[0] = NULL;
3507 return;
3508 }
3509 break;
3510 }
3511
3512 return;
3513}
3514
3515
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003516/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3517 * For each proxy, it does the following:
3518 * - opens its server state file (either one or local one)
3519 * - read whole file, line by line
3520 * - analyse each line to check if it matches our current backend:
3521 * - backend name matches
3522 * - backend id matches if id is forced and name doesn't match
3523 * - if the server pointed by the line is found, then state is applied
3524 *
3525 * If the running backend uuid or id differs from the state file, then HAProxy reports
3526 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003527 *
3528 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003529 */
3530void apply_server_state(void)
3531{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003532 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003533 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3534 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003535 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003536 FILE *f;
3537 char *filepath;
3538 char globalfilepath[MAXPATHLEN + 1];
3539 char localfilepath[MAXPATHLEN + 1];
3540 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003541 struct proxy *curproxy, *bk;
3542 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003543 char *line;
3544 char *bkname, *srvname;
3545 struct state_line *st;
3546 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003547
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003548
3549 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003550 globalfilepathlen = 0;
3551 /* create the globalfilepath variable */
3552 if (global.server_state_file) {
3553 /* absolute path or no base directory provided */
3554 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3555 len = strlen(global.server_state_file);
3556 if (len > MAXPATHLEN) {
3557 globalfilepathlen = 0;
3558 goto globalfileerror;
3559 }
3560 memcpy(globalfilepath, global.server_state_file, len);
3561 globalfilepath[len] = '\0';
3562 globalfilepathlen = len;
3563 }
3564 else if (global.server_state_base) {
3565 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003566 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003567 globalfilepathlen = 0;
3568 goto globalfileerror;
3569 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003570 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003571 globalfilepath[len] = 0;
3572 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003573
3574 /* append a slash if needed */
3575 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3576 if (globalfilepathlen + 1 > MAXPATHLEN) {
3577 globalfilepathlen = 0;
3578 goto globalfileerror;
3579 }
3580 globalfilepath[globalfilepathlen++] = '/';
3581 }
3582
3583 len = strlen(global.server_state_file);
3584 if (globalfilepathlen + len > MAXPATHLEN) {
3585 globalfilepathlen = 0;
3586 goto globalfileerror;
3587 }
3588 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3589 globalfilepathlen += len;
3590 globalfilepath[globalfilepathlen++] = 0;
3591 }
3592 }
3593 globalfileerror:
3594 if (globalfilepathlen == 0)
3595 globalfilepath[0] = '\0';
3596
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003597 /* Load global server state in a tree */
3598 if (globalfilepathlen > 0) {
3599 errno = 0;
3600 f = fopen(globalfilepath, "r");
3601 if (errno)
3602 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
Vedran Furac5d486272019-10-16 14:49:38 +02003603 if (!f)
3604 goto out_load_server_state_in_tree;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003605
3606 global_file_version = srv_state_get_version(f);
3607 if (global_file_version == 0)
3608 goto out_load_server_state_in_tree;
3609
3610 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3611 line = NULL;
3612
3613 line = strdup(mybuf);
3614 if (line == NULL)
3615 continue;
3616
3617 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3618 if (params[0] == NULL)
3619 continue;
3620
3621 /* bkname */
3622 bkname = params[1];
3623 /* srvname */
3624 srvname = params[3];
3625
3626 /* key */
3627 chunk_printf(&trash, "%s %s", bkname, srvname);
3628
3629 /* store line in tree */
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003630 st = calloc(1, sizeof(*st) + trash.data + 1);
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003631 if (st == NULL) {
3632 goto nextline;
3633 }
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003634 memcpy(st->name_name.key, trash.area, trash.data + 1);
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003635 ebst_insert(&state_file, &st->name_name);
3636
3637 /* save line */
3638 st->line = line;
3639
3640 continue;
3641
3642 nextline:
3643 /* free up memory in case of error during the processing of the line */
3644 free(line);
3645 }
3646 }
3647 out_load_server_state_in_tree:
3648
3649 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003650 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003651 /* servers are only in backends */
3652 if (!(curproxy->cap & PR_CAP_BE))
3653 continue;
3654 fileopenerr = 0;
3655 filepath = NULL;
3656
3657 /* search server state file path and name */
3658 switch (curproxy->load_server_state_from_file) {
3659 /* read servers state from global file */
3660 case PR_SRV_STATE_FILE_GLOBAL:
3661 /* there was an error while generating global server state file path */
3662 if (globalfilepathlen == 0)
3663 continue;
3664 filepath = globalfilepath;
3665 fileopenerr = 1;
3666 break;
3667 /* this backend has its own file */
3668 case PR_SRV_STATE_FILE_LOCAL:
3669 localfilepathlen = 0;
3670 localfilepath[0] = '\0';
3671 len = 0;
3672 /* create the localfilepath variable */
3673 /* absolute path or no base directory provided */
3674 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3675 len = strlen(curproxy->server_state_file_name);
3676 if (len > MAXPATHLEN) {
3677 localfilepathlen = 0;
3678 goto localfileerror;
3679 }
3680 memcpy(localfilepath, curproxy->server_state_file_name, len);
3681 localfilepath[len] = '\0';
3682 localfilepathlen = len;
3683 }
3684 else if (global.server_state_base) {
3685 len = strlen(global.server_state_base);
3686 localfilepathlen += len;
3687
3688 if (localfilepathlen > MAXPATHLEN) {
3689 localfilepathlen = 0;
3690 goto localfileerror;
3691 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003692 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003693 localfilepath[localfilepathlen] = 0;
3694
3695 /* append a slash if needed */
3696 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3697 if (localfilepathlen + 1 > MAXPATHLEN) {
3698 localfilepathlen = 0;
3699 goto localfileerror;
3700 }
3701 localfilepath[localfilepathlen++] = '/';
3702 }
3703
3704 len = strlen(curproxy->server_state_file_name);
3705 if (localfilepathlen + len > MAXPATHLEN) {
3706 localfilepathlen = 0;
3707 goto localfileerror;
3708 }
3709 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3710 localfilepathlen += len;
3711 localfilepath[localfilepathlen++] = 0;
3712 }
3713 filepath = localfilepath;
3714 localfileerror:
3715 if (localfilepathlen == 0)
3716 localfilepath[0] = '\0';
3717
3718 break;
3719 case PR_SRV_STATE_FILE_NONE:
3720 default:
3721 continue;
3722 }
3723
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003724 /* when global file is used, we get data from the tree
3725 * Note that in such case we don't check backend name neither uuid.
3726 * Backend name can't be wrong since it's used as a key to retrieve the server state
3727 * line from the tree.
3728 */
3729 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3730 struct server *srv = curproxy->srv;
3731 while (srv) {
3732 struct ebmb_node *node;
3733 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003734
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003735 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3736 node = ebst_lookup(&state_file, trash.area);
3737 if (!node)
3738 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003739
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003740 st = container_of(node, struct state_line, name_name);
3741 memcpy(mybuf, st->line, strlen(st->line));
3742 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003743
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003744 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3745 if (params[0] == NULL)
3746 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003747
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003748 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003749
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003750 next:
3751 srv = srv->next;
3752 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003753
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003754 continue; /* next proxy in list */
3755 }
3756 else {
3757 /* load 'local' state file */
3758 errno = 0;
3759 f = fopen(filepath, "r");
3760 if (errno && fileopenerr)
3761 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3762 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003763 continue;
3764
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003765 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003766
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003767 /* first character of first line of the file must contain the version of the export */
3768 version = srv_state_get_version(f);
3769 if (version == 0) {
3770 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3771 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003772 }
3773
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003774 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3775 int bk_f_forced_id = 0;
3776 int check_id = 0;
3777 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003778
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003779 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003780
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003781 if (params[0] == NULL) {
3782 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003783 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003784
3785 /* if line is incomplete line, then ignore it.
3786 * otherwise, update useful flags */
3787 switch (version) {
3788 case 1:
3789 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3790 check_id = (atoi(params[0]) == curproxy->uuid);
3791 check_name = (strcmp(curproxy->id, params[1]) == 0);
3792 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003793 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003794
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003795 bk = curproxy;
3796
3797 /* if backend can't be found, let's continue */
3798 if (!check_id && !check_name)
3799 continue;
3800 else if (!check_id && check_name) {
3801 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3802 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3803 }
3804 else if (check_id && !check_name) {
3805 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3806 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3807 /* if name doesn't match, we still want to update curproxy if the backend id
3808 * was forced in previous the previous configuration */
3809 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003810 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003811 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003812
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003813 /* look for the server by its name: param[3] */
3814 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003815
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003816 if (!srv) {
3817 /* if no server found, then warning and continue with next line */
3818 ha_warning("can't find server '%s' in backend '%s'\n",
3819 params[3], params[1]);
3820 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3821 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003822 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003823 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003824
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003825 /* now we can proceed with server's state update */
3826 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003827 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003828 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003829fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003830 fclose(f);
3831 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003832
3833 /* now free memory allocated for the tree */
3834 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3835 node;
3836 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3837 st = container_of(node, struct state_line, name_name);
3838 ebmb_delete(&st->name_name);
3839 free(st->line);
3840 free(st);
3841 }
3842
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003843}
3844
Simon Horman7d09b9a2013-02-12 10:45:51 +09003845/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003846 * update a server's current IP address.
3847 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3848 * ip is in network format.
3849 * updater is a string which contains an information about the requester of the update.
3850 * updater is used if not NULL.
3851 *
3852 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003853 *
3854 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003855 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003856int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003857{
3858 /* generates a log line and a warning on stderr */
3859 if (1) {
3860 /* book enough space for both IPv4 and IPv6 */
3861 char oldip[INET6_ADDRSTRLEN];
3862 char newip[INET6_ADDRSTRLEN];
3863
3864 memset(oldip, '\0', INET6_ADDRSTRLEN);
3865 memset(newip, '\0', INET6_ADDRSTRLEN);
3866
3867 /* copy old IP address in a string */
3868 switch (s->addr.ss_family) {
3869 case AF_INET:
3870 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3871 break;
3872 case AF_INET6:
3873 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3874 break;
3875 };
3876
3877 /* copy new IP address in a string */
3878 switch (ip_sin_family) {
3879 case AF_INET:
3880 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3881 break;
3882 case AF_INET6:
3883 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3884 break;
3885 };
3886
3887 /* save log line into a buffer */
3888 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3889 s->proxy->id, s->id, oldip, newip, updater);
3890
3891 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003892 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003893
3894 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003895 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003896 }
3897
3898 /* save the new IP family */
3899 s->addr.ss_family = ip_sin_family;
3900 /* save the new IP address */
3901 switch (ip_sin_family) {
3902 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003903 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003904 break;
3905 case AF_INET6:
3906 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3907 break;
3908 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003909 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003910
3911 return 0;
3912}
3913
3914/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003915 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3916 *
3917 * Caller can pass its name through <updater> to get it integrated in the response message
3918 * returned by the function.
3919 *
3920 * The function first does the following, in that order:
3921 * - validates the new addr and/or port
3922 * - checks if an update is required (new IP or port is different than current ones)
3923 * - checks the update is allowed:
3924 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3925 * - allow all changes if no CHECKS are configured
3926 * - if CHECK is configured:
3927 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3928 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3929 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003930 *
3931 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003932 */
3933const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3934{
3935 struct sockaddr_storage sa;
3936 int ret, port_change_required;
3937 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003938 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003939 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003940 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003941
3942 msg = get_trash_chunk();
3943 chunk_reset(msg);
3944
3945 if (addr) {
3946 memset(&sa, 0, sizeof(struct sockaddr_storage));
3947 if (str2ip2(addr, &sa, 0) == NULL) {
3948 chunk_printf(msg, "Invalid addr '%s'", addr);
3949 goto out;
3950 }
3951
3952 /* changes are allowed on AF_INET* families only */
3953 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3954 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3955 goto out;
3956 }
3957
3958 /* collecting data currently setup */
3959 memset(current_addr, '\0', sizeof(current_addr));
3960 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3961 /* changes are allowed on AF_INET* families only */
3962 if ((ret != AF_INET) && (ret != AF_INET6)) {
3963 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3964 goto out;
3965 }
3966
3967 /* applying ADDR changes if required and allowed
3968 * ipcmp returns 0 when both ADDR are the same
3969 */
3970 if (ipcmp(&s->addr, &sa) == 0) {
3971 chunk_appendf(msg, "no need to change the addr");
3972 goto port;
3973 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003974 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003975 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003976
3977 /* we also need to update check's ADDR only if it uses the server's one */
3978 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003979 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003980 }
3981
3982 /* we also need to update agent ADDR only if it use the server's one */
3983 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003984 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003985 }
3986
3987 /* update report for caller */
3988 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3989 }
3990
3991 port:
3992 if (port) {
3993 char sign = '\0';
3994 char *endptr;
3995
3996 if (addr)
3997 chunk_appendf(msg, ", ");
3998
3999 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01004000 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004001
4002 /* check if PORT change is required */
4003 port_change_required = 0;
4004
4005 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04004006 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004007 new_port = strtol(port, &endptr, 10);
4008 if ((errno != 0) || (port == endptr)) {
4009 chunk_appendf(msg, "problem converting port '%s' to an int", port);
4010 goto out;
4011 }
4012
4013 /* check if caller triggers a port mapped or offset */
4014 if (sign == '-' || (sign == '+')) {
4015 /* check if server currently uses port map */
4016 if (!(s->flags & SRV_F_MAPPORTS)) {
4017 /* switch from fixed port to port map mandatorily triggers
4018 * a port change */
4019 port_change_required = 1;
4020 /* check is configured
4021 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
4022 * prevent PORT change if check doesn't have it's dedicated port while switching
4023 * to port mapping */
4024 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
4025 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.");
4026 goto out;
4027 }
4028 }
4029 /* we're already using port maps */
4030 else {
4031 port_change_required = current_port != new_port;
4032 }
4033 }
4034 /* fixed port */
4035 else {
4036 port_change_required = current_port != new_port;
4037 }
4038
4039 /* applying PORT changes if required and update response message */
4040 if (port_change_required) {
4041 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01004042 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01004043 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004044
4045 /* prepare message */
4046 chunk_appendf(msg, "port changed from '");
4047 if (s->flags & SRV_F_MAPPORTS)
4048 chunk_appendf(msg, "+");
4049 chunk_appendf(msg, "%d' to '", current_port);
4050
4051 if (sign == '-') {
4052 s->flags |= SRV_F_MAPPORTS;
4053 chunk_appendf(msg, "%c", sign);
4054 /* just use for result output */
4055 new_port = -new_port;
4056 }
4057 else if (sign == '+') {
4058 s->flags |= SRV_F_MAPPORTS;
4059 chunk_appendf(msg, "%c", sign);
4060 }
4061 else {
4062 s->flags &= ~SRV_F_MAPPORTS;
4063 }
4064
4065 chunk_appendf(msg, "%d'", new_port);
4066
4067 /* we also need to update health checks port only if it uses server's realport */
4068 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
4069 s->check.port = new_port;
4070 }
4071 }
4072 else {
4073 chunk_appendf(msg, "no need to change the port");
4074 }
4075 }
4076
4077out:
Olivier Houchard4e694042017-03-14 20:01:29 +01004078 if (changed)
4079 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004080 if (updater)
4081 chunk_appendf(msg, " by '%s'", updater);
4082 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004083 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004084}
4085
4086
4087/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004088 * update server status based on result of name resolution
4089 * returns:
4090 * 0 if server status is updated
4091 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02004092 *
4093 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004094 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004095int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004096{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004097 struct dns_resolvers *resolvers = s->resolvers;
4098 struct dns_resolution *resolution = s->dns_requester->resolution;
4099 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004100
4101 switch (resolution->status) {
4102 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004103 /* status when HAProxy has just (re)started.
4104 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004105 break;
4106
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004107 case RSLV_STATUS_VALID:
4108 /*
4109 * resume health checks
4110 * server will be turned back on if health check is safe
4111 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004112 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02004113 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004114 return 1;
4115 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
4116 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02004117 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004118 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02004119
Emeric Brun52a91d32017-08-31 14:41:55 +02004120 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004121 return 1;
4122 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
4123 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
4124 s->proxy->id, s->id);
4125
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004126 ha_warning("%s.\n", trash.area);
4127 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004128 return 0;
4129
4130 case RSLV_STATUS_NX:
4131 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004132 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
4133 if (!tick_is_expired(exp, now_ms))
4134 break;
4135
4136 if (s->next_admin & SRV_ADMF_RMAINT)
4137 return 1;
4138 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
4139 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004140
4141 case RSLV_STATUS_TIMEOUT:
4142 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004143 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
4144 if (!tick_is_expired(exp, now_ms))
4145 break;
4146
4147 if (s->next_admin & SRV_ADMF_RMAINT)
4148 return 1;
4149 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
4150 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004151
4152 case RSLV_STATUS_REFUSED:
4153 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004154 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
4155 if (!tick_is_expired(exp, now_ms))
4156 break;
4157
4158 if (s->next_admin & SRV_ADMF_RMAINT)
4159 return 1;
4160 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
4161 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004162
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004163 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004164 /* stop server if resolution failed for a long enough period */
4165 exp = tick_add(resolution->last_valid, resolvers->hold.other);
4166 if (!tick_is_expired(exp, now_ms))
4167 break;
4168
4169 if (s->next_admin & SRV_ADMF_RMAINT)
4170 return 1;
4171 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
4172 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004173 }
4174
4175 return 1;
4176}
4177
4178/*
4179 * Server Name Resolution valid response callback
4180 * It expects:
4181 * - <nameserver>: the name server which answered the valid response
4182 * - <response>: buffer containing a valid DNS response
4183 * - <response_len>: size of <response>
4184 * It performs the following actions:
4185 * - ignore response if current ip found and server family not met
4186 * - update with first new ip found if family is met and current IP is not found
4187 * returns:
4188 * 0 on error
4189 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01004190 *
4191 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004192 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004193int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004194{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004195 struct server *s = NULL;
4196 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004197 void *serverip, *firstip;
4198 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004199 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02004200 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004201 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004202
Christopher Faulet67957bd2017-09-27 11:00:59 +02004203 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004204 if (!s)
4205 return 1;
4206
Christopher Faulet67957bd2017-09-27 11:00:59 +02004207 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004208
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004209 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004210 firstip = NULL; /* pointer to the first valid response found */
4211 /* it will be used as the new IP if a change is required */
4212 firstip_sin_family = AF_UNSPEC;
4213 serverip = NULL; /* current server IP address */
4214
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004215 /* initializing server IP pointer */
4216 server_sin_family = s->addr.ss_family;
4217 switch (server_sin_family) {
4218 case AF_INET:
4219 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
4220 break;
4221
4222 case AF_INET6:
4223 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
4224 break;
4225
Willy Tarreau3acfcd12017-01-06 19:18:32 +01004226 case AF_UNSPEC:
4227 break;
4228
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004229 default:
4230 goto invalid;
4231 }
4232
Baptiste Assmann729c9012017-05-22 15:13:10 +02004233 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01004234 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004235 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004236
4237 switch (ret) {
4238 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004239 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004240
4241 case DNS_UPD_SRVIP_NOT_FOUND:
4242 goto save_ip;
4243
4244 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004245 goto invalid;
4246
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004247 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004248 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004249 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004250
Baptiste Assmannfad03182015-10-28 02:03:32 +01004251 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01004252 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004253 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004254 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01004255
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004256 default:
4257 goto invalid;
4258
4259 }
4260
4261 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004262 if (nameserver) {
4263 nameserver->counters.update++;
4264 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004265 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004266 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004267 else
4268 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004269 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004270
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004271 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004272 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004273 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004274
4275 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004276 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02004277 nameserver->counters.invalid++;
4278 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004279 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004280 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004281 return 0;
4282}
4283
4284/*
4285 * Server Name Resolution error management callback
4286 * returns:
4287 * 0 on error
4288 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004289 *
4290 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004291 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004292int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004293{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004294 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004295
Christopher Faulet67957bd2017-09-27 11:00:59 +02004296 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004297 if (!s)
4298 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004299 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004300 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004301 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004302 return 1;
4303}
4304
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004305/*
4306 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004307 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004308 * It returns a pointer to the first server found or NULL if <ip> is not yet
4309 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004310 *
4311 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004312 */
4313struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4314{
4315 struct server *tmpsrv;
4316 struct proxy *be;
4317
4318 if (!srv)
4319 return NULL;
4320
4321 be = srv->proxy;
4322 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004323 /* we found the current server is the same, ignore it */
4324 if (srv == tmpsrv)
4325 continue;
4326
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004327 /* We want to compare the IP in the record with the IP of the servers in the
4328 * same backend, only if:
4329 * * DNS resolution is enabled on the server
4330 * * the hostname used for the resolution by our server is the same than the
4331 * one used for the server found in the backend
4332 * * the server found in the backend is not our current server
4333 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004334 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004335 if ((tmpsrv->hostname_dn == NULL) ||
4336 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4337 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004338 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004339 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004340 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004341 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004342
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004343 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004344 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004345 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004346 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004347 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004348
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004349 /* At this point, we have 2 different servers using the same DNS hostname
4350 * for their respective resolution.
4351 */
4352 if (*ip_family == tmpsrv->addr.ss_family &&
4353 ((tmpsrv->addr.ss_family == AF_INET &&
4354 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4355 (tmpsrv->addr.ss_family == AF_INET6 &&
4356 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004357 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004358 return tmpsrv;
4359 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004360 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004361 }
4362
Emeric Brune9fd6b52017-11-02 17:20:39 +01004363
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004364 return NULL;
4365}
4366
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004367/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4368 * resolver. This is suited for initial address configuration. Returns 0 on
4369 * success otherwise a non-zero error code. In case of error, *err_code, if
4370 * not NULL, is filled up.
4371 */
4372int srv_set_addr_via_libc(struct server *srv, int *err_code)
4373{
4374 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004375 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004376 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004377 return 1;
4378 }
4379 return 0;
4380}
4381
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004382/* Set the server's FDQN (->hostname) from <hostname>.
4383 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004384 *
4385 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004386 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004387int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004388{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004389 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004390 char *hostname_dn;
4391 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004392
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004393 /* Note that the server lock is already held. */
4394 if (!srv->resolvers)
4395 return -1;
4396
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004397 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004398 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004399 /* run time DNS resolution was not active for this server
4400 * and we can't enable it at run time for now.
4401 */
4402 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004403 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004404
4405 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004406 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004407 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004408 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4409 hostname_dn, trash.size);
4410 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004411 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004412
Christopher Faulet67957bd2017-09-27 11:00:59 +02004413 resolution = srv->dns_requester->resolution;
4414 if (resolution &&
4415 resolution->hostname_dn &&
4416 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004417 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004418
Christopher Faulet67957bd2017-09-27 11:00:59 +02004419 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004420
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004421 free(srv->hostname);
4422 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004423 srv->hostname = strdup(hostname);
4424 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004425 srv->hostname_dn_len = hostname_dn_len;
4426 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004427 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004428
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004429 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004430 goto err;
4431
4432 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004433 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004434 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004435 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004436
4437 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004438 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004439 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004440 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004441}
4442
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004443/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4444 * from the state file. This is suited for initial address configuration.
4445 * Returns 0 on success otherwise a non-zero error code. In case of error,
4446 * *err_code, if not NULL, is filled up.
4447 */
4448static int srv_apply_lastaddr(struct server *srv, int *err_code)
4449{
4450 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4451 if (err_code)
4452 *err_code |= ERR_WARN;
4453 return 1;
4454 }
4455 return 0;
4456}
4457
Willy Tarreau25e51522016-11-04 15:10:17 +01004458/* returns 0 if no error, otherwise a combination of ERR_* flags */
4459static int srv_iterate_initaddr(struct server *srv)
4460{
4461 int return_code = 0;
4462 int err_code;
4463 unsigned int methods;
4464
4465 methods = srv->init_addr_methods;
4466 if (!methods) { // default to "last,libc"
4467 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4468 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4469 }
4470
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004471 /* "-dr" : always append "none" so that server addresses resolution
4472 * failures are silently ignored, this is convenient to validate some
4473 * configs out of their environment.
4474 */
4475 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4476 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4477
Willy Tarreau25e51522016-11-04 15:10:17 +01004478 while (methods) {
4479 err_code = 0;
4480 switch (srv_get_next_initaddr(&methods)) {
4481 case SRV_IADDR_LAST:
4482 if (!srv->lastaddr)
4483 continue;
4484 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004485 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004486 return_code |= err_code;
4487 break;
4488
4489 case SRV_IADDR_LIBC:
4490 if (!srv->hostname)
4491 continue;
4492 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004493 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004494 return_code |= err_code;
4495 break;
4496
Willy Tarreau37ebe122016-11-04 15:17:58 +01004497 case SRV_IADDR_NONE:
4498 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004499 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004500 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4501 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004502 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004503 return return_code;
4504
Willy Tarreau4310d362016-11-02 15:05:56 +01004505 case SRV_IADDR_IP:
4506 ipcpy(&srv->init_addr, &srv->addr);
4507 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004508 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4509 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004510 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004511 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004512
Willy Tarreau25e51522016-11-04 15:10:17 +01004513 default: /* unhandled method */
4514 break;
4515 }
4516 }
4517
4518 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004519 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004520 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4521 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004522 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004523 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004524 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4525 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004526
4527 return_code |= ERR_ALERT | ERR_FATAL;
4528 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004529out:
4530 srv_set_dyncookie(srv);
4531 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004532}
4533
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004534/*
4535 * This function parses all backends and all servers within each backend
4536 * and performs servers' addr resolution based on information provided by:
4537 * - configuration file
4538 * - server-state file (states provided by an 'old' haproxy process)
4539 *
4540 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4541 */
4542int srv_init_addr(void)
4543{
4544 struct proxy *curproxy;
4545 int return_code = 0;
4546
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004547 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004548 while (curproxy) {
4549 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004550
4551 /* servers are in backend only */
4552 if (!(curproxy->cap & PR_CAP_BE))
4553 goto srv_init_addr_next;
4554
Willy Tarreau25e51522016-11-04 15:10:17 +01004555 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004556 if (srv->hostname)
4557 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004558
4559 srv_init_addr_next:
4560 curproxy = curproxy->next;
4561 }
4562
4563 return return_code;
4564}
4565
Willy Tarreau46b7f532018-08-21 11:54:26 +02004566/*
4567 * Must be called with the server lock held.
4568 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004569const 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 +02004570{
4571
Willy Tarreau83061a82018-07-13 11:56:34 +02004572 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004573
4574 msg = get_trash_chunk();
4575 chunk_reset(msg);
4576
Olivier Houchard8da5f982017-08-04 18:35:36 +02004577 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004578 chunk_appendf(msg, "no need to change the FDQN");
4579 goto out;
4580 }
4581
4582 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4583 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4584 goto out;
4585 }
4586
4587 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4588 server->proxy->id, server->id, server->hostname, fqdn);
4589
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004590 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004591 chunk_reset(msg);
4592 chunk_appendf(msg, "could not update %s/%s FQDN",
4593 server->proxy->id, server->id);
4594 goto out;
4595 }
4596
4597 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004598 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004599
4600 out:
4601 if (updater)
4602 chunk_appendf(msg, " by '%s'", updater);
4603 chunk_appendf(msg, "\n");
4604
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004605 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004606}
4607
4608
Willy Tarreau21b069d2016-11-23 17:15:08 +01004609/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4610 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004611 * 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 +01004612 * used for CLI commands requiring a server name.
4613 * Important: the <arg> is modified to remove the '/'.
4614 */
4615struct server *cli_find_server(struct appctx *appctx, char *arg)
4616{
4617 struct proxy *px;
4618 struct server *sv;
4619 char *line;
4620
4621 /* split "backend/server" and make <line> point to server */
4622 for (line = arg; *line; line++)
4623 if (*line == '/') {
4624 *line++ = '\0';
4625 break;
4626 }
4627
4628 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004629 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004630 return NULL;
4631 }
4632
4633 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004634 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004635 return NULL;
4636 }
4637
4638 if (px->state == PR_STSTOPPED) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004639 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004640 return NULL;
4641 }
4642
4643 return sv;
4644}
4645
William Lallemand222baf22016-11-19 02:00:33 +01004646
Willy Tarreau46b7f532018-08-21 11:54:26 +02004647/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004648static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004649{
4650 struct server *sv;
4651 const char *warning;
4652
4653 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4654 return 1;
4655
4656 sv = cli_find_server(appctx, args[2]);
4657 if (!sv)
4658 return 1;
4659
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004660 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004661
William Lallemand222baf22016-11-19 02:00:33 +01004662 if (strcmp(args[3], "weight") == 0) {
4663 warning = server_parse_weight_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004664 if (warning)
4665 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004666 }
4667 else if (strcmp(args[3], "state") == 0) {
4668 if (strcmp(args[4], "ready") == 0)
4669 srv_adm_set_ready(sv);
4670 else if (strcmp(args[4], "drain") == 0)
4671 srv_adm_set_drain(sv);
4672 else if (strcmp(args[4], "maint") == 0)
4673 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004674 else
4675 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004676 }
4677 else if (strcmp(args[3], "health") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004678 if (sv->track)
4679 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004680 else if (strcmp(args[4], "up") == 0) {
4681 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004682 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004683 }
4684 else if (strcmp(args[4], "stopping") == 0) {
4685 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004686 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004687 }
4688 else if (strcmp(args[4], "down") == 0) {
4689 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004690 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004691 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004692 else
4693 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004694 }
4695 else if (strcmp(args[3], "agent") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004696 if (!(sv->agent.state & CHK_ST_ENABLED))
4697 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004698 else if (strcmp(args[4], "up") == 0) {
4699 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004700 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004701 }
4702 else if (strcmp(args[4], "down") == 0) {
4703 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004704 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004705 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004706 else
4707 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004708 }
Misiek2da082d2017-01-09 09:40:42 +01004709 else if (strcmp(args[3], "agent-addr") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004710 if (!(sv->agent.state & CHK_ST_ENABLED))
4711 cli_err(appctx, "agent checks are not enabled on this server.\n");
4712 else if (str2ip(args[4], &sv->agent.addr) == NULL)
4713 cli_err(appctx, "incorrect addr address given for agent.\n");
Misiek2da082d2017-01-09 09:40:42 +01004714 }
4715 else if (strcmp(args[3], "agent-send") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004716 if (!(sv->agent.state & CHK_ST_ENABLED))
4717 cli_err(appctx, "agent checks are not enabled on this server.\n");
4718 else {
Misiek2da082d2017-01-09 09:40:42 +01004719 char *nss = strdup(args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004720 if (!nss)
4721 cli_err(appctx, "cannot allocate memory for new string.\n");
4722 else {
Misiek2da082d2017-01-09 09:40:42 +01004723 free(sv->agent.send_string);
4724 sv->agent.send_string = nss;
4725 sv->agent.send_string_len = strlen(args[4]);
4726 }
4727 }
4728 }
William Lallemand222baf22016-11-19 02:00:33 +01004729 else if (strcmp(args[3], "check-port") == 0) {
4730 int i = 0;
4731 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004732 cli_err(appctx, "'set server <srv> check-port' expects an integer as argument.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004733 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004734 }
4735 if ((i < 0) || (i > 65535)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004736 cli_err(appctx, "provided port is not valid.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004737 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004738 }
4739 /* prevent the update of port to 0 if MAPPORTS are in use */
4740 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004741 cli_err(appctx, "can't unset 'port' since MAPPORTS is in use.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004742 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004743 }
4744 sv->check.port = i;
Willy Tarreau9d008692019-08-09 11:21:01 +02004745 cli_msg(appctx, LOG_NOTICE, "health check port updated.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004746 }
4747 else if (strcmp(args[3], "addr") == 0) {
4748 char *addr = NULL;
4749 char *port = NULL;
4750 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004751 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004752 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004753 }
4754 else {
4755 addr = args[4];
4756 }
4757 if (strcmp(args[5], "port") == 0) {
4758 port = args[6];
4759 }
4760 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004761 if (warning)
4762 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004763 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4764 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004765 else if (strcmp(args[3], "fqdn") == 0) {
4766 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004767 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004768 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004769 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004770 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Willy Tarreau9d008692019-08-09 11:21:01 +02004771 if (warning)
4772 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004773 }
William Lallemand222baf22016-11-19 02:00:33 +01004774 else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004775 cli_err(appctx,
4776 "'set server <srv>' only supports 'agent', 'health', 'state',"
4777 " 'weight', 'addr', 'fqdn' and 'check-port'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004778 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004779 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004780 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004781 return 1;
4782}
4783
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004784static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004785{
4786 struct stream_interface *si = appctx->owner;
4787 struct proxy *px;
4788 struct server *sv;
4789 char *line;
4790
4791
4792 /* split "backend/server" and make <line> point to server */
4793 for (line = args[2]; *line; line++)
4794 if (*line == '/') {
4795 *line++ = '\0';
4796 break;
4797 }
4798
Willy Tarreau9d008692019-08-09 11:21:01 +02004799 if (!*line)
4800 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004801
Willy Tarreau9d008692019-08-09 11:21:01 +02004802 if (!get_backend_server(args[2], line, &px, &sv))
4803 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004804
4805 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004806 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4807 sv->iweight);
4808 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004809 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004810 return 0;
4811 }
William Lallemand6b160942016-11-22 12:34:35 +01004812 return 1;
4813}
4814
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004815/* Parse a "set weight" command.
4816 *
4817 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004818 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004819static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004820{
4821 struct server *sv;
4822 const char *warning;
4823
4824 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4825 return 1;
4826
4827 sv = cli_find_server(appctx, args[2]);
4828 if (!sv)
4829 return 1;
4830
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004831 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4832
William Lallemand6b160942016-11-22 12:34:35 +01004833 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004834 if (warning)
4835 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004836
4837 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4838
William Lallemand6b160942016-11-22 12:34:35 +01004839 return 1;
4840}
4841
Willy Tarreau46b7f532018-08-21 11:54:26 +02004842/* parse a "set maxconn server" command. It always returns 1.
4843 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004844 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004845 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004846static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004847{
4848 struct server *sv;
4849 const char *warning;
4850
4851 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4852 return 1;
4853
4854 sv = cli_find_server(appctx, args[3]);
4855 if (!sv)
4856 return 1;
4857
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004858 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4859
Willy Tarreaub8026272016-11-23 11:26:56 +01004860 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004861 if (warning)
4862 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004863
4864 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4865
Willy Tarreaub8026272016-11-23 11:26:56 +01004866 return 1;
4867}
William Lallemand6b160942016-11-22 12:34:35 +01004868
Willy Tarreau46b7f532018-08-21 11:54:26 +02004869/* parse a "disable agent" command. It always returns 1.
4870 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004871 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004872 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004873static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004874{
4875 struct server *sv;
4876
4877 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4878 return 1;
4879
4880 sv = cli_find_server(appctx, args[2]);
4881 if (!sv)
4882 return 1;
4883
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004884 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004885 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004886 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004887 return 1;
4888}
4889
Willy Tarreau46b7f532018-08-21 11:54:26 +02004890/* parse a "disable health" command. It always returns 1.
4891 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004892 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004893 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004894static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004895{
4896 struct server *sv;
4897
4898 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4899 return 1;
4900
4901 sv = cli_find_server(appctx, args[2]);
4902 if (!sv)
4903 return 1;
4904
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004905 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004906 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004907 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004908 return 1;
4909}
4910
Willy Tarreau46b7f532018-08-21 11:54:26 +02004911/* parse a "disable server" command. It always returns 1.
4912 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004913 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004914 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004915static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004916{
4917 struct server *sv;
4918
4919 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4920 return 1;
4921
4922 sv = cli_find_server(appctx, args[2]);
4923 if (!sv)
4924 return 1;
4925
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004926 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004927 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004928 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004929 return 1;
4930}
4931
Willy Tarreau46b7f532018-08-21 11:54:26 +02004932/* parse a "enable agent" command. It always returns 1.
4933 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004934 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004935 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004936static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004937{
4938 struct server *sv;
4939
4940 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4941 return 1;
4942
4943 sv = cli_find_server(appctx, args[2]);
4944 if (!sv)
4945 return 1;
4946
Willy Tarreau9d008692019-08-09 11:21:01 +02004947 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4948 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004949
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004950 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004951 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004952 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004953 return 1;
4954}
4955
Willy Tarreau46b7f532018-08-21 11:54:26 +02004956/* parse a "enable health" command. It always returns 1.
4957 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004958 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004959 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004960static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004961{
4962 struct server *sv;
4963
4964 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4965 return 1;
4966
4967 sv = cli_find_server(appctx, args[2]);
4968 if (!sv)
4969 return 1;
4970
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004971 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004972 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004973 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004974 return 1;
4975}
4976
Willy Tarreau46b7f532018-08-21 11:54:26 +02004977/* parse a "enable server" command. It always returns 1.
4978 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004979 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004980 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004981static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004982{
4983 struct server *sv;
4984
4985 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4986 return 1;
4987
4988 sv = cli_find_server(appctx, args[2]);
4989 if (!sv)
4990 return 1;
4991
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004992 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004993 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004994 if (!(sv->flags & SRV_F_COOKIESET)
4995 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4996 sv->cookie)
4997 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004998 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004999 return 1;
5000}
5001
William Lallemand222baf22016-11-19 02:00:33 +01005002/* register cli keywords */
5003static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005004 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005005 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01005006 { { "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 +01005007 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005008 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01005009 { { "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 +01005010 { { "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 +01005011 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01005012 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
5013 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
5014
William Lallemand222baf22016-11-19 02:00:33 +01005015 {{},}
5016}};
5017
Willy Tarreau0108d902018-11-25 19:14:37 +01005018INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01005019
Emeric Brun64cc49c2017-10-03 14:46:45 +02005020/*
5021 * This function applies server's status changes, it is
5022 * is designed to be called asynchronously.
5023 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02005024 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005025 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005026static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005027{
5028 struct check *check = &s->check;
5029 int xferred;
5030 struct proxy *px = s->proxy;
5031 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5032 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5033 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02005034 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005035
Emeric Brun64cc49c2017-10-03 14:46:45 +02005036 /* If currently main is not set we try to apply pending state changes */
5037 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
5038 int next_admin;
5039
5040 /* Backup next admin */
5041 next_admin = s->next_admin;
5042
5043 /* restore current admin state */
5044 s->next_admin = s->cur_admin;
5045
5046 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
5047 s->last_change = now.tv_sec;
5048 if (s->proxy->lbprm.set_server_status_down)
5049 s->proxy->lbprm.set_server_status_down(s);
5050
5051 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5052 srv_shutdown_streams(s, SF_ERR_DOWN);
5053
5054 /* we might have streams queued on this server and waiting for
5055 * a connection. Those which are redispatchable will be queued
5056 * to another server or to the proxy itself.
5057 */
5058 xferred = pendconn_redistribute(s);
5059
5060 tmptrash = alloc_trash_chunk();
5061 if (tmptrash) {
5062 chunk_printf(tmptrash,
5063 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5064 s->proxy->id, s->id);
5065
Emeric Brun5a133512017-10-19 14:42:30 +02005066 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005067 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005068
5069 /* we don't send an alert if the server was previously paused */
5070 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005071 send_log(s->proxy, log_level, "%s.\n",
5072 tmptrash->area);
5073 send_email_alert(s, log_level, "%s",
5074 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005075 free_trash_chunk(tmptrash);
5076 tmptrash = NULL;
5077 }
5078 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5079 set_backend_down(s->proxy);
5080
5081 s->counters.down_trans++;
5082 }
5083 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
5084 s->last_change = now.tv_sec;
5085 if (s->proxy->lbprm.set_server_status_down)
5086 s->proxy->lbprm.set_server_status_down(s);
5087
5088 /* we might have streams queued on this server and waiting for
5089 * a connection. Those which are redispatchable will be queued
5090 * to another server or to the proxy itself.
5091 */
5092 xferred = pendconn_redistribute(s);
5093
5094 tmptrash = alloc_trash_chunk();
5095 if (tmptrash) {
5096 chunk_printf(tmptrash,
5097 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5098 s->proxy->id, s->id);
5099
Emeric Brun5a133512017-10-19 14:42:30 +02005100 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005101
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005102 ha_warning("%s.\n", tmptrash->area);
5103 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5104 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005105 free_trash_chunk(tmptrash);
5106 tmptrash = NULL;
5107 }
5108
5109 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5110 set_backend_down(s->proxy);
5111 }
5112 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5113 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
5114 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5115 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5116 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5117 s->proxy->last_change = now.tv_sec;
5118 }
5119
5120 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
5121 s->down_time += now.tv_sec - s->last_change;
5122
5123 s->last_change = now.tv_sec;
5124 if (s->next_state == SRV_ST_STARTING)
5125 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5126
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005127 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005128 /* now propagate the status change to any LB algorithms */
5129 if (px->lbprm.update_server_eweight)
5130 px->lbprm.update_server_eweight(s);
5131 else if (srv_willbe_usable(s)) {
5132 if (px->lbprm.set_server_status_up)
5133 px->lbprm.set_server_status_up(s);
5134 }
5135 else {
5136 if (px->lbprm.set_server_status_down)
5137 px->lbprm.set_server_status_down(s);
5138 }
5139
5140 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5141 * and it's not a backup server and its effective weight is > 0,
5142 * then it can accept new connections, so we shut down all streams
5143 * on all backup servers.
5144 */
5145 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5146 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5147 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5148
5149 /* check if we can handle some connections queued at the proxy. We
5150 * will take as many as we can handle.
5151 */
5152 xferred = pendconn_grab_from_px(s);
5153
5154 tmptrash = alloc_trash_chunk();
5155 if (tmptrash) {
5156 chunk_printf(tmptrash,
5157 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5158 s->proxy->id, s->id);
5159
Emeric Brun5a133512017-10-19 14:42:30 +02005160 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005161 ha_warning("%s.\n", tmptrash->area);
5162 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5163 tmptrash->area);
5164 send_email_alert(s, LOG_NOTICE, "%s",
5165 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005166 free_trash_chunk(tmptrash);
5167 tmptrash = NULL;
5168 }
5169
5170 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5171 set_backend_down(s->proxy);
5172 }
5173 else if (s->cur_eweight != s->next_eweight) {
5174 /* now propagate the status change to any LB algorithms */
5175 if (px->lbprm.update_server_eweight)
5176 px->lbprm.update_server_eweight(s);
5177 else if (srv_willbe_usable(s)) {
5178 if (px->lbprm.set_server_status_up)
5179 px->lbprm.set_server_status_up(s);
5180 }
5181 else {
5182 if (px->lbprm.set_server_status_down)
5183 px->lbprm.set_server_status_down(s);
5184 }
5185
5186 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5187 set_backend_down(s->proxy);
5188 }
5189
5190 s->next_admin = next_admin;
5191 }
5192
Emeric Brun5a133512017-10-19 14:42:30 +02005193 /* reset operational state change */
5194 *s->op_st_chg.reason = 0;
5195 s->op_st_chg.status = s->op_st_chg.code = -1;
5196 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005197
5198 /* Now we try to apply pending admin changes */
5199
5200 /* Maintenance must also disable health checks */
5201 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5202 if (s->check.state & CHK_ST_ENABLED) {
5203 s->check.state |= CHK_ST_PAUSED;
5204 check->health = 0;
5205 }
5206
5207 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005208 tmptrash = alloc_trash_chunk();
5209 if (tmptrash) {
5210 chunk_printf(tmptrash,
5211 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5212 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5213 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005214
Olivier Houchard796a2b32017-10-24 17:42:47 +02005215 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005216
Olivier Houchard796a2b32017-10-24 17:42:47 +02005217 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005218 ha_warning("%s.\n", tmptrash->area);
5219 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5220 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005221 }
5222 free_trash_chunk(tmptrash);
5223 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005224 }
Emeric Brun8f298292017-12-06 16:47:17 +01005225 /* commit new admin status */
5226
5227 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005228 }
5229 else { /* server was still running */
5230 check->health = 0; /* failure */
5231 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005232
5233 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005234 if (s->proxy->lbprm.set_server_status_down)
5235 s->proxy->lbprm.set_server_status_down(s);
5236
Emeric Brun64cc49c2017-10-03 14:46:45 +02005237 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5238 srv_shutdown_streams(s, SF_ERR_DOWN);
5239
5240 /* we might have streams queued on this server and waiting for
5241 * a connection. Those which are redispatchable will be queued
5242 * to another server or to the proxy itself.
5243 */
5244 xferred = pendconn_redistribute(s);
5245
5246 tmptrash = alloc_trash_chunk();
5247 if (tmptrash) {
5248 chunk_printf(tmptrash,
5249 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5250 s->flags & SRV_F_BACKUP ? "Backup " : "",
5251 s->proxy->id, s->id,
5252 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5253
5254 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5255
5256 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005257 ha_warning("%s.\n", tmptrash->area);
5258 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5259 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005260 }
5261 free_trash_chunk(tmptrash);
5262 tmptrash = NULL;
5263 }
5264 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5265 set_backend_down(s->proxy);
5266
5267 s->counters.down_trans++;
5268 }
5269 }
5270 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5271 /* OK here we're leaving maintenance, we have many things to check,
5272 * because the server might possibly be coming back up depending on
5273 * its state. In practice, leaving maintenance means that we should
5274 * immediately turn to UP (more or less the slowstart) under the
5275 * following conditions :
5276 * - server is neither checked nor tracked
5277 * - server tracks another server which is not checked
5278 * - server tracks another server which is already up
5279 * Which sums up as something simpler :
5280 * "either the tracking server is up or the server's checks are disabled
5281 * or up". Otherwise we only re-enable health checks. There's a special
5282 * case associated to the stopping state which can be inherited. Note
5283 * that the server might still be in drain mode, which is naturally dealt
5284 * with by the lower level functions.
5285 */
5286
5287 if (s->check.state & CHK_ST_ENABLED) {
5288 s->check.state &= ~CHK_ST_PAUSED;
5289 check->health = check->rise; /* start OK but check immediately */
5290 }
5291
5292 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5293 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5294 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5295 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5296 s->next_state = SRV_ST_STOPPING;
5297 }
5298 else {
5299 s->next_state = SRV_ST_STARTING;
5300 if (s->slowstart > 0)
5301 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5302 else
5303 s->next_state = SRV_ST_RUNNING;
5304 }
5305
5306 }
5307
5308 tmptrash = alloc_trash_chunk();
5309 if (tmptrash) {
5310 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5311 chunk_printf(tmptrash,
5312 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5313 s->flags & SRV_F_BACKUP ? "Backup " : "",
5314 s->proxy->id, s->id,
5315 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5316 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5317 }
5318 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5319 chunk_printf(tmptrash,
5320 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5321 s->flags & SRV_F_BACKUP ? "Backup " : "",
5322 s->proxy->id, s->id, s->hostname,
5323 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5324 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5325 }
5326 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5327 chunk_printf(tmptrash,
5328 "%sServer %s/%s is %s/%s (leaving maintenance)",
5329 s->flags & SRV_F_BACKUP ? "Backup " : "",
5330 s->proxy->id, s->id,
5331 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5332 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5333 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005334 ha_warning("%s.\n", tmptrash->area);
5335 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5336 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005337 free_trash_chunk(tmptrash);
5338 tmptrash = NULL;
5339 }
5340
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005341 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005342 /* now propagate the status change to any LB algorithms */
5343 if (px->lbprm.update_server_eweight)
5344 px->lbprm.update_server_eweight(s);
5345 else if (srv_willbe_usable(s)) {
5346 if (px->lbprm.set_server_status_up)
5347 px->lbprm.set_server_status_up(s);
5348 }
5349 else {
5350 if (px->lbprm.set_server_status_down)
5351 px->lbprm.set_server_status_down(s);
5352 }
5353
5354 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5355 set_backend_down(s->proxy);
5356
Willy Tarreau6a78e612018-08-07 10:14:53 +02005357 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5358 * and it's not a backup server and its effective weight is > 0,
5359 * then it can accept new connections, so we shut down all streams
5360 * on all backup servers.
5361 */
5362 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5363 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5364 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5365
5366 /* check if we can handle some connections queued at the proxy. We
5367 * will take as many as we can handle.
5368 */
5369 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005370 }
5371 else if (s->next_admin & SRV_ADMF_MAINT) {
5372 /* remaining in maintenance mode, let's inform precisely about the
5373 * situation.
5374 */
5375 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5376 tmptrash = alloc_trash_chunk();
5377 if (tmptrash) {
5378 chunk_printf(tmptrash,
5379 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5380 s->flags & SRV_F_BACKUP ? "Backup " : "",
5381 s->proxy->id, s->id);
5382
5383 if (s->track) /* normally it's mandatory here */
5384 chunk_appendf(tmptrash, " via %s/%s",
5385 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005386 ha_warning("%s.\n", tmptrash->area);
5387 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5388 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005389 free_trash_chunk(tmptrash);
5390 tmptrash = NULL;
5391 }
5392 }
5393 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5394 tmptrash = alloc_trash_chunk();
5395 if (tmptrash) {
5396 chunk_printf(tmptrash,
5397 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5398 s->flags & SRV_F_BACKUP ? "Backup " : "",
5399 s->proxy->id, s->id, s->hostname);
5400
5401 if (s->track) /* normally it's mandatory here */
5402 chunk_appendf(tmptrash, " via %s/%s",
5403 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005404 ha_warning("%s.\n", tmptrash->area);
5405 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5406 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005407 free_trash_chunk(tmptrash);
5408 tmptrash = NULL;
5409 }
5410 }
5411 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5412 tmptrash = alloc_trash_chunk();
5413 if (tmptrash) {
5414 chunk_printf(tmptrash,
5415 "%sServer %s/%s remains in forced maintenance",
5416 s->flags & SRV_F_BACKUP ? "Backup " : "",
5417 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005418 ha_warning("%s.\n", tmptrash->area);
5419 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5420 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005421 free_trash_chunk(tmptrash);
5422 tmptrash = NULL;
5423 }
5424 }
5425 /* don't report anything when leaving drain mode and remaining in maintenance */
5426
5427 s->cur_admin = s->next_admin;
5428 }
5429
5430 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5431 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5432 /* drain state is applied only if not yet in maint */
5433
5434 s->last_change = now.tv_sec;
5435 if (px->lbprm.set_server_status_down)
5436 px->lbprm.set_server_status_down(s);
5437
5438 /* we might have streams queued on this server and waiting for
5439 * a connection. Those which are redispatchable will be queued
5440 * to another server or to the proxy itself.
5441 */
5442 xferred = pendconn_redistribute(s);
5443
5444 tmptrash = alloc_trash_chunk();
5445 if (tmptrash) {
5446 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5447 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5448 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5449
5450 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5451
5452 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005453 ha_warning("%s.\n", tmptrash->area);
5454 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5455 tmptrash->area);
5456 send_email_alert(s, LOG_NOTICE, "%s",
5457 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005458 }
5459 free_trash_chunk(tmptrash);
5460 tmptrash = NULL;
5461 }
5462
5463 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5464 set_backend_down(s->proxy);
5465 }
5466 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5467 /* OK completely leaving drain mode */
5468 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5469 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5470 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5471 s->proxy->last_change = now.tv_sec;
5472 }
5473
5474 if (s->last_change < now.tv_sec) // ignore negative times
5475 s->down_time += now.tv_sec - s->last_change;
5476 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005477 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005478
5479 tmptrash = alloc_trash_chunk();
5480 if (tmptrash) {
5481 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5482 chunk_printf(tmptrash,
5483 "%sServer %s/%s is %s (leaving forced drain)",
5484 s->flags & SRV_F_BACKUP ? "Backup " : "",
5485 s->proxy->id, s->id,
5486 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5487 }
5488 else {
5489 chunk_printf(tmptrash,
5490 "%sServer %s/%s is %s (leaving drain)",
5491 s->flags & SRV_F_BACKUP ? "Backup " : "",
5492 s->proxy->id, s->id,
5493 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5494 if (s->track) /* normally it's mandatory here */
5495 chunk_appendf(tmptrash, " via %s/%s",
5496 s->track->proxy->id, s->track->id);
5497 }
5498
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005499 ha_warning("%s.\n", tmptrash->area);
5500 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5501 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005502 free_trash_chunk(tmptrash);
5503 tmptrash = NULL;
5504 }
5505
5506 /* now propagate the status change to any LB algorithms */
5507 if (px->lbprm.update_server_eweight)
5508 px->lbprm.update_server_eweight(s);
5509 else if (srv_willbe_usable(s)) {
5510 if (px->lbprm.set_server_status_up)
5511 px->lbprm.set_server_status_up(s);
5512 }
5513 else {
5514 if (px->lbprm.set_server_status_down)
5515 px->lbprm.set_server_status_down(s);
5516 }
5517 }
5518 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5519 /* remaining in drain mode after removing one of its flags */
5520
5521 tmptrash = alloc_trash_chunk();
5522 if (tmptrash) {
5523 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5524 chunk_printf(tmptrash,
5525 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5526 s->flags & SRV_F_BACKUP ? "Backup " : "",
5527 s->proxy->id, s->id);
5528
5529 if (s->track) /* normally it's mandatory here */
5530 chunk_appendf(tmptrash, " via %s/%s",
5531 s->track->proxy->id, s->track->id);
5532 }
5533 else {
5534 chunk_printf(tmptrash,
5535 "%sServer %s/%s remains in forced drain mode",
5536 s->flags & SRV_F_BACKUP ? "Backup " : "",
5537 s->proxy->id, s->id);
5538 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005539 ha_warning("%s.\n", tmptrash->area);
5540 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5541 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005542 free_trash_chunk(tmptrash);
5543 tmptrash = NULL;
5544 }
5545
5546 /* commit new admin status */
5547
5548 s->cur_admin = s->next_admin;
5549 }
5550 }
5551
5552 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005553 *s->adm_st_chg_cause = 0;
5554}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005555
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005556struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5557{
5558 struct connection *conn;
5559
Olivier Houchard859dc802019-08-08 15:47:21 +02005560 while ((conn = MT_LIST_POP(&toremove_connections[tid],
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005561 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005562 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005563 }
5564
5565 return task;
5566}
5567
Willy Tarreau980855b2019-02-07 14:59:29 +01005568struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005569{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005570 struct server *srv;
5571 struct eb32_node *eb;
5572 int i;
5573 unsigned int next_wakeup;
5574 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005575
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005576 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5577 while (1) {
5578 int srv_is_empty = 1;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005579
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005580 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5581 if (!eb) {
5582 /* we might have reached the end of the tree, typically because
5583 * <now_ms> is in the first half and we're first scanning the last
5584 * half. Let's loop back to the beginning of the tree now.
5585 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005586
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005587 eb = eb32_first(&idle_conn_srv);
5588 if (likely(!eb))
5589 break;
5590 }
5591 if (tick_is_lt(now_ms, eb->key)) {
5592 /* timer not expired yet, revisit it later */
5593 next_wakeup = eb->key;
5594 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005595 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005596 }
5597 srv = eb32_entry(eb, struct server, idle_node);
5598 for (i = 0; i < global.nbthread; i++) {
5599 int max_conn = (srv->curr_idle_thr[i] / 2) +
5600 (srv->curr_idle_thr[i] & 1);
5601 int j;
5602 int did_remove = 0;
5603
Olivier Houchard4be71902019-07-11 15:49:00 +02005604 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005605 for (j = 0; j < max_conn; j++) {
Olivier Houchard859dc802019-08-08 15:47:21 +02005606 struct connection *conn = MT_LIST_POP(&srv->idle_orphan_conns[i], struct connection *, list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005607 if (!conn)
5608 break;
5609 did_remove = 1;
Olivier Houchard859dc802019-08-08 15:47:21 +02005610 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005611 }
Olivier Houchard4be71902019-07-11 15:49:00 +02005612 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005613 if (did_remove && max_conn < srv->curr_idle_thr[i])
5614 srv_is_empty = 0;
5615 if (did_remove)
5616 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5617 }
5618 eb32_delete(&srv->idle_node);
5619 if (!srv_is_empty) {
5620 /* There are still more idle connections, add the
5621 * server back in the tree.
5622 */
5623 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5624 now_ms);
5625 eb32_insert(&idle_conn_srv, &srv->idle_node);
5626 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005627 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005628 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5629
5630 if (need_wakeup)
5631 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005632 else
5633 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005634
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005635 return task;
5636}
Olivier Houchard88698d92019-04-16 19:07:22 +02005637
5638/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5639static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5640 struct proxy *defpx, const char *file, int line,
5641 char **err)
5642{
5643 int arg = -1;
5644
5645 if (too_many_args(1, args, err, NULL))
5646 return -1;
5647
5648 if (*(args[1]) != 0)
5649 arg = atoi(args[1]);
5650
5651 if (arg < 0 || arg > 100) {
5652 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5653 return -1;
5654 }
5655
5656 if (args[0][10] == 'h')
5657 global.tune.pool_high_ratio = arg;
5658 else
5659 global.tune.pool_low_ratio = arg;
5660 return 0;
5661}
5662
5663/* config keyword parsers */
5664static struct cfg_kw_list cfg_kws = {ILH, {
5665 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5666 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5667 { 0, NULL, NULL }
5668}};
5669
5670INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5671
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005672/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005673 * Local variables:
5674 * c-indent-level: 8
5675 * c-basic-offset: 8
5676 * End:
5677 */