blob: f90cfff5a03396ad8f8b6184ab15e8ea416febe9 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
Olivier Houchard4e694042017-03-14 20:01:29 +010017#include <import/xxhash.h>
18
Willy Tarreau272adea2014-03-31 10:39:59 +020019#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020020#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020021#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010022#include <common/initcall.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010023#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020024#include <common/time.h>
25
William Lallemand222baf22016-11-19 02:00:33 +010026#include <types/applet.h>
27#include <types/cli.h>
Frédéric Lécaille7da71292019-05-20 09:47:07 +020028#include <types/dict.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020029#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010030#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020031#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010032#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020033
William Lallemand222baf22016-11-19 02:00:33 +010034#include <proto/applet.h>
35#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090036#include <proto/checks.h>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020037#include <proto/connection.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020038#include <proto/port_range.h>
39#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020040#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010041#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020042#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020043#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010044#include <proto/stream_interface.h>
45#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020046#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020047#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010048#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020049
Baptiste Assmannda29fe22019-06-13 13:24:29 +020050#include <ebsttree.h>
51
Willy Tarreau3ff577e2018-08-02 11:48:52 +020052static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020053static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010054static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010055static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Baptiste Assmannda29fe22019-06-13 13:24:29 +020056static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params);
57static int srv_state_get_version(FILE *f);
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau21faa912012-10-10 08:27:36 +020059/* List head of all known server keywords */
60static struct srv_kw_list srv_keywords = {
61 .list = LIST_HEAD_INIT(srv_keywords.list)
62};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020063
Olivier Houchard9ea5d362019-02-14 18:29:09 +010064__decl_hathreads(HA_SPINLOCK_T idle_conn_srv_lock);
65struct eb_root idle_conn_srv = EB_ROOT;
66struct task *idle_conn_task = NULL;
67struct task *idle_conn_cleanup[MAX_THREADS] = { NULL };
Olivier Houchard859dc802019-08-08 15:47:21 +020068struct mt_list toremove_connections[MAX_THREADS];
Olivier Houchard4be71902019-07-11 15:49:00 +020069__decl_hathreads(HA_SPINLOCK_T toremove_lock[MAX_THREADS]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010070
Frédéric Lécaille7da71292019-05-20 09:47:07 +020071/* The server names dictionary */
72struct dict server_name_dict = {
73 .name = "server names",
74 .values = EB_ROOT_UNIQUE,
75};
76
Baptiste Assmannda29fe22019-06-13 13:24:29 +020077/* tree where global state_file is loaded */
Willy Tarreaufd1aa012019-12-20 17:23:40 +010078struct eb_root state_file = EB_ROOT_UNIQUE;
Baptiste Assmannda29fe22019-06-13 13:24:29 +020079
Simon Hormana3608442013-11-01 16:46:15 +090080int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020081{
Emeric Brun52a91d32017-08-31 14:41:55 +020082 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020083 return s->down_time;
84
85 return now.tv_sec - s->last_change + s->down_time;
86}
Willy Tarreaubaaee002006-06-26 02:48:02 +020087
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050088int srv_lastsession(const struct server *s)
89{
90 if (s->counters.last_sess)
91 return now.tv_sec - s->counters.last_sess;
92
93 return -1;
94}
95
Simon Horman4a741432013-02-23 15:35:38 +090096int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020097{
Simon Horman4a741432013-02-23 15:35:38 +090098 const struct server *s = check->server;
99
Willy Tarreauff5ae352013-12-11 20:36:34 +0100100 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +0900101 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100102
Emeric Brun52a91d32017-08-31 14:41:55 +0200103 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900104 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100105
Simon Horman4a741432013-02-23 15:35:38 +0900106 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100107}
108
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100109/*
110 * Check that we did not get a hash collision.
111 * Unlikely, but it can happen.
112 */
113static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100114{
115 struct proxy *p = s->proxy;
116 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100117
118 for (tmpserv = p->srv; tmpserv != NULL;
119 tmpserv = tmpserv->next) {
120 if (tmpserv == s)
121 continue;
122 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
123 continue;
124 if (tmpserv->cookie &&
125 strcmp(tmpserv->cookie, s->cookie) == 0) {
126 ha_warning("We generated two equal cookies for two different servers.\n"
127 "Please change the secret key for '%s'.\n",
128 s->proxy->id);
129 }
130 }
131
132}
133
Willy Tarreau46b7f532018-08-21 11:54:26 +0200134/*
Willy Tarreau5e83d992019-07-30 11:59:34 +0200135 * Must be called with the server lock held, and will grab the proxy lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200136 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100137void srv_set_dyncookie(struct server *s)
138{
139 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100140 char *tmpbuf;
141 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100142 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100143 size_t buffer_len;
144 int addr_len;
145 int port;
146
Willy Tarreau5e83d992019-07-30 11:59:34 +0200147 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
148
Olivier Houchard4e694042017-03-14 20:01:29 +0100149 if ((s->flags & SRV_F_COOKIESET) ||
150 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
151 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200152 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100153 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100154
155 if (s->addr.ss_family != AF_INET &&
156 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200157 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100158 /*
159 * Buffer to calculate the cookie value.
160 * The buffer contains the secret key + the server IP address
161 * + the TCP port.
162 */
163 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
164 /*
165 * The TCP port should use only 2 bytes, but is stored in
166 * an unsigned int in struct server, so let's use 4, to be
167 * on the safe side.
168 */
169 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200170 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100171 memcpy(tmpbuf, p->dyncookie_key, key_len);
172 memcpy(&(tmpbuf[key_len]),
173 s->addr.ss_family == AF_INET ?
174 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
175 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
176 addr_len);
177 /*
178 * Make sure it's the same across all the load balancers,
179 * no matter their endianness.
180 */
181 port = htonl(s->svc_port);
182 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
183 hash_value = XXH64(tmpbuf, buffer_len, 0);
184 memprintf(&s->cookie, "%016llx", hash_value);
185 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200186 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100187 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100188
189 /* Don't bother checking if the dyncookie is duplicated if
190 * the server is marked as "disabled", maybe it doesn't have
191 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100192 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100193 if (!(s->next_admin & SRV_ADMF_FMAINT))
194 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200195 out:
196 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100197}
198
Willy Tarreau21faa912012-10-10 08:27:36 +0200199/*
200 * Registers the server keyword list <kwl> as a list of valid keywords for next
201 * parsing sessions.
202 */
203void srv_register_keywords(struct srv_kw_list *kwl)
204{
205 LIST_ADDQ(&srv_keywords.list, &kwl->list);
206}
207
208/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
209 * keyword is found with a NULL ->parse() function, then an attempt is made to
210 * find one with a valid ->parse() function. This way it is possible to declare
211 * platform-dependant, known keywords as NULL, then only declare them as valid
212 * if some options are met. Note that if the requested keyword contains an
213 * opening parenthesis, everything from this point is ignored.
214 */
215struct srv_kw *srv_find_kw(const char *kw)
216{
217 int index;
218 const char *kwend;
219 struct srv_kw_list *kwl;
220 struct srv_kw *ret = NULL;
221
222 kwend = strchr(kw, '(');
223 if (!kwend)
224 kwend = kw + strlen(kw);
225
226 list_for_each_entry(kwl, &srv_keywords.list, list) {
227 for (index = 0; kwl->kw[index].kw != NULL; index++) {
228 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
229 kwl->kw[index].kw[kwend-kw] == 0) {
230 if (kwl->kw[index].parse)
231 return &kwl->kw[index]; /* found it !*/
232 else
233 ret = &kwl->kw[index]; /* may be OK */
234 }
235 }
236 }
237 return ret;
238}
239
240/* Dumps all registered "server" keywords to the <out> string pointer. The
241 * unsupported keywords are only dumped if their supported form was not
242 * found.
243 */
244void srv_dump_kws(char **out)
245{
246 struct srv_kw_list *kwl;
247 int index;
248
249 *out = NULL;
250 list_for_each_entry(kwl, &srv_keywords.list, list) {
251 for (index = 0; kwl->kw[index].kw != NULL; index++) {
252 if (kwl->kw[index].parse ||
253 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
254 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
255 kwl->scope,
256 kwl->kw[index].kw,
257 kwl->kw[index].skip ? " <arg>" : "",
258 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
259 kwl->kw[index].parse ? "" : " (not supported)");
260 }
261 }
262 }
263}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100264
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100265/* Parse the "addr" server keyword */
266static int srv_parse_addr(char **args, int *cur_arg,
267 struct proxy *curproxy, struct server *newsrv, char **err)
268{
269 char *errmsg, *arg;
270 struct sockaddr_storage *sk;
271 int port1, port2;
272 struct protocol *proto;
273
274 errmsg = NULL;
275 arg = args[*cur_arg + 1];
276
277 if (!*arg) {
278 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
279 goto err;
280 }
281
282 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
283 if (!sk) {
284 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
285 goto err;
286 }
287
288 proto = protocol_by_family(sk->ss_family);
289 if (!proto || !proto->connect) {
290 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
291 args[*cur_arg], arg);
292 goto err;
293 }
294
295 if (port1 != port2) {
296 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
297 args[*cur_arg], arg);
298 goto err;
299 }
300
301 newsrv->check.addr = newsrv->agent.addr = *sk;
302 newsrv->flags |= SRV_F_CHECKADDR;
303 newsrv->flags |= SRV_F_AGENTADDR;
304
305 return 0;
306
307 err:
308 free(errmsg);
309 return ERR_ALERT | ERR_FATAL;
310}
311
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100312/* Parse the "agent-check" server keyword */
313static int srv_parse_agent_check(char **args, int *cur_arg,
314 struct proxy *curproxy, struct server *newsrv, char **err)
315{
316 newsrv->do_agent = 1;
317 return 0;
318}
319
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100320/* Parse the "backup" server keyword */
321static int srv_parse_backup(char **args, int *cur_arg,
322 struct proxy *curproxy, struct server *newsrv, char **err)
323{
324 newsrv->flags |= SRV_F_BACKUP;
325 return 0;
326}
327
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100328/* Parse the "check" server keyword */
329static int srv_parse_check(char **args, int *cur_arg,
330 struct proxy *curproxy, struct server *newsrv, char **err)
331{
332 newsrv->do_check = 1;
333 return 0;
334}
335
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100336/* Parse the "check-send-proxy" server keyword */
337static int srv_parse_check_send_proxy(char **args, int *cur_arg,
338 struct proxy *curproxy, struct server *newsrv, char **err)
339{
340 newsrv->check.send_proxy = 1;
341 return 0;
342}
343
Alexander Liu2a54bb72019-05-22 19:44:48 +0800344/* Parse the "check-via-socks4" server keyword */
345static int srv_parse_check_via_socks4(char **args, int *cur_arg,
346 struct proxy *curproxy, struct server *newsrv, char **err)
347{
348 newsrv->check.via_socks4 = 1;
349 return 0;
350}
351
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100352/* Parse the "cookie" server keyword */
353static int srv_parse_cookie(char **args, int *cur_arg,
354 struct proxy *curproxy, struct server *newsrv, char **err)
355{
356 char *arg;
357
358 arg = args[*cur_arg + 1];
359 if (!*arg) {
360 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
361 return ERR_ALERT | ERR_FATAL;
362 }
363
364 free(newsrv->cookie);
365 newsrv->cookie = strdup(arg);
366 newsrv->cklen = strlen(arg);
367 newsrv->flags |= SRV_F_COOKIESET;
368 return 0;
369}
370
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100371/* Parse the "disabled" server keyword */
372static int srv_parse_disabled(char **args, int *cur_arg,
373 struct proxy *curproxy, struct server *newsrv, char **err)
374{
Emeric Brun52a91d32017-08-31 14:41:55 +0200375 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
376 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100377 newsrv->check.state |= CHK_ST_PAUSED;
378 newsrv->check.health = 0;
379 return 0;
380}
381
382/* Parse the "enabled" server keyword */
383static int srv_parse_enabled(char **args, int *cur_arg,
384 struct proxy *curproxy, struct server *newsrv, char **err)
385{
Emeric Brun52a91d32017-08-31 14:41:55 +0200386 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
387 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100388 newsrv->check.state &= ~CHK_ST_PAUSED;
389 newsrv->check.health = newsrv->check.rise;
390 return 0;
391}
392
Willy Tarreau9c538e02019-01-23 10:21:49 +0100393static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
394{
395 char *arg;
396
397 arg = args[*cur_arg + 1];
398 if (!*arg) {
399 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
400 return ERR_ALERT | ERR_FATAL;
401 }
402 newsrv->max_reuse = atoi(arg);
403
404 return 0;
405}
406
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100407static int srv_parse_pool_purge_delay(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100408{
409 const char *res;
410 char *arg;
411 unsigned int time;
412
413 arg = args[*cur_arg + 1];
414 if (!*arg) {
415 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
416 return ERR_ALERT | ERR_FATAL;
417 }
418 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200419 if (res == PARSE_TIME_OVER) {
420 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
421 args[*cur_arg+1], args[*cur_arg]);
422 return ERR_ALERT | ERR_FATAL;
423 }
424 else if (res == PARSE_TIME_UNDER) {
425 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
426 args[*cur_arg+1], args[*cur_arg]);
427 return ERR_ALERT | ERR_FATAL;
428 }
429 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100430 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
431 *res, args[*cur_arg]);
432 return ERR_ALERT | ERR_FATAL;
433 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100434 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100435
436 return 0;
437}
438
Olivier Houchard006e3102018-12-10 18:30:32 +0100439static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
440{
441 char *arg;
442
443 arg = args[*cur_arg + 1];
444 if (!*arg) {
445 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
446 return ERR_ALERT | ERR_FATAL;
447 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100448
Olivier Houchard006e3102018-12-10 18:30:32 +0100449 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100450 if ((int)newsrv->max_idle_conns < -1) {
451 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
452 return ERR_ALERT | ERR_FATAL;
453 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100454
455 return 0;
456}
457
Willy Tarreaudff55432012-10-10 17:51:05 +0200458/* parse the "id" server keyword */
459static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
460{
461 struct eb32_node *node;
462
463 if (!*args[*cur_arg + 1]) {
464 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
465 return ERR_ALERT | ERR_FATAL;
466 }
467
468 newsrv->puid = atol(args[*cur_arg + 1]);
469 newsrv->conf.id.key = newsrv->puid;
470
471 if (newsrv->puid <= 0) {
472 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
473 return ERR_ALERT | ERR_FATAL;
474 }
475
476 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
477 if (node) {
478 struct server *target = container_of(node, struct server, conf.id);
479 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
480 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
481 target->id);
482 return ERR_ALERT | ERR_FATAL;
483 }
484
485 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200486 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200487 return 0;
488}
489
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100490/* Parse the "namespace" server keyword */
491static int srv_parse_namespace(char **args, int *cur_arg,
492 struct proxy *curproxy, struct server *newsrv, char **err)
493{
Willy Tarreaue5733232019-05-22 19:24:06 +0200494#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100495 char *arg;
496
497 arg = args[*cur_arg + 1];
498 if (!*arg) {
499 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
500 return ERR_ALERT | ERR_FATAL;
501 }
502
503 if (!strcmp(arg, "*")) {
504 /* Use the namespace associated with the connection (if present). */
505 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
506 return 0;
507 }
508
509 /*
510 * As this parser may be called several times for the same 'default-server'
511 * object, or for a new 'server' instance deriving from a 'default-server'
512 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
513 */
514 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
515
516 newsrv->netns = netns_store_lookup(arg, strlen(arg));
517 if (!newsrv->netns)
518 newsrv->netns = netns_store_insert(arg);
519
520 if (!newsrv->netns) {
521 memprintf(err, "Cannot open namespace '%s'", arg);
522 return ERR_ALERT | ERR_FATAL;
523 }
524
525 return 0;
526#else
527 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
528 return ERR_ALERT | ERR_FATAL;
529#endif
530}
531
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100532/* Parse the "no-agent-check" server keyword */
533static int srv_parse_no_agent_check(char **args, int *cur_arg,
534 struct proxy *curproxy, struct server *newsrv, char **err)
535{
536 free_check(&newsrv->agent);
537 newsrv->agent.inter = 0;
538 newsrv->agent.port = 0;
539 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
540 newsrv->do_agent = 0;
541 return 0;
542}
543
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100544/* Parse the "no-backup" server keyword */
545static int srv_parse_no_backup(char **args, int *cur_arg,
546 struct proxy *curproxy, struct server *newsrv, char **err)
547{
548 newsrv->flags &= ~SRV_F_BACKUP;
549 return 0;
550}
551
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100552/* Parse the "no-check" server keyword */
553static int srv_parse_no_check(char **args, int *cur_arg,
554 struct proxy *curproxy, struct server *newsrv, char **err)
555{
556 free_check(&newsrv->check);
557 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
558 newsrv->do_check = 0;
559 return 0;
560}
561
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100562/* Parse the "no-check-send-proxy" server keyword */
563static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
564 struct proxy *curproxy, struct server *newsrv, char **err)
565{
566 newsrv->check.send_proxy = 0;
567 return 0;
568}
569
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100570/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200571static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100572{
573 srv->pp_opts &= ~flags;
574 return 0;
575}
576
577/* Parse the "no-send-proxy" server keyword */
578static int srv_parse_no_send_proxy(char **args, int *cur_arg,
579 struct proxy *curproxy, struct server *newsrv, char **err)
580{
581 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
582}
583
584/* Parse the "no-send-proxy-v2" server keyword */
585static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
586 struct proxy *curproxy, struct server *newsrv, char **err)
587{
588 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
589}
590
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200591/* Parse the "no-tfo" server keyword */
592static int srv_parse_no_tfo(char **args, int *cur_arg,
593 struct proxy *curproxy, struct server *newsrv, char **err)
594{
595 newsrv->flags &= ~SRV_F_FASTOPEN;
596 return 0;
597}
598
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100599/* Parse the "non-stick" server keyword */
600static int srv_parse_non_stick(char **args, int *cur_arg,
601 struct proxy *curproxy, struct server *newsrv, char **err)
602{
603 newsrv->flags |= SRV_F_NON_STICK;
604 return 0;
605}
606
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100607/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200608static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100609{
610 srv->pp_opts |= flags;
611 return 0;
612}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200613/* parse the "proto" server keyword */
614static int srv_parse_proto(char **args, int *cur_arg,
615 struct proxy *px, struct server *newsrv, char **err)
616{
617 struct ist proto;
618
619 if (!*args[*cur_arg + 1]) {
620 memprintf(err, "'%s' : missing value", args[*cur_arg]);
621 return ERR_ALERT | ERR_FATAL;
622 }
623 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
624 newsrv->mux_proto = get_mux_proto(proto);
625 if (!newsrv->mux_proto) {
626 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
627 return ERR_ALERT | ERR_FATAL;
628 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200629 return 0;
630}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100631
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100632/* parse the "proxy-v2-options" */
633static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
634 struct proxy *px, struct server *newsrv, char **err)
635{
636 char *p, *n;
637 for (p = args[*cur_arg+1]; p; p = n) {
638 n = strchr(p, ',');
639 if (n)
640 *n++ = '\0';
641 if (!strcmp(p, "ssl")) {
642 newsrv->pp_opts |= SRV_PP_V2_SSL;
643 } else if (!strcmp(p, "cert-cn")) {
644 newsrv->pp_opts |= SRV_PP_V2_SSL;
645 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100646 } else if (!strcmp(p, "cert-key")) {
647 newsrv->pp_opts |= SRV_PP_V2_SSL;
648 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
649 } else if (!strcmp(p, "cert-sig")) {
650 newsrv->pp_opts |= SRV_PP_V2_SSL;
651 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
652 } else if (!strcmp(p, "ssl-cipher")) {
653 newsrv->pp_opts |= SRV_PP_V2_SSL;
654 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100655 } else if (!strcmp(p, "authority")) {
656 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100657 } else if (!strcmp(p, "crc32c")) {
658 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100659 } else if (!strcmp(p, "unique-id")) {
660 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100661 } else
662 goto fail;
663 }
664 return 0;
665 fail:
666 if (err)
667 memprintf(err, "'%s' : proxy v2 option not implemented", p);
668 return ERR_ALERT | ERR_FATAL;
669}
670
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100671/* Parse the "observe" server keyword */
672static int srv_parse_observe(char **args, int *cur_arg,
673 struct proxy *curproxy, struct server *newsrv, char **err)
674{
675 char *arg;
676
677 arg = args[*cur_arg + 1];
678 if (!*arg) {
679 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
680 return ERR_ALERT | ERR_FATAL;
681 }
682
683 if (!strcmp(arg, "none")) {
684 newsrv->observe = HANA_OBS_NONE;
685 }
686 else if (!strcmp(arg, "layer4")) {
687 newsrv->observe = HANA_OBS_LAYER4;
688 }
689 else if (!strcmp(arg, "layer7")) {
690 if (curproxy->mode != PR_MODE_HTTP) {
691 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
692 return ERR_ALERT;
693 }
694 newsrv->observe = HANA_OBS_LAYER7;
695 }
696 else {
697 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
698 "but got '%s'\n", args[*cur_arg], arg);
699 return ERR_ALERT | ERR_FATAL;
700 }
701
702 return 0;
703}
704
Frédéric Lécaille16186232017-03-14 16:42:49 +0100705/* Parse the "redir" server keyword */
706static int srv_parse_redir(char **args, int *cur_arg,
707 struct proxy *curproxy, struct server *newsrv, char **err)
708{
709 char *arg;
710
711 arg = args[*cur_arg + 1];
712 if (!*arg) {
713 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
714 return ERR_ALERT | ERR_FATAL;
715 }
716
717 free(newsrv->rdr_pfx);
718 newsrv->rdr_pfx = strdup(arg);
719 newsrv->rdr_len = strlen(arg);
720
721 return 0;
722}
723
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100724/* Parse the "send-proxy" server keyword */
725static int srv_parse_send_proxy(char **args, int *cur_arg,
726 struct proxy *curproxy, struct server *newsrv, char **err)
727{
728 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
729}
730
731/* Parse the "send-proxy-v2" server keyword */
732static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
733 struct proxy *curproxy, struct server *newsrv, char **err)
734{
735 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
736}
737
Frédéric Lécailledba97072017-03-17 15:33:50 +0100738
739/* Parse the "source" server keyword */
740static int srv_parse_source(char **args, int *cur_arg,
741 struct proxy *curproxy, struct server *newsrv, char **err)
742{
743 char *errmsg;
744 int port_low, port_high;
745 struct sockaddr_storage *sk;
746 struct protocol *proto;
747
748 errmsg = NULL;
749
750 if (!*args[*cur_arg + 1]) {
751 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
752 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
753 goto err;
754 }
755
756 /* 'sk' is statically allocated (no need to be freed). */
757 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
758 if (!sk) {
759 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
760 goto err;
761 }
762
763 proto = protocol_by_family(sk->ss_family);
764 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100765 ha_alert("'%s %s' : connect() not supported for this address family.\n",
766 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100767 goto err;
768 }
769
770 newsrv->conn_src.opts |= CO_SRC_BIND;
771 newsrv->conn_src.source_addr = *sk;
772
773 if (port_low != port_high) {
774 int i;
775
776 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100777 ha_alert("'%s' does not support port offsets (found '%s').\n",
778 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100779 goto err;
780 }
781
782 if (port_low <= 0 || port_low > 65535 ||
783 port_high <= 0 || port_high > 65535 ||
784 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100785 ha_alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100786 goto err;
787 }
788 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
789 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
790 newsrv->conn_src.sport_range->ports[i] = port_low + i;
791 }
792
793 *cur_arg += 2;
794 while (*(args[*cur_arg])) {
795 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
796#if defined(CONFIG_HAP_TRANSPARENT)
797 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100798 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
799 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100800 goto err;
801 }
802 if (!strcmp(args[*cur_arg + 1], "client")) {
803 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
804 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
805 }
806 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
807 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
808 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
809 }
810 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
811 char *name, *end;
812
813 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +0100814 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100815 name++;
816
817 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +0100818 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +0100819 end++;
820
821 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
822 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
823 free(newsrv->conn_src.bind_hdr_name);
824 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
825 newsrv->conn_src.bind_hdr_len = end - name;
826 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
827 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
828 newsrv->conn_src.bind_hdr_occ = -1;
829
830 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +0100831 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100832 end++;
833 if (*end == ',') {
834 end++;
835 name = end;
836 if (*end == '-')
837 end++;
Willy Tarreau90807112020-02-25 08:16:33 +0100838 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100839 end++;
840 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
841 }
842
843 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100844 ha_alert("usesrc hdr_ip(name,num) does not support negative"
845 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100846 goto err;
847 }
848 }
849 else {
850 struct sockaddr_storage *sk;
851 int port1, port2;
852
853 /* 'sk' is statically allocated (no need to be freed). */
854 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
855 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100856 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100857 goto err;
858 }
859
860 proto = protocol_by_family(sk->ss_family);
861 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100862 ha_alert("'%s %s' : connect() not supported for this address family.\n",
863 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100864 goto err;
865 }
866
867 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100868 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
869 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100870 goto err;
871 }
872 newsrv->conn_src.tproxy_addr = *sk;
873 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
874 }
875 global.last_checks |= LSTCHK_NETADM;
876 *cur_arg += 2;
877 continue;
878#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100879 ha_alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100880 goto err;
881#endif /* defined(CONFIG_HAP_TRANSPARENT) */
882 } /* "usesrc" */
883
884 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
885#ifdef SO_BINDTODEVICE
886 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100887 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100888 goto err;
889 }
890 free(newsrv->conn_src.iface_name);
891 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
892 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
893 global.last_checks |= LSTCHK_NETADM;
894#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100895 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100896 goto err;
897#endif
898 *cur_arg += 2;
899 continue;
900 }
901 /* this keyword in not an option of "source" */
902 break;
903 } /* while */
904
905 return 0;
906
907 err:
908 free(errmsg);
909 return ERR_ALERT | ERR_FATAL;
910}
911
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100912/* Parse the "stick" server keyword */
913static int srv_parse_stick(char **args, int *cur_arg,
914 struct proxy *curproxy, struct server *newsrv, char **err)
915{
916 newsrv->flags &= ~SRV_F_NON_STICK;
917 return 0;
918}
919
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100920/* Parse the "track" server keyword */
921static int srv_parse_track(char **args, int *cur_arg,
922 struct proxy *curproxy, struct server *newsrv, char **err)
923{
924 char *arg;
925
926 arg = args[*cur_arg + 1];
927 if (!*arg) {
928 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
929 return ERR_ALERT | ERR_FATAL;
930 }
931
932 free(newsrv->trackit);
933 newsrv->trackit = strdup(arg);
934
935 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800936}
937
938/* Parse the "socks4" server keyword */
939static int srv_parse_socks4(char **args, int *cur_arg,
940 struct proxy *curproxy, struct server *newsrv, char **err)
941{
942 char *errmsg;
943 int port_low, port_high;
944 struct sockaddr_storage *sk;
945 struct protocol *proto;
946
947 errmsg = NULL;
948
949 if (!*args[*cur_arg + 1]) {
950 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
951 goto err;
952 }
953
954 /* 'sk' is statically allocated (no need to be freed). */
955 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
956 if (!sk) {
957 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
958 goto err;
959 }
960
961 proto = protocol_by_family(sk->ss_family);
962 if (!proto || !proto->connect) {
963 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
964 goto err;
965 }
966
967 newsrv->flags |= SRV_F_SOCKS4_PROXY;
968 newsrv->socks4_addr = *sk;
969
970 if (port_low != port_high) {
971 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
972 goto err;
973 }
974
975 if (!port_low) {
976 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
977 goto err;
978 }
979
980 return 0;
981
982 err:
983 free(errmsg);
984 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100985}
986
Frédéric Lécailledba97072017-03-17 15:33:50 +0100987
Willy Tarreau034c88c2017-01-23 23:36:45 +0100988/* parse the "tfo" server keyword */
989static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
990{
991 newsrv->flags |= SRV_F_FASTOPEN;
992 return 0;
993}
994
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200995/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200996 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200997 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200998 *
999 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001000 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001001void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001002{
Willy Tarreau87b09662015-04-03 00:22:06 +02001003 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001004
Willy Tarreau87b09662015-04-03 00:22:06 +02001005 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
1006 if (stream->srv_conn == srv)
1007 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001008}
1009
1010/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001011 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001012 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001013 *
1014 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001015 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001016void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001017{
1018 struct server *srv;
1019
1020 for (srv = px->srv; srv != NULL; srv = srv->next)
1021 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001022 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001023}
1024
Willy Tarreaubda92272014-05-20 21:55:30 +02001025/* Appends some information to a message string related to a server going UP or
1026 * DOWN. If both <forced> and <reason> are null and the server tracks another
1027 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001028 * If <check> is non-null, an entire string describing the check result will be
1029 * appended after a comma and a space (eg: to report some information from the
1030 * check that changed the state). In the other case, the string will be built
1031 * using the check results stored into the struct server if present.
1032 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001033 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001034 *
1035 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001036 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001037void srv_append_status(struct buffer *msg, struct server *s,
1038 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001039{
Emeric Brun5a133512017-10-19 14:42:30 +02001040 short status = s->op_st_chg.status;
1041 short code = s->op_st_chg.code;
1042 long duration = s->op_st_chg.duration;
1043 char *desc = s->op_st_chg.reason;
1044
1045 if (check) {
1046 status = check->status;
1047 code = check->code;
1048 duration = check->duration;
1049 desc = check->desc;
1050 }
1051
1052 if (status != -1) {
1053 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1054
1055 if (status >= HCHK_STATUS_L57DATA)
1056 chunk_appendf(msg, ", code: %d", code);
1057
1058 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001059 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001060
1061 chunk_appendf(msg, ", info: \"");
1062
1063 chunk_initlen(&src, desc, 0, strlen(desc));
1064 chunk_asciiencode(msg, &src, '"');
1065
1066 chunk_appendf(msg, "\"");
1067 }
1068
1069 if (duration >= 0)
1070 chunk_appendf(msg, ", check duration: %ldms", duration);
1071 }
1072 else if (desc && *desc) {
1073 chunk_appendf(msg, ", %s", desc);
1074 }
1075 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001076 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001077 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001078
1079 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001080 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001081 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1082 " %d sessions active, %d requeued, %d remaining in queue",
1083 s->proxy->srv_act, s->proxy->srv_bck,
1084 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1085 s->cur_sess, xferred, s->nbpend);
1086 else
1087 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1088 " %d sessions requeued, %d total in queue",
1089 s->proxy->srv_act, s->proxy->srv_bck,
1090 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
1091 xferred, s->nbpend);
1092 }
1093}
1094
Emeric Brun5a133512017-10-19 14:42:30 +02001095/* Marks server <s> down, regardless of its checks' statuses. The server is
1096 * registered in a list to postpone the counting of the remaining servers on
1097 * the proxy and transfers queued streams whenever possible to other servers at
1098 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1099 * non-null as the reason for going down or the available data from the check
1100 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001101 *
1102 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001103 */
Emeric Brun5a133512017-10-19 14:42:30 +02001104void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001105{
1106 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001107
Emeric Brun64cc49c2017-10-03 14:46:45 +02001108 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001109 return;
1110
Emeric Brun52a91d32017-08-31 14:41:55 +02001111 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001112 *s->op_st_chg.reason = 0;
1113 s->op_st_chg.status = -1;
1114 if (reason) {
1115 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1116 }
1117 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001118 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001119 s->op_st_chg.code = check->code;
1120 s->op_st_chg.status = check->status;
1121 s->op_st_chg.duration = check->duration;
1122 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001123
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001124 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001125 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001126
Emeric Brun9f0b4582017-10-23 14:39:51 +02001127 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001128 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001129 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001130 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001131 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001132}
1133
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001134/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001135 * in maintenance. The server is registered in a list to postpone the counting
1136 * of the remaining servers on the proxy and tries to grab requests from the
1137 * proxy at a sync point. Maintenance servers are ignored. It stores the
1138 * <reason> if non-null as the reason for going down or the available data
1139 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001140 *
1141 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001142 */
Emeric Brun5a133512017-10-19 14:42:30 +02001143void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001144{
1145 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001146
Emeric Brun64cc49c2017-10-03 14:46:45 +02001147 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001148 return;
1149
Emeric Brun52a91d32017-08-31 14:41:55 +02001150 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001151 return;
1152
Emeric Brun52a91d32017-08-31 14:41:55 +02001153 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001154 *s->op_st_chg.reason = 0;
1155 s->op_st_chg.status = -1;
1156 if (reason) {
1157 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1158 }
1159 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001160 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001161 s->op_st_chg.code = check->code;
1162 s->op_st_chg.status = check->status;
1163 s->op_st_chg.duration = check->duration;
1164 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001165
Emeric Brun64cc49c2017-10-03 14:46:45 +02001166 if (s->slowstart <= 0)
1167 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001168
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001169 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001170 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001171
Emeric Brun9f0b4582017-10-23 14:39:51 +02001172 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001173 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001174 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001175 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001176 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001177}
1178
Willy Tarreau8eb77842014-05-21 13:54:57 +02001179/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001180 * isn't in maintenance. The server is registered in a list to postpone the
1181 * counting of the remaining servers on the proxy and tries to grab requests
1182 * from the proxy. Maintenance servers are ignored. It stores the
1183 * <reason> if non-null as the reason for going down or the available data
1184 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001185 * up. Note that it makes use of the trash to build the log strings, so <reason>
1186 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001187 *
1188 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001189 */
Emeric Brun5a133512017-10-19 14:42:30 +02001190void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001191{
1192 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001193
Emeric Brun64cc49c2017-10-03 14:46:45 +02001194 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001195 return;
1196
Emeric Brun52a91d32017-08-31 14:41:55 +02001197 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001198 return;
1199
Emeric Brun52a91d32017-08-31 14:41:55 +02001200 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001201 *s->op_st_chg.reason = 0;
1202 s->op_st_chg.status = -1;
1203 if (reason) {
1204 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1205 }
1206 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001207 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001208 s->op_st_chg.code = check->code;
1209 s->op_st_chg.status = check->status;
1210 s->op_st_chg.duration = check->duration;
1211 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001212
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001213 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001214 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001215
Emeric Brun9f0b4582017-10-23 14:39:51 +02001216 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001217 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001218 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001219 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001220 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001221}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001222
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001223/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1224 * enforce either maint mode or drain mode. It is not allowed to set more than
1225 * one flag at once. The equivalent "inherited" flag is propagated to all
1226 * tracking servers. Maintenance mode disables health checks (but not agent
1227 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001228 * is done. If <cause> is non-null, it will be displayed at the end of the log
1229 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001230 *
1231 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001232 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001233void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001234{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001235 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001236
1237 if (!mode)
1238 return;
1239
1240 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001241 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001242 return;
1243
Emeric Brun52a91d32017-08-31 14:41:55 +02001244 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001245 if (cause)
1246 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1247
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001248 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001249 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001250
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001251 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001252 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1253 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001254 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001255
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001256 /* compute the inherited flag to propagate */
1257 if (mode & SRV_ADMF_MAINT)
1258 mode = SRV_ADMF_IMAINT;
1259 else if (mode & SRV_ADMF_DRAIN)
1260 mode = SRV_ADMF_IDRAIN;
1261
Emeric Brun9f0b4582017-10-23 14:39:51 +02001262 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001263 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001264 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001265 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001266 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001267}
1268
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001269/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1270 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1271 * than one flag at once. The equivalent "inherited" flag is propagated to all
1272 * tracking servers. Leaving maintenance mode re-enables health checks. When
1273 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001274 *
1275 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001276 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001277void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001278{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001279 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001280
1281 if (!mode)
1282 return;
1283
1284 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001285 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001286 return;
1287
Emeric Brun52a91d32017-08-31 14:41:55 +02001288 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001289
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001290 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001291 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001292
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001293 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001294 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1295 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001296 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001297
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001298 if (mode & SRV_ADMF_MAINT)
1299 mode = SRV_ADMF_IMAINT;
1300 else if (mode & SRV_ADMF_DRAIN)
1301 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001302
Emeric Brun9f0b4582017-10-23 14:39:51 +02001303 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001304 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001305 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001306 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001307 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001308}
1309
Willy Tarreau757478e2016-11-03 19:22:19 +01001310/* principle: propagate maint and drain to tracking servers. This is useful
1311 * upon startup so that inherited states are correct.
1312 */
1313static void srv_propagate_admin_state(struct server *srv)
1314{
1315 struct server *srv2;
1316
1317 if (!srv->trackers)
1318 return;
1319
1320 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001321 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001322 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001323 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001324
Emeric Brun52a91d32017-08-31 14:41:55 +02001325 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001326 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001327 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001328 }
1329}
1330
1331/* Compute and propagate the admin states for all servers in proxy <px>.
1332 * Only servers *not* tracking another one are considered, because other
1333 * ones will be handled when the server they track is visited.
1334 */
1335void srv_compute_all_admin_states(struct proxy *px)
1336{
1337 struct server *srv;
1338
1339 for (srv = px->srv; srv; srv = srv->next) {
1340 if (srv->track)
1341 continue;
1342 srv_propagate_admin_state(srv);
1343 }
1344}
1345
Willy Tarreaudff55432012-10-10 17:51:05 +02001346/* Note: must not be declared <const> as its list will be overwritten.
1347 * Please take care of keeping this list alphabetically sorted, doing so helps
1348 * all code contributors.
1349 * Optional keywords are also declared with a NULL ->parse() function so that
1350 * the config parser can report an appropriate error when a known keyword was
1351 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001352 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001353 */
1354static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001355 { "addr", srv_parse_addr, 1, 1 }, /* IP address to send health to or to probe from agent-check */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001356 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001357 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001358 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001359 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1 }, /* enable PROXY protocol for health checks */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001360 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001361 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1362 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001363 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001364 { "max-reuse", srv_parse_max_reuse, 1, 1 }, /* Set the max number of requests on a connection, -1 means unlimited */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001365 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001366 { "no-agent-check", srv_parse_no_agent_check, 0, 1 }, /* Do not enable any auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001367 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001368 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001369 { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1 }, /* disable PROXY protol for health checks */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001370 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1371 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +02001372 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001373 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001374 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001375 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1 }, /* Set the max number of orphan idle connections, 0 means unlimited */
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001376 { "pool-purge-delay", srv_parse_pool_purge_delay, 1, 1 }, /* Set the time before we destroy orphan idle connections, defaults to 1s */
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001377 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001378 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1 }, /* options for send-proxy-v2 */
Frédéric Lécaille16186232017-03-14 16:42:49 +01001379 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001380 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1381 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1 }, /* Enforce use of PROXY V2 protocol */
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001382 { "source", srv_parse_source, -1, 1 }, /* Set the source address to be used to connect to the server */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001383 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001384 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001385 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001386 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
1387 { "check-via-socks4", srv_parse_check_via_socks4, 0, 1 }, /* enable socks4 proxy for health checks */
Willy Tarreaudff55432012-10-10 17:51:05 +02001388 { NULL, NULL, 0 },
1389}};
1390
Willy Tarreau0108d902018-11-25 19:14:37 +01001391INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001392
Willy Tarreau004e0452013-11-21 11:22:01 +01001393/* Recomputes the server's eweight based on its state, uweight, the current time,
1394 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001395 * state is automatically disabled if the time is elapsed. If <must_update> is
1396 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001397 *
1398 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001399 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001400void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001401{
1402 struct proxy *px = sv->proxy;
1403 unsigned w;
1404
1405 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1406 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001407 if (sv->next_state == SRV_ST_STARTING)
1408 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001409 }
1410
1411 /* We must take care of not pushing the server to full throttle during slow starts.
1412 * It must also start immediately, at least at the minimal step when leaving maintenance.
1413 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001414 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001415 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1416 else
1417 w = px->lbprm.wdiv;
1418
Emeric Brun52a91d32017-08-31 14:41:55 +02001419 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001420
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001421 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001422 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001423 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001424}
1425
Willy Tarreaubaaee002006-06-26 02:48:02 +02001426/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001427 * Parses weight_str and configures sv accordingly.
1428 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001429 *
1430 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001431 */
1432const char *server_parse_weight_change_request(struct server *sv,
1433 const char *weight_str)
1434{
1435 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001436 long int w;
1437 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001438
1439 px = sv->proxy;
1440
1441 /* if the weight is terminated with '%', it is set relative to
1442 * the initial weight, otherwise it is absolute.
1443 */
1444 if (!*weight_str)
1445 return "Require <weight> or <weight%>.\n";
1446
Simon Hormanb796afa2013-02-12 10:45:53 +09001447 w = strtol(weight_str, &end, 10);
1448 if (end == weight_str)
1449 return "Empty weight string empty or preceded by garbage";
1450 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001451 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001452 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001453 /* Avoid integer overflow */
1454 if (w > 25600)
1455 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001456 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001457 if (w > 256)
1458 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001459 }
1460 else if (w < 0 || w > 256)
1461 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001462 else if (end[0] != '\0')
1463 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001464
1465 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1466 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1467
1468 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001469 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001470
1471 return NULL;
1472}
1473
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001474/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001475 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1476 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001477 * Returns:
1478 * - error string on error
1479 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001480 *
1481 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001482 */
1483const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001484 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001485{
1486 unsigned char ip[INET6_ADDRSTRLEN];
1487
1488 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001489 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001490 return NULL;
1491 }
1492 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001493 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001494 return NULL;
1495 }
1496
1497 return "Could not understand IP address format.\n";
1498}
1499
Willy Tarreau46b7f532018-08-21 11:54:26 +02001500/*
1501 * Must be called with the server lock held.
1502 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001503const char *server_parse_maxconn_change_request(struct server *sv,
1504 const char *maxconn_str)
1505{
1506 long int v;
1507 char *end;
1508
1509 if (!*maxconn_str)
1510 return "Require <maxconn>.\n";
1511
1512 v = strtol(maxconn_str, &end, 10);
1513 if (end == maxconn_str)
1514 return "maxconn string empty or preceded by garbage";
1515 else if (end[0] != '\0')
1516 return "Trailing garbage in maxconn string";
1517
1518 if (sv->maxconn == sv->minconn) { // static maxconn
1519 sv->maxconn = sv->minconn = v;
1520 } else { // dynamic maxconn
1521 sv->maxconn = v;
1522 }
1523
1524 if (may_dequeue_tasks(sv, sv->proxy))
1525 process_srv_queue(sv);
1526
1527 return NULL;
1528}
1529
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001530#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001531static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1532 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001533{
1534 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001535 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001536 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001537 NULL,
1538 };
1539
1540 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001541 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001542
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001543 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001544}
1545
1546static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1547{
1548 struct sample_expr *expr;
1549
1550 expr = srv_sni_sample_parse_expr(newsrv, px, px->conf.file, px->conf.line, err);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001551 if (!expr) {
1552 memprintf(err, "error detected while parsing sni expression : %s", *err);
1553 return ERR_ALERT | ERR_FATAL;
1554 }
1555
1556 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1557 memprintf(err, "error detected while parsing sni expression : "
1558 " fetch method '%s' extracts information from '%s', "
1559 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001560 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001561 return ERR_ALERT | ERR_FATAL;
1562 }
1563
1564 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1565 release_sample_expr(newsrv->ssl_ctx.sni);
1566 newsrv->ssl_ctx.sni = expr;
1567
1568 return 0;
1569}
1570#endif
1571
1572static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1573{
1574 if (err && *err) {
1575 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001576 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 +01001577 }
1578 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001579 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1580 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001581}
1582
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001583static void srv_conn_src_sport_range_cpy(struct server *srv,
1584 struct server *src)
1585{
1586 int range_sz;
1587
1588 range_sz = src->conn_src.sport_range->size;
1589 if (range_sz > 0) {
1590 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1591 if (srv->conn_src.sport_range != NULL) {
1592 int i;
1593
1594 for (i = 0; i < range_sz; i++) {
1595 srv->conn_src.sport_range->ports[i] =
1596 src->conn_src.sport_range->ports[i];
1597 }
1598 }
1599 }
1600}
1601
1602/*
1603 * Copy <src> server connection source settings to <srv> server everything needed.
1604 */
1605static void srv_conn_src_cpy(struct server *srv, struct server *src)
1606{
1607 srv->conn_src.opts = src->conn_src.opts;
1608 srv->conn_src.source_addr = src->conn_src.source_addr;
1609
1610 /* Source port range copy. */
1611 if (src->conn_src.sport_range != NULL)
1612 srv_conn_src_sport_range_cpy(srv, src);
1613
1614#ifdef CONFIG_HAP_TRANSPARENT
1615 if (src->conn_src.bind_hdr_name != NULL) {
1616 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1617 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1618 }
1619 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1620 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1621#endif
1622 if (src->conn_src.iface_name != NULL)
1623 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1624}
1625
1626/*
1627 * Copy <src> server SSL settings to <srv> server allocating
1628 * everything needed.
1629 */
1630#if defined(USE_OPENSSL)
1631static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1632{
1633 if (src->ssl_ctx.ca_file != NULL)
1634 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1635 if (src->ssl_ctx.crl_file != NULL)
1636 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1637 if (src->ssl_ctx.client_crt != NULL)
1638 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1639
1640 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1641
1642 if (src->ssl_ctx.verify_host != NULL)
1643 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1644 if (src->ssl_ctx.ciphers != NULL)
1645 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001646 if (src->ssl_ctx.options)
1647 srv->ssl_ctx.options = src->ssl_ctx.options;
1648 if (src->ssl_ctx.methods.flags)
1649 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1650 if (src->ssl_ctx.methods.min)
1651 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1652 if (src->ssl_ctx.methods.max)
1653 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1654
Willy Tarreau5db847a2019-05-09 14:13:35 +02001655#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001656 if (src->ssl_ctx.ciphersuites != NULL)
1657 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1658#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001659 if (src->sni_expr != NULL)
1660 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001661
1662#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1663 if (src->ssl_ctx.alpn_str) {
1664 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1665 if (srv->ssl_ctx.alpn_str) {
1666 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1667 src->ssl_ctx.alpn_len);
1668 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1669 }
1670 }
1671#endif
1672#ifdef OPENSSL_NPN_NEGOTIATED
1673 if (src->ssl_ctx.npn_str) {
1674 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1675 if (srv->ssl_ctx.npn_str) {
1676 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1677 src->ssl_ctx.npn_len);
1678 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1679 }
1680 }
1681#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001682}
1683#endif
1684
1685/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001686 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001687 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001688 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001689 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001690static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001691{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001692 char *hostname_dn;
1693 int hostname_len, hostname_dn_len;
1694
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001695 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001696 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001697
Christopher Faulet67957bd2017-09-27 11:00:59 +02001698 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001699 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001700 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1701 hostname_dn, trash.size);
1702 if (hostname_dn_len == -1)
1703 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001704
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001705
Christopher Faulet67957bd2017-09-27 11:00:59 +02001706 free(srv->hostname);
1707 free(srv->hostname_dn);
1708 srv->hostname = strdup(hostname);
1709 srv->hostname_dn = strdup(hostname_dn);
1710 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001711 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001712 goto err;
1713
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001714 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001715
1716 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001717 free(srv->hostname); srv->hostname = NULL;
1718 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001719 return -1;
1720}
1721
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001722/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001723 * Copy <src> server settings to <srv> server allocating
1724 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001725 * This function is not supposed to be called at any time, but only
1726 * during server settings parsing or during server allocations from
1727 * a server template, and just after having calloc()'ed a new server.
1728 * So, <src> may only be a default server (when parsing server settings)
1729 * or a server template (during server allocations from a server template).
1730 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1731 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001732 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001733static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001734{
1735 /* Connection source settings copy */
1736 srv_conn_src_cpy(srv, src);
1737
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001738 if (srv_tmpl) {
1739 srv->addr = src->addr;
1740 srv->svc_port = src->svc_port;
1741 }
1742
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001743 srv->pp_opts = src->pp_opts;
1744 if (src->rdr_pfx != NULL) {
1745 srv->rdr_pfx = strdup(src->rdr_pfx);
1746 srv->rdr_len = src->rdr_len;
1747 }
1748 if (src->cookie != NULL) {
1749 srv->cookie = strdup(src->cookie);
1750 srv->cklen = src->cklen;
1751 }
1752 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01001753 srv->check.addr = src->check.addr;
1754 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001755 srv->check.use_ssl = src->check.use_ssl;
1756 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001757 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001758 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001759 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001760 /* Note: 'flags' field has potentially been already initialized. */
1761 srv->flags |= src->flags;
1762 srv->do_check = src->do_check;
1763 srv->do_agent = src->do_agent;
1764 if (srv->check.port)
1765 srv->flags |= SRV_F_CHECKPORT;
1766 srv->check.inter = src->check.inter;
1767 srv->check.fastinter = src->check.fastinter;
1768 srv->check.downinter = src->check.downinter;
1769 srv->agent.use_ssl = src->agent.use_ssl;
1770 srv->agent.port = src->agent.port;
1771 if (src->agent.send_string != NULL)
1772 srv->agent.send_string = strdup(src->agent.send_string);
1773 srv->agent.send_string_len = src->agent.send_string_len;
1774 srv->agent.inter = src->agent.inter;
1775 srv->agent.fastinter = src->agent.fastinter;
1776 srv->agent.downinter = src->agent.downinter;
1777 srv->maxqueue = src->maxqueue;
1778 srv->minconn = src->minconn;
1779 srv->maxconn = src->maxconn;
1780 srv->slowstart = src->slowstart;
1781 srv->observe = src->observe;
1782 srv->onerror = src->onerror;
1783 srv->onmarkeddown = src->onmarkeddown;
1784 srv->onmarkedup = src->onmarkedup;
1785 if (src->trackit != NULL)
1786 srv->trackit = strdup(src->trackit);
1787 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1788 srv->uweight = srv->iweight = src->iweight;
1789
1790 srv->check.send_proxy = src->check.send_proxy;
1791 /* health: up, but will fall down at first failure */
1792 srv->check.rise = srv->check.health = src->check.rise;
1793 srv->check.fall = src->check.fall;
1794
1795 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001796 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1797 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1798 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001799 srv->check.state |= CHK_ST_PAUSED;
1800 srv->check.health = 0;
1801 }
1802
1803 /* health: up but will fall down at first failure */
1804 srv->agent.rise = srv->agent.health = src->agent.rise;
1805 srv->agent.fall = src->agent.fall;
1806
1807 if (src->resolvers_id != NULL)
1808 srv->resolvers_id = strdup(src->resolvers_id);
1809 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001810 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Daniel Corbettf8716912019-11-17 09:48:56 -05001811 srv->dns_opts.ignore_weight = src->dns_opts.ignore_weight;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001812 if (srv->dns_opts.family_prio == AF_UNSPEC)
1813 srv->dns_opts.family_prio = AF_INET6;
1814 memcpy(srv->dns_opts.pref_net,
1815 src->dns_opts.pref_net,
1816 sizeof srv->dns_opts.pref_net);
1817 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1818
1819 srv->init_addr_methods = src->init_addr_methods;
1820 srv->init_addr = src->init_addr;
1821#if defined(USE_OPENSSL)
1822 srv_ssl_settings_cpy(srv, src);
1823#endif
1824#ifdef TCP_USER_TIMEOUT
1825 srv->tcp_ut = src->tcp_ut;
1826#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001827 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001828 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001829 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001830 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001831
Olivier Houchard8da5f982017-08-04 18:35:36 +02001832 if (srv_tmpl)
1833 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001834
1835 srv->check.via_socks4 = src->check.via_socks4;
1836 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001837}
1838
William Lallemand313bfd12018-10-26 14:47:32 +02001839struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001840{
1841 struct server *srv;
1842
1843 srv = calloc(1, sizeof *srv);
1844 if (!srv)
1845 return NULL;
1846
1847 srv->obj_type = OBJ_TYPE_SERVER;
1848 srv->proxy = proxy;
1849 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001850 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001851
Emeric Brun52a91d32017-08-31 14:41:55 +02001852 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001853 srv->last_change = now.tv_sec;
1854
1855 srv->check.status = HCHK_STATUS_INI;
1856 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001857 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001858 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1859
1860 srv->agent.status = HCHK_STATUS_INI;
1861 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001862 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001863 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1864
Willy Tarreau975b1552019-06-06 16:25:55 +02001865 /* please don't put default server settings here, they are set in
1866 * init_default_instance().
1867 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001868 return srv;
1869}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001870
1871/*
1872 * Validate <srv> server health-check settings.
1873 * Returns 0 if everything is OK, -1 if not.
1874 */
1875static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1876{
1877 struct tcpcheck_rule *r = NULL;
1878 struct list *l;
1879
1880 /*
1881 * We need at least a service port, a check port or the first tcp-check rule must
1882 * be a 'connect' one when checking an IPv4/IPv6 server.
1883 */
1884 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1885 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1886 return 0;
1887
1888 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1889 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001890 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1891 "Check has been disabled.\n",
1892 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001893 return -1;
1894 }
1895
1896 /* search the first action (connect / send / expect) in the list */
1897 l = &srv->proxy->tcpcheck_rules;
1898 list_for_each_entry(r, l, list) {
1899 if (r->action != TCPCHK_ACT_COMMENT)
1900 break;
1901 }
1902
1903 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001904 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1905 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1906 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001907 return -1;
1908 }
1909
1910 /* scan the tcp-check ruleset to ensure a port has been configured */
1911 l = &srv->proxy->tcpcheck_rules;
1912 list_for_each_entry(r, l, list) {
1913 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001914 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1915 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1916 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001917 return -1;
1918 }
1919 }
1920
1921 return 0;
1922}
1923
1924/*
1925 * Initialize <srv> health-check structure.
1926 * Returns the error string in case of memory allocation failure, NULL if not.
1927 */
1928static const char *do_health_check_init(struct server *srv, int check_type, int state)
1929{
1930 const char *ret;
1931
1932 if (!srv->do_check)
1933 return NULL;
1934
1935 ret = init_check(&srv->check, check_type);
1936 if (ret)
1937 return ret;
1938
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001939 srv->check.state |= state;
1940 global.maxsock++;
1941
1942 return NULL;
1943}
1944
1945static int server_health_check_init(const char *file, int linenum,
1946 struct server *srv, struct proxy *curproxy)
1947{
1948 const char *ret;
1949
1950 if (!srv->do_check)
1951 return 0;
1952
1953 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001954 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1955 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001956 return ERR_ALERT | ERR_FATAL;
1957 }
1958
1959 if (server_healthcheck_validate(file, linenum, srv) < 0)
1960 return ERR_ALERT | ERR_ABORT;
1961
1962 /* note: check type will be set during the config review phase */
1963 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1964 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001965 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001966 return ERR_ALERT | ERR_ABORT;
1967 }
1968
1969 return 0;
1970}
1971
1972/*
1973 * Initialize <srv> agent check structure.
1974 * Returns the error string in case of memory allocation failure, NULL if not.
1975 */
1976static const char *do_server_agent_check_init(struct server *srv, int state)
1977{
1978 const char *ret;
1979
1980 if (!srv->do_agent)
1981 return NULL;
1982
1983 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1984 if (ret)
1985 return ret;
1986
1987 if (!srv->agent.inter)
1988 srv->agent.inter = srv->check.inter;
1989
1990 srv->agent.state |= state;
1991 global.maxsock++;
1992
1993 return NULL;
1994}
1995
1996static int server_agent_check_init(const char *file, int linenum,
1997 struct server *srv, struct proxy *curproxy)
1998{
1999 const char *ret;
2000
2001 if (!srv->do_agent)
2002 return 0;
2003
2004 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002005 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 +02002006 file, linenum, srv->id);
2007 return ERR_ALERT | ERR_FATAL;
2008 }
2009
2010 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
2011 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002012 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002013 return ERR_ALERT | ERR_ABORT;
2014 }
2015
2016 return 0;
2017}
2018
2019#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2020static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
2021 struct server *srv, struct proxy *proxy)
2022{
2023 int ret;
2024 char *err = NULL;
2025
2026 if (!srv->sni_expr)
2027 return 0;
2028
2029 ret = server_parse_sni_expr(srv, proxy, &err);
2030 if (!ret)
2031 return 0;
2032
2033 display_parser_err(file, linenum, args, cur_arg, &err);
2034 free(err);
2035
2036 return ret;
2037}
2038#endif
2039
2040/*
2041 * Server initializations finalization.
2042 * Initialize health check, agent check and SNI expression if enabled.
2043 * Must not be called for a default server instance.
2044 */
2045static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
2046 struct server *srv, struct proxy *px)
2047{
2048 int ret;
2049
2050 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
2051 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
2052 return ret;
2053 }
2054
2055#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2056 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
2057 return ret;
2058#endif
2059
2060 if (srv->flags & SRV_F_BACKUP)
2061 px->srv_bck++;
2062 else
2063 px->srv_act++;
2064 srv_lb_commit_status(srv);
2065
2066 return 0;
2067}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002068
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002069/*
2070 * Parse as much as possible such a range string argument: low[-high]
2071 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2072 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2073 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2074 * Returns 0 if succeeded, -1 if not.
2075 */
2076static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
2077{
2078 char *nb_high_arg;
2079
2080 *nb_high = 0;
2081 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002082 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002083
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002084 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002085 *nb_high_arg++ = '\0';
2086 *nb_high = atoi(nb_high_arg);
2087 }
2088 else {
2089 *nb_high += *nb_low;
2090 *nb_low = 1;
2091 }
2092
2093 if (*nb_low < 0 || *nb_high < *nb_low)
2094 return -1;
2095
2096 return 0;
2097}
2098
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002099static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
2100{
2101 chunk_printf(&trash, "%s%d", prefix, nb);
2102 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002103 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002104}
2105
2106/*
2107 * Initialize as much as possible servers from <srv> server template.
2108 * Note that a server template is a special server with
2109 * a few different parameters than a server which has
2110 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08002111 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002112 * 'srv' template included.
2113 */
2114static int server_template_init(struct server *srv, struct proxy *px)
2115{
2116 int i;
2117 struct server *newsrv;
2118
2119 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
2120 int check_init_state;
2121 int agent_init_state;
2122
2123 newsrv = new_server(px);
2124 if (!newsrv)
2125 goto err;
2126
2127 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002128 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002129#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2130 if (newsrv->sni_expr) {
2131 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2132 if (!newsrv->ssl_ctx.sni)
2133 goto err;
2134 }
2135#endif
2136 /* Set this new server ID. */
2137 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2138
2139 /* Initial checks states. */
2140 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2141 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2142
2143 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2144 do_server_agent_check_init(newsrv, agent_init_state))
2145 goto err;
2146
2147 /* Linked backwards first. This will be restablished after parsing. */
2148 newsrv->next = px->srv;
2149 px->srv = newsrv;
2150 }
2151 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2152
2153 return i - srv->tmpl_info.nb_low;
2154
2155 err:
2156 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2157 if (newsrv) {
2158#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2159 release_sample_expr(newsrv->ssl_ctx.sni);
2160#endif
2161 free_check(&newsrv->agent);
2162 free_check(&newsrv->check);
2163 }
2164 free(newsrv);
2165 return i - srv->tmpl_info.nb_low;
2166}
2167
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02002168int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy, int parse_addr, int in_peers_section)
Willy Tarreau272adea2014-03-31 10:39:59 +02002169{
2170 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002171 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002172 char *errmsg = NULL;
2173 int err_code = 0;
2174 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002175 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002176
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002177 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002178 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002179 !strcmp(args[0], "default-server") ||
2180 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002181 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002182 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002183 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002184 int srv_tmpl = !defsrv && !srv;
2185 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002186
2187 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002188 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002189 err_code |= ERR_ALERT | ERR_FATAL;
2190 goto out;
2191 }
2192 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002193 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002194
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002195 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002196 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002197 if (!*args[2]) {
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02002198 if (in_peers_section) {
2199 return 0;
2200 }
2201 else {
2202 /* 'server' line number of argument check. */
2203 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
2204 file, linenum, args[0]);
2205 err_code |= ERR_ALERT | ERR_FATAL;
2206 goto out;
2207 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002208 }
2209
2210 err = invalid_char(args[1]);
2211 }
2212 else if (srv_tmpl) {
2213 if (!*args[3]) {
2214 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002215 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 +02002216 file, linenum, args[0]);
2217 err_code |= ERR_ALERT | ERR_FATAL;
2218 goto out;
2219 }
2220
2221 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002222 }
2223
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002224 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002225 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 +02002226 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002227 err_code |= ERR_ALERT | ERR_FATAL;
2228 goto out;
2229 }
2230
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002231 cur_arg = 2;
2232 if (srv_tmpl) {
2233 /* Parse server-template <nb | range> arg. */
2234 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002235 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002236 file, linenum, args[0], args[cur_arg]);
2237 err_code |= ERR_ALERT | ERR_FATAL;
2238 goto out;
2239 }
2240 cur_arg++;
2241 }
2242
Willy Tarreau272adea2014-03-31 10:39:59 +02002243 if (!defsrv) {
2244 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002245 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002246 struct protocol *proto;
2247
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002248 newsrv = new_server(curproxy);
2249 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002250 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002251 err_code |= ERR_ALERT | ERR_ABORT;
2252 goto out;
2253 }
2254
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002255 if (srv_tmpl) {
2256 newsrv->tmpl_info.nb_low = tmpl_range_low;
2257 newsrv->tmpl_info.nb_high = tmpl_range_high;
2258 }
2259
Willy Tarreau272adea2014-03-31 10:39:59 +02002260 /* the servers are linked backwards first */
2261 newsrv->next = curproxy->srv;
2262 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002263 newsrv->conf.file = strdup(file);
2264 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002265 /* Note: for a server template, its id is its prefix.
2266 * This is a temporary id which will be used for server allocations to come
2267 * after parsing.
2268 */
2269 if (srv)
2270 newsrv->id = strdup(args[1]);
2271 else
2272 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002273
2274 /* several ways to check the port component :
2275 * - IP => port=+0, relative (IPv4 only)
2276 * - IP: => port=+0, relative
2277 * - IP:N => port=N, absolute
2278 * - IP:+N => port=+N, relative
2279 * - IP:-N => port=-N, relative
2280 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002281 if (!parse_addr)
2282 goto skip_addr;
2283
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002284 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002285 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002286 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002287 err_code |= ERR_ALERT | ERR_FATAL;
2288 goto out;
2289 }
2290
2291 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002292 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002293 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002294 file, linenum, args[0], args[1]);
2295 err_code |= ERR_ALERT | ERR_FATAL;
2296 goto out;
2297 }
2298
2299 if (!port1 || !port2) {
2300 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002301 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002302 }
2303 else if (port1 != port2) {
2304 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002305 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002306 file, linenum, args[0], args[1], args[2]);
2307 err_code |= ERR_ALERT | ERR_FATAL;
2308 goto out;
2309 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002310
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002311 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002312 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002313 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002314 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002315 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2316 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2317 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002318 err_code |= ERR_ALERT | ERR_FATAL;
2319 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002320 }
2321 }
2322 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002323 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002324 file, linenum, newsrv->id);
2325 err_code |= ERR_ALERT | ERR_FATAL;
2326 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002327 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002328 }
2329
Willy Tarreau272adea2014-03-31 10:39:59 +02002330 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002331 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002332
Olivier Houchard8da5f982017-08-04 18:35:36 +02002333 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002334 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002335 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002336 err_code |= ERR_ALERT | ERR_FATAL;
2337 goto out;
2338 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002339
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002340 cur_arg++;
2341 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002342 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002343 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002344 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002345 } else {
2346 newsrv = &curproxy->defsrv;
2347 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002348 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002349 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002350 }
2351
2352 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002353 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002354 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002355
2356 if (err == PARSE_TIME_OVER) {
2357 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2358 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2359 err_code |= ERR_ALERT | ERR_FATAL;
2360 goto out;
2361 }
2362 else if (err == PARSE_TIME_UNDER) {
2363 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2364 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2365 err_code |= ERR_ALERT | ERR_FATAL;
2366 goto out;
2367 }
2368 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002369 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002370 file, linenum, *err, newsrv->id);
2371 err_code |= ERR_ALERT | ERR_FATAL;
2372 goto out;
2373 }
2374 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002375 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002376 file, linenum, val, args[cur_arg], newsrv->id);
2377 err_code |= ERR_ALERT | ERR_FATAL;
2378 goto out;
2379 }
2380 newsrv->agent.inter = val;
2381 cur_arg += 2;
2382 }
Misiekea849332017-01-09 09:39:51 +01002383 else if (!strcmp(args[cur_arg], "agent-addr")) {
2384 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002385 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002386 goto out;
2387 }
2388
2389 cur_arg += 2;
2390 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002391 else if (!strcmp(args[cur_arg], "agent-port")) {
2392 global.maxsock++;
2393 newsrv->agent.port = atol(args[cur_arg + 1]);
2394 cur_arg += 2;
2395 }
James Brown55f9ff12015-10-21 18:19:05 -07002396 else if (!strcmp(args[cur_arg], "agent-send")) {
2397 global.maxsock++;
2398 free(newsrv->agent.send_string);
2399 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2400 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2401 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2402 cur_arg += 2;
2403 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002404 else if (!strcmp(args[cur_arg], "init-addr")) {
2405 char *p, *end;
2406 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002407 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002408
2409 newsrv->init_addr_methods = 0;
2410 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2411
2412 for (p = args[cur_arg + 1]; *p; p = end) {
2413 /* cut on next comma */
2414 for (end = p; *end && *end != ','; end++);
2415 if (*end)
2416 *(end++) = 0;
2417
Willy Tarreau4310d362016-11-02 15:05:56 +01002418 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002419 if (!strcmp(p, "libc")) {
2420 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2421 }
2422 else if (!strcmp(p, "last")) {
2423 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2424 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002425 else if (!strcmp(p, "none")) {
2426 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2427 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002428 else if (str2ip2(p, &sa, 0)) {
2429 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002430 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002431 file, linenum, args[cur_arg], p);
2432 err_code |= ERR_ALERT | ERR_FATAL;
2433 goto out;
2434 }
2435 newsrv->init_addr = sa;
2436 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2437 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002438 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002439 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 +02002440 file, linenum, args[cur_arg], p);
2441 err_code |= ERR_ALERT | ERR_FATAL;
2442 goto out;
2443 }
2444 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002445 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002446 file, linenum, args[cur_arg], p);
2447 err_code |= ERR_ALERT | ERR_FATAL;
2448 goto out;
2449 }
2450 }
2451 cur_arg += 2;
2452 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002453 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002454 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002455 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2456 cur_arg += 2;
2457 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002458 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2459 char *p, *end;
2460
2461 for (p = args[cur_arg + 1]; *p; p = end) {
2462 /* cut on next comma */
2463 for (end = p; *end && *end != ','; end++);
2464 if (*end)
2465 *(end++) = 0;
2466
2467 if (!strcmp(p, "allow-dup-ip")) {
2468 newsrv->dns_opts.accept_duplicate_ip = 1;
2469 }
Daniel Corbettf8716912019-11-17 09:48:56 -05002470 else if (!strcmp(p, "ignore-weight")) {
2471 newsrv->dns_opts.ignore_weight = 1;
2472 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002473 else if (!strcmp(p, "prevent-dup-ip")) {
2474 newsrv->dns_opts.accept_duplicate_ip = 0;
2475 }
2476 else {
Daniel Corbettf8716912019-11-17 09:48:56 -05002477 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 +02002478 file, linenum, args[cur_arg], p);
2479 err_code |= ERR_ALERT | ERR_FATAL;
2480 goto out;
2481 }
2482 }
2483
2484 cur_arg += 2;
2485 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002486 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2487 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002488 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002489 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002490 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002491 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002492 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002493 file, linenum, args[cur_arg]);
2494 err_code |= ERR_ALERT | ERR_FATAL;
2495 goto out;
2496 }
2497 cur_arg += 2;
2498 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002499 else if (!strcmp(args[cur_arg], "resolve-net")) {
2500 char *p, *e;
2501 unsigned char mask;
2502 struct dns_options *opt;
2503
2504 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002505 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002506 file, linenum, args[cur_arg]);
2507 err_code |= ERR_ALERT | ERR_FATAL;
2508 goto out;
2509 }
2510
2511 opt = &newsrv->dns_opts;
2512
2513 /* Split arguments by comma, and convert it from ipv4 or ipv6
2514 * string network in in_addr or in6_addr.
2515 */
2516 p = args[cur_arg + 1];
2517 e = p;
2518 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002519 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002520 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002521 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002522 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2523 err_code |= ERR_ALERT | ERR_FATAL;
2524 goto out;
2525 }
2526 /* look for end or comma. */
2527 while (*e != ',' && *e != '\0')
2528 e++;
2529 if (*e == ',') {
2530 *e = '\0';
2531 e++;
2532 }
2533 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2534 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2535 /* Try to convert input string from ipv4 or ipv6 network. */
2536 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2537 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2538 &mask)) {
2539 /* Try to convert input string from ipv6 network. */
2540 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2541 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2542 } else {
2543 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002544 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002545 file, linenum, args[cur_arg], p);
2546 err_code |= ERR_ALERT | ERR_FATAL;
2547 goto out;
2548 }
2549 opt->pref_net_nb++;
2550 p = e;
2551 }
2552
2553 cur_arg += 2;
2554 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002555 else if (!strcmp(args[cur_arg], "rise")) {
2556 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002557 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002558 file, linenum, args[cur_arg]);
2559 err_code |= ERR_ALERT | ERR_FATAL;
2560 goto out;
2561 }
2562
2563 newsrv->check.rise = atol(args[cur_arg + 1]);
2564 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002565 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\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.health)
2572 newsrv->check.health = newsrv->check.rise;
2573 cur_arg += 2;
2574 }
2575 else if (!strcmp(args[cur_arg], "fall")) {
2576 newsrv->check.fall = atol(args[cur_arg + 1]);
2577
2578 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002579 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002580 file, linenum, args[cur_arg]);
2581 err_code |= ERR_ALERT | ERR_FATAL;
2582 goto out;
2583 }
2584
2585 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002586 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002587 file, linenum, args[cur_arg]);
2588 err_code |= ERR_ALERT | ERR_FATAL;
2589 goto out;
2590 }
2591
2592 cur_arg += 2;
2593 }
2594 else if (!strcmp(args[cur_arg], "inter")) {
2595 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002596
2597 if (err == PARSE_TIME_OVER) {
2598 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2599 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2600 err_code |= ERR_ALERT | ERR_FATAL;
2601 goto out;
2602 }
2603 else if (err == PARSE_TIME_UNDER) {
2604 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2605 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2606 err_code |= ERR_ALERT | ERR_FATAL;
2607 goto out;
2608 }
2609 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002610 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002611 file, linenum, *err, newsrv->id);
2612 err_code |= ERR_ALERT | ERR_FATAL;
2613 goto out;
2614 }
2615 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002616 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002617 file, linenum, val, args[cur_arg], newsrv->id);
2618 err_code |= ERR_ALERT | ERR_FATAL;
2619 goto out;
2620 }
2621 newsrv->check.inter = val;
2622 cur_arg += 2;
2623 }
2624 else if (!strcmp(args[cur_arg], "fastinter")) {
2625 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002626
2627 if (err == PARSE_TIME_OVER) {
2628 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2629 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2630 err_code |= ERR_ALERT | ERR_FATAL;
2631 goto out;
2632 }
2633 else if (err == PARSE_TIME_UNDER) {
2634 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2635 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2636 err_code |= ERR_ALERT | ERR_FATAL;
2637 goto out;
2638 }
2639 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002640 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002641 file, linenum, *err, newsrv->id);
2642 err_code |= ERR_ALERT | ERR_FATAL;
2643 goto out;
2644 }
2645 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002646 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002647 file, linenum, val, args[cur_arg], newsrv->id);
2648 err_code |= ERR_ALERT | ERR_FATAL;
2649 goto out;
2650 }
2651 newsrv->check.fastinter = val;
2652 cur_arg += 2;
2653 }
2654 else if (!strcmp(args[cur_arg], "downinter")) {
2655 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002656
2657 if (err == PARSE_TIME_OVER) {
2658 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2659 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2660 err_code |= ERR_ALERT | ERR_FATAL;
2661 goto out;
2662 }
2663 else if (err == PARSE_TIME_UNDER) {
2664 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2665 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2666 err_code |= ERR_ALERT | ERR_FATAL;
2667 goto out;
2668 }
2669 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002670 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002671 file, linenum, *err, newsrv->id);
2672 err_code |= ERR_ALERT | ERR_FATAL;
2673 goto out;
2674 }
2675 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002676 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002677 file, linenum, val, args[cur_arg], newsrv->id);
2678 err_code |= ERR_ALERT | ERR_FATAL;
2679 goto out;
2680 }
2681 newsrv->check.downinter = val;
2682 cur_arg += 2;
2683 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002684 else if (!strcmp(args[cur_arg], "port")) {
2685 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002686 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002687 cur_arg += 2;
2688 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002689 else if (!strcmp(args[cur_arg], "weight")) {
2690 int w;
2691 w = atol(args[cur_arg + 1]);
2692 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002693 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002694 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2695 err_code |= ERR_ALERT | ERR_FATAL;
2696 goto out;
2697 }
2698 newsrv->uweight = newsrv->iweight = w;
2699 cur_arg += 2;
2700 }
2701 else if (!strcmp(args[cur_arg], "minconn")) {
2702 newsrv->minconn = atol(args[cur_arg + 1]);
2703 cur_arg += 2;
2704 }
2705 else if (!strcmp(args[cur_arg], "maxconn")) {
2706 newsrv->maxconn = atol(args[cur_arg + 1]);
2707 cur_arg += 2;
2708 }
2709 else if (!strcmp(args[cur_arg], "maxqueue")) {
2710 newsrv->maxqueue = atol(args[cur_arg + 1]);
2711 cur_arg += 2;
2712 }
2713 else if (!strcmp(args[cur_arg], "slowstart")) {
2714 /* slowstart is stored in seconds */
2715 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002716
2717 if (err == PARSE_TIME_OVER) {
2718 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2719 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2720 err_code |= ERR_ALERT | ERR_FATAL;
2721 goto out;
2722 }
2723 else if (err == PARSE_TIME_UNDER) {
2724 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2725 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2726 err_code |= ERR_ALERT | ERR_FATAL;
2727 goto out;
2728 }
2729 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002730 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002731 file, linenum, *err, newsrv->id);
2732 err_code |= ERR_ALERT | ERR_FATAL;
2733 goto out;
2734 }
2735 newsrv->slowstart = (val + 999) / 1000;
2736 cur_arg += 2;
2737 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002738 else if (!strcmp(args[cur_arg], "on-error")) {
2739 if (!strcmp(args[cur_arg + 1], "fastinter"))
2740 newsrv->onerror = HANA_ONERR_FASTINTER;
2741 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2742 newsrv->onerror = HANA_ONERR_FAILCHK;
2743 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2744 newsrv->onerror = HANA_ONERR_SUDDTH;
2745 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2746 newsrv->onerror = HANA_ONERR_MARKDWN;
2747 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002748 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002749 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2750 file, linenum, args[cur_arg], args[cur_arg + 1]);
2751 err_code |= ERR_ALERT | ERR_FATAL;
2752 goto out;
2753 }
2754
2755 cur_arg += 2;
2756 }
2757 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2758 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2759 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2760 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002761 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002762 file, linenum, args[cur_arg], args[cur_arg + 1]);
2763 err_code |= ERR_ALERT | ERR_FATAL;
2764 goto out;
2765 }
2766
2767 cur_arg += 2;
2768 }
2769 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2770 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2771 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2772 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002773 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002774 file, linenum, args[cur_arg], args[cur_arg + 1]);
2775 err_code |= ERR_ALERT | ERR_FATAL;
2776 goto out;
2777 }
2778
2779 cur_arg += 2;
2780 }
2781 else if (!strcmp(args[cur_arg], "error-limit")) {
2782 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002783 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002784 file, linenum, args[cur_arg]);
2785 err_code |= ERR_ALERT | ERR_FATAL;
2786 goto out;
2787 }
2788
2789 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2790
2791 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002792 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002793 file, linenum, args[cur_arg]);
2794 err_code |= ERR_ALERT | ERR_FATAL;
2795 goto out;
2796 }
2797 cur_arg += 2;
2798 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002799 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002800 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002801 file, linenum, "usesrc", "source");
2802 err_code |= ERR_ALERT | ERR_FATAL;
2803 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002804 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002805 else {
2806 static int srv_dumped;
2807 struct srv_kw *kw;
2808 char *err;
2809
2810 kw = srv_find_kw(args[cur_arg]);
2811 if (kw) {
2812 char *err = NULL;
2813 int code;
2814
2815 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002816 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 +02002817 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002818 if (kw->skip != -1)
2819 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002820 err_code |= ERR_ALERT | ERR_FATAL;
2821 goto out;
2822 }
2823
2824 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002825 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002826 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002827 if (kw->skip != -1)
2828 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002829 err_code |= ERR_ALERT;
2830 continue;
2831 }
2832
2833 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2834 err_code |= code;
2835
2836 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002837 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002838 if (code & ERR_FATAL) {
2839 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002840 if (kw->skip != -1)
2841 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002842 goto out;
2843 }
2844 }
2845 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002846 if (kw->skip != -1)
2847 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002848 continue;
2849 }
2850
2851 err = NULL;
2852 if (!srv_dumped) {
2853 srv_dump_kws(&err);
2854 indent_msg(&err, 4);
2855 srv_dumped = 1;
2856 }
2857
Christopher Faulet767a84b2017-11-24 16:50:31 +01002858 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002859 file, linenum, args[0], args[1], args[cur_arg],
2860 err ? " Registered keywords :" : "", err ? err : "");
2861 free(err);
2862
2863 err_code |= ERR_ALERT | ERR_FATAL;
2864 goto out;
2865 }
2866 }
2867
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002868 if (!defsrv)
2869 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2870 if (err_code & ERR_FATAL)
2871 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002872 if (srv_tmpl)
2873 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002874 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002875 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002876 return 0;
2877
2878 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002879 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002880 free(errmsg);
2881 return err_code;
2882}
2883
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002884/* Returns a pointer to the first server matching either id <id>.
2885 * NULL is returned if no match is found.
2886 * the lookup is performed in the backend <bk>
2887 */
2888struct server *server_find_by_id(struct proxy *bk, int id)
2889{
2890 struct eb32_node *eb32;
2891 struct server *curserver;
2892
2893 if (!bk || (id ==0))
2894 return NULL;
2895
2896 /* <bk> has no backend capabilities, so it can't have a server */
2897 if (!(bk->cap & PR_CAP_BE))
2898 return NULL;
2899
2900 curserver = NULL;
2901
2902 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2903 if (eb32)
2904 curserver = container_of(eb32, struct server, conf.id);
2905
2906 return curserver;
2907}
2908
2909/* Returns a pointer to the first server matching either name <name>, or id
2910 * if <name> starts with a '#'. NULL is returned if no match is found.
2911 * the lookup is performed in the backend <bk>
2912 */
2913struct server *server_find_by_name(struct proxy *bk, const char *name)
2914{
2915 struct server *curserver;
2916
2917 if (!bk || !name)
2918 return NULL;
2919
2920 /* <bk> has no backend capabilities, so it can't have a server */
2921 if (!(bk->cap & PR_CAP_BE))
2922 return NULL;
2923
2924 curserver = NULL;
2925 if (*name == '#') {
2926 curserver = server_find_by_id(bk, atoi(name + 1));
2927 if (curserver)
2928 return curserver;
2929 }
2930 else {
2931 curserver = bk->srv;
2932
2933 while (curserver && (strcmp(curserver->id, name) != 0))
2934 curserver = curserver->next;
2935
2936 if (curserver)
2937 return curserver;
2938 }
2939
2940 return NULL;
2941}
2942
2943struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2944{
2945 struct server *byname;
2946 struct server *byid;
2947
2948 if (!name && !id)
2949 return NULL;
2950
2951 if (diff)
2952 *diff = 0;
2953
2954 byname = byid = NULL;
2955
2956 if (name) {
2957 byname = server_find_by_name(bk, name);
2958 if (byname && (!id || byname->puid == id))
2959 return byname;
2960 }
2961
2962 /* remaining possibilities :
2963 * - name not set
2964 * - name set but not found
2965 * - name found but ID doesn't match
2966 */
2967 if (id) {
2968 byid = server_find_by_id(bk, id);
2969 if (byid) {
2970 if (byname) {
2971 /* use id only if forced by configuration */
2972 if (byid->flags & SRV_F_FORCED_ID) {
2973 if (diff)
2974 *diff |= 2;
2975 return byid;
2976 }
2977 else {
2978 if (diff)
2979 *diff |= 1;
2980 return byname;
2981 }
2982 }
2983
2984 /* remaining possibilities:
2985 * - name not set
2986 * - name set but not found
2987 */
2988 if (name && diff)
2989 *diff |= 2;
2990 return byid;
2991 }
2992
2993 /* id bot found */
2994 if (byname) {
2995 if (diff)
2996 *diff |= 1;
2997 return byname;
2998 }
2999 }
3000
3001 return NULL;
3002}
3003
Willy Tarreau46b7f532018-08-21 11:54:26 +02003004/* Update a server state using the parameters available in the params list.
3005 *
3006 * Grabs the server lock during operation.
3007 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003008static void srv_update_state(struct server *srv, int version, char **params)
3009{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003010 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02003011 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003012
3013 /* fields since version 1
3014 * and common to all other upcoming versions
3015 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003016 enum srv_state srv_op_state;
3017 enum srv_admin srv_admin_state;
3018 unsigned srv_uweight, srv_iweight;
3019 unsigned long srv_last_time_change;
3020 short srv_check_status;
3021 enum chk_result srv_check_result;
3022 int srv_check_health;
3023 int srv_check_state, srv_agent_state;
3024 int bk_f_forced_id;
3025 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003026 int fqdn_set_by_cli;
3027 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02003028 const char *port_str;
3029 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003030 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003031
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003032 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02003033 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02003034 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003035 switch (version) {
3036 case 1:
3037 /*
3038 * now we can proceed with server's state update:
3039 * srv_addr: params[0]
3040 * srv_op_state: params[1]
3041 * srv_admin_state: params[2]
3042 * srv_uweight: params[3]
3043 * srv_iweight: params[4]
3044 * srv_last_time_change: params[5]
3045 * srv_check_status: params[6]
3046 * srv_check_result: params[7]
3047 * srv_check_health: params[8]
3048 * srv_check_state: params[9]
3049 * srv_agent_state: params[10]
3050 * bk_f_forced_id: params[11]
3051 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003052 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003053 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003054 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003055 */
3056
3057 /* validating srv_op_state */
3058 p = NULL;
3059 errno = 0;
3060 srv_op_state = strtol(params[1], &p, 10);
3061 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
3062 (srv_op_state != SRV_ST_STOPPED &&
3063 srv_op_state != SRV_ST_STARTING &&
3064 srv_op_state != SRV_ST_RUNNING &&
3065 srv_op_state != SRV_ST_STOPPING)) {
3066 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
3067 }
3068
3069 /* validating srv_admin_state */
3070 p = NULL;
3071 errno = 0;
3072 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003073 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01003074
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003075 /* inherited statuses will be recomputed later.
3076 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
3077 */
3078 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01003079
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003080 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
3081 (srv_admin_state != 0 &&
3082 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003083 srv_admin_state != SRV_ADMF_CMAINT &&
3084 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01003085 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01003086 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003087 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
3088 }
3089
3090 /* validating srv_uweight */
3091 p = NULL;
3092 errno = 0;
3093 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003094 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003095 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
3096
3097 /* validating srv_iweight */
3098 p = NULL;
3099 errno = 0;
3100 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02003101 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003102 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
3103
3104 /* validating srv_last_time_change */
3105 p = NULL;
3106 errno = 0;
3107 srv_last_time_change = strtol(params[5], &p, 10);
3108 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
3109 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
3110
3111 /* validating srv_check_status */
3112 p = NULL;
3113 errno = 0;
3114 srv_check_status = strtol(params[6], &p, 10);
3115 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
3116 (srv_check_status >= HCHK_STATUS_SIZE))
3117 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
3118
3119 /* validating srv_check_result */
3120 p = NULL;
3121 errno = 0;
3122 srv_check_result = strtol(params[7], &p, 10);
3123 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
3124 (srv_check_result != CHK_RES_UNKNOWN &&
3125 srv_check_result != CHK_RES_NEUTRAL &&
3126 srv_check_result != CHK_RES_FAILED &&
3127 srv_check_result != CHK_RES_PASSED &&
3128 srv_check_result != CHK_RES_CONDPASS)) {
3129 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
3130 }
3131
3132 /* validating srv_check_health */
3133 p = NULL;
3134 errno = 0;
3135 srv_check_health = strtol(params[8], &p, 10);
3136 if (p == params[8] || errno == EINVAL || errno == ERANGE)
3137 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
3138
3139 /* validating srv_check_state */
3140 p = NULL;
3141 errno = 0;
3142 srv_check_state = strtol(params[9], &p, 10);
3143 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
3144 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3145 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
3146
3147 /* validating srv_agent_state */
3148 p = NULL;
3149 errno = 0;
3150 srv_agent_state = strtol(params[10], &p, 10);
3151 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
3152 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
3153 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
3154
3155 /* validating bk_f_forced_id */
3156 p = NULL;
3157 errno = 0;
3158 bk_f_forced_id = strtol(params[11], &p, 10);
3159 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
3160 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
3161
3162 /* validating srv_f_forced_id */
3163 p = NULL;
3164 errno = 0;
3165 srv_f_forced_id = strtol(params[12], &p, 10);
3166 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
3167 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
3168
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003169 /* validating srv_fqdn */
3170 fqdn = params[13];
3171 if (fqdn && *fqdn == '-')
3172 fqdn = NULL;
3173 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
3174 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
3175 fqdn = NULL;
3176 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003177
Frédéric Lécaille31694712017-08-01 08:47:19 +02003178 port_str = params[14];
3179 if (port_str) {
3180 port = strl2uic(port_str, strlen(port_str));
3181 if (port > USHRT_MAX) {
3182 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
3183 port_str = NULL;
3184 }
3185 }
3186
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003187 /* SRV record
3188 * NOTE: in HAProxy, SRV records must start with an underscore '_'
3189 */
3190 srvrecord = params[15];
3191 if (srvrecord && *srvrecord != '_')
3192 srvrecord = NULL;
3193
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003194 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003195 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003196 goto out;
3197
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003198 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003199 /* recover operational state and apply it to this server
3200 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003201 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003202 switch (srv_op_state) {
3203 case SRV_ST_STOPPED:
3204 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003205 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003206 break;
3207 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003208 /* If rise == 1 there is no STARTING state, let's switch to
3209 * RUNNING
3210 */
3211 if (srv->check.rise == 1) {
3212 srv->check.health = srv->check.rise + srv->check.fall - 1;
3213 srv_set_running(srv, "", NULL);
3214 break;
3215 }
3216 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3217 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003218 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003219 break;
3220 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003221 /* If fall == 1 there is no STOPPING state, let's switch to
3222 * STOPPED
3223 */
3224 if (srv->check.fall == 1) {
3225 srv->check.health = 0;
3226 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3227 break;
3228 }
3229 if (srv->check.health < srv->check.rise ||
3230 srv->check.health > srv->check.rise + srv->check.fall - 2)
3231 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003232 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003233 break;
3234 case SRV_ST_RUNNING:
3235 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003236 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003237 break;
3238 }
3239
3240 /* When applying server state, the following rules apply:
3241 * - in case of a configuration change, we apply the setting from the new
3242 * configuration, regardless of old running state
3243 * - if no configuration change, we apply old running state only if old running
3244 * state is different from new configuration state
3245 */
3246 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003247 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3248 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003249 srv_adm_set_maint(srv);
3250 else
3251 srv_adm_set_ready(srv);
3252 }
3253 /* configuration is the same, let's compate old running state and new conf state */
3254 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003255 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003256 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003257 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003258 srv_adm_set_ready(srv);
3259 }
3260 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003261 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003262 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003263 * (srv->iweight is the weight set up in configuration).
3264 * There are two possible reasons for FDRAIN to have been present :
3265 * - previous config weight was zero
3266 * - "set server b/s drain" was sent to the CLI
3267 *
3268 * In the first case, we simply want to drop this drain state
3269 * if the new weight is not zero anymore, meaning the administrator
3270 * has intentionally turned the weight back to a positive value to
3271 * enable the server again after an operation. In the second case,
3272 * the drain state was forced on the CLI regardless of the config's
3273 * weight so we don't want a change to the config weight to lose this
3274 * status. What this means is :
3275 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3276 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003277 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003278 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003279 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003280 }
3281
3282 srv->last_change = date.tv_sec - srv_last_time_change;
3283 srv->check.status = srv_check_status;
3284 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003285
3286 /* Only case we want to apply is removing ENABLED flag which could have been
3287 * done by the "disable health" command over the stats socket
3288 */
3289 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3290 (srv_check_state & CHK_ST_CONFIGURED) &&
3291 !(srv_check_state & CHK_ST_ENABLED))
3292 srv->check.state &= ~CHK_ST_ENABLED;
3293
3294 /* Only case we want to apply is removing ENABLED flag which could have been
3295 * done by the "disable agent" command over the stats socket
3296 */
3297 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3298 (srv_agent_state & CHK_ST_CONFIGURED) &&
3299 !(srv_agent_state & CHK_ST_ENABLED))
3300 srv->agent.state &= ~CHK_ST_ENABLED;
3301
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003302 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3303 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003304 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003305 * It means that a configuration file change has precedence over a unix socket change
3306 * for server's weight
3307 *
3308 * by default, HAProxy applies the following weight when parsing the configuration
3309 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003310 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003311 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003312 srv->uweight = srv_uweight;
3313 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003314 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003315
Willy Tarreaue5a60682016-11-09 14:54:53 +01003316 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003317 if (strcmp(params[0], "-"))
3318 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003319
3320 if (fqdn && srv->hostname) {
3321 if (!strcmp(srv->hostname, fqdn)) {
3322 /* Here we reset the 'set from stats socket FQDN' flag
3323 * to support such transitions:
3324 * Let's say initial FQDN value is foo1 (in configuration file).
3325 * - FQDN changed from stats socket, from foo1 to foo2 value,
3326 * - FQDN changed again from file configuration (with the same previous value
3327 set from stats socket, from foo1 to foo2 value),
3328 * - reload for any other reason than a FQDN modification,
3329 * the configuration file FQDN matches the fqdn server state file value.
3330 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003331 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003332 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003333 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003334 }
3335 else {
3336 /* If the FDQN has been changed from stats socket,
3337 * apply fqdn state file value (which is the value set
3338 * from stats socket).
Baptiste Assmann13a92322019-06-07 09:40:55 +02003339 * Also ensure the runtime resolver will process this resolution.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003340 */
3341 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003342 srv_set_fqdn(srv, fqdn, 0);
Baptiste Assmann13a92322019-06-07 09:40:55 +02003343 srv->flags &= ~SRV_F_NO_RESOLUTION;
Emeric Brun52a91d32017-08-31 14:41:55 +02003344 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003345 }
3346 }
3347 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003348 /* If all the conditions below are validated, this means
3349 * we're evaluating a server managed by SRV resolution
3350 */
3351 else if (fqdn && !srv->hostname && srvrecord) {
3352 int res;
3353
3354 /* we can't apply previous state if SRV record has changed */
3355 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3356 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);
3357 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3358 goto out;
3359 }
3360
3361 /* create or find a SRV resolution for this srv record */
3362 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3363 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3364 if (srv->srvrq == NULL) {
3365 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3366 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3367 goto out;
3368 }
3369
3370 /* prepare DNS resolution for this server */
3371 res = srv_prepare_for_resolution(srv, fqdn);
3372 if (res == -1) {
3373 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3374 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3375 goto out;
3376 }
3377
3378 /* configure check.port accordingly */
3379 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3380 !(srv->flags & SRV_F_CHECKPORT))
3381 srv->check.port = port;
3382
3383 /* Unset SRV_F_MAPPORTS for SRV records.
3384 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3385 * because no ports are provided in the configuration file.
3386 * This is because HAProxy will use the port found into the SRV record.
3387 */
3388 srv->flags &= ~SRV_F_MAPPORTS;
3389 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003390
Frédéric Lécaille31694712017-08-01 08:47:19 +02003391 if (port_str)
3392 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003393 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003394
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003395 break;
3396 default:
3397 chunk_appendf(msg, ", version '%d' not supported", version);
3398 }
3399
3400 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003401 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003402 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003403 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003404 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003405 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003406}
3407
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003408
3409/*
3410 * read next line from file <f> and return the server state version if one found.
3411 * If no version is found, then 0 is returned
3412 * Note that this should be the first read on <f>
3413 */
3414static int srv_state_get_version(FILE *f) {
3415 char buf[2];
3416 int ret;
3417
3418 /* first character of first line of the file must contain the version of the export */
3419 if (fgets(buf, 2, f) == NULL) {
3420 return 0;
3421 }
3422
3423 ret = atoi(buf);
3424 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
3425 (ret > SRV_STATE_FILE_VERSION_MAX))
3426 return 0;
3427
3428 return ret;
3429}
3430
3431
3432/*
3433 * parses server state line stored in <buf> and supposedly in version <version>.
3434 * Set <params> and <srv_params> accordingly.
3435 * In case of error, params[0] is set to NULL.
3436 */
3437static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
3438{
3439 int buflen, arg, srv_arg;
3440 char *cur, *end;
3441
3442 buflen = strlen(buf);
3443 cur = buf;
3444 end = cur + buflen;
3445
3446 /* we need at least one character */
3447 if (buflen == 0) {
3448 params[0] = NULL;
3449 return;
3450 }
3451
3452 /* ignore blank characters at the beginning of the line */
Willy Tarreau90807112020-02-25 08:16:33 +01003453 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003454 ++cur;
3455
3456 /* Ignore empty or commented lines */
3457 if (cur == end || *cur == '#') {
3458 params[0] = NULL;
3459 return;
3460 }
3461
3462 /* truncated lines */
3463 if (buf[buflen - 1] != '\n') {
3464 //ha_warning("server-state file '%s': truncated line\n", filepath);
3465 params[0] = NULL;
3466 return;
3467 }
3468
3469 /* Removes trailing '\n' */
3470 buf[buflen - 1] = '\0';
3471
3472 /* we're now ready to move the line into *srv_params[] */
3473 params[0] = cur;
3474 arg = 1;
3475 srv_arg = 0;
3476 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
Willy Tarreau90807112020-02-25 08:16:33 +01003477 if (isspace((unsigned char)*cur)) {
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003478 *cur = '\0';
3479 ++cur;
Willy Tarreau90807112020-02-25 08:16:33 +01003480 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003481 ++cur;
3482 switch (version) {
3483 case 1:
3484 /*
3485 * srv_addr: params[4] => srv_params[0]
3486 * srv_op_state: params[5] => srv_params[1]
3487 * srv_admin_state: params[6] => srv_params[2]
3488 * srv_uweight: params[7] => srv_params[3]
3489 * srv_iweight: params[8] => srv_params[4]
3490 * srv_last_time_change: params[9] => srv_params[5]
3491 * srv_check_status: params[10] => srv_params[6]
3492 * srv_check_result: params[11] => srv_params[7]
3493 * srv_check_health: params[12] => srv_params[8]
3494 * srv_check_state: params[13] => srv_params[9]
3495 * srv_agent_state: params[14] => srv_params[10]
3496 * bk_f_forced_id: params[15] => srv_params[11]
3497 * srv_f_forced_id: params[16] => srv_params[12]
3498 * srv_fqdn: params[17] => srv_params[13]
3499 * srv_port: params[18] => srv_params[14]
3500 * srvrecord: params[19] => srv_params[15]
3501 */
3502 if (arg >= 4) {
3503 srv_params[srv_arg] = cur;
3504 ++srv_arg;
3505 }
3506 break;
3507 }
3508
3509 params[arg] = cur;
3510 ++arg;
3511 }
3512 else {
3513 ++cur;
3514 }
3515 }
3516
3517 /* if line is incomplete line, then ignore it.
3518 * otherwise, update useful flags */
3519 switch (version) {
3520 case 1:
3521 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3522 params[0] = NULL;
3523 return;
3524 }
3525 break;
3526 }
3527
3528 return;
3529}
3530
3531
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003532/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3533 * For each proxy, it does the following:
3534 * - opens its server state file (either one or local one)
3535 * - read whole file, line by line
3536 * - analyse each line to check if it matches our current backend:
3537 * - backend name matches
3538 * - backend id matches if id is forced and name doesn't match
3539 * - if the server pointed by the line is found, then state is applied
3540 *
3541 * If the running backend uuid or id differs from the state file, then HAProxy reports
3542 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003543 *
3544 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003545 */
3546void apply_server_state(void)
3547{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003548 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003549 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3550 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003551 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003552 FILE *f;
3553 char *filepath;
3554 char globalfilepath[MAXPATHLEN + 1];
3555 char localfilepath[MAXPATHLEN + 1];
3556 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003557 struct proxy *curproxy, *bk;
3558 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003559 char *line;
3560 char *bkname, *srvname;
3561 struct state_line *st;
3562 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003563
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003564
3565 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003566 globalfilepathlen = 0;
3567 /* create the globalfilepath variable */
3568 if (global.server_state_file) {
3569 /* absolute path or no base directory provided */
3570 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3571 len = strlen(global.server_state_file);
3572 if (len > MAXPATHLEN) {
3573 globalfilepathlen = 0;
3574 goto globalfileerror;
3575 }
3576 memcpy(globalfilepath, global.server_state_file, len);
3577 globalfilepath[len] = '\0';
3578 globalfilepathlen = len;
3579 }
3580 else if (global.server_state_base) {
3581 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003582 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003583 globalfilepathlen = 0;
3584 goto globalfileerror;
3585 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003586 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003587 globalfilepath[len] = 0;
3588 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003589
3590 /* append a slash if needed */
3591 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3592 if (globalfilepathlen + 1 > MAXPATHLEN) {
3593 globalfilepathlen = 0;
3594 goto globalfileerror;
3595 }
3596 globalfilepath[globalfilepathlen++] = '/';
3597 }
3598
3599 len = strlen(global.server_state_file);
3600 if (globalfilepathlen + len > MAXPATHLEN) {
3601 globalfilepathlen = 0;
3602 goto globalfileerror;
3603 }
3604 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3605 globalfilepathlen += len;
3606 globalfilepath[globalfilepathlen++] = 0;
3607 }
3608 }
3609 globalfileerror:
3610 if (globalfilepathlen == 0)
3611 globalfilepath[0] = '\0';
3612
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003613 /* Load global server state in a tree */
3614 if (globalfilepathlen > 0) {
3615 errno = 0;
3616 f = fopen(globalfilepath, "r");
3617 if (errno)
3618 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
Vedran Furac5d486272019-10-16 14:49:38 +02003619 if (!f)
3620 goto out_load_server_state_in_tree;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003621
3622 global_file_version = srv_state_get_version(f);
3623 if (global_file_version == 0)
3624 goto out_load_server_state_in_tree;
3625
3626 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3627 line = NULL;
3628
3629 line = strdup(mybuf);
3630 if (line == NULL)
3631 continue;
3632
3633 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3634 if (params[0] == NULL)
Willy Tarreauca7a5af2019-12-20 17:26:27 +01003635 goto nextline;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003636
3637 /* bkname */
3638 bkname = params[1];
3639 /* srvname */
3640 srvname = params[3];
3641
3642 /* key */
3643 chunk_printf(&trash, "%s %s", bkname, srvname);
3644
3645 /* store line in tree */
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003646 st = calloc(1, sizeof(*st) + trash.data + 1);
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003647 if (st == NULL) {
3648 goto nextline;
3649 }
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003650 memcpy(st->name_name.key, trash.area, trash.data + 1);
Willy Tarreaufd1aa012019-12-20 17:23:40 +01003651 if (ebst_insert(&state_file, &st->name_name) != &st->name_name) {
3652 /* this is a duplicate key, probably a hand-crafted file,
3653 * drop it!
3654 */
3655 goto nextline;
3656 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003657
3658 /* save line */
3659 st->line = line;
3660
3661 continue;
3662
3663 nextline:
3664 /* free up memory in case of error during the processing of the line */
3665 free(line);
3666 }
3667 }
3668 out_load_server_state_in_tree:
3669
3670 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003671 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003672 /* servers are only in backends */
3673 if (!(curproxy->cap & PR_CAP_BE))
3674 continue;
3675 fileopenerr = 0;
3676 filepath = NULL;
3677
3678 /* search server state file path and name */
3679 switch (curproxy->load_server_state_from_file) {
3680 /* read servers state from global file */
3681 case PR_SRV_STATE_FILE_GLOBAL:
3682 /* there was an error while generating global server state file path */
3683 if (globalfilepathlen == 0)
3684 continue;
3685 filepath = globalfilepath;
3686 fileopenerr = 1;
3687 break;
3688 /* this backend has its own file */
3689 case PR_SRV_STATE_FILE_LOCAL:
3690 localfilepathlen = 0;
3691 localfilepath[0] = '\0';
3692 len = 0;
3693 /* create the localfilepath variable */
3694 /* absolute path or no base directory provided */
3695 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3696 len = strlen(curproxy->server_state_file_name);
3697 if (len > MAXPATHLEN) {
3698 localfilepathlen = 0;
3699 goto localfileerror;
3700 }
3701 memcpy(localfilepath, curproxy->server_state_file_name, len);
3702 localfilepath[len] = '\0';
3703 localfilepathlen = len;
3704 }
3705 else if (global.server_state_base) {
3706 len = strlen(global.server_state_base);
3707 localfilepathlen += len;
3708
3709 if (localfilepathlen > MAXPATHLEN) {
3710 localfilepathlen = 0;
3711 goto localfileerror;
3712 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003713 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003714 localfilepath[localfilepathlen] = 0;
3715
3716 /* append a slash if needed */
3717 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3718 if (localfilepathlen + 1 > MAXPATHLEN) {
3719 localfilepathlen = 0;
3720 goto localfileerror;
3721 }
3722 localfilepath[localfilepathlen++] = '/';
3723 }
3724
3725 len = strlen(curproxy->server_state_file_name);
3726 if (localfilepathlen + len > MAXPATHLEN) {
3727 localfilepathlen = 0;
3728 goto localfileerror;
3729 }
3730 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3731 localfilepathlen += len;
3732 localfilepath[localfilepathlen++] = 0;
3733 }
3734 filepath = localfilepath;
3735 localfileerror:
3736 if (localfilepathlen == 0)
3737 localfilepath[0] = '\0';
3738
3739 break;
3740 case PR_SRV_STATE_FILE_NONE:
3741 default:
3742 continue;
3743 }
3744
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003745 /* when global file is used, we get data from the tree
3746 * Note that in such case we don't check backend name neither uuid.
3747 * Backend name can't be wrong since it's used as a key to retrieve the server state
3748 * line from the tree.
3749 */
3750 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3751 struct server *srv = curproxy->srv;
3752 while (srv) {
3753 struct ebmb_node *node;
3754 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003755
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003756 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3757 node = ebst_lookup(&state_file, trash.area);
3758 if (!node)
3759 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003760
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003761 st = container_of(node, struct state_line, name_name);
3762 memcpy(mybuf, st->line, strlen(st->line));
3763 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003764
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003765 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3766 if (params[0] == NULL)
3767 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003768
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003769 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003770
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003771 next:
3772 srv = srv->next;
3773 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003774
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003775 continue; /* next proxy in list */
3776 }
3777 else {
3778 /* load 'local' state file */
3779 errno = 0;
3780 f = fopen(filepath, "r");
3781 if (errno && fileopenerr)
3782 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3783 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003784 continue;
3785
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003786 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003787
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003788 /* first character of first line of the file must contain the version of the export */
3789 version = srv_state_get_version(f);
3790 if (version == 0) {
3791 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3792 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003793 }
3794
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003795 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3796 int bk_f_forced_id = 0;
3797 int check_id = 0;
3798 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003799
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003800 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003801
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003802 if (params[0] == NULL) {
3803 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003804 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003805
3806 /* if line is incomplete line, then ignore it.
3807 * otherwise, update useful flags */
3808 switch (version) {
3809 case 1:
3810 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3811 check_id = (atoi(params[0]) == curproxy->uuid);
3812 check_name = (strcmp(curproxy->id, params[1]) == 0);
3813 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003814 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003815
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003816 bk = curproxy;
3817
3818 /* if backend can't be found, let's continue */
3819 if (!check_id && !check_name)
3820 continue;
3821 else if (!check_id && check_name) {
3822 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3823 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3824 }
3825 else if (check_id && !check_name) {
3826 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3827 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3828 /* if name doesn't match, we still want to update curproxy if the backend id
3829 * was forced in previous the previous configuration */
3830 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003831 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003832 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003833
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003834 /* look for the server by its name: param[3] */
3835 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003836
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003837 if (!srv) {
3838 /* if no server found, then warning and continue with next line */
3839 ha_warning("can't find server '%s' in backend '%s'\n",
3840 params[3], params[1]);
3841 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3842 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003843 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003844 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003845
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003846 /* now we can proceed with server's state update */
3847 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003848 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003849 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003850fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003851 fclose(f);
3852 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003853
3854 /* now free memory allocated for the tree */
3855 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3856 node;
3857 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3858 st = container_of(node, struct state_line, name_name);
3859 ebmb_delete(&st->name_name);
3860 free(st->line);
3861 free(st);
3862 }
3863
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003864}
3865
Simon Horman7d09b9a2013-02-12 10:45:51 +09003866/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003867 * update a server's current IP address.
3868 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3869 * ip is in network format.
3870 * updater is a string which contains an information about the requester of the update.
3871 * updater is used if not NULL.
3872 *
3873 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003874 *
3875 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003876 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003877int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003878{
3879 /* generates a log line and a warning on stderr */
3880 if (1) {
3881 /* book enough space for both IPv4 and IPv6 */
3882 char oldip[INET6_ADDRSTRLEN];
3883 char newip[INET6_ADDRSTRLEN];
3884
3885 memset(oldip, '\0', INET6_ADDRSTRLEN);
3886 memset(newip, '\0', INET6_ADDRSTRLEN);
3887
3888 /* copy old IP address in a string */
3889 switch (s->addr.ss_family) {
3890 case AF_INET:
3891 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3892 break;
3893 case AF_INET6:
3894 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3895 break;
3896 };
3897
3898 /* copy new IP address in a string */
3899 switch (ip_sin_family) {
3900 case AF_INET:
3901 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3902 break;
3903 case AF_INET6:
3904 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3905 break;
3906 };
3907
3908 /* save log line into a buffer */
3909 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3910 s->proxy->id, s->id, oldip, newip, updater);
3911
3912 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003913 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003914
3915 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003916 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003917 }
3918
3919 /* save the new IP family */
3920 s->addr.ss_family = ip_sin_family;
3921 /* save the new IP address */
3922 switch (ip_sin_family) {
3923 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003924 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003925 break;
3926 case AF_INET6:
3927 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3928 break;
3929 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003930 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003931
3932 return 0;
3933}
3934
3935/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003936 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3937 *
3938 * Caller can pass its name through <updater> to get it integrated in the response message
3939 * returned by the function.
3940 *
3941 * The function first does the following, in that order:
3942 * - validates the new addr and/or port
3943 * - checks if an update is required (new IP or port is different than current ones)
3944 * - checks the update is allowed:
3945 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3946 * - allow all changes if no CHECKS are configured
3947 * - if CHECK is configured:
3948 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3949 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3950 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003951 *
3952 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003953 */
3954const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3955{
3956 struct sockaddr_storage sa;
3957 int ret, port_change_required;
3958 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003959 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003960 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003961 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003962
3963 msg = get_trash_chunk();
3964 chunk_reset(msg);
3965
3966 if (addr) {
3967 memset(&sa, 0, sizeof(struct sockaddr_storage));
3968 if (str2ip2(addr, &sa, 0) == NULL) {
3969 chunk_printf(msg, "Invalid addr '%s'", addr);
3970 goto out;
3971 }
3972
3973 /* changes are allowed on AF_INET* families only */
3974 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3975 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3976 goto out;
3977 }
3978
3979 /* collecting data currently setup */
3980 memset(current_addr, '\0', sizeof(current_addr));
3981 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3982 /* changes are allowed on AF_INET* families only */
3983 if ((ret != AF_INET) && (ret != AF_INET6)) {
3984 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3985 goto out;
3986 }
3987
3988 /* applying ADDR changes if required and allowed
3989 * ipcmp returns 0 when both ADDR are the same
3990 */
3991 if (ipcmp(&s->addr, &sa) == 0) {
3992 chunk_appendf(msg, "no need to change the addr");
3993 goto port;
3994 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003995 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003996 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003997
3998 /* we also need to update check's ADDR only if it uses the server's one */
3999 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004000 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004001 }
4002
4003 /* we also need to update agent ADDR only if it use the server's one */
4004 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004005 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004006 }
4007
4008 /* update report for caller */
4009 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
4010 }
4011
4012 port:
4013 if (port) {
4014 char sign = '\0';
4015 char *endptr;
4016
4017 if (addr)
4018 chunk_appendf(msg, ", ");
4019
4020 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01004021 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004022
4023 /* check if PORT change is required */
4024 port_change_required = 0;
4025
4026 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04004027 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004028 new_port = strtol(port, &endptr, 10);
4029 if ((errno != 0) || (port == endptr)) {
4030 chunk_appendf(msg, "problem converting port '%s' to an int", port);
4031 goto out;
4032 }
4033
4034 /* check if caller triggers a port mapped or offset */
4035 if (sign == '-' || (sign == '+')) {
4036 /* check if server currently uses port map */
4037 if (!(s->flags & SRV_F_MAPPORTS)) {
4038 /* switch from fixed port to port map mandatorily triggers
4039 * a port change */
4040 port_change_required = 1;
4041 /* check is configured
4042 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
4043 * prevent PORT change if check doesn't have it's dedicated port while switching
4044 * to port mapping */
4045 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
4046 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.");
4047 goto out;
4048 }
4049 }
4050 /* we're already using port maps */
4051 else {
4052 port_change_required = current_port != new_port;
4053 }
4054 }
4055 /* fixed port */
4056 else {
4057 port_change_required = current_port != new_port;
4058 }
4059
4060 /* applying PORT changes if required and update response message */
4061 if (port_change_required) {
4062 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01004063 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01004064 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004065
4066 /* prepare message */
4067 chunk_appendf(msg, "port changed from '");
4068 if (s->flags & SRV_F_MAPPORTS)
4069 chunk_appendf(msg, "+");
4070 chunk_appendf(msg, "%d' to '", current_port);
4071
4072 if (sign == '-') {
4073 s->flags |= SRV_F_MAPPORTS;
4074 chunk_appendf(msg, "%c", sign);
4075 /* just use for result output */
4076 new_port = -new_port;
4077 }
4078 else if (sign == '+') {
4079 s->flags |= SRV_F_MAPPORTS;
4080 chunk_appendf(msg, "%c", sign);
4081 }
4082 else {
4083 s->flags &= ~SRV_F_MAPPORTS;
4084 }
4085
4086 chunk_appendf(msg, "%d'", new_port);
4087
4088 /* we also need to update health checks port only if it uses server's realport */
4089 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
4090 s->check.port = new_port;
4091 }
4092 }
4093 else {
4094 chunk_appendf(msg, "no need to change the port");
4095 }
4096 }
4097
4098out:
Olivier Houchard4e694042017-03-14 20:01:29 +01004099 if (changed)
4100 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004101 if (updater)
4102 chunk_appendf(msg, " by '%s'", updater);
4103 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004104 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02004105}
4106
4107
4108/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004109 * update server status based on result of name resolution
4110 * returns:
4111 * 0 if server status is updated
4112 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02004113 *
4114 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004115 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004116int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004117{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004118 struct dns_resolvers *resolvers = s->resolvers;
4119 struct dns_resolution *resolution = s->dns_requester->resolution;
4120 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004121
4122 switch (resolution->status) {
4123 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004124 /* status when HAProxy has just (re)started.
4125 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004126 break;
4127
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004128 case RSLV_STATUS_VALID:
4129 /*
4130 * resume health checks
4131 * server will be turned back on if health check is safe
4132 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004133 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02004134 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004135 return 1;
4136 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
4137 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02004138 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004139 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02004140
Emeric Brun52a91d32017-08-31 14:41:55 +02004141 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004142 return 1;
4143 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
4144 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
4145 s->proxy->id, s->id);
4146
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004147 ha_warning("%s.\n", trash.area);
4148 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004149 return 0;
4150
4151 case RSLV_STATUS_NX:
4152 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004153 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
4154 if (!tick_is_expired(exp, now_ms))
4155 break;
4156
4157 if (s->next_admin & SRV_ADMF_RMAINT)
4158 return 1;
4159 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
4160 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004161
4162 case RSLV_STATUS_TIMEOUT:
4163 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004164 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
4165 if (!tick_is_expired(exp, now_ms))
4166 break;
4167
4168 if (s->next_admin & SRV_ADMF_RMAINT)
4169 return 1;
4170 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
4171 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004172
4173 case RSLV_STATUS_REFUSED:
4174 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004175 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
4176 if (!tick_is_expired(exp, now_ms))
4177 break;
4178
4179 if (s->next_admin & SRV_ADMF_RMAINT)
4180 return 1;
4181 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
4182 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01004183
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004184 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004185 /* stop server if resolution failed for a long enough period */
4186 exp = tick_add(resolution->last_valid, resolvers->hold.other);
4187 if (!tick_is_expired(exp, now_ms))
4188 break;
4189
4190 if (s->next_admin & SRV_ADMF_RMAINT)
4191 return 1;
4192 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
4193 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004194 }
4195
4196 return 1;
4197}
4198
4199/*
4200 * Server Name Resolution valid response callback
4201 * It expects:
4202 * - <nameserver>: the name server which answered the valid response
4203 * - <response>: buffer containing a valid DNS response
4204 * - <response_len>: size of <response>
4205 * It performs the following actions:
4206 * - ignore response if current ip found and server family not met
4207 * - update with first new ip found if family is met and current IP is not found
4208 * returns:
4209 * 0 on error
4210 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01004211 *
4212 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004213 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004214int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004215{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004216 struct server *s = NULL;
4217 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004218 void *serverip, *firstip;
4219 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004220 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02004221 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004222 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004223
Christopher Faulet67957bd2017-09-27 11:00:59 +02004224 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004225 if (!s)
4226 return 1;
4227
Christopher Faulet67957bd2017-09-27 11:00:59 +02004228 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004229
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004230 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004231 firstip = NULL; /* pointer to the first valid response found */
4232 /* it will be used as the new IP if a change is required */
4233 firstip_sin_family = AF_UNSPEC;
4234 serverip = NULL; /* current server IP address */
4235
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004236 /* initializing server IP pointer */
4237 server_sin_family = s->addr.ss_family;
4238 switch (server_sin_family) {
4239 case AF_INET:
4240 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
4241 break;
4242
4243 case AF_INET6:
4244 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
4245 break;
4246
Willy Tarreau3acfcd12017-01-06 19:18:32 +01004247 case AF_UNSPEC:
4248 break;
4249
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004250 default:
4251 goto invalid;
4252 }
4253
Baptiste Assmann729c9012017-05-22 15:13:10 +02004254 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01004255 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004256 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004257
4258 switch (ret) {
4259 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004260 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004261
4262 case DNS_UPD_SRVIP_NOT_FOUND:
4263 goto save_ip;
4264
4265 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004266 goto invalid;
4267
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004268 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004269 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004270 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02004271
Baptiste Assmannfad03182015-10-28 02:03:32 +01004272 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01004273 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02004274 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004275 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01004276
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004277 default:
4278 goto invalid;
4279
4280 }
4281
4282 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02004283 if (nameserver) {
4284 nameserver->counters.update++;
4285 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004286 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004287 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004288 else
4289 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004290 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004291
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004292 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004293 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004294 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004295
4296 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004297 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02004298 nameserver->counters.invalid++;
4299 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02004300 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004301 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004302 return 0;
4303}
4304
4305/*
4306 * Server Name Resolution error management callback
4307 * returns:
4308 * 0 on error
4309 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02004310 *
4311 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004312 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004313int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004314{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004315 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004316
Christopher Faulet67957bd2017-09-27 11:00:59 +02004317 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004318 if (!s)
4319 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004320 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004321 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004322 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004323 return 1;
4324}
4325
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004326/*
4327 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004328 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004329 * It returns a pointer to the first server found or NULL if <ip> is not yet
4330 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004331 *
4332 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004333 */
4334struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4335{
4336 struct server *tmpsrv;
4337 struct proxy *be;
4338
4339 if (!srv)
4340 return NULL;
4341
4342 be = srv->proxy;
4343 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004344 /* we found the current server is the same, ignore it */
4345 if (srv == tmpsrv)
4346 continue;
4347
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004348 /* We want to compare the IP in the record with the IP of the servers in the
4349 * same backend, only if:
4350 * * DNS resolution is enabled on the server
4351 * * the hostname used for the resolution by our server is the same than the
4352 * one used for the server found in the backend
4353 * * the server found in the backend is not our current server
4354 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004355 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004356 if ((tmpsrv->hostname_dn == NULL) ||
4357 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4358 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004359 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004360 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004361 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004362 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004363
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004364 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004365 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004366 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004367 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004368 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004369
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004370 /* At this point, we have 2 different servers using the same DNS hostname
4371 * for their respective resolution.
4372 */
4373 if (*ip_family == tmpsrv->addr.ss_family &&
4374 ((tmpsrv->addr.ss_family == AF_INET &&
4375 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4376 (tmpsrv->addr.ss_family == AF_INET6 &&
4377 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004378 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004379 return tmpsrv;
4380 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004381 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004382 }
4383
Emeric Brune9fd6b52017-11-02 17:20:39 +01004384
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004385 return NULL;
4386}
4387
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004388/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4389 * resolver. This is suited for initial address configuration. Returns 0 on
4390 * success otherwise a non-zero error code. In case of error, *err_code, if
4391 * not NULL, is filled up.
4392 */
4393int srv_set_addr_via_libc(struct server *srv, int *err_code)
4394{
4395 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004396 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004397 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004398 return 1;
4399 }
4400 return 0;
4401}
4402
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004403/* Set the server's FDQN (->hostname) from <hostname>.
4404 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004405 *
4406 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004407 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004408int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004409{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004410 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004411 char *hostname_dn;
4412 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004413
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004414 /* Note that the server lock is already held. */
4415 if (!srv->resolvers)
4416 return -1;
4417
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004418 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004419 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004420 /* run time DNS resolution was not active for this server
4421 * and we can't enable it at run time for now.
4422 */
4423 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004424 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004425
4426 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004427 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004428 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004429 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4430 hostname_dn, trash.size);
4431 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004432 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004433
Christopher Faulet67957bd2017-09-27 11:00:59 +02004434 resolution = srv->dns_requester->resolution;
4435 if (resolution &&
4436 resolution->hostname_dn &&
4437 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004438 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004439
Christopher Faulet67957bd2017-09-27 11:00:59 +02004440 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004441
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004442 free(srv->hostname);
4443 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004444 srv->hostname = strdup(hostname);
4445 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004446 srv->hostname_dn_len = hostname_dn_len;
4447 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004448 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004449
Baptiste Assmann13a92322019-06-07 09:40:55 +02004450 if (srv->flags & SRV_F_NO_RESOLUTION)
4451 goto end;
4452
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004453 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004454 goto err;
4455
4456 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004457 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004458 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004459 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004460
4461 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004462 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004463 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004464 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004465}
4466
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004467/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4468 * from the state file. This is suited for initial address configuration.
4469 * Returns 0 on success otherwise a non-zero error code. In case of error,
4470 * *err_code, if not NULL, is filled up.
4471 */
4472static int srv_apply_lastaddr(struct server *srv, int *err_code)
4473{
4474 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4475 if (err_code)
4476 *err_code |= ERR_WARN;
4477 return 1;
4478 }
4479 return 0;
4480}
4481
Willy Tarreau25e51522016-11-04 15:10:17 +01004482/* returns 0 if no error, otherwise a combination of ERR_* flags */
4483static int srv_iterate_initaddr(struct server *srv)
4484{
4485 int return_code = 0;
4486 int err_code;
4487 unsigned int methods;
4488
4489 methods = srv->init_addr_methods;
4490 if (!methods) { // default to "last,libc"
4491 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4492 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4493 }
4494
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004495 /* "-dr" : always append "none" so that server addresses resolution
4496 * failures are silently ignored, this is convenient to validate some
4497 * configs out of their environment.
4498 */
4499 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4500 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4501
Willy Tarreau25e51522016-11-04 15:10:17 +01004502 while (methods) {
4503 err_code = 0;
4504 switch (srv_get_next_initaddr(&methods)) {
4505 case SRV_IADDR_LAST:
4506 if (!srv->lastaddr)
4507 continue;
4508 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004509 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004510 return_code |= err_code;
4511 break;
4512
4513 case SRV_IADDR_LIBC:
4514 if (!srv->hostname)
4515 continue;
4516 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004517 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004518 return_code |= err_code;
4519 break;
4520
Willy Tarreau37ebe122016-11-04 15:17:58 +01004521 case SRV_IADDR_NONE:
4522 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004523 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004524 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4525 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004526 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004527 return return_code;
4528
Willy Tarreau4310d362016-11-02 15:05:56 +01004529 case SRV_IADDR_IP:
4530 ipcpy(&srv->init_addr, &srv->addr);
4531 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004532 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4533 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004534 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004535 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004536
Willy Tarreau25e51522016-11-04 15:10:17 +01004537 default: /* unhandled method */
4538 break;
4539 }
4540 }
4541
4542 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004543 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004544 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4545 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004546 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004547 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004548 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4549 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004550
4551 return_code |= ERR_ALERT | ERR_FATAL;
4552 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004553out:
4554 srv_set_dyncookie(srv);
4555 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004556}
4557
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004558/*
4559 * This function parses all backends and all servers within each backend
4560 * and performs servers' addr resolution based on information provided by:
4561 * - configuration file
4562 * - server-state file (states provided by an 'old' haproxy process)
4563 *
4564 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4565 */
4566int srv_init_addr(void)
4567{
4568 struct proxy *curproxy;
4569 int return_code = 0;
4570
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004571 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004572 while (curproxy) {
4573 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004574
4575 /* servers are in backend only */
4576 if (!(curproxy->cap & PR_CAP_BE))
4577 goto srv_init_addr_next;
4578
Willy Tarreau25e51522016-11-04 15:10:17 +01004579 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004580 if (srv->hostname)
4581 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004582
4583 srv_init_addr_next:
4584 curproxy = curproxy->next;
4585 }
4586
4587 return return_code;
4588}
4589
Willy Tarreau46b7f532018-08-21 11:54:26 +02004590/*
4591 * Must be called with the server lock held.
4592 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004593const 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 +02004594{
4595
Willy Tarreau83061a82018-07-13 11:56:34 +02004596 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004597
4598 msg = get_trash_chunk();
4599 chunk_reset(msg);
4600
Olivier Houchard8da5f982017-08-04 18:35:36 +02004601 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004602 chunk_appendf(msg, "no need to change the FDQN");
4603 goto out;
4604 }
4605
4606 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4607 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4608 goto out;
4609 }
4610
4611 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4612 server->proxy->id, server->id, server->hostname, fqdn);
4613
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004614 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004615 chunk_reset(msg);
4616 chunk_appendf(msg, "could not update %s/%s FQDN",
4617 server->proxy->id, server->id);
4618 goto out;
4619 }
4620
4621 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004622 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004623
4624 out:
4625 if (updater)
4626 chunk_appendf(msg, " by '%s'", updater);
4627 chunk_appendf(msg, "\n");
4628
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004629 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004630}
4631
4632
Willy Tarreau21b069d2016-11-23 17:15:08 +01004633/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4634 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004635 * 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 +01004636 * used for CLI commands requiring a server name.
4637 * Important: the <arg> is modified to remove the '/'.
4638 */
4639struct server *cli_find_server(struct appctx *appctx, char *arg)
4640{
4641 struct proxy *px;
4642 struct server *sv;
4643 char *line;
4644
4645 /* split "backend/server" and make <line> point to server */
4646 for (line = arg; *line; line++)
4647 if (*line == '/') {
4648 *line++ = '\0';
4649 break;
4650 }
4651
4652 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004653 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004654 return NULL;
4655 }
4656
4657 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004658 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004659 return NULL;
4660 }
4661
4662 if (px->state == PR_STSTOPPED) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004663 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004664 return NULL;
4665 }
4666
4667 return sv;
4668}
4669
William Lallemand222baf22016-11-19 02:00:33 +01004670
Willy Tarreau46b7f532018-08-21 11:54:26 +02004671/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004672static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004673{
4674 struct server *sv;
4675 const char *warning;
4676
4677 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4678 return 1;
4679
4680 sv = cli_find_server(appctx, args[2]);
4681 if (!sv)
4682 return 1;
4683
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004684 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004685
William Lallemand222baf22016-11-19 02:00:33 +01004686 if (strcmp(args[3], "weight") == 0) {
4687 warning = server_parse_weight_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004688 if (warning)
4689 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004690 }
4691 else if (strcmp(args[3], "state") == 0) {
4692 if (strcmp(args[4], "ready") == 0)
4693 srv_adm_set_ready(sv);
4694 else if (strcmp(args[4], "drain") == 0)
4695 srv_adm_set_drain(sv);
4696 else if (strcmp(args[4], "maint") == 0)
4697 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004698 else
4699 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004700 }
4701 else if (strcmp(args[3], "health") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004702 if (sv->track)
4703 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004704 else if (strcmp(args[4], "up") == 0) {
4705 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004706 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004707 }
4708 else if (strcmp(args[4], "stopping") == 0) {
4709 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004710 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004711 }
4712 else if (strcmp(args[4], "down") == 0) {
4713 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004714 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004715 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004716 else
4717 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004718 }
4719 else if (strcmp(args[3], "agent") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004720 if (!(sv->agent.state & CHK_ST_ENABLED))
4721 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004722 else if (strcmp(args[4], "up") == 0) {
4723 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004724 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004725 }
4726 else if (strcmp(args[4], "down") == 0) {
4727 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004728 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004729 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004730 else
4731 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004732 }
Misiek2da082d2017-01-09 09:40:42 +01004733 else if (strcmp(args[3], "agent-addr") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004734 if (!(sv->agent.state & CHK_ST_ENABLED))
4735 cli_err(appctx, "agent checks are not enabled on this server.\n");
4736 else if (str2ip(args[4], &sv->agent.addr) == NULL)
4737 cli_err(appctx, "incorrect addr address given for agent.\n");
Misiek2da082d2017-01-09 09:40:42 +01004738 }
4739 else if (strcmp(args[3], "agent-send") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004740 if (!(sv->agent.state & CHK_ST_ENABLED))
4741 cli_err(appctx, "agent checks are not enabled on this server.\n");
4742 else {
Misiek2da082d2017-01-09 09:40:42 +01004743 char *nss = strdup(args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004744 if (!nss)
4745 cli_err(appctx, "cannot allocate memory for new string.\n");
4746 else {
Misiek2da082d2017-01-09 09:40:42 +01004747 free(sv->agent.send_string);
4748 sv->agent.send_string = nss;
4749 sv->agent.send_string_len = strlen(args[4]);
4750 }
4751 }
4752 }
William Lallemand222baf22016-11-19 02:00:33 +01004753 else if (strcmp(args[3], "check-port") == 0) {
4754 int i = 0;
4755 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004756 cli_err(appctx, "'set server <srv> check-port' expects an integer as argument.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004757 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004758 }
4759 if ((i < 0) || (i > 65535)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004760 cli_err(appctx, "provided port is not valid.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004761 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004762 }
4763 /* prevent the update of port to 0 if MAPPORTS are in use */
4764 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004765 cli_err(appctx, "can't unset 'port' since MAPPORTS is in use.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004766 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004767 }
4768 sv->check.port = i;
Willy Tarreau9d008692019-08-09 11:21:01 +02004769 cli_msg(appctx, LOG_NOTICE, "health check port updated.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004770 }
4771 else if (strcmp(args[3], "addr") == 0) {
4772 char *addr = NULL;
4773 char *port = NULL;
4774 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004775 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004776 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004777 }
4778 else {
4779 addr = args[4];
4780 }
4781 if (strcmp(args[5], "port") == 0) {
4782 port = args[6];
4783 }
4784 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004785 if (warning)
4786 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004787 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4788 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004789 else if (strcmp(args[3], "fqdn") == 0) {
4790 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004791 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004792 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004793 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02004794 /* ensure runtime resolver will process this new fqdn */
4795 if (sv->flags & SRV_F_NO_RESOLUTION) {
4796 sv->flags &= ~SRV_F_NO_RESOLUTION;
4797 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004798 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Willy Tarreau9d008692019-08-09 11:21:01 +02004799 if (warning)
4800 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004801 }
William Lallemand222baf22016-11-19 02:00:33 +01004802 else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004803 cli_err(appctx,
4804 "'set server <srv>' only supports 'agent', 'health', 'state',"
4805 " 'weight', 'addr', 'fqdn' and 'check-port'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004806 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004807 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004808 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004809 return 1;
4810}
4811
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004812static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004813{
4814 struct stream_interface *si = appctx->owner;
4815 struct proxy *px;
4816 struct server *sv;
4817 char *line;
4818
4819
4820 /* split "backend/server" and make <line> point to server */
4821 for (line = args[2]; *line; line++)
4822 if (*line == '/') {
4823 *line++ = '\0';
4824 break;
4825 }
4826
Willy Tarreau9d008692019-08-09 11:21:01 +02004827 if (!*line)
4828 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004829
Willy Tarreau9d008692019-08-09 11:21:01 +02004830 if (!get_backend_server(args[2], line, &px, &sv))
4831 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004832
4833 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004834 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4835 sv->iweight);
4836 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004837 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004838 return 0;
4839 }
William Lallemand6b160942016-11-22 12:34:35 +01004840 return 1;
4841}
4842
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004843/* Parse a "set weight" command.
4844 *
4845 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004846 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004847static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004848{
4849 struct server *sv;
4850 const char *warning;
4851
4852 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4853 return 1;
4854
4855 sv = cli_find_server(appctx, args[2]);
4856 if (!sv)
4857 return 1;
4858
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004859 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4860
William Lallemand6b160942016-11-22 12:34:35 +01004861 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004862 if (warning)
4863 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004864
4865 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4866
William Lallemand6b160942016-11-22 12:34:35 +01004867 return 1;
4868}
4869
Willy Tarreau46b7f532018-08-21 11:54:26 +02004870/* parse a "set maxconn server" command. It always returns 1.
4871 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004872 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004873 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004874static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004875{
4876 struct server *sv;
4877 const char *warning;
4878
4879 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4880 return 1;
4881
4882 sv = cli_find_server(appctx, args[3]);
4883 if (!sv)
4884 return 1;
4885
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004886 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4887
Willy Tarreaub8026272016-11-23 11:26:56 +01004888 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004889 if (warning)
4890 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004891
4892 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4893
Willy Tarreaub8026272016-11-23 11:26:56 +01004894 return 1;
4895}
William Lallemand6b160942016-11-22 12:34:35 +01004896
Willy Tarreau46b7f532018-08-21 11:54:26 +02004897/* parse a "disable agent" command. It always returns 1.
4898 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004899 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004900 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004901static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004902{
4903 struct server *sv;
4904
4905 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4906 return 1;
4907
4908 sv = cli_find_server(appctx, args[2]);
4909 if (!sv)
4910 return 1;
4911
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004912 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004913 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004914 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004915 return 1;
4916}
4917
Willy Tarreau46b7f532018-08-21 11:54:26 +02004918/* parse a "disable health" command. It always returns 1.
4919 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004920 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004921 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004922static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004923{
4924 struct server *sv;
4925
4926 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4927 return 1;
4928
4929 sv = cli_find_server(appctx, args[2]);
4930 if (!sv)
4931 return 1;
4932
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004933 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004934 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004935 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004936 return 1;
4937}
4938
Willy Tarreau46b7f532018-08-21 11:54:26 +02004939/* parse a "disable server" command. It always returns 1.
4940 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004941 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004942 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004943static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004944{
4945 struct server *sv;
4946
4947 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4948 return 1;
4949
4950 sv = cli_find_server(appctx, args[2]);
4951 if (!sv)
4952 return 1;
4953
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004954 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004955 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004956 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004957 return 1;
4958}
4959
Willy Tarreau46b7f532018-08-21 11:54:26 +02004960/* parse a "enable agent" command. It always returns 1.
4961 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004962 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004963 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004964static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004965{
4966 struct server *sv;
4967
4968 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4969 return 1;
4970
4971 sv = cli_find_server(appctx, args[2]);
4972 if (!sv)
4973 return 1;
4974
Willy Tarreau9d008692019-08-09 11:21:01 +02004975 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4976 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004977
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004978 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004979 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004980 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004981 return 1;
4982}
4983
Willy Tarreau46b7f532018-08-21 11:54:26 +02004984/* parse a "enable health" command. It always returns 1.
4985 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004986 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004987 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004988static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004989{
4990 struct server *sv;
4991
4992 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4993 return 1;
4994
4995 sv = cli_find_server(appctx, args[2]);
4996 if (!sv)
4997 return 1;
4998
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004999 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005000 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005001 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005002 return 1;
5003}
5004
Willy Tarreau46b7f532018-08-21 11:54:26 +02005005/* parse a "enable server" command. It always returns 1.
5006 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005007 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02005008 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005009static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01005010{
5011 struct server *sv;
5012
5013 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5014 return 1;
5015
5016 sv = cli_find_server(appctx, args[2]);
5017 if (!sv)
5018 return 1;
5019
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005020 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01005021 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01005022 if (!(sv->flags & SRV_F_COOKIESET)
5023 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
5024 sv->cookie)
5025 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02005026 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01005027 return 1;
5028}
5029
William Lallemand222baf22016-11-19 02:00:33 +01005030/* register cli keywords */
5031static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01005032 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005033 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01005034 { { "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 +01005035 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01005036 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01005037 { { "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 +01005038 { { "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 +01005039 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01005040 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
5041 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
5042
William Lallemand222baf22016-11-19 02:00:33 +01005043 {{},}
5044}};
5045
Willy Tarreau0108d902018-11-25 19:14:37 +01005046INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01005047
Emeric Brun64cc49c2017-10-03 14:46:45 +02005048/*
5049 * This function applies server's status changes, it is
5050 * is designed to be called asynchronously.
5051 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02005052 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005053 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005054static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005055{
5056 struct check *check = &s->check;
5057 int xferred;
5058 struct proxy *px = s->proxy;
5059 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5060 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5061 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02005062 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005063
Emeric Brun64cc49c2017-10-03 14:46:45 +02005064 /* If currently main is not set we try to apply pending state changes */
5065 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
5066 int next_admin;
5067
5068 /* Backup next admin */
5069 next_admin = s->next_admin;
5070
5071 /* restore current admin state */
5072 s->next_admin = s->cur_admin;
5073
5074 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
5075 s->last_change = now.tv_sec;
5076 if (s->proxy->lbprm.set_server_status_down)
5077 s->proxy->lbprm.set_server_status_down(s);
5078
5079 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5080 srv_shutdown_streams(s, SF_ERR_DOWN);
5081
5082 /* we might have streams queued on this server and waiting for
5083 * a connection. Those which are redispatchable will be queued
5084 * to another server or to the proxy itself.
5085 */
5086 xferred = pendconn_redistribute(s);
5087
5088 tmptrash = alloc_trash_chunk();
5089 if (tmptrash) {
5090 chunk_printf(tmptrash,
5091 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5092 s->proxy->id, s->id);
5093
Emeric Brun5a133512017-10-19 14:42:30 +02005094 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005095 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005096
5097 /* we don't send an alert if the server was previously paused */
5098 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005099 send_log(s->proxy, log_level, "%s.\n",
5100 tmptrash->area);
5101 send_email_alert(s, log_level, "%s",
5102 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005103 free_trash_chunk(tmptrash);
5104 tmptrash = NULL;
5105 }
5106 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5107 set_backend_down(s->proxy);
5108
5109 s->counters.down_trans++;
5110 }
5111 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
5112 s->last_change = now.tv_sec;
5113 if (s->proxy->lbprm.set_server_status_down)
5114 s->proxy->lbprm.set_server_status_down(s);
5115
5116 /* we might have streams queued on this server and waiting for
5117 * a connection. Those which are redispatchable will be queued
5118 * to another server or to the proxy itself.
5119 */
5120 xferred = pendconn_redistribute(s);
5121
5122 tmptrash = alloc_trash_chunk();
5123 if (tmptrash) {
5124 chunk_printf(tmptrash,
5125 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5126 s->proxy->id, s->id);
5127
Emeric Brun5a133512017-10-19 14:42:30 +02005128 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005129
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005130 ha_warning("%s.\n", tmptrash->area);
5131 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5132 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005133 free_trash_chunk(tmptrash);
5134 tmptrash = NULL;
5135 }
5136
5137 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5138 set_backend_down(s->proxy);
5139 }
5140 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5141 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
5142 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5143 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5144 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5145 s->proxy->last_change = now.tv_sec;
5146 }
5147
5148 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
5149 s->down_time += now.tv_sec - s->last_change;
5150
5151 s->last_change = now.tv_sec;
5152 if (s->next_state == SRV_ST_STARTING)
5153 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5154
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005155 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005156 /* now propagate the status change to any LB algorithms */
5157 if (px->lbprm.update_server_eweight)
5158 px->lbprm.update_server_eweight(s);
5159 else if (srv_willbe_usable(s)) {
5160 if (px->lbprm.set_server_status_up)
5161 px->lbprm.set_server_status_up(s);
5162 }
5163 else {
5164 if (px->lbprm.set_server_status_down)
5165 px->lbprm.set_server_status_down(s);
5166 }
5167
5168 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5169 * and it's not a backup server and its effective weight is > 0,
5170 * then it can accept new connections, so we shut down all streams
5171 * on all backup servers.
5172 */
5173 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5174 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5175 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5176
5177 /* check if we can handle some connections queued at the proxy. We
5178 * will take as many as we can handle.
5179 */
5180 xferred = pendconn_grab_from_px(s);
5181
5182 tmptrash = alloc_trash_chunk();
5183 if (tmptrash) {
5184 chunk_printf(tmptrash,
5185 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5186 s->proxy->id, s->id);
5187
Emeric Brun5a133512017-10-19 14:42:30 +02005188 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005189 ha_warning("%s.\n", tmptrash->area);
5190 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5191 tmptrash->area);
5192 send_email_alert(s, LOG_NOTICE, "%s",
5193 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005194 free_trash_chunk(tmptrash);
5195 tmptrash = NULL;
5196 }
5197
5198 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5199 set_backend_down(s->proxy);
5200 }
5201 else if (s->cur_eweight != s->next_eweight) {
5202 /* now propagate the status change to any LB algorithms */
5203 if (px->lbprm.update_server_eweight)
5204 px->lbprm.update_server_eweight(s);
5205 else if (srv_willbe_usable(s)) {
5206 if (px->lbprm.set_server_status_up)
5207 px->lbprm.set_server_status_up(s);
5208 }
5209 else {
5210 if (px->lbprm.set_server_status_down)
5211 px->lbprm.set_server_status_down(s);
5212 }
5213
5214 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5215 set_backend_down(s->proxy);
5216 }
5217
5218 s->next_admin = next_admin;
5219 }
5220
Emeric Brun5a133512017-10-19 14:42:30 +02005221 /* reset operational state change */
5222 *s->op_st_chg.reason = 0;
5223 s->op_st_chg.status = s->op_st_chg.code = -1;
5224 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005225
5226 /* Now we try to apply pending admin changes */
5227
5228 /* Maintenance must also disable health checks */
5229 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5230 if (s->check.state & CHK_ST_ENABLED) {
5231 s->check.state |= CHK_ST_PAUSED;
5232 check->health = 0;
5233 }
5234
5235 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005236 tmptrash = alloc_trash_chunk();
5237 if (tmptrash) {
5238 chunk_printf(tmptrash,
5239 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5240 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5241 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005242
Olivier Houchard796a2b32017-10-24 17:42:47 +02005243 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005244
Olivier Houchard796a2b32017-10-24 17:42:47 +02005245 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005246 ha_warning("%s.\n", tmptrash->area);
5247 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5248 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005249 }
5250 free_trash_chunk(tmptrash);
5251 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005252 }
Emeric Brun8f298292017-12-06 16:47:17 +01005253 /* commit new admin status */
5254
5255 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005256 }
5257 else { /* server was still running */
5258 check->health = 0; /* failure */
5259 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005260
5261 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005262 if (s->proxy->lbprm.set_server_status_down)
5263 s->proxy->lbprm.set_server_status_down(s);
5264
Emeric Brun64cc49c2017-10-03 14:46:45 +02005265 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5266 srv_shutdown_streams(s, SF_ERR_DOWN);
5267
5268 /* we might have streams queued on this server and waiting for
5269 * a connection. Those which are redispatchable will be queued
5270 * to another server or to the proxy itself.
5271 */
5272 xferred = pendconn_redistribute(s);
5273
5274 tmptrash = alloc_trash_chunk();
5275 if (tmptrash) {
5276 chunk_printf(tmptrash,
5277 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5278 s->flags & SRV_F_BACKUP ? "Backup " : "",
5279 s->proxy->id, s->id,
5280 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5281
5282 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5283
5284 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005285 ha_warning("%s.\n", tmptrash->area);
5286 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5287 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005288 }
5289 free_trash_chunk(tmptrash);
5290 tmptrash = NULL;
5291 }
5292 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5293 set_backend_down(s->proxy);
5294
5295 s->counters.down_trans++;
5296 }
5297 }
5298 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5299 /* OK here we're leaving maintenance, we have many things to check,
5300 * because the server might possibly be coming back up depending on
5301 * its state. In practice, leaving maintenance means that we should
5302 * immediately turn to UP (more or less the slowstart) under the
5303 * following conditions :
5304 * - server is neither checked nor tracked
5305 * - server tracks another server which is not checked
5306 * - server tracks another server which is already up
5307 * Which sums up as something simpler :
5308 * "either the tracking server is up or the server's checks are disabled
5309 * or up". Otherwise we only re-enable health checks. There's a special
5310 * case associated to the stopping state which can be inherited. Note
5311 * that the server might still be in drain mode, which is naturally dealt
5312 * with by the lower level functions.
5313 */
5314
5315 if (s->check.state & CHK_ST_ENABLED) {
5316 s->check.state &= ~CHK_ST_PAUSED;
5317 check->health = check->rise; /* start OK but check immediately */
5318 }
5319
5320 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5321 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5322 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5323 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5324 s->next_state = SRV_ST_STOPPING;
5325 }
5326 else {
5327 s->next_state = SRV_ST_STARTING;
5328 if (s->slowstart > 0)
5329 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5330 else
5331 s->next_state = SRV_ST_RUNNING;
5332 }
5333
5334 }
5335
5336 tmptrash = alloc_trash_chunk();
5337 if (tmptrash) {
5338 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5339 chunk_printf(tmptrash,
5340 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5341 s->flags & SRV_F_BACKUP ? "Backup " : "",
5342 s->proxy->id, s->id,
5343 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5344 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5345 }
5346 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5347 chunk_printf(tmptrash,
5348 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5349 s->flags & SRV_F_BACKUP ? "Backup " : "",
5350 s->proxy->id, s->id, s->hostname,
5351 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5352 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5353 }
5354 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5355 chunk_printf(tmptrash,
5356 "%sServer %s/%s is %s/%s (leaving maintenance)",
5357 s->flags & SRV_F_BACKUP ? "Backup " : "",
5358 s->proxy->id, s->id,
5359 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5360 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5361 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005362 ha_warning("%s.\n", tmptrash->area);
5363 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5364 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005365 free_trash_chunk(tmptrash);
5366 tmptrash = NULL;
5367 }
5368
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005369 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005370 /* now propagate the status change to any LB algorithms */
5371 if (px->lbprm.update_server_eweight)
5372 px->lbprm.update_server_eweight(s);
5373 else if (srv_willbe_usable(s)) {
5374 if (px->lbprm.set_server_status_up)
5375 px->lbprm.set_server_status_up(s);
5376 }
5377 else {
5378 if (px->lbprm.set_server_status_down)
5379 px->lbprm.set_server_status_down(s);
5380 }
5381
5382 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5383 set_backend_down(s->proxy);
5384
Willy Tarreau6a78e612018-08-07 10:14:53 +02005385 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5386 * and it's not a backup server and its effective weight is > 0,
5387 * then it can accept new connections, so we shut down all streams
5388 * on all backup servers.
5389 */
5390 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5391 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5392 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5393
5394 /* check if we can handle some connections queued at the proxy. We
5395 * will take as many as we can handle.
5396 */
5397 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005398 }
5399 else if (s->next_admin & SRV_ADMF_MAINT) {
5400 /* remaining in maintenance mode, let's inform precisely about the
5401 * situation.
5402 */
5403 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5404 tmptrash = alloc_trash_chunk();
5405 if (tmptrash) {
5406 chunk_printf(tmptrash,
5407 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5408 s->flags & SRV_F_BACKUP ? "Backup " : "",
5409 s->proxy->id, s->id);
5410
5411 if (s->track) /* normally it's mandatory here */
5412 chunk_appendf(tmptrash, " via %s/%s",
5413 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005414 ha_warning("%s.\n", tmptrash->area);
5415 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5416 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005417 free_trash_chunk(tmptrash);
5418 tmptrash = NULL;
5419 }
5420 }
5421 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5422 tmptrash = alloc_trash_chunk();
5423 if (tmptrash) {
5424 chunk_printf(tmptrash,
5425 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5426 s->flags & SRV_F_BACKUP ? "Backup " : "",
5427 s->proxy->id, s->id, s->hostname);
5428
5429 if (s->track) /* normally it's mandatory here */
5430 chunk_appendf(tmptrash, " via %s/%s",
5431 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005432 ha_warning("%s.\n", tmptrash->area);
5433 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5434 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005435 free_trash_chunk(tmptrash);
5436 tmptrash = NULL;
5437 }
5438 }
5439 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5440 tmptrash = alloc_trash_chunk();
5441 if (tmptrash) {
5442 chunk_printf(tmptrash,
5443 "%sServer %s/%s remains in forced maintenance",
5444 s->flags & SRV_F_BACKUP ? "Backup " : "",
5445 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005446 ha_warning("%s.\n", tmptrash->area);
5447 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5448 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005449 free_trash_chunk(tmptrash);
5450 tmptrash = NULL;
5451 }
5452 }
5453 /* don't report anything when leaving drain mode and remaining in maintenance */
5454
5455 s->cur_admin = s->next_admin;
5456 }
5457
5458 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5459 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5460 /* drain state is applied only if not yet in maint */
5461
5462 s->last_change = now.tv_sec;
5463 if (px->lbprm.set_server_status_down)
5464 px->lbprm.set_server_status_down(s);
5465
5466 /* we might have streams queued on this server and waiting for
5467 * a connection. Those which are redispatchable will be queued
5468 * to another server or to the proxy itself.
5469 */
5470 xferred = pendconn_redistribute(s);
5471
5472 tmptrash = alloc_trash_chunk();
5473 if (tmptrash) {
5474 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5475 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5476 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5477
5478 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5479
5480 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005481 ha_warning("%s.\n", tmptrash->area);
5482 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5483 tmptrash->area);
5484 send_email_alert(s, LOG_NOTICE, "%s",
5485 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005486 }
5487 free_trash_chunk(tmptrash);
5488 tmptrash = NULL;
5489 }
5490
5491 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5492 set_backend_down(s->proxy);
5493 }
5494 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5495 /* OK completely leaving drain mode */
5496 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5497 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5498 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5499 s->proxy->last_change = now.tv_sec;
5500 }
5501
5502 if (s->last_change < now.tv_sec) // ignore negative times
5503 s->down_time += now.tv_sec - s->last_change;
5504 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005505 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005506
5507 tmptrash = alloc_trash_chunk();
5508 if (tmptrash) {
5509 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5510 chunk_printf(tmptrash,
5511 "%sServer %s/%s is %s (leaving forced drain)",
5512 s->flags & SRV_F_BACKUP ? "Backup " : "",
5513 s->proxy->id, s->id,
5514 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5515 }
5516 else {
5517 chunk_printf(tmptrash,
5518 "%sServer %s/%s is %s (leaving drain)",
5519 s->flags & SRV_F_BACKUP ? "Backup " : "",
5520 s->proxy->id, s->id,
5521 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5522 if (s->track) /* normally it's mandatory here */
5523 chunk_appendf(tmptrash, " via %s/%s",
5524 s->track->proxy->id, s->track->id);
5525 }
5526
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005527 ha_warning("%s.\n", tmptrash->area);
5528 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5529 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005530 free_trash_chunk(tmptrash);
5531 tmptrash = NULL;
5532 }
5533
5534 /* now propagate the status change to any LB algorithms */
5535 if (px->lbprm.update_server_eweight)
5536 px->lbprm.update_server_eweight(s);
5537 else if (srv_willbe_usable(s)) {
5538 if (px->lbprm.set_server_status_up)
5539 px->lbprm.set_server_status_up(s);
5540 }
5541 else {
5542 if (px->lbprm.set_server_status_down)
5543 px->lbprm.set_server_status_down(s);
5544 }
5545 }
5546 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5547 /* remaining in drain mode after removing one of its flags */
5548
5549 tmptrash = alloc_trash_chunk();
5550 if (tmptrash) {
5551 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5552 chunk_printf(tmptrash,
5553 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5554 s->flags & SRV_F_BACKUP ? "Backup " : "",
5555 s->proxy->id, s->id);
5556
5557 if (s->track) /* normally it's mandatory here */
5558 chunk_appendf(tmptrash, " via %s/%s",
5559 s->track->proxy->id, s->track->id);
5560 }
5561 else {
5562 chunk_printf(tmptrash,
5563 "%sServer %s/%s remains in forced drain mode",
5564 s->flags & SRV_F_BACKUP ? "Backup " : "",
5565 s->proxy->id, s->id);
5566 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005567 ha_warning("%s.\n", tmptrash->area);
5568 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5569 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005570 free_trash_chunk(tmptrash);
5571 tmptrash = NULL;
5572 }
5573
5574 /* commit new admin status */
5575
5576 s->cur_admin = s->next_admin;
5577 }
5578 }
5579
5580 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005581 *s->adm_st_chg_cause = 0;
5582}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005583
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005584struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5585{
5586 struct connection *conn;
5587
Olivier Houchard859dc802019-08-08 15:47:21 +02005588 while ((conn = MT_LIST_POP(&toremove_connections[tid],
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005589 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005590 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005591 }
5592
5593 return task;
5594}
5595
Willy Tarreau980855b2019-02-07 14:59:29 +01005596struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005597{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005598 struct server *srv;
5599 struct eb32_node *eb;
5600 int i;
5601 unsigned int next_wakeup;
5602 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005603
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005604 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5605 while (1) {
5606 int srv_is_empty = 1;
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005607 int exceed_conns;
5608 int to_kill;
5609 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005610
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005611 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5612 if (!eb) {
5613 /* we might have reached the end of the tree, typically because
5614 * <now_ms> is in the first half and we're first scanning the last
5615 * half. Let's loop back to the beginning of the tree now.
5616 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005617
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005618 eb = eb32_first(&idle_conn_srv);
5619 if (likely(!eb))
5620 break;
5621 }
5622 if (tick_is_lt(now_ms, eb->key)) {
5623 /* timer not expired yet, revisit it later */
5624 next_wakeup = eb->key;
5625 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005626 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005627 }
5628 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005629
5630 /* Calculate how many idle connections we want to kill :
5631 * we want to remove half the difference between the total
5632 * of established connections (used or idle) and the max
5633 * number of used connections.
5634 */
5635 curr_idle = srv->curr_idle_conns;
5636 if (curr_idle == 0)
5637 goto remove;
5638 exceed_conns = srv->curr_used_conns + curr_idle -
5639 srv->max_used_conns;
5640 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
5641 srv->max_used_conns = srv->curr_used_conns;
5642
5643 for (i = 0; i < global.nbthread && to_kill > 0; i++) {
5644 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005645 int j;
5646 int did_remove = 0;
5647
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005648 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
5649 curr_idle + 1;
Olivier Houchard4be71902019-07-11 15:49:00 +02005650 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005651 for (j = 0; j < max_conn; j++) {
Olivier Houcharddc2f2752020-02-13 19:12:07 +01005652 struct connection *conn = MT_LIST_POP(&srv->idle_conns[i], struct connection *, list);
5653 if (!conn)
5654 conn = MT_LIST_POP(&srv->safe_conns[i],
5655 struct connection *, list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005656 if (!conn)
5657 break;
5658 did_remove = 1;
Olivier Houchard859dc802019-08-08 15:47:21 +02005659 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005660 }
Olivier Houchard4be71902019-07-11 15:49:00 +02005661 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005662 if (did_remove && max_conn < srv->curr_idle_thr[i])
5663 srv_is_empty = 0;
5664 if (did_remove)
5665 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5666 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005667remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005668 eb32_delete(&srv->idle_node);
5669 if (!srv_is_empty) {
5670 /* There are still more idle connections, add the
5671 * server back in the tree.
5672 */
5673 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5674 now_ms);
5675 eb32_insert(&idle_conn_srv, &srv->idle_node);
5676 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005677 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005678 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5679
5680 if (need_wakeup)
5681 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005682 else
5683 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005684
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005685 return task;
5686}
Olivier Houchard88698d92019-04-16 19:07:22 +02005687
5688/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5689static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5690 struct proxy *defpx, const char *file, int line,
5691 char **err)
5692{
5693 int arg = -1;
5694
5695 if (too_many_args(1, args, err, NULL))
5696 return -1;
5697
5698 if (*(args[1]) != 0)
5699 arg = atoi(args[1]);
5700
5701 if (arg < 0 || arg > 100) {
5702 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5703 return -1;
5704 }
5705
5706 if (args[0][10] == 'h')
5707 global.tune.pool_high_ratio = arg;
5708 else
5709 global.tune.pool_low_ratio = arg;
5710 return 0;
5711}
5712
5713/* config keyword parsers */
5714static struct cfg_kw_list cfg_kws = {ILH, {
5715 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5716 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5717 { 0, NULL, NULL }
5718}};
5719
5720INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5721
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005722/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005723 * Local variables:
5724 * c-indent-level: 8
5725 * c-basic-offset: 8
5726 * End:
5727 */